From 4224a6d5680b7274346820d60c9f4dc866713c08 Mon Sep 17 00:00:00 2001 From: Victor Alfaro Date: Fri, 4 Oct 2024 14:29:42 -0600 Subject: [PATCH] #28717: linking issue to PR when PR is opened --- .../workflows/issue_comp_link-issue-to-pr.yml | 90 +++++++++++++++++++ .../workflows/issue_comp_link-pr-to-issue.yml | 1 - .github/workflows/issue_open-pr.yml | 12 +++ 3 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/issue_comp_link-issue-to-pr.yml create mode 100644 .github/workflows/issue_open-pr.yml diff --git a/.github/workflows/issue_comp_link-issue-to-pr.yml b/.github/workflows/issue_comp_link-issue-to-pr.yml new file mode 100644 index 000000000000..84562167db56 --- /dev/null +++ b/.github/workflows/issue_comp_link-issue-to-pr.yml @@ -0,0 +1,90 @@ +name: Link Issue to PR + +on: + workflow_call: + inputs: + pr_branch: + description: 'Pull Request branch' + type: string + required: true + workflow_dispatch: + inputs: + pr_branch: + description: 'Pull Request branch' + type: string + required: true + +env: + GH_TOKEN: ${{ github.token }} + +jobs: + add-issue-to-pr: + runs-on: ubuntu-latest + + steps: + - run: echo 'GitHub context' + env: + GITHUB_CONTEXT: ${{ toJson(github) }} + + - name: Extract issue number from branch name + id: extract_issue_number + run: | + branch_name="${{ inputs.pr_branch }}" + if [[ "$branch_name" =~ ^([0-9]+)- ]]; then + issue_number="${BASH_REMATCH[1]}" + elif [[ "$branch_name" =~ ^issue-([0-9]+)- ]]; then + issue_number="${BASH_REMATCH[1]}" + else + echo "Branch name doesn't match the expected pattern" + exit 1 + fi + + echo "issue_number=$issue_number" >> "$GITHUB_OUTPUT" + + - name: Get existing issue comments + id: get_comments + run: | + comments=$(\ + gh api \ + -H "Accept: application/vnd.github+json" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + /repos/${{ github.repository }}/issues/${{ steps.extract_issue_number.outputs.issue_number }}/comments \ + ) + + echo "comments=$comments" >> "$GITHUB_OUTPUT" + + - name: Check if PR comment exists + id: check_comment + run: | + pr_url="${{ github.event.pull_request.html_url }}" + existing_comment_id="" + pr_list="PRs:\n- $pr_url" + for comment in $(echo "${{ steps.get_comments.outputs.comments }}" | jq -c '.[]'); do + comment_body=$(echo "$comment" | jq -r '.body') + if [[ "$comment_body" == PRs:* ]]; then + existing_comment_id=$(echo "$comment" | jq -r '.id') + pr_list="${comment_body}\n- $pr_url" + break + fi + done + + echo "pr_list=$pr_list" >> "$GITHUB_OUTPUT" + echo "existing_comment_id=$existing_comment_id" >> "$GITHUB_OUTPUT" + + - name: Update or create comment + if: steps.check_comment.outputs.existing_comment_id == '' + uses: peter-evans/create-or-update-comment@v4 + with: + issue-number: ${{ steps.extract_issue_number.outputs.issue_number }} + body: ${{ steps.check_comment.outputs.pr_list }} + + - name: Update existing comment + if: steps.check_comment.outputs.existing_comment_id != '' + run: | + comment_id="${{ steps.check_comment.outputs.existing_comment_id }}" + pr_list="${{ steps.check_comment.outputs.pr_list }}" + curl -X PATCH \ + -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ + -H "Content-Type: application/json" \ + -d "{\"body\":\"$pr_list\"}" \ + "https://api.github.com/repos/${{ github.repository }}/issues/comments/$comment_id" diff --git a/.github/workflows/issue_comp_link-pr-to-issue.yml b/.github/workflows/issue_comp_link-pr-to-issue.yml index 66400cd03f67..b18666b8d25e 100644 --- a/.github/workflows/issue_comp_link-pr-to-issue.yml +++ b/.github/workflows/issue_comp_link-pr-to-issue.yml @@ -1,4 +1,3 @@ -# action.yml name: 'Link Pull Request to Issue' on: workflow_call: diff --git a/.github/workflows/issue_open-pr.yml b/.github/workflows/issue_open-pr.yml new file mode 100644 index 000000000000..37249fdeead7 --- /dev/null +++ b/.github/workflows/issue_open-pr.yml @@ -0,0 +1,12 @@ +name: PR opened + +on: + pull_request: + types: [opened] + +jobs: + add-issue-to-pr: + name: Add Issue to PR + uses: ./.github/workflows/issue_comp-link-issue-to-pr.yml + with: + pr_branch: ${{ github.head_ref }}