Skip to content

feat: implement gh pr revert - #8826

Merged
BagToad merged 8 commits into
cli:trunkfrom
lucasmelin:lucasmelin/add-pr-revert-command
Oct 23, 2025
Merged

BagToad merged 8 commits into
cli:trunkfrom
lucasmelin:lucasmelin/add-pr-revert-command

Conversation

@lucasmelin

Copy link
Copy Markdown
Contributor

Fixes #6034

Adds a new pr revert command that enables reverting a merged PR using the gh CLI.

Usage

# Revert a PR
$ gh pr revert 123

# Provide revert PR options
$ gh pr revert --draft --title 'Example title' --body 'Example body' 123

@lucasmelin
lucasmelin requested a review from a team as a code owner March 17, 2024 03:43
@lucasmelin
lucasmelin requested review from williammartin and removed request for a team March 17, 2024 03:43
@cliAutomation cliAutomation added the external pull request originating outside of the CLI core team label Mar 17, 2024
@lucasmelin

Copy link
Copy Markdown
Contributor Author

Putting back into draft to add the revert PR details to the success message output.

@lucasmelin
lucasmelin marked this pull request as draft March 17, 2024 13:06
@lucasmelin

Copy link
Copy Markdown
Contributor Author

Added the revert PR details to the message output, ready for review.

@lucasmelin
lucasmelin marked this pull request as ready for review March 17, 2024 13:28
Copilot AI review requested due to automatic review settings July 27, 2025 16:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements a new gh pr revert command that allows users to revert merged pull requests using the GitHub CLI. The feature supports customization of the revert PR with options for title, body, and draft status.

  • Adds core revert functionality with validation to ensure only merged PRs can be reverted
  • Includes comprehensive test coverage for successful reverts, error cases, and option handling
  • Integrates the new command into the existing PR command structure

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
pkg/cmd/pr/revert/revert.go Core implementation of the revert command with options handling and API integration
pkg/cmd/pr/revert/revert_test.go Comprehensive test suite covering success cases, error handling, and option validation
pkg/cmd/pr/pr.go Integration of the revert command into the main PR command structure
api/queries_pr.go GraphQL API function for executing pull request revert mutations

Comment thread pkg/cmd/pr/revert/revert.go Resolved
Comment thread pkg/cmd/pr/revert/revert.go Resolved
Comment thread api/queries_pr.go Resolved
@BagToad

BagToad commented Sep 25, 2025

Copy link
Copy Markdown
Member

👋 Hi @lucasmelin! I just posted the Acceptance Criteria for this issue in #6034 (comment). I also added the help wanted label, so this is now ready for community contributions per our contributing guidelines 😁

It looks like there's some tests failing - please let me know if you're still interested in contributing to this! Please also let me know if you have any questions about the Acceptance Criteria or anything else 😃

cc @connor15mcc since you commented on the issue as well.

@lucasmelin
lucasmelin requested review from a team as code owners October 6, 2025 16:58

@BagToad BagToad left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @lucasmelin 👋 Doing some testing with this, and it's looking good! While testing, I noticed some type errors when we use the full PullRequest type:

