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
name: Close or Merge corresponding PR on the data repo | |
env: | |
TARGET_REPO_API_URL: ${{ github.api_url }}/repos/nsarlin-zama/test-target-repo | |
# only trigger on pull request closed events | |
on: | |
pull_request: | |
types: [ closed ] | |
jobs: | |
merge_job: | |
# this job will only run if the PR has been merged | |
if: github.event.pull_request.merged == true | |
runs-on: ubuntu-latest | |
steps: | |
- name: Find corresponding Pull Request in the data repo | |
env: | |
PR_BRANCH: ${{ github.head_ref || github.ref_name }} | |
run: | | |
{ | |
echo 'TARGET_REPO_PR<<EOF' | |
curl -f --no-progress-meter -L -X GET \ | |
-H "Accept: application/vnd.github+json" \ | |
-H "X-GitHub-Api-Version: 2022-11-28" \ | |
${{ env.TARGET_REPO_API_URL }}/pulls \ | |
-d '{ "head": "${{ env.GITHUB_REPOSITORY_OWNER }}:${{ env.PR_BRANCH }}", "state": "open" }' | jq -e '.[0]' | |
echo EOF | |
} >> "${GITHUB_ENV}" | |
- name: Comment on the PR to indicate the reason of the merge | |
run: | | |
set -o pipefail | |
curl --fail-with-body --no-progress-meter -L -X POST \ | |
-H "Accept: application/vnd.github+json" \ | |
-H "Authorization: Bearer ${{ secrets.TARGET_REPO_TOKEN }}" \ | |
-H "X-GitHub-Api-Version: 2022-11-28" \ | |
${{ fromJson(env.TARGET_REPO_PR).comments_url }} \ | |
-d '{ "body": "PR merged because the corresponding PR in main repo was merged: ${{ github.repository }}#${{ github.event.number }}" }' | tee curl_ouptut.txt | |
- name: Merge the Pull Request in the data repo | |
run: | | |
set -o pipefail | |
curl --fail-with-body --no-progress-meter -L -X PUT \ | |
-H "Accept: application/vnd.github+json" \ | |
-H "Authorization: Bearer ${{ secrets.TARGET_REPO_TOKEN }}" \ | |
-H "X-GitHub-Api-Version: 2022-11-28" \ | |
${{ fromJson(env.TARGET_REPO_PR).url }}/merge \ | |
-d '{ "merge_method": "rebase" }' | tee curl_ouptut.txt | |
- name: Delete the associated branch in the data repo | |
env: | |
PR_BRANCH: ${{ github.head_ref || github.ref_name }} | |
run: | | |
set -o pipefail | |
curl --fail-with-body --no-progress-meter -L -X DELETE \ | |
-H "Accept: application/vnd.github+json" \ | |
-H "Authorization: Bearer ${{ secrets.TARGET_REPO_TOKEN }}" \ | |
-H "X-GitHub-Api-Version: 2022-11-28" \ | |
${{ env.TARGET_REPO_API_URL }}/git/refs/heads/${{ env.PR_BRANCH }} | tee curl_ouptut.txt | |
- name: Slack Notification | |
if: ${{ always() && job.status == 'failure' }} | |
continue-on-error: true | |
run: | | |
echo "Failed to auto-close : ${{ fromJson(env.GH_API_RES).errors[0].message }}" | |
- name: Slack Notification | |
if: ${{ always() && job.status == 'failure' }} | |
continue-on-error: true | |
run: | | |
echo "Failed to auto-close !" | |
close_job: | |
# this job will only run if the PR has been closed without being merged | |
if: github.event.pull_request.merged == false | |
runs-on: ubuntu-latest | |
steps: | |
- name: Find corresponding Pull Request in the data repo | |
env: | |
PR_BRANCH: ${{ github.head_ref || github.ref_name }} | |
run: | | |
{ | |
echo 'TARGET_REPO_PR<<EOF' | |
curl -f --no-progress-meter -L -X GET \ | |
-H "Accept: application/vnd.github+json" \ | |
-H "X-GitHub-Api-Version: 2022-11-28" \ | |
${{ env.TARGET_REPO_API_URL }}/pulls \ | |
-d '{ "head": "${{ env.GITHUB_REPOSITORY_OWNER }}:${{ env.PR_BRANCH }}", "state": "open" }' | jq -e '.[0]' | |
echo EOF | |
} >> "${GITHUB_ENV}" | |
- name: Comment on the PR to indicate the reason of the close | |
run: | | |
curl -f --no-progress-meter -L -X POST \ | |
-H "Accept: application/vnd.github+json" \ | |
-H "Authorization: Bearer ${{ secrets.TARGET_REPO_TOKEN }}" \ | |
-H "X-GitHub-Api-Version: 2022-11-28" \ | |
${{ fromJson(env.TARGET_REPO_PR).comments_url }} \ | |
-d '{ "body": "PR closed because the corresponding PR in main repo was closed: ${{ github.repository }}#${{ github.event.number }}" }' | |
- name: Close the Pull Request in the data repo | |
run: | | |
curl -f --no-progress-meter -L -X PATCH \ | |
-H "Accept: application/vnd.github+json" \ | |
-H "Authorization: Bearer ${{ secrets.TARGET_REPO_TOKEN }}" \ | |
-H "X-GitHub-Api-Version: 2022-11-28" \ | |
${{ fromJson(env.TARGET_REPO_PR).url }} \ | |
-d '{ "state": "closed" }' | |
- name: Delete the associated branch in the data repo | |
env: | |
PR_BRANCH: ${{ github.head_ref || github.ref_name }} | |
run: | | |
curl -f --no-progress-meter -L -X DELETE \ | |
-H "Accept: application/vnd.github+json" \ | |
-H "Authorization: Bearer ${{ secrets.TARGET_REPO_TOKEN }}" \ | |
-H "X-GitHub-Api-Version: 2022-11-28" \ | |
${{ env.TARGET_REPO_API_URL }}/git/refs/heads/${{ env.PR_BRANCH }} | |