From 28a32f9c50b0fc4ef62f2b00ed07a4f59da490bb Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Fri, 6 Dec 2024 14:43:17 +0800 Subject: [PATCH] github: do not nest ${{}} inside condition In commit 2596d157, we added a condition to run auto-backport.py only when the GitHub Action is triggered by a push to the default branch. However, this introduced an unexpected error due to incorrect condition handling. Problem: - `github.event.before` evaluates to an empty string - GitHub Actions' single-pass expression evaluation system causes the step to always execute, regardless of `github.event_name` Despite GitHub's documentation suggesting that ${{ }} can be omitted, it recommends using explicit ${{}} expressions for compound conditions. Changes: - Use explicit ${{}} expression for compound conditions - Avoid string interpolation in conditional statements Root Cause: The previous implementation failed because of how GitHub Actions evaluates conditional expressions, leading to an unintended script execution and a 404 error when attempting to compare commits. Example Error: ``` python .github/scripts/auto-backport.py --repo scylladb/scylladb --base-branch refs/heads/master --commits ..2b07d93beac7bc83d955dadc20ccc307f13f20b6 shell: /usr/bin/bash -e {0} env: DEFAULT_BRANCH: master GITHUB_TOKEN: *** Traceback (most recent call last): File "/home/runner/work/scylladb/scylladb/.github/scripts/auto-backport.py", line 201, in main() File "/home/runner/work/scylladb/scylladb/.github/scripts/auto-backport.py", line 162, in main commits = repo.compare(start_commit, end_commit).commits File "/usr/lib/python3/dist-packages/github/Repository.py", line 888, in compare headers, data = self._requester.requestJsonAndCheck( File "/usr/lib/python3/dist-packages/github/Requester.py", line 353, in requestJsonAndCheck return self.__check( File "/usr/lib/python3/dist-packages/github/Requester.py", line 378, in __check raise self.__createException(status, responseHeaders, output) github.GithubException.UnknownObjectException: 404 {"message": "Not Found", "documentation_url": "https://docs.github.com/rest/commits/commits#compare-two-commits", "status": "404"} ``` Fixes scylladb/scylladb#21808 Signed-off-by: Kefu Chai Closes scylladb/scylladb#21809 (cherry picked from commit e04aca7efe112eb1f568b49f63a0cb19804700a6) Closes scylladb/scylladb#21820 --- .github/workflows/add-label-when-promoted.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/add-label-when-promoted.yaml b/.github/workflows/add-label-when-promoted.yaml index 15b6c7e1fd..a071084740 100644 --- a/.github/workflows/add-label-when-promoted.yaml +++ b/.github/workflows/add-label-when-promoted.yaml @@ -49,7 +49,7 @@ jobs: GITHUB_TOKEN: ${{ secrets.AUTO_BACKPORT_TOKEN }} run: python .github/scripts/label_promoted_commits.py --commits ${{ github.event.before }}..${{ github.sha }} --repository ${{ github.repository }} --ref ${{ github.ref }} - name: Run auto-backport.py when promotion completed - if: github.event_name == 'push' && github.ref == 'refs/heads/${{ env.DEFAULT_BRANCH }}' + if: ${{ github.event_name == 'push' && github.ref == format('refs/heads/{0}', env.DEFAULT_BRANCH) }} env: GITHUB_TOKEN: ${{ secrets.AUTO_BACKPORT_TOKEN }} run: python .github/scripts/auto-backport.py --repo ${{ github.repository }} --base-branch ${{ github.ref }} --commits ${{ github.event.before }}..${{ github.sha }} @@ -65,7 +65,7 @@ jobs: echo "backport_label=false" >> $GITHUB_OUTPUT fi - name: Run auto-backport.py when label was added - if: github.event_name == 'pull_request_target' && steps.check_label.outputs.backport_label == 'true' && (github.event.pull_request.state == 'closed' && github.event.pull_request.merged == true) + if: ${{ github.event_name == 'pull_request_target' && steps.check_label.outputs.backport_label == 'true' && github.event.pull_request.merged == true }} env: GITHUB_TOKEN: ${{ secrets.AUTO_BACKPORT_TOKEN }} run: python .github/scripts/auto-backport.py --repo ${{ github.repository }} --base-branch ${{ github.ref }} --pull-request ${{ github.event.pull_request.number }} --head-commit ${{ github.event.pull_request.base.sha }}