❯ gh pr revert 75
X GraphQL: Field 'id' doesn't exist on type 'Actor' (mutation PullRequestRevert.revertPullRequest.revertPullRequest.autoMergeRequest.enabledBy.id), Field 'name' doesn't exist on type 'Actor' (mutation PullRequestRevert.revertPullRequest.revertPullRequest.autoMergeRequest.enabledBy.name), Field 'id' doesn't exist on type 'Actor' (mutation PullRequestRevert.revertPullRequest.revertPullRequest.author.id), Field 'name' doesn't exist on type 'Actor' (mutation PullRequestRevert.revertPullRequest.revertPullRequest.author.name), Field 'id' doesn't exist on type 'Actor' (mutation PullRequestRevert.revertPullRequest.revertPullRequest.mergedBy.id), Field 'name' doesn't exist on type 'Actor' (mutation PullRequestRevert.revertPullRequest.revertPullRequest.mergedBy.name), Field 'name' doesn't exist on type 'RepositoryOwner' (mutation PullRequestRevert.revertPullRequest.revertPullRequest.headRepositoryOwner.name), Field 'nodes' doesn't exist on type 'StatusCheckRollup' (mutation PullRequestRevert.revertPullRequest.revertPullRequest.statusCheckRollup.nodes), Selections can't be made directly on unions (see selections on Assignee) (mutation PullRequestRevert.revertPullRequest.revertPullRequest.assignedActors.nodes.id), Selections can't be made directly on unions (see selections on Assignee) (mutation PullRequestRevert.revertPullRequest.revertPullRequest.assignedActors.nodes.login), Selections can't be made directly on unions (see selections on Assignee) (mutation PullRequestRevert.revertPullRequest.revertPullRequest.assignedActors.nodes.name), Selections can't be made directly on unions (see selections on Assignee) (mutation PullRequestRevert.revertPullRequest.revertPullRequest.assignedActors.nodes.typeName), Field 'status' doesn't exist on type 'ProjectV2Item' (mutation PullRequestRevert.revertPullRequest.revertPullRequest.projectItems.nodes.status), Selections can't be made directly on unions (see selections on RequestedReviewer) (mutation PullRequestRevert.revertPullRequest.revertPullRequest.reviewRequests.nodes.requestedReviewer.typeName), Selections can't be made directly on unions (see selections on RequestedReviewer) (mutation PullRequestRevert.revertPullRequest.revertPullRequest.reviewRequests.nodes.requestedReviewer.login), Selections can't be made directly on unions (see selections on RequestedReviewer) (mutation PullRequestRevert.revertPullRequest.revertPullRequest.reviewRequests.nodes.requestedReviewer.name), Selections can't be made directly on unions (see selections on RequestedReviewer) (mutation PullRequestRevert.revertPullRequest.revertPullRequest.reviewRequests.nodes.requestedReviewer.slug), Selections can't be made directly on unions (see selections on RequestedReviewer) (mutation PullRequestRevert.revertPullRequest.revertPullRequest.reviewRequests.nodes.requestedReviewer.organization)
API call failed: GraphQL: Field 'id' doesn't exist on type 'Actor' (mutation PullRequestRevert.revertPullRequest.revertPullRequest.autoMergeRequest.enabledBy.id), Field 'name' doesn't exist on type 'Actor' (mutation PullRequestRevert.revertPullRequest.revertPullRequest.autoMergeRequest.enabledBy.name), Field 'id' doesn't exist on type 'Actor' (mutation PullRequestRevert.revertPullRequest.revertPullRequest.author.id), Field 'name' doesn't exist on type 'Actor' (mutation PullRequestRevert.revertPullRequest.revertPullRequest.author.name), Field 'id' doesn't exist on type 'Actor' (mutation PullRequestRevert.revertPullRequest.revertPullRequest.mergedBy.id), Field 'name' doesn't exist on type 'Actor' (mutation PullRequestRevert.revertPullRequest.revertPullRequest.mergedBy.name), Field 'name' doesn't exist on type 'RepositoryOwner' (mutation PullRequestRevert.revertPullRequest.revertPullRequest.headRepositoryOwner.name), Field 'nodes' doesn't exist on type 'StatusCheckRollup' (mutation PullRequestRevert.revertPullRequest.revertPullRequest.statusCheckRollup.nodes), Selections can't be made directly on unions (see selections on Assignee) (mutation PullRequestRevert.revertPullRequest.revertPullRequest.assignedActors.nodes.id), Selections can't be made directly on unions (see selections on Assignee) (mutation PullRequestRevert.revertPullRequest.revertPullRequest.assignedActors.nodes.login), Selections can't be made directly on unions (see selections on Assignee) (mutation PullRequestRevert.revertPullRequest.revertPullRequest.assignedActors.nodes.name), Selections can't be made directly on unions (see selections on Assignee) (mutation PullRequestRevert.revertPullRequest.revertPullRequest.assignedActors.nodes.typeName), Field 'status' doesn't exist on type 'ProjectV2Item' (mutation PullRequestRevert.revertPullRequest.revertPullRequest.projectItems.nodes.status), Selections can't be made directly on unions (see selections on RequestedReviewer) (mutation PullRequestRevert.revertPullRequest.revertPullRequest.reviewRequests.nodes.requestedReviewer.typeName), Selections can't be made directly on unions (see selections on RequestedReviewer) (mutation PullRequestRevert.revertPullRequest.revertPullRequest.reviewRequests.nodes.requestedReviewer.login), Selections can't be made directly on unions (see selections on RequestedReviewer) (mutation PullRequestRevert.revertPullRequest.revertPullRequest.reviewRequests.nodes.requestedReviewer.name), Selections can't be made directly on unions (see selections on RequestedReviewer) (mutation PullRequestRevert.revertPullRequest.revertPullRequest.reviewRequests.nodes.requestedReviewer.slug), Selections can't be made directly on unions (see selections on RequestedReviewer) (mutation PullRequestRevert.revertPullRequest.revertPullRequest.reviewRequests.nodes.requestedReviewer.organization)

