Skip to content

Commit

Permalink
Link merged PR to issue (#28808)
Browse files Browse the repository at this point in the history
  • Loading branch information
victoralfaro-dotcms authored Jun 10, 2024
1 parent 7b54550 commit 97fed9e
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
65 changes: 65 additions & 0 deletions .github/workflows/link-pr-to-issue.yml
Original file line number Diff line number Diff line change
@@ -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}\""
17 changes: 17 additions & 0 deletions .github/workflows/post-pr-merge.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 97fed9e

Please sign in to comment.