diff --git a/workflows/backport.yml b/workflows/backport.yml index c5d3471..1a7487a 100644 --- a/workflows/backport.yml +++ b/workflows/backport.yml @@ -23,6 +23,14 @@ # leaves no branch behind: the job answers on the pull request with the reason # and, for a conflict, the commands for a manual backport. # +# A comment of "@ncmake-updater rebase" on an open backport pull request runs +# its backport again on the current target branch, as a new "/backport to" on +# the original would, and answers on the backport pull request. +# create-pull-request rebuilds the branch, so the pull request keeps its one +# verified commit; GitHub's update button would add a merge commit to it. On +# any other pull request the comment is left alone, for the updater workflows +# that answer it. +# # Authentication is the GitHub App of the workflow updater (Contents, Pull # requests and Workflows: write), so the backport pull request triggers CI and # may touch workflow files. Store its credentials as the @@ -33,9 +41,10 @@ name: ncmake backport on: - # "/backport to " in a pull request comment. issue_comment fires for - # every comment in the repository, plain issues included; the collect job - # filters down to pull request comments that carry the command. + # "/backport to " in a pull request comment, and "@ncmake-updater + # rebase" on a backport pull request. issue_comment fires for every comment + # in the repository, plain issues included; the collect job filters down to + # pull request comments that carry a command. issue_comment: # zizmor: ignore[dangerous-triggers] commands only from OWNER/MEMBER/COLLABORATOR, no comment text in run: types: [created] # The merge of a pull request that has commands waiting, seen as the push of @@ -60,7 +69,10 @@ jobs: ( github.event_name == 'issue_comment' && github.event.issue.pull_request - && contains(github.event.comment.body, '/backport') + && ( + contains(github.event.comment.body, '/backport') + || contains(github.event.comment.body, '@ncmake-updater rebase') + ) ) || ( github.event_name == 'push' @@ -75,6 +87,7 @@ jobs: pull-requests: write outputs: pr: ${{ steps.collect.outputs.pr }} + answer: ${{ steps.collect.outputs.answer }} title: ${{ steps.collect.outputs.title }} targets: ${{ steps.collect.outputs.targets }} steps: @@ -149,7 +162,44 @@ jobs: gh api --method POST "repos/$GITHUB_REPOSITORY/issues/$PR/comments" -f body="$1" > /dev/null } - if [ "$GITHUB_EVENT_NAME" = issue_comment ]; then + # "@ncmake-updater rebase" counts on a backport pull request of this + # repository, whose branch ncmake/backport// names the + # original pull request and the line it was opened against. On any + # other pull request the command belongs to the updater workflows, + # and the comment goes on to the /backport checks, which leave it + # alone. The answers stay on the pull request the command is on. + answer_pr=$PR + original= + if [ "$GITHUB_EVENT_NAME" = issue_comment ] \ + && jq -e '.[0].body | contains("@ncmake-updater rebase")' "$comments" > /dev/null; then + read -r head base head_repo < <(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR" \ + --jq '"\(.head.ref) \(.base.ref) \(.head.repo.full_name)"') + rest=${head#ncmake/backport/} + if [ "$head_repo" = "$GITHUB_REPOSITORY" ] && [ "$rest" != "$head" ] \ + && [[ ${rest%%/*} =~ ^[0-9]+$ ]] && [ "${rest#*/}" = "$base" ]; then + original=${rest%%/*} + fi + fi + + if [ -n "$original" ]; then + id=$(jq -r '.[0].id' "$comments") + if ! jq -e --argjson allowed "$allowed" '.[0].author_association as $a | $allowed | index($a)' "$comments" > /dev/null; then + answer "A rebase can be requested by the owner, members and collaborators of this repository only." + echo "targets=[]" >> "$GITHUB_OUTPUT" + exit 0 + fi + if [ "$PR_STATE" != open ]; then + answer "This pull request is closed, so there is nothing to rebase." + echo "targets=[]" >> "$GITHUB_OUTPUT" + exit 0 + fi + react "$id" eyes + # The backport of the original to this line runs again, as a new + # "/backport to" on the original would. + targets=$(jq -c --arg branch "$base" '[{branch: $branch, comment: .[0].id, user: .[0].user.login}]' "$comments") + title=$(gh api "repos/$GITHUB_REPOSITORY/pulls/$original" --jq '.title') + PR=$original + elif [ "$GITHUB_EVENT_NAME" = issue_comment ]; then id=$(jq -r '.[0].id' "$comments") # The job also runs for a comment that only mentions /backport, so # only a line that starts with the command gets an answer. @@ -182,6 +232,7 @@ jobs: { echo "pr=$PR" + echo "answer=$answer_pr" echo "title=$title" echo "targets=$targets" } >> "$GITHUB_OUTPUT" @@ -208,6 +259,9 @@ jobs: pull-requests: write env: PR: ${{ needs.collect.outputs.pr }} + # The pull request the command was written on: the original, or for a + # rebase the backport pull request. + ANSWER_PR: ${{ needs.collect.outputs.answer }} TARGET: ${{ matrix.branch }} COMMENT_ID: ${{ matrix.comment }} COMMENT_USER: ${{ matrix.user }} @@ -286,7 +340,13 @@ jobs: echo "git switch -c $BACKPORT_BRANCH origin/$TARGET" echo "git cherry-pick -x ${commits[*]}" echo "# resolve the conflicts, then git cherry-pick --continue" - echo "git push origin $BACKPORT_BRANCH" + # A rebase finds the backport branch already there, and the + # branch rebuilt by hand has to replace it. + if git ls-remote --exit-code origin "refs/heads/$BACKPORT_BRANCH" > /dev/null; then + echo "git push --force origin $BACKPORT_BRANCH" + else + echo "git push origin $BACKPORT_BRANCH" + fi echo '```' } > "$RUNNER_TEMP/backport-failure.md" exit 1 @@ -335,6 +395,12 @@ jobs: ${{ steps.apply.outputs.commits }} + --- + + Commands, written as a comment on this pull request: + + - `@ncmake-updater rebase` rebuilds this branch on top of the current `${{ matrix.branch }}` + # On the default token: it already has issues and pull-requests write, # so the app token needs no further permission. - name: Label and assign the backport pull request @@ -378,7 +444,7 @@ jobs: env: GH_TOKEN: ${{ github.token }} run: | - gh api --method POST "repos/$GITHUB_REPOSITORY/issues/$PR/comments" \ + gh api --method POST "repos/$GITHUB_REPOSITORY/issues/$ANSWER_PR/comments" \ -f body="The change is already on \`$TARGET\`, so there is nothing to backport." > /dev/null # The failed steps leave their reason in backport-failure.md. @@ -399,5 +465,5 @@ jobs: } > "$RUNNER_TEMP/answer.md" gh api --method POST "repos/$GITHUB_REPOSITORY/issues/comments/$COMMENT_ID/reactions" \ -f content=-1 > /dev/null || true - gh api --method POST "repos/$GITHUB_REPOSITORY/issues/$PR/comments" \ + gh api --method POST "repos/$GITHUB_REPOSITORY/issues/$ANSWER_PR/comments" \ -F body=@"$RUNNER_TEMP/answer.md" > /dev/null