-
Notifications
You must be signed in to change notification settings - Fork 7.4k
feat: implement gh pr revert
#8826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: implement gh pr revert
#8826
Conversation
|
Putting back into draft to add the revert PR details to the success message output. |
|
Added the revert PR details to the message output, ready for review. |
There was a problem hiding this comment.
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 |
|
👋 Hi @lucasmelin! I just posted the Acceptance Criteria for this issue in #6034 (comment). I also added the 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. |
Also adds test cases for the new acceptance criteria.
912fcfe to
ef7ac8c
Compare
BagToad
left a comment
There was a problem hiding this comment.
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?
Co-authored-by: Kynan Ware <47394200+BagToad@users.noreply.github.com>
941efb6 to
6db1441
Compare
|
@BagToad, makes sense. 👍🏻 Implemented your suggestion in the most recent commit. |
BagToad
left a comment
There was a problem hiding this comment.
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!
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 [@​babakks](https://github.com/babakks) in [#​12064](cli/cli#12064) - `gh agent-task create`: support `--custom-agent`/`-a` flag by [@​BagToad](https://github.com/BagToad) in [#​12068](cli/cli#12068) - 💡 (gh repo delete) Add warning when `--yes` is ignored without a repository, Closes: [#​12033](cli/cli#12033) by [@​Shion1305](https://github.com/Shion1305) in [#​12039](cli/cli#12039) - feat: implement gh `pr revert` by [@​lucasmelin](https://github.com/lucasmelin) in [#​8826](cli/cli#8826) ##### 🐛 Fixes - fix(gist): add support for editing & viewing large files by [@​luxass](https://github.com/luxass) in [#​11761](cli/cli#11761) - Fix gh attestation verify to work when Public Good Instance of Sigstore is unavailable by [@​Copilot](https://github.com/Copilot) in [#​11989](cli/cli#11989) ##### 📚 Docs & Chores - chore: add basic linters by [@​babakks](https://github.com/babakks) in [#​12084](cli/cli#12084) - CI: Update lint govulncheck to use source mode by [@​BagToad](https://github.com/BagToad) in [#​12089](cli/cli#12089) - chore: add `workflow_dispatch` to govulncheck triggers by [@​babakks](https://github.com/babakks) in [#​12085](cli/cli#12085) - Exclude `third-party` from Golangci-lint formatting paths by [@​babakks](https://github.com/babakks) in [#​12058](cli/cli#12058) - Apply `go fix` to remove deprecated `// +build` tags by [@​babakks](https://github.com/babakks) in [#​12056](cli/cli#12056) - Bump Golangci-lint to `v2.6.0` by [@​babakks](https://github.com/babakks) in [#​12049](cli/cli#12049) - Mention `pr checks` in `run list` docs by [@​babakks](https://github.com/babakks) in [#​12050](cli/cli#12050) - Fix typo in comment for `gh issue develop` branch checkout command by [@​jonzfisher](https://github.com/jonzfisher) in [#​12042](cli/cli#12042) - Use "release" sentinel value for release attestation verification by [@​Copilot](https://github.com/Copilot) in [#​11991](cli/cli#11991) - Improve docstring for release-create by [@​bdehamer](https://github.com/bdehamer) in [#​11945](cli/cli#11945) - Improve `api` command docs around `--input` and `--field` by [@​babakks](https://github.com/babakks) in [#​12062](cli/cli#12062) - Fix `--interval` flags docs in `gh pr checks` by [@​2003Aditya](https://github.com/2003Aditya) in [#​12053](cli/cli#12053) #####Dependencies - Bump Go to 1.25.3 by [@​github-actions](https://github.com/github-actions)\[bot] in [#​11926](cli/cli#11926) - chore(deps): bump github.com/cli/go-gh/v2 from 2.12.2 to 2.13.0 by [@​dependabot](https://github.com/dependabot)\[bot] in [#​12095](cli/cli#12095) - Update Go toolchain version to 1.24.9 by [@​BagToad](https://github.com/BagToad) in [#​12054](cli/cli#12054) - chore(deps): bump golang.org/x/text from 0.29.0 to 0.30.0 by [@​dependabot](https://github.com/dependabot)\[bot] in [#​11973](cli/cli#11973) - chore(deps): bump golang.org/x/crypto from 0.42.0 to 0.43.0 by [@​dependabot](https://github.com/dependabot)\[bot] in [#​11974](cli/cli#11974) - chore(deps): bump actions/upload-artifact from 4 to 5 by [@​dependabot](https://github.com/dependabot)\[bot] in [#​12031](cli/cli#12031) - chore(deps): bump actions/download-artifact from 5 to 6 by [@​dependabot](https://github.com/dependabot)\[bot] in [#​12032](cli/cli#12032) - chore(deps): bump github.com/rivo/tview from 0.0.0-20250625164341-a4a78f1e05cb to 0.42.0 by [@​dependabot](https://github.com/dependabot)\[bot] in [#​12000](cli/cli#12000) - chore(deps): bump goreleaser/goreleaser-action from 6.3.0 to 6.4.0 by [@​dependabot](https://github.com/dependabot)\[bot] in [#​11509](cli/cli#11509) - chore(deps): bump mislav/bump-homebrew-formula-action from 3.4 to 3.6 by [@​dependabot](https://github.com/dependabot)\[bot] in [#​11750](cli/cli#11750) #### New Contributors - [@​lucasmelin](https://github.com/lucasmelin) made their first contribution in [#​8826](cli/cli#8826) - [@​jonzfisher](https://github.com/jonzfisher) made their first contribution in [#​12042](cli/cli#12042) - [@​2003Aditya](https://github.com/2003Aditya) made their first contribution in [#​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=-->
Fixes #6034
Adds a new
pr revertcommand that enables reverting a merged PR using theghCLI.Usage