-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#28717: linking issue to PR when PR is opened
- Loading branch information
1 parent
4b3e528
commit 4224a6d
Showing
3 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |