|
| 1 | +name: Get linked PR |
| 2 | +description: Get the PR linked to this one, if any. |
| 3 | + |
| 4 | +inputs: |
| 5 | + github-token: |
| 6 | + description: GitHub token to use for API requests |
| 7 | + default: ${{ github.token }} |
| 8 | + target-repo: |
| 9 | + description: Repository in which the PR should exist (e.g. "wasp-lang/wasp") |
| 10 | + required: true |
| 11 | + source-repo: |
| 12 | + description: Repository in which the current PR exists (e.g. "wasp-lang/open-saas") |
| 13 | + default: ${{ github.repository }} |
| 14 | + source-pr: |
| 15 | + description: Current PR number |
| 16 | + default: ${{ github.event.pull_request.number }} |
| 17 | + |
| 18 | +runs: |
| 19 | + using: composite |
| 20 | + steps: |
| 21 | + - name: Get source PR |
| 22 | + id: source-pr |
| 23 | + env: |
| 24 | + GH_TOKEN: ${{ inputs.github-token }} |
| 25 | + SOURCE_REPO: ${{ inputs.source-repo }} |
| 26 | + SOURCE_PR: ${{ inputs.source-pr }} |
| 27 | + shell: bash |
| 28 | + run: | |
| 29 | + { |
| 30 | + echo 'pr-body<<EOF' |
| 31 | + gh pr view "$SOURCE_PR" \ |
| 32 | + --repo "$SOURCE_REPO" \ |
| 33 | + --json body \ |
| 34 | + --jq '.body' |
| 35 | + echo EOF |
| 36 | + } >> "$GITHUB_OUTPUT" |
| 37 | +
|
| 38 | + - name: Get linked PR URL |
| 39 | + id: get-url |
| 40 | + env: |
| 41 | + PR_BODY: ${{ steps.source-pr.outputs.pr-body }} |
| 42 | + TARGET_REPO: ${{ inputs.target-repo }} |
| 43 | + uses: actions/github-script@v8 |
| 44 | + with: |
| 45 | + script: | |
| 46 | + const prBody = process.env.PR_BODY; |
| 47 | + const targetRepo = process.env.TARGET_REPO; |
| 48 | +
|
| 49 | + const prUrlPrefix = RegExp.escape(`https://github.com/${targetRepo}/pull`); |
| 50 | +
|
| 51 | + // Captures the PR URL in a "link to ..." phrase. |
| 52 | + const prUrlRegex = new RegExp(`\\blink to (${prUrlPrefix}\/\\d+)\/?`, "i"); |
| 53 | +
|
| 54 | + const prUrl = prBody.match(prUrlRegex)?.[1]; |
| 55 | + return prUrl || ""; |
| 56 | + result-encoding: string |
| 57 | + |
| 58 | + - name: Get linked PR branch |
| 59 | + if: steps.get-url.outputs.result |
| 60 | + id: get-branch |
| 61 | + env: |
| 62 | + GH_TOKEN: ${{ inputs.github-token }} |
| 63 | + PR_URL: ${{ steps.get-url.outputs.result }} |
| 64 | + shell: bash |
| 65 | + run: | |
| 66 | + pr_branch=$( |
| 67 | + gh pr view "$PR_URL" \ |
| 68 | + --json headRefName \ |
| 69 | + --jq '.headRefName' |
| 70 | + ) |
| 71 | + echo "pr-branch=$pr_branch" >> "$GITHUB_OUTPUT" |
| 72 | +
|
| 73 | +outputs: |
| 74 | + pr-url: |
| 75 | + description: URL of the linked PR, or empty if none found |
| 76 | + value: ${{ steps.get-url.outputs.result }} |
| 77 | + |
| 78 | + pr-branch: |
| 79 | + description: Branch name of the linked PR, or empty if none found |
| 80 | + value: ${{ steps.get-branch.outputs.pr-branch }} |
0 commit comments