To solve this, I think we should do something similar to what the create pull request mutation does and return just the basic pull request info. Here's a diff of the change I think we need:

diff --git a/api/queries_pr.go b/api/queries_pr.go
index c34c9a80c..0eb8441ed 100644
--- a/api/queries_pr.go
+++ b/api/queries_pr.go
@@ -770,7 +770,11 @@ func PullRequestRevert(client *Client, repo ghrepo.Interface, params githubv4.Re
 			PullRequest struct {
 				ID githubv4.ID
 			}
-			RevertPullRequest PullRequest
+			RevertPullRequest struct {
+				ID     string
+				Number int
+				URL    string
+			}
 		} `graphql:"revertPullRequest(input: $input)"`
 	}
 
@@ -781,7 +785,12 @@ func PullRequestRevert(client *Client, repo ghrepo.Interface, params githubv4.Re
 	if err != nil {
 		return nil, err
 	}
-	revertPR := &mutation.RevertPullRequest.RevertPullRequest
+	pr := &mutation.RevertPullRequest.RevertPullRequest
+	revertPR := &PullRequest{
+		ID:     pr.ID,
+		Number: pr.Number,
+		URL:    pr.URL,
+	}
 	return revertPR, nil
 }

What do you think?

@cmbrose
cmbrose removed the request for review from a team October 22, 2025 20:03
@lucasmelin

Copy link
Copy Markdown
Contributor Author

@BagToad, makes sense. 👍🏻 Implemented your suggestion in the most recent commit.

@BagToad BagToad left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your patience @lucasmelin 🙏

I've tested this and reviewed the code and it looks good to me 👍 Thank you for this contribution!

@BagToad
BagToad merged commit b90e39e into cli:trunk Oct 23, 2025
tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request Nov 6, 2025
This MR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [cli/cli](https://github.com/cli/cli) | minor | `v2.82.1` -> `v2.83.0` |

MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot).

**Proposed changes to behavior should be submitted there as MRs.**

---

### Release Notes

<details>
<summary>cli/cli (cli/cli)</summary>

### [`v2.83.0`](https://github.com/cli/cli/releases/tag/v2.83.0): GitHub CLI 2.83.0

[Compare Source](cli/cli@v2.82.1...v2.83.0)

#### What's Changed

##### ✨ Features

