-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yml
More file actions
55 lines (47 loc) · 2.33 KB
/
action.yml
File metadata and controls
55 lines (47 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
---
name: Get Current PR Data
author: Rishav Dhar (@rdhar)
description: "Get current PR data from any event trigger, including: issue_comment, merge_group, push, and workflow_dispatch."
runs:
using: composite
steps:
- shell: bash
run: |
# Populate environment variables.
echo GH_API="X-GitHub-Api-Version:2022-11-28" >> "$GITHUB_ENV"
echo GH_TOKEN="${{ inputs.token }}" >> "$GITHUB_ENV"
if [[ "$GITHUB_SERVER_URL" != "https://github.com" ]]; then echo GH_HOST=$(echo "$GITHUB_SERVER_URL" | sed 's/.*:\/\///') >> "$GITHUB_ENV"; fi
- id: data
shell: bash
run: |
# Fallback on empty string if the branch is not found.
branch=${{ github.ref_name || github.head_ref || github.ref || '0' }}
# Get PR number using GitHub API for different event triggers.
if [[ "$GITHUB_EVENT_NAME" == "push" ]]; then
# List PRs associated with the commit, then get the PR number from the head ref or the latest PR.
associated_prs=$(gh api /repos/${{ github.repository }}/commits/${{ github.event.pull_request.head.sha || github.sha }}/pulls --header "$GH_API" --method GET --field per_page=100)
number=$(echo "$associated_prs" | jq --raw-output '(.[] | select(.head.ref == env.GITHUB_REF_NAME) | .number) // .[0].number // 0')
elif [[ "$GITHUB_EVENT_NAME" == "merge_group" ]]; then
# Get the PR number by parsing the ref name.
number=$(echo "${GITHUB_REF_NAME}" | sed -n 's/.*pr-\([0-9]*\)-.*/\1/p')
else
# Get the PR number from branch name, otherwise fallback on 0 if the PR number is not found.
number=${{ github.event.number || github.event.issue.number }} || $(gh api /repos/${{ github.repository }}/pulls --header "$GH_API" --method GET --field per_page=100 --field head="${branch}" | jq '.[0].number // 0')
fi
echo "branch=$branch" >> "$GITHUB_OUTPUT"
echo "number=$number" >> "$GITHUB_OUTPUT"
outputs:
branch:
description: "Current PR branch."
value: ${{ steps.data.outputs.branch }}
number:
description: "Current PR number."
value: ${{ steps.data.outputs.number }}
inputs:
token:
default: ${{ github.token }}
description: "Specify a GitHub token (e.g., `secrets.GITHUB_TOKEN`)."
required: false
branding:
color: gray-dark
icon: git-pull-request