[GHF] Preserve revert reason in commit message (#77798)
If explanation were given to mergebot it should be preserved in commit
history
Pull Request resolved: https://github.com/pytorch/pytorch/pull/77798
Approved by: https://github.com/seemethere, https://github.com/janeyx99
diff --git a/.github/scripts/trymerge.py b/.github/scripts/trymerge.py
index 23ea7d5..a362fdc 100755
--- a/.github/scripts/trymerge.py
+++ b/.github/scripts/trymerge.py
@@ -386,6 +386,7 @@
parser.add_argument("--revert", action="store_true")
parser.add_argument("--force", action="store_true")
parser.add_argument("--comment-id", type=int)
+ parser.add_argument("--reason", type=str)
parser.add_argument("pr_num", type=int)
return parser.parse_args()
@@ -824,7 +825,10 @@
raise RuntimeError(reject_reason)
-def try_revert(repo: GitRepo, pr: GitHubPR, *, dry_run: bool = False, comment_id: Optional[int] = None) -> None:
+def try_revert(repo: GitRepo, pr: GitHubPR, *,
+ dry_run: bool = False,
+ comment_id: Optional[int] = None,
+ reason: Optional[str] = None) -> None:
def post_comment(msg: str) -> None:
gh_post_comment(pr.org, pr.project, pr.pr_num, msg, dry_run=dry_run)
if not pr.is_closed():
@@ -858,7 +862,8 @@
repo.revert(commit_sha)
msg = repo.commit_message("HEAD")
msg = re.sub(RE_PULL_REQUEST_RESOLVED, "", msg)
- msg += f"\nReverted {pr.get_pr_url()} on behalf of {prefix_with_github_url(author_login)}\n"
+ msg += f"\nReverted {pr.get_pr_url()} on behalf of {prefix_with_github_url(author_login)}"
+ msg += f" due to {reason}\n" if reason is not None else "\n"
repo.amend_commit_message(msg)
repo.push(pr.default_branch(), dry_run)
if not dry_run:
@@ -911,7 +916,7 @@
if args.revert:
try:
- try_revert(repo, pr, dry_run=args.dry_run, comment_id=args.comment_id)
+ try_revert(repo, pr, dry_run=args.dry_run, comment_id=args.comment_id, reason=args.reason)
except Exception as e:
handle_exception(e, f"Reverting PR {args.pr_num} failed")
return
diff --git a/.github/workflows/revert.yml b/.github/workflows/revert.yml
index 05e7e68..5b7ca20 100644
--- a/.github/workflows/revert.yml
+++ b/.github/workflows/revert.yml
@@ -29,13 +29,22 @@
GITHUB_TOKEN: ${{ secrets.MERGEBOT_TOKEN }}
PR_NUM: ${{ github.event.client_payload.pr_num }}
COMMENT_ID: ${{ github.event.client_payload.comment_id }}
+ REASON: ${{ github.event.client_payload.reason }}
GH_RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
run: |
set -ex
if [ -n "${COMMENT_ID}" ]; then
- python3 .github/scripts/trymerge.py --revert --comment-id "${COMMENT_ID}" "${PR_NUM}"
+ if [ -n "${REASON}" ]; then
+ python3 .github/scripts/trymerge.py --revert --comment-id "${COMMENT_ID}" --reason "${REASON}" "${PR_NUM}"
+ else
+ python3 .github/scripts/trymerge.py --revert --comment-id "${COMMENT_ID}" "${PR_NUM}"
+ fi
else
- python3 .github/scripts/trymerge.py --revert "${PR_NUM}"
+ if [ -n "${REASON}" ]; then
+ python3 .github/scripts/trymerge.py --revert --reason "${REASON}" "${PR_NUM}"
+ else
+ python3 .github/scripts/trymerge.py --revert "${PR_NUM}"
+ fi
fi
concurrency: try-revert