feat: include revert PR info in message output

This commit is contained in:
Lucas Melin 2024-03-17 09:27:45 -04:00
parent 9467d66b11
commit 023a639c8c
No known key found for this signature in database
3 changed files with 42 additions and 11 deletions

View file

@ -764,20 +764,25 @@ func PullRequestReady(client *Client, repo ghrepo.Interface, pr *PullRequest) er
return client.Mutate(repo.RepoHost(), "PullRequestReadyForReview", &mutation, variables)
}
func PullRequestRevert(client *Client, repo ghrepo.Interface, params githubv4.RevertPullRequestInput) error {
func PullRequestRevert(client *Client, repo ghrepo.Interface, params githubv4.RevertPullRequestInput) (*PullRequest, error) {
var mutation struct {
RevertPullRequest struct {
PullRequest struct {
ID githubv4.ID
}
RevertPullRequest PullRequest
} `graphql:"revertPullRequest(input: $input)"`
}
variables := map[string]interface{}{
"input": params,
}
return client.Mutate(repo.RepoHost(), "PullRequestRevert", &mutation, variables)
err := client.Mutate(repo.RepoHost(), "PullRequestRevert", &mutation, variables)
if err != nil {
return nil, err
}
revertPR := &mutation.RevertPullRequest.RevertPullRequest
return revertPR, err
}
func ConvertPullRequestToDraft(client *Client, repo ghrepo.Interface, pr *PullRequest) error {

View file

@ -111,12 +111,22 @@ func revertRun(opts *RevertOptions) error {
Draft: githubv4.NewBoolean(githubv4.Boolean(opts.IsDraft)),
}
err = api.PullRequestRevert(apiClient, baseRepo, params)
revertPR, err := api.PullRequestRevert(apiClient, baseRepo, params)
if err != nil {
return fmt.Errorf("API call failed: %w", err)
}
fmt.Fprintf(opts.IO.ErrOut, "%s Created revert PR for pull request %s#%d (%s)\n", cs.SuccessIconWithColor(cs.Green), ghrepo.FullName(baseRepo), pr.Number, pr.Title)
fmt.Fprintf(
opts.IO.ErrOut,
"%s Created pull request %s#%d (%s) that reverts %s#%d (%s)\n",
cs.SuccessIconWithColor(cs.Green),
ghrepo.FullName(baseRepo),
revertPR.Number,
revertPR.Title,
ghrepo.FullName(baseRepo),
pr.Number,
pr.Title,
)
return nil
}

View file

@ -62,7 +62,15 @@ func TestPRRevert(t *testing.T) {
http.Register(
httpmock.GraphQL(`mutation PullRequestRevert\b`),
httpmock.GraphQLMutation(`{"id": "SOME-ID"}`,
httpmock.GraphQLMutation(`
{ "data": { "revertPullRequest": { "pullRequest": {
"ID": "SOME-ID"
}, "revertPullRequest": {
"ID": "NEW-ID",
"Title": "Revert PR title",
"Number": 456
} } } }
`,
func(inputs map[string]interface{}) {
assert.Equal(t, inputs["pullRequestId"], "SOME-ID")
}),
@ -71,7 +79,7 @@ func TestPRRevert(t *testing.T) {
output, err := runCommand(http, true, "123")
assert.NoError(t, err)
assert.Equal(t, "", output.String())
assert.Equal(t, "✓ Created revert PR for pull request OWNER/REPO#123 (The title of the PR)\n", output.Stderr())
assert.Equal(t, "✓ Created pull request OWNER/REPO#456 (Revert PR title) that reverts OWNER/REPO#123 (The title of the PR)\n", output.Stderr())
}
func TestPRRevert_notRevertable(t *testing.T) {
@ -96,7 +104,7 @@ func TestPRRevert_withAllOpts(t *testing.T) {
defer http.Verify(t)
shared.RunCommandFinder("123", &api.PullRequest{
ID: "THE-ID",
ID: "SOME-ID",
Number: 123,
State: "MERGED",
Title: "The title of the PR",
@ -104,9 +112,17 @@ func TestPRRevert_withAllOpts(t *testing.T) {
http.Register(
httpmock.GraphQL(`mutation PullRequestRevert\b`),
httpmock.GraphQLMutation(`{"id": "THE-ID" }`,
httpmock.GraphQLMutation(`
{ "data": { "revertPullRequest": { "pullRequest": {
"ID": "SOME-ID"
}, "revertPullRequest": {
"ID": "NEW-ID",
"Title": "Revert PR title",
"Number": 456
} } } }
`,
func(inputs map[string]interface{}) {
assert.Equal(t, inputs["pullRequestId"], "THE-ID")
assert.Equal(t, inputs["pullRequestId"], "SOME-ID")
assert.Equal(t, inputs["title"], "Revert PR title")
assert.Equal(t, inputs["body"], "Revert PR body")
assert.Equal(t, inputs["draft"], true)
@ -116,5 +132,5 @@ func TestPRRevert_withAllOpts(t *testing.T) {
output, err := runCommand(http, true, "123 --title 'Revert PR title' --body 'Revert PR body' --draft")
assert.NoError(t, err)
assert.Equal(t, "", output.String())
assert.Equal(t, "✓ Created revert PR for pull request OWNER/REPO#123 (The title of the PR)\n", output.Stderr())
assert.Equal(t, "✓ Created pull request OWNER/REPO#456 (Revert PR title) that reverts OWNER/REPO#123 (The title of the PR)\n", output.Stderr())
}