-
Couldn't load subscription status.
- Fork 7.3k
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
Merged
BagToad
merged 8 commits into
cli:trunk
from
lucasmelin:lucasmelin/add-pr-revert-command
Oct 23, 2025
+490
−0
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
60b6a5e
feat: implement `pr revert`
lucasmelin 9467d66
Undo autoformat changes
lucasmelin 023a639
feat: include revert PR info in message output
lucasmelin 5368f40
Fix revert unit tests by using renamed helper
lucasmelin b229402
Return nil when err is nil
lucasmelin 4f10a52
Adjust PR revert based on new acceptance criteria
lucasmelin ef7ac8c
Address remaining PR comments for revert implementation
lucasmelin 6db1441
Return only basic pull request info when reverting a PR
lucasmelin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| package revert | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "github.com/cli/cli/v2/api" | ||
| "github.com/cli/cli/v2/internal/ghrepo" | ||
| "github.com/cli/cli/v2/pkg/cmd/pr/shared" | ||
| "github.com/cli/cli/v2/pkg/cmdutil" | ||
| "github.com/cli/cli/v2/pkg/iostreams" | ||
| "github.com/shurcooL/githubv4" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| type RevertOptions struct { | ||
| HttpClient func() (*http.Client, error) | ||
| IO *iostreams.IOStreams | ||
|
|
||
| Finder shared.PRFinder | ||
|
|
||
| SelectorArg string | ||
|
|
||
| Body string | ||
| BodySet bool | ||
| Title string | ||
| IsDraft bool | ||
| } | ||
|
|
||
| func NewCmdRevert(f *cmdutil.Factory, runF func(*RevertOptions) error) *cobra.Command { | ||
| opts := &RevertOptions{ | ||
| IO: f.IOStreams, | ||
| HttpClient: f.HttpClient, | ||
| } | ||
|
|
||
| var bodyFile string | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "revert {<number> | <url> | <branch>}", | ||
| Short: "Revert a pull request", | ||
| Args: cmdutil.ExactArgs(1, "cannot revert pull request: number, url, or branch required"), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| opts.Finder = shared.NewFinder(f) | ||
|
|
||
| if len(args) > 0 { | ||
| opts.SelectorArg = args[0] | ||
| } | ||
|
|
||
| bodyProvided := cmd.Flags().Changed("body") | ||
| bodyFileProvided := bodyFile != "" | ||
|
|
||
| if err := cmdutil.MutuallyExclusive( | ||
| "specify only one of `--body` or `--body-file`", | ||
| bodyProvided, | ||
| bodyFileProvided, | ||
| ); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if bodyProvided || bodyFileProvided { | ||
| opts.BodySet = true | ||
| if bodyFileProvided { | ||
| b, err := cmdutil.ReadFile(bodyFile, opts.IO.In) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| opts.Body = string(b) | ||
| } | ||
| } | ||
|
|
||
| if runF != nil { | ||
| return runF(opts) | ||
| } | ||
| return revertRun(opts) | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().BoolVarP(&opts.IsDraft, "draft", "d", false, "Mark revert pull request as a draft") | ||
| cmd.Flags().StringVarP(&opts.Title, "title", "t", "", "Title for the revert pull request") | ||
| cmd.Flags().StringVarP(&opts.Body, "body", "b", "", "Body for the revert pull request") | ||
| cmd.Flags().StringVarP(&bodyFile, "body-file", "F", "", "Read body text from `file` (use \"-\" to read from standard input)") | ||
| return cmd | ||
| } | ||
|
|
||
| func revertRun(opts *RevertOptions) error { | ||
| cs := opts.IO.ColorScheme() | ||
|
|
||
| findOptions := shared.FindOptions{ | ||
| Selector: opts.SelectorArg, | ||
| Fields: []string{"id", "number", "state", "title"}, | ||
| } | ||
| pr, baseRepo, err := opts.Finder.Find(findOptions) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if pr.State != "MERGED" { | ||
| fmt.Fprintf(opts.IO.ErrOut, "%s Pull request %s#%d (%s) can't be reverted because it has not been merged\n", cs.FailureIcon(), ghrepo.FullName(baseRepo), pr.Number, pr.Title) | ||
| return cmdutil.SilentError | ||
| } | ||
|
|
||
| httpClient, err := opts.HttpClient() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| apiClient := api.NewClientFromHTTP(httpClient) | ||
|
|
||
| params := githubv4.RevertPullRequestInput{ | ||
| PullRequestID: pr.ID, | ||
| Draft: githubv4.NewBoolean(githubv4.Boolean(opts.IsDraft)), | ||
| } | ||
| // Only set the Body field when opts.BodySet is true to avoid overriding | ||
| // GitHub's default revert body generation. | ||
| if opts.BodySet { | ||
| params.Body = githubv4.NewString(githubv4.String(opts.Body)) | ||
| } | ||
| // Only set the Title field when opts.Title is not empty to avoid overriding | ||
| // GitHub's default revert title generation. | ||
| if opts.Title != "" { | ||
| params.Title = githubv4.NewString(githubv4.String(opts.Title)) | ||
| } | ||
|
|
||
lucasmelin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| revertPR, err := api.PullRequestRevert(apiClient, baseRepo, params) | ||
| if err != nil { | ||
| fmt.Fprintf(opts.IO.ErrOut, "%s %s\n", cs.FailureIcon(), err) | ||
| return fmt.Errorf("API call failed: %w", err) | ||
| } | ||
|
|
||
| if revertPR != nil { | ||
| fmt.Fprintln(opts.IO.Out, revertPR.URL) | ||
| } | ||
| return nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.