From 97fed9eba00db6ed7570859e5f22d73e144e0925 Mon Sep 17 00:00:00 2001 From: Victor Alfaro Date: Mon, 10 Jun 2024 08:32:01 -0600 Subject: [PATCH] Link merged PR to issue (#28808) --- .github/workflows/link-pr-to-issue.yml | 65 ++++++++++++++++++++++++++ .github/workflows/post-pr-merge.yml | 17 +++++++ 2 files changed, 82 insertions(+) create mode 100644 .github/workflows/link-pr-to-issue.yml create mode 100644 .github/workflows/post-pr-merge.yml diff --git a/.github/workflows/link-pr-to-issue.yml b/.github/workflows/link-pr-to-issue.yml new file mode 100644 index 000000000000..070302928cd9 --- /dev/null +++ b/.github/workflows/link-pr-to-issue.yml @@ -0,0 +1,65 @@ +# action.yml +name: 'Link Pull Request to Issue' +on: + workflow_call: + inputs: + pr_number: + description: 'Pull Request number' + type: number + required: true + pr_branch: + description: 'Pull Request branch' + type: string + required: true + validate_merge: + description: 'Validate merge' + type: boolean + required: false + default: true + +jobs: + link-pr-to-issue: + runs-on: ubuntu-20.04 + continue-on-error: true + steps: + - run: echo 'GitHub context' + env: + GITHUB_CONTEXT: ${{ toJson(github) }} + + - name: Link PR to Issue + shell: bash + run: | + pr_json=$(\ + gh api \ + --method GET \ + repos/${{ github.repository }}/pulls/${{ inputs.pr_number }} \ + | jq -c -r \ + ) + + pr_merged=$(echo "${pr_json}" | jq '.merged') + [[ "${{ inputs.validate_merge }}" == 'true' && "${pr_merged}" != 'true' ]] && echo "PR [${{ inputs.pr_number }}] is not merged" && exit 1 + echo "pr_merged=${pr_merged}" + + pr_branch=${{ inputs.pr_branch }} + [[ ${pr_branch} =~ ^[0-9]+- ]] && issue_number=${pr_branch%%-*} || issue_number=${pr_branch} + [[ -z "${issue_number}" ]] && echo 'Issue number could not be resolved' && exit 2 + echo "issue_number=${issue_number}" + + issue_json=$(\ + gh api \ + --method GET \ + repos/${{ github.repository }}/issues/${issue_number} \ + ) + fetched_issue_number=$(echo "${issue_json}" | jq -r '.number') + [[ -z "${fetched_issue_number}" ]] && echo "Issue [${issue_number}] could not be resolved" && exit 3 + + pr_body=$(echo "${pr_json}" | jq -r '.body') + pr_body="${pr_body}"$'\n\n'"This PR fixes: #${issue_number}" + echo "pr_body=${pr_body}" + + gh api \ + --method PATCH \ + -H "Accept: application/vnd.github+json" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + /repos/${{ github.repository }}/pulls/${{ inputs.pr_number }} \ + -f "body=\"${pr_body}\"" diff --git a/.github/workflows/post-pr-merge.yml b/.github/workflows/post-pr-merge.yml new file mode 100644 index 000000000000..af40c7edb632 --- /dev/null +++ b/.github/workflows/post-pr-merge.yml @@ -0,0 +1,17 @@ +name: Post PR Merge +on: + pull_request: + branches: + - 28716-test + types: + - closed + +jobs: + post-pr-merge: + name: Post Merge PR + if: github.event.pull_request.merged == true + uses: ./.github/workflows/link-pr-to-issue.yml + with: + pr_number: ${{ github.event.pull_request.number }} + pr_branch: ${{ github.event.pull_request.head.ref }} + validate_merge: false \ No newline at end of file