From 661ee6a2443882575107b6d1044144306e38e64f Mon Sep 17 00:00:00 2001 From: Nicolas Sarlin Date: Mon, 22 Jul 2024 17:52:00 +0200 Subject: [PATCH] chore(ci): automatically merge pr in the data repo --- .github/workflows/data_pr_close.yml | 189 ++++++++++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 .github/workflows/data_pr_close.yml diff --git a/.github/workflows/data_pr_close.yml b/.github/workflows/data_pr_close.yml new file mode 100644 index 0000000000..58d13aa60c --- /dev/null +++ b/.github/workflows/data_pr_close.yml @@ -0,0 +1,189 @@ +name: Close or Merge corresponding PR on the data repo + +# When a PR with the data_PR tag is closed or merged, this will close the corresponding PR in the data repo. + +env: + TARGET_REPO_API_URL: ${{ github.api_url }}/repos/zama-ai/tfhe-backward-compat-data + SLACK_CHANNEL: ${{ secrets.SLACK_CHANNEL }} + SLACK_ICON: https://pbs.twimg.com/profile_images/1274014582265298945/OjBKP9kn_400x400.png + SLACK_USERNAME: ${{ secrets.BOT_USERNAME }} + SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }} + +# only trigger on pull request closed events +on: + pull_request: + types: [ closed ] + +# The same pattern is used for jobs that use the github api: +# - save the result of the API call in the env var "GH_API_RES". Since the var is multiline +# we use this trick: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#example-of-a-multiline-string +# - "set +e" will make sure we reach the last "echo EOF" even in case of error +# - "set -o" pipefail makes one line piped command return the error of the first failure +# - 'RES="$?"' and 'exit $RES' are used to return the error code if a command failed. Without it, with "set +e" +# the script will always return 0 because of the "echo EOF". + + +jobs: + merge_job: + # this job will only run if the PR has been merged + if: ${{ github.event.pull_request.merged == true && contains(github.event.pull_request.labels.*.name, 'data_PR') }} + runs-on: ubuntu-latest + steps: + - name: Find corresponding Pull Request in the data repo + env: + PR_BRANCH: ${{ github.head_ref || github.ref_name }} + run: | + { + set +e + set -o pipefail + echo 'GH_API_RES<> "${GITHUB_ENV}" + exit $RES + + - name: Comment on the PR to indicate the reason of the merge + run: | + { + set +e + set -o pipefail + echo 'GH_API_RES<> "${GITHUB_ENV}" + exit $RES + + - name: Merge the Pull Request in the data repo + run: | + { + set +e + set -o pipefail + echo 'GH_API_RES<> "${GITHUB_ENV}" + exit $RES + + - name: Delete the associated branch in the data repo + env: + PR_BRANCH: ${{ github.head_ref || github.ref_name }} + run: | + { + set +e + set -o pipefail + echo 'GH_API_RES<> "${GITHUB_ENV}" + exit $RES + + - name: Slack Notification + if: ${{ always() && job.status == 'failure' }} + continue-on-error: true + uses: rtCamp/action-slack-notify@4e5fb42d249be6a45a298f3c9543b111b02f7907 + env: + SLACK_COLOR: ${{ job.status }} + SLACK_MESSAGE: "Failed to auto-merge PR on data repo: ${{ fromJson(env.GH_API_RES).message }}" + + close_job: + # this job will only run if the PR has been closed without being merged + if: ${{ github.event.pull_request.merged == false && contains(github.event.pull_request.labels.*.name, 'data_PR') }} + runs-on: ubuntu-latest + steps: + - name: Find corresponding Pull Request in the data repo + env: + PR_BRANCH: ${{ github.head_ref || github.ref_name }} + run: | + { + set +e + set -o pipefail + echo 'GH_API_RES<> "${GITHUB_ENV}" + exit $RES + + - name: Comment on the PR to indicate the reason of the close + run: | + { + set +e + set -o pipefail + echo 'GH_API_RES<> "${GITHUB_ENV}" + exit $RES + + - name: Close the Pull Request in the data repo + run: | + { + set +e + set -o pipefail + echo 'GH_API_RES<> "${GITHUB_ENV}" + exit $RES + + - name: Delete the associated branch in the data repo + env: + PR_BRANCH: ${{ github.head_ref || github.ref_name }} + run: | + { + set +e + set -o pipefail + echo 'GH_API_RES<> "${GITHUB_ENV}" + exit $RES + + - name: Slack Notification + if: ${{ always() && job.status == 'failure' }} + continue-on-error: true + uses: rtCamp/action-slack-notify@4e5fb42d249be6a45a298f3c9543b111b02f7907 + env: + SLACK_COLOR: ${{ job.status }} + SLACK_MESSAGE: "Failed to auto-close PR on data repo: ${{ fromJson(env.GH_API_RES).message }}"