Skip to content

Commit

Permalink
#28717: linking issue to PR when PR is opened
Browse files Browse the repository at this point in the history
  • Loading branch information
victoralfaro-dotcms committed Oct 4, 2024
1 parent 4b3e528 commit 4224a6d
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
90 changes: 90 additions & 0 deletions .github/workflows/issue_comp_link-issue-to-pr.yml
Original file line number Diff line number Diff line change
@@ -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"
1 change: 0 additions & 1 deletion .github/workflows/issue_comp_link-pr-to-issue.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# action.yml
name: 'Link Pull Request to Issue'
on:
workflow_call:
Expand Down
12 changes: 12 additions & 0 deletions .github/workflows/issue_open-pr.yml
Original file line number Diff line number Diff line change
@@ -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 }}

0 comments on commit 4224a6d

Please sign in to comment.