Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#28717: linking issue to PR when PR is opened #30266

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }}