From 70ac1b036a6452cfb06f4be83693aae0ba3ccc08 Mon Sep 17 00:00:00 2001 From: Jack Green Date: Mon, 25 Nov 2024 17:21:17 +0000 Subject: [PATCH 01/13] centralise-to-workflow --- .github/actions/backport/action.yml | 67 +++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 .github/actions/backport/action.yml diff --git a/.github/actions/backport/action.yml b/.github/actions/backport/action.yml new file mode 100644 index 0000000..3d7ca30 --- /dev/null +++ b/.github/actions/backport/action.yml @@ -0,0 +1,67 @@ +name: Backport + +inputs: + GITHUB_TOKEN: + required: true + REPO_LOCATION: + required: true + TARGET_BRANCH: + required: true + COMMIT_SHA: + required: true + BACKPORT_OPTIONS: + +env: + # Not possible to set this as a default + # https://github.com/orgs/community/discussions/46670 + shell: bash + backport_location: backport + +runs: + using: composite + steps: + - uses: actions/checkout@v4 + with: + repository: hazelcast/backport + path: ${{ env.backport_location }} + + - name: Checkout maintenance branch and cherry-pick + shell: ${{ env.shell }} + working-directory: ${{ inputs.REPO_LOCATION }} + run: | + # Git metadata is required but not available out-of-the-box, inherit from the action + git config user.name "${GITHUB_ACTOR}" + git config user.email "${GITHUB_ACTOR}@users.noreply.github.com" + + # Add "upstream" remote as checkout action doesn't include by default + git remote add upstream "${{ github.event.repository.clone_url }}" + git fetch --all + + backport_target_branch=upstream/"${{ inputs.TARGET_BRANCH }}" + echo "::debug::Running backport script to backport "${{ inputs.COMMIT_SHA }}" into \"${backport_target_branch}\"" + + ${GITHUB_WORKSPACE}/${backport_location}/backport \ + "${{ inputs.COMMIT_SHA }}" \ + "${backport_target_branch}" \ + --non-interactive \ + ${{ inputs.BACKPORT_OPTIONS }} + env: + GH_TOKEN: ${{ github.token }} + + - name: Report errors + if: failure() + run: | + echo "Error running action" + echo "Troubleshooting Information" + echo "- Repositories' GitHub action configuration - $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/settings/actions" + echo "- Action does not run when triggered from a forked repo's PR?" + echo " Enable \"Run workflows from fork pull requests\"" + echo "- Backport fails with \"GraphQL: GitHub Actions is not permitted to create or approve pull requests (createPullRequest)\"?" + echo " Either:" + echo " * Enable \"Allow GitHub Actions to create and approve pull requests\"" + echo " * Use a different \"GITHUB_TOKEN\" with appropriate permissions" + echo "- \"GraphQL: Resource not accessible by integration (addComment)\"?" + echo " Either:" + echo " * Enable \"Send write tokens to workflows from fork pull requests\"" + echo " * Use a different \"GITHUB_TOKEN\" with appropriate permissions" + gh pr comment "${{ github.event.pull_request.number }}" --repo $GITHUB_SERVER_URL/$GITHUB_REPOSITORY --body "❌ [Failed to backport]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID), change must be manually backported." From ac5e8c2bf8a4a88e74cb98ed6aa5016596b47fbe Mon Sep 17 00:00:00 2001 From: Jack Green Date: Tue, 26 Nov 2024 11:29:18 +0000 Subject: [PATCH 02/13] Update action.yml --- .github/actions/backport/action.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/actions/backport/action.yml b/.github/actions/backport/action.yml index 3d7ca30..d356017 100644 --- a/.github/actions/backport/action.yml +++ b/.github/actions/backport/action.yml @@ -10,6 +10,7 @@ inputs: COMMIT_SHA: required: true BACKPORT_OPTIONS: + required: false env: # Not possible to set this as a default @@ -49,10 +50,11 @@ runs: GH_TOKEN: ${{ github.token }} - name: Report errors + shell: ${{ env.shell }} if: failure() run: | - echo "Error running action" - echo "Troubleshooting Information" + echo ":error::Error running action" + echo "::group::Troubleshooting Information:" echo "- Repositories' GitHub action configuration - $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/settings/actions" echo "- Action does not run when triggered from a forked repo's PR?" echo " Enable \"Run workflows from fork pull requests\"" @@ -64,4 +66,5 @@ runs: echo " Either:" echo " * Enable \"Send write tokens to workflows from fork pull requests\"" echo " * Use a different \"GITHUB_TOKEN\" with appropriate permissions" + echo "::endgroup::" gh pr comment "${{ github.event.pull_request.number }}" --repo $GITHUB_SERVER_URL/$GITHUB_REPOSITORY --body "❌ [Failed to backport]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID), change must be manually backported." From 799cd10743005ec303e5220618002b3b09a5cbae Mon Sep 17 00:00:00 2001 From: Jack Green Date: Tue, 26 Nov 2024 11:38:46 +0000 Subject: [PATCH 03/13] Update action.yml --- .github/actions/backport/action.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.github/actions/backport/action.yml b/.github/actions/backport/action.yml index d356017..4f4f980 100644 --- a/.github/actions/backport/action.yml +++ b/.github/actions/backport/action.yml @@ -26,6 +26,11 @@ runs: repository: hazelcast/backport path: ${{ env.backport_location }} + - name: Debug + shell: ${{ env.shell }} + run: | + ls + - name: Checkout maintenance branch and cherry-pick shell: ${{ env.shell }} working-directory: ${{ inputs.REPO_LOCATION }} @@ -68,3 +73,5 @@ runs: echo " * Use a different \"GITHUB_TOKEN\" with appropriate permissions" echo "::endgroup::" gh pr comment "${{ github.event.pull_request.number }}" --repo $GITHUB_SERVER_URL/$GITHUB_REPOSITORY --body "❌ [Failed to backport]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID), change must be manually backported." + env: + GH_TOKEN: ${{ github.token }} From 0a7bdd1410d05da1007c2bf952889b509d956d28 Mon Sep 17 00:00:00 2001 From: Jack Green Date: Tue, 26 Nov 2024 11:42:57 +0000 Subject: [PATCH 04/13] Update action.yml --- .github/actions/backport/action.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/actions/backport/action.yml b/.github/actions/backport/action.yml index 4f4f980..18ab367 100644 --- a/.github/actions/backport/action.yml +++ b/.github/actions/backport/action.yml @@ -24,7 +24,8 @@ runs: - uses: actions/checkout@v4 with: repository: hazelcast/backport - path: ${{ env.backport_location }} + # path: ${{ env.backport_location }} + path: backport - name: Debug shell: ${{ env.shell }} From 3934262327851d02baae3b37193b9a8ec2780aa6 Mon Sep 17 00:00:00 2001 From: Jack Green Date: Tue, 26 Nov 2024 11:45:04 +0000 Subject: [PATCH 05/13] Update action.yml --- .github/actions/backport/action.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/actions/backport/action.yml b/.github/actions/backport/action.yml index 18ab367..a26af51 100644 --- a/.github/actions/backport/action.yml +++ b/.github/actions/backport/action.yml @@ -16,7 +16,6 @@ env: # Not possible to set this as a default # https://github.com/orgs/community/discussions/46670 shell: bash - backport_location: backport runs: using: composite @@ -24,7 +23,8 @@ runs: - uses: actions/checkout@v4 with: repository: hazelcast/backport - # path: ${{ env.backport_location }} + # Hardcoded as composite actions don't support env vars properly + # https://github.com/orgs/community/discussions/51280 path: backport - name: Debug @@ -47,7 +47,7 @@ runs: backport_target_branch=upstream/"${{ inputs.TARGET_BRANCH }}" echo "::debug::Running backport script to backport "${{ inputs.COMMIT_SHA }}" into \"${backport_target_branch}\"" - ${GITHUB_WORKSPACE}/${backport_location}/backport \ + ${GITHUB_WORKSPACE}/backport/backport \ "${{ inputs.COMMIT_SHA }}" \ "${backport_target_branch}" \ --non-interactive \ From d2a1625fe1e36e68c1df3917cc4e5ffe01eb5f78 Mon Sep 17 00:00:00 2001 From: Jack Green Date: Tue, 26 Nov 2024 11:51:19 +0000 Subject: [PATCH 06/13] Update action.yml --- .github/actions/backport/action.yml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/.github/actions/backport/action.yml b/.github/actions/backport/action.yml index a26af51..f38da37 100644 --- a/.github/actions/backport/action.yml +++ b/.github/actions/backport/action.yml @@ -27,11 +27,6 @@ runs: # https://github.com/orgs/community/discussions/51280 path: backport - - name: Debug - shell: ${{ env.shell }} - run: | - ls - - name: Checkout maintenance branch and cherry-pick shell: ${{ env.shell }} working-directory: ${{ inputs.REPO_LOCATION }} From a14b9c75bf63357ca377f66d4c9168b2ce827c1a Mon Sep 17 00:00:00 2001 From: Jack Date: Tue, 26 Nov 2024 13:08:14 +0000 Subject: [PATCH 07/13] Add better documentation --- .github/actions/backport/action.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/actions/backport/action.yml b/.github/actions/backport/action.yml index 3d7ca30..c975ac7 100644 --- a/.github/actions/backport/action.yml +++ b/.github/actions/backport/action.yml @@ -1,13 +1,18 @@ name: Backport +# A composite action that allows Backporting via GitHub Actions (e.g. to be driven based on labels etc) + inputs: GITHUB_TOKEN: required: true REPO_LOCATION: + description: The location of the checked-out repository to work with required: true TARGET_BRANCH: + description: The branch in the repository to backport changes into required: true COMMIT_SHA: + description: The reference of the commit to be backported required: true BACKPORT_OPTIONS: From f2a5e0e5d98e3e23b46a1008af0c721e0cbe73e7 Mon Sep 17 00:00:00 2001 From: Jack Date: Tue, 26 Nov 2024 13:09:03 +0000 Subject: [PATCH 08/13] wip --- .github/actions/backport/action.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/actions/backport/action.yml b/.github/actions/backport/action.yml index 914f954..06d11d1 100644 --- a/.github/actions/backport/action.yml +++ b/.github/actions/backport/action.yml @@ -15,6 +15,7 @@ inputs: description: The reference of the commit to be backported required: true BACKPORT_OPTIONS: + description: Additional options to pass through to the tool required: false env: From 0b32856d58db1e20736a01f8a2c2debb87f2b753 Mon Sep 17 00:00:00 2001 From: Jack Date: Tue, 26 Nov 2024 13:10:18 +0000 Subject: [PATCH 09/13] `COMMIT_SHA` -> `REF_TO_BACKPORT` --- .github/actions/backport/action.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/actions/backport/action.yml b/.github/actions/backport/action.yml index 06d11d1..095bc37 100644 --- a/.github/actions/backport/action.yml +++ b/.github/actions/backport/action.yml @@ -11,7 +11,7 @@ inputs: TARGET_BRANCH: description: The branch in the repository to backport changes into required: true - COMMIT_SHA: + REF_TO_BACKPORT: description: The reference of the commit to be backported required: true BACKPORT_OPTIONS: @@ -46,10 +46,10 @@ runs: git fetch --all backport_target_branch=upstream/"${{ inputs.TARGET_BRANCH }}" - echo "::debug::Running backport script to backport "${{ inputs.COMMIT_SHA }}" into \"${backport_target_branch}\"" + echo "::debug::Running backport script to backport "${{ inputs.REF_TO_BACKPORT }}" into \"${backport_target_branch}\"" ${GITHUB_WORKSPACE}/backport/backport \ - "${{ inputs.COMMIT_SHA }}" \ + "${{ inputs.REF_TO_BACKPORT }}" \ "${backport_target_branch}" \ --non-interactive \ ${{ inputs.BACKPORT_OPTIONS }} From 77557243fa481e00351ba426b7b17ef3a541d576 Mon Sep 17 00:00:00 2001 From: Jack Date: Tue, 26 Nov 2024 16:37:42 +0000 Subject: [PATCH 10/13] checkout -> `GITHUB_ACTION_PATH` --- .github/actions/backport/action.yml | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/.github/actions/backport/action.yml b/.github/actions/backport/action.yml index 095bc37..3e9d970 100644 --- a/.github/actions/backport/action.yml +++ b/.github/actions/backport/action.yml @@ -26,14 +26,7 @@ env: runs: using: composite steps: - - uses: actions/checkout@v4 - with: - repository: hazelcast/backport - # Hardcoded as composite actions don't support env vars properly - # https://github.com/orgs/community/discussions/51280 - path: backport - - - name: Checkout maintenance branch and cherry-pick + - name: Backport shell: ${{ env.shell }} working-directory: ${{ inputs.REPO_LOCATION }} run: | @@ -48,7 +41,7 @@ runs: backport_target_branch=upstream/"${{ inputs.TARGET_BRANCH }}" echo "::debug::Running backport script to backport "${{ inputs.REF_TO_BACKPORT }}" into \"${backport_target_branch}\"" - ${GITHUB_WORKSPACE}/backport/backport \ + ${GITHUB_ACTION_PATH}/../../../backport \ "${{ inputs.REF_TO_BACKPORT }}" \ "${backport_target_branch}" \ --non-interactive \ From 17853e2d5636f5853e3efd71427acec7fecab464 Mon Sep 17 00:00:00 2001 From: Jack Date: Tue, 26 Nov 2024 17:04:53 +0000 Subject: [PATCH 11/13] Remove `REPO_LOCATION` --- .github/actions/backport/action.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/actions/backport/action.yml b/.github/actions/backport/action.yml index 3e9d970..aeb6303 100644 --- a/.github/actions/backport/action.yml +++ b/.github/actions/backport/action.yml @@ -5,9 +5,6 @@ name: Backport inputs: GITHUB_TOKEN: required: true - REPO_LOCATION: - description: The location of the checked-out repository to work with - required: true TARGET_BRANCH: description: The branch in the repository to backport changes into required: true @@ -28,7 +25,6 @@ runs: steps: - name: Backport shell: ${{ env.shell }} - working-directory: ${{ inputs.REPO_LOCATION }} run: | # Git metadata is required but not available out-of-the-box, inherit from the action git config user.name "${GITHUB_ACTOR}" From 23f82cb71bab6677b8dfbc0594dce3f40d031804 Mon Sep 17 00:00:00 2001 From: Jack Date: Tue, 26 Nov 2024 17:24:09 +0000 Subject: [PATCH 12/13] Migrate assert branch exists --- .github/actions/backport/action.yml | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/.github/actions/backport/action.yml b/.github/actions/backport/action.yml index aeb6303..97f5706 100644 --- a/.github/actions/backport/action.yml +++ b/.github/actions/backport/action.yml @@ -23,6 +23,23 @@ env: runs: using: composite steps: + - name: Assert branch exists + id: assert-branch-exists + shell: ${{ env.shell }} + run: | + if git ls-remote --exit-code --heads origin "${{ inputs.TARGET_BRANCH }}"; then + echo "::debug::Branch ${{ inputs.TARGET_BRANCH }} exists" + else + echo "::error::Branch ${{ inputs.TARGET_BRANCH }} does not exist" + gh pr comment "${{ github.event.pull_request.number }}" \ + --repo ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} \ + --body "❌ The backport branch \`${{ inputs.TARGET_BRANCH }}\` doesn't exist." + echo "failure-already-reported=true" >> ${GITHUB_OUTPUT} + exit 1 + fi + env: + GH_TOKEN: ${{ github.token }} + - name: Backport shell: ${{ env.shell }} run: | @@ -47,11 +64,11 @@ runs: - name: Report errors shell: ${{ env.shell }} - if: failure() + if: failure() && steps.assert-branch-exists.outputs.failure-already-reported != 'true' run: | echo ":error::Error running action" echo "::group::Troubleshooting Information:" - echo "- Repositories' GitHub action configuration - $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/settings/actions" + echo "- Repositories' GitHub action configuration - ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/settings/actions" echo "- Action does not run when triggered from a forked repo's PR?" echo " Enable \"Run workflows from fork pull requests\"" echo "- Backport fails with \"GraphQL: GitHub Actions is not permitted to create or approve pull requests (createPullRequest)\"?" @@ -63,6 +80,8 @@ runs: echo " * Enable \"Send write tokens to workflows from fork pull requests\"" echo " * Use a different \"GITHUB_TOKEN\" with appropriate permissions" echo "::endgroup::" - gh pr comment "${{ github.event.pull_request.number }}" --repo $GITHUB_SERVER_URL/$GITHUB_REPOSITORY --body "❌ [Failed to backport]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID), change must be manually backported." + gh pr comment "${{ github.event.pull_request.number }}" \ + --repo ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} \ + --body "❌ [Failed to backport](${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}), change must be manually backported." env: GH_TOKEN: ${{ github.token }} From a91e220a87adea9d9a1522109b55e6dc7a394378 Mon Sep 17 00:00:00 2001 From: Jack Green Date: Wed, 27 Nov 2024 09:33:56 +0000 Subject: [PATCH 13/13] Remove additional options before merge --- .github/actions/backport/action.yml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/.github/actions/backport/action.yml b/.github/actions/backport/action.yml index 97f5706..107de84 100644 --- a/.github/actions/backport/action.yml +++ b/.github/actions/backport/action.yml @@ -11,9 +11,6 @@ inputs: REF_TO_BACKPORT: description: The reference of the commit to be backported required: true - BACKPORT_OPTIONS: - description: Additional options to pass through to the tool - required: false env: # Not possible to set this as a default @@ -57,8 +54,7 @@ runs: ${GITHUB_ACTION_PATH}/../../../backport \ "${{ inputs.REF_TO_BACKPORT }}" \ "${backport_target_branch}" \ - --non-interactive \ - ${{ inputs.BACKPORT_OPTIONS }} + --non-interactive env: GH_TOKEN: ${{ github.token }}