- Add `isImmutable` to `release list` JSON output by [@&#8203;babakks](https://github.com/babakks) in [#&#8203;12064](cli/cli#12064)
- `gh agent-task create`: support `--custom-agent`/`-a` flag by [@&#8203;BagToad](https://github.com/BagToad) in [#&#8203;12068](cli/cli#12068)
- 💡 (gh repo delete) Add warning when `--yes` is ignored without a repository, Closes: [#&#8203;12033](cli/cli#12033) by [@&#8203;Shion1305](https://github.com/Shion1305) in [#&#8203;12039](cli/cli#12039)
- feat: implement gh `pr revert` by [@&#8203;lucasmelin](https://github.com/lucasmelin) in [#&#8203;8826](cli/cli#8826)

##### 🐛 Fixes

- fix(gist): add support for editing & viewing large files  by [@&#8203;luxass](https://github.com/luxass) in [#&#8203;11761](cli/cli#11761)
- Fix gh attestation verify to work when Public Good Instance of Sigstore is unavailable by [@&#8203;Copilot](https://github.com/Copilot) in [#&#8203;11989](cli/cli#11989)

##### 📚 Docs & Chores

- chore: add basic linters by [@&#8203;babakks](https://github.com/babakks) in [#&#8203;12084](cli/cli#12084)
- CI: Update lint govulncheck to use source mode by [@&#8203;BagToad](https://github.com/BagToad) in [#&#8203;12089](cli/cli#12089)
- chore: add `workflow_dispatch` to govulncheck triggers by [@&#8203;babakks](https://github.com/babakks) in [#&#8203;12085](cli/cli#12085)
- Exclude `third-party` from Golangci-lint formatting paths by [@&#8203;babakks](https://github.com/babakks) in [#&#8203;12058](cli/cli#12058)
- Apply `go fix` to remove deprecated `// +build` tags by [@&#8203;babakks](https://github.com/babakks) in [#&#8203;12056](cli/cli#12056)
- Bump Golangci-lint to `v2.6.0` by [@&#8203;babakks](https://github.com/babakks) in [#&#8203;12049](cli/cli#12049)
- Mention `pr checks` in `run list` docs by [@&#8203;babakks](https://github.com/babakks) in [#&#8203;12050](cli/cli#12050)
- Fix typo in comment for `gh issue develop` branch checkout command by [@&#8203;jonzfisher](https://github.com/jonzfisher) in [#&#8203;12042](cli/cli#12042)
- Use "release" sentinel value for release attestation verification by [@&#8203;Copilot](https://github.com/Copilot) in [#&#8203;11991](cli/cli#11991)
- Improve docstring for release-create by [@&#8203;bdehamer](https://github.com/bdehamer) in [#&#8203;11945](cli/cli#11945)
- Improve `api` command docs around `--input` and `--field` by [@&#8203;babakks](https://github.com/babakks) in [#&#8203;12062](cli/cli#12062)
- Fix `--interval` flags docs in `gh pr checks` by [@&#8203;2003Aditya](https://github.com/2003Aditya) in [#&#8203;12053](cli/cli#12053)

##### :dependabot: Dependencies

- Bump Go to 1.25.3 by [@&#8203;github-actions](https://github.com/github-actions)\[bot] in [#&#8203;11926](cli/cli#11926)
- chore(deps): bump github.com/cli/go-gh/v2 from 2.12.2 to 2.13.0 by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;12095](cli/cli#12095)
- Update Go toolchain version to 1.24.9 by [@&#8203;BagToad](https://github.com/BagToad) in [#&#8203;12054](cli/cli#12054)
- chore(deps): bump golang.org/x/text from 0.29.0 to 0.30.0 by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;11973](cli/cli#11973)
- chore(deps): bump golang.org/x/crypto from 0.42.0 to 0.43.0 by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;11974](cli/cli#11974)
- chore(deps): bump actions/upload-artifact from 4 to 5 by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;12031](cli/cli#12031)
- chore(deps): bump actions/download-artifact from 5 to 6 by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;12032](cli/cli#12032)
- chore(deps): bump github.com/rivo/tview from 0.0.0-20250625164341-a4a78f1e05cb to 0.42.0 by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;12000](cli/cli#12000)
- chore(deps): bump goreleaser/goreleaser-action from 6.3.0 to 6.4.0 by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;11509](cli/cli#11509)
- chore(deps): bump mislav/bump-homebrew-formula-action from 3.4 to 3.6 by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;11750](cli/cli#11750)

#### New Contributors

- [@&#8203;lucasmelin](https://github.com/lucasmelin) made their first contribution in [#&#8203;8826](cli/cli#8826)
- [@&#8203;jonzfisher](https://github.com/jonzfisher) made their first contribution in [#&#8203;12042](cli/cli#12042)
- [@&#8203;2003Aditya](https://github.com/2003Aditya) made their first contribution in [#&#8203;12053](cli/cli#12053)

**Full Changelog**: <cli/cli@v2.82.1...v2.83.0>

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever MR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this MR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MS4xNjkuMyIsInVwZGF0ZWRJblZlciI6IjQxLjE2OS4zIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJSZW5vdmF0ZSBCb3QiXX0=-->
coolbress added a commit to coolbress/plinth that referenced this pull request Oct 22, 2026
…only walk recorded (#632)

## What and why

The second of two pull requests for #617, the docs-only walk's stalls on the pages #603 moved but did not change (items 14 to 19). The first covers the tutorial and `run-a-project.md`.

`docs/how-to/give-the-agent-a-token.md`:

- Item 14: step 2 says why the pasted token takes the browser login's place (it is for the same account, and `gh` keeps one login per account on a host) and that there is nothing left to log out; step 5's bullet says that a token for a different account is what is added beside the stored ones. The two no longer read as contradicting each other.
- Item 15: "The first two" names them: another account stored in `gh` and another git credential helper.
- Item 16: step 3 says which line of `gh auth status` to read (`Logged in to github.com account <you>`) and what its ending means, `(keyring)` or a file path against `(GH_TOKEN)` or `(GITHUB_TOKEN)`.

`docs/how-to/give-the-agent-its-own-account.md`:

- Item 17: step 5 says it is the token page's step 5 done once more, and what differs the second time (your own login goes too, so that only the agent account's is left). The "what it does not cover" paragraph says how to keep the browser that merges out of the agent's reach: merge from a device or browser the agent cannot drive, and keep the browser the extension drives signed out of your account (the private-window alternative was removed in review round 1).
- Item 18: the intro names a ruleset before counting a second one, and says the repository carries two (one requires the checks, one reserves the merge).
- Item 12 on these pages: every issue number links to plinth's issue, and the first on each page says "plinth's issue".

`docs/how-to/set-four-things-by-hand.md`:

- Item 19: item 3 gives the browser's **Revert** button and `gh pr revert <n>`, and says the result is a new pull request that goes through the checks and the merge like any other. `gh pr revert` arrived in gh 2.83.0 (cli/cli#8826, in that release's notes); the tutorial names no gh minimum, so the line names 2.83.0 itself.

## How it was verified

Run on this branch: the three `claude plugin validate --strict` calls, `scripts/check-ruleset.sh` and every `tests/*.sh` (markdownlint included), all passing. `gh pr revert --help` on gh 2.101.0 shows the command; the gh release that added it was read from the cli/cli release notes. The `gh auth status` line shape was read from gh 2.101.0's output. Not measured: that `gh auth login` with a token for the same account replaces that account's stored login rather than adding a second one; it is how gh's multi-account support is documented (one login per account per host, `gh auth switch` between accounts) and no second token was made for this change. Not verified: the new text with a reader; `ci / docs` runs the link check.

## Review

Round 1, on `26958b3`:

- P1 "Do not treat a private window as isolated from desktop control" (inline 4343080795): fixed. The private-window alternative is gone. The paragraph now says that with desktop control on no window on this screen is out of the agent's reach, a private window included, and that the merge goes through a device or browser the agent cannot drive: this machine's browser after desktop control is turned off, or your phone. The browser the extension drives stays signed out of your account.

Part of #617

Assisted-by: Claude:claude-fable-5-1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external pull request originating outside of the CLI core team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide gh cli Support to Revert Pull Request

4 participants