From 1eee56ec00ae1ff0c08e03adcd0d7a4c79dd79aa Mon Sep 17 00:00:00 2001 From: Dylan Ancel Date: Wed, 11 Jun 2025 16:28:03 +0200 Subject: [PATCH] Add accurate context when `run rerun` fails (#10774) * Add accurate context when run rerun fails * Update tests to verify behaviour for API errors Signed-off-by: Babak K. Shandiz * Use the new `httpmock.JSONErrorResponse` helper Signed-off-by: Babak K. Shandiz --------- Signed-off-by: Babak K. Shandiz Co-authored-by: Babak K. Shandiz --- pkg/cmd/run/rerun/rerun.go | 5 +---- pkg/cmd/run/rerun/rerun_test.go | 39 ++++++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/pkg/cmd/run/rerun/rerun.go b/pkg/cmd/run/rerun/rerun.go index 66d5d6f64..8777e0a8a 100644 --- a/pkg/cmd/run/rerun/rerun.go +++ b/pkg/cmd/run/rerun/rerun.go @@ -202,10 +202,7 @@ func rerunRun(client *api.Client, repo ghrepo.Interface, run *shared.Run, onlyFa if err != nil { var httpError api.HTTPError if errors.As(err, &httpError) && httpError.StatusCode == 403 { - if httpError.Message == "Unable to retry this workflow run because it was created over a month ago" { - return fmt.Errorf("run %d cannot be rerun; %s", run.ID, httpError.Message) - } - return fmt.Errorf("run %d cannot be rerun; its workflow file may be broken", run.ID) + return fmt.Errorf("run %d cannot be rerun; %s", run.ID, httpError.Message) } return fmt.Errorf("failed to rerun: %w", err) } diff --git a/pkg/cmd/run/rerun/rerun_test.go b/pkg/cmd/run/rerun/rerun_test.go index 0dc74129e..77f0a922f 100644 --- a/pkg/cmd/run/rerun/rerun_test.go +++ b/pkg/cmd/run/rerun/rerun_test.go @@ -14,6 +14,7 @@ import ( "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/httpmock" "github.com/cli/cli/v2/pkg/iostreams" + "github.com/cli/go-gh/v2/pkg/api" "github.com/google/shlex" "github.com/stretchr/testify/assert" ) @@ -374,7 +375,7 @@ func TestRerun(t *testing.T) { errOut: "no recent runs have failed; please specify a specific ``", }, { - name: "unrerunnable", + name: "API error (403)", tty: true, opts: &RerunOptions{ RunID: "3", @@ -392,10 +393,42 @@ func TestRerun(t *testing.T) { })) reg.Register( httpmock.REST("POST", "repos/OWNER/REPO/actions/runs/3/rerun"), - httpmock.StatusStringResponse(403, "no")) + httpmock.JSONErrorResponse(403, api.HTTPError{ + StatusCode: 403, + Message: "blah blah", + }), + ) }, wantErr: true, - errOut: "run 3 cannot be rerun; its workflow file may be broken", + errOut: "run 3 cannot be rerun; blah blah", + }, + { + name: "API error (non-403)", + tty: true, + opts: &RerunOptions{ + RunID: "3", + }, + httpStubs: func(reg *httpmock.Registry) { + reg.Register( + httpmock.REST("GET", "repos/OWNER/REPO/actions/runs/3"), + httpmock.JSONResponse(shared.SuccessfulRun)) + reg.Register( + httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows/123"), + httpmock.JSONResponse(workflowShared.WorkflowsPayload{ + Workflows: []workflowShared.Workflow{ + shared.TestWorkflow, + }, + })) + reg.Register( + httpmock.REST("POST", "repos/OWNER/REPO/actions/runs/3/rerun"), + httpmock.JSONErrorResponse(500, api.HTTPError{ + StatusCode: 500, + Message: "blah blah", + }), + ) + }, + wantErr: true, + errOut: "failed to rerun: HTTP 500: blah blah (https://api.github.com/repos/OWNER/REPO/actions/runs/3/rerun)", }, }