diff --git a/.github/actions/backport/action.yml b/.github/actions/backport/action.yml new file mode 100644 index 0000000..107de84 --- /dev/null +++ b/.github/actions/backport/action.yml @@ -0,0 +1,83 @@ +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 + TARGET_BRANCH: + description: The branch in the repository to backport changes into + required: true + REF_TO_BACKPORT: + description: The reference of the commit to be backported + required: true + +env: + # Not possible to set this as a default + # https://github.com/orgs/community/discussions/46670 + shell: bash + +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: | + # 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.REF_TO_BACKPORT }}" into \"${backport_target_branch}\"" + + ${GITHUB_ACTION_PATH}/../../../backport \ + "${{ inputs.REF_TO_BACKPORT }}" \ + "${backport_target_branch}" \ + --non-interactive + env: + GH_TOKEN: ${{ github.token }} + + - name: Report errors + shell: ${{ env.shell }} + 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 "- 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" + 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 }}