Skip to content

Commit cc48130

Browse files
authored
Merge pull request #2225 from nebius/SCHED-993/revert-latest-pr
SCHED-993: Add workflow to trigger revert-latest-pr via PR comment
2 parents 09d4535 + c0b3b51 commit cc48130

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: "Revert last PR in merge-to-main"
2+
3+
on:
4+
issue_comment:
5+
types: [created]
6+
7+
jobs:
8+
revert:
9+
name: Revert last PR
10+
if: |
11+
github.event.issue.pull_request
12+
&& startsWith(github.event.issue.title, 'Merge to ')
13+
&& github.event.comment.body == '/revert-last-pr'
14+
runs-on: self-hosted
15+
16+
permissions:
17+
contents: write
18+
pull-requests: write
19+
20+
steps:
21+
- name: Checkout repository
22+
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
23+
with:
24+
fetch-depth: 0
25+
26+
- name: Revert last PR
27+
env:
28+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
29+
run: .github/workflows/scripts/revert-latest-pr.sh "${{ github.event.issue.number }}"

.github/workflows/scripts/auto-merge-back.sh

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,23 @@ create_pull_request() {
133133
134134
# Original PR Description
135135
136-
${PR_BODY}"
136+
${PR_BODY}
137+
138+
---
139+
140+
> You cannot skip this merge, but if you really don't want these changes (conflicts or doesn't make sense), just comment \`/revert-last-pr\`, and wait for the revert to come, then merge this PR (even if 0 changes)."
137141
else
138142
# Fallback for commits without PRs
139143
pr_body="This is merge back of commit ${COMMIT_SHORT_SHA} by @${USERNAME}
140144
141145
Commit message:
142146
\`\`\`
143147
${COMMIT_MESSAGE}
144-
\`\`\`"
148+
\`\`\`
149+
150+
---
151+
152+
> You cannot skip this merge, but if you really don't want these changes (conflicts or doesn't make sense), just comment \`/revert-last-pr\`, and wait for the revert to come, then merge this PR (even if 0 changes)."
145153
fi
146154

147155
# Create the PR
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
MERGE_PR="$1"
5+
WORKTREE_DIR=""
6+
7+
cleanup() {
8+
if [ -n "$WORKTREE_DIR" ] && [ -d "$WORKTREE_DIR" ]; then
9+
echo "Cleaning up worktree..."
10+
git worktree remove --force "$WORKTREE_DIR"
11+
fi
12+
}
13+
trap cleanup EXIT
14+
15+
ORIG_PR=$(gh pr view "$MERGE_PR" --json body -q '.body' \
16+
| sed -n 's/.*Pull Request #\([0-9]*\).*/\1/p')
17+
if [ -z "$ORIG_PR" ]; then
18+
echo "Error: could not find original PR number in PR #$MERGE_PR body"
19+
exit 1
20+
fi
21+
echo "Original PR: #$ORIG_PR"
22+
23+
MERGE_SHA=$(gh pr view "$ORIG_PR" --json mergeCommit -q '.mergeCommit.oid')
24+
if [ -z "$MERGE_SHA" ]; then
25+
echo "Error: PR #$ORIG_PR has no merge commit (not merged yet?)"
26+
exit 1
27+
fi
28+
echo "Merge commit: ${MERGE_SHA:0:7}"
29+
30+
BRANCH=$(gh pr view "$MERGE_PR" --json headRefName -q '.headRefName')
31+
echo "Branch: $BRANCH"
32+
33+
git fetch origin "$BRANCH"
34+
WORKTREE_DIR=$(mktemp -d)
35+
git worktree add "$WORKTREE_DIR" "origin/$BRANCH"
36+
37+
pushd "$WORKTREE_DIR" > /dev/null
38+
git checkout -B "$BRANCH" "origin/$BRANCH"
39+
# Merge commits need -m 1 to specify which parent to revert to
40+
PARENT_COUNT=$(git cat-file -p "$MERGE_SHA" | grep -c '^parent ')
41+
if [ "$PARENT_COUNT" -gt 1 ]; then
42+
echo "Merge commit detected, reverting with -m 1"
43+
git revert --no-edit -m 1 "$MERGE_SHA"
44+
else
45+
git revert --no-edit "$MERGE_SHA"
46+
fi
47+
popd > /dev/null
48+
49+
git -C "$WORKTREE_DIR" push origin "$BRANCH"
50+
51+
gh pr comment "$MERGE_PR" \
52+
--body "Reverted changes from PR #$ORIG_PR (commit ${MERGE_SHA:0:7})"
53+
echo "Done. Reverted PR #$ORIG_PR on merge-to-main PR #$MERGE_PR"

0 commit comments

Comments
 (0)