From 5817f6fce92b36b935c8d7653e1abe51f9cae17d Mon Sep 17 00:00:00 2001 From: Kynan Ware <47394200+BagToad@users.noreply.github.com> Date: Mon, 24 Mar 2025 12:34:50 -0600 Subject: [PATCH] fix(run list): do not fail on org workflows --- pkg/cmd/run/list/list_test.go | 38 +++++++++++++++++++++++++++++++++++ pkg/cmd/run/shared/shared.go | 10 +++++++++ pkg/cmd/run/shared/test.go | 16 +++++++++++++++ 3 files changed, 64 insertions(+) diff --git a/pkg/cmd/run/list/list_test.go b/pkg/cmd/run/list/list_test.go index 7717c2ff9..e41809bf1 100644 --- a/pkg/cmd/run/list/list_test.go +++ b/pkg/cmd/run/list/list_test.go @@ -366,6 +366,44 @@ func TestListRun(t *testing.T) { completed stale cool commit CI trunk push 10 4m34s 2021-02-23T04:51:00Z `), }, + { + name: "organization required workflow in run list (workflow GET returns 404)", + opts: &ListOptions{ + Limit: defaultLimit, + now: shared.TestRunStartTime.Add(time.Minute*4 + time.Second*34), + }, + isTTY: true, + stubs: func(reg *httpmock.Registry) { + reg.Register( + httpmock.REST("GET", "repos/OWNER/REPO/actions/runs"), + httpmock.JSONResponse(shared.RunsPayload{ + WorkflowRuns: shared.TestRunsWithOrgRequiredWorkflows, + })) + reg.Register( + httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows"), + httpmock.JSONResponse(workflowShared.WorkflowsPayload{ + Workflows: []workflowShared.Workflow{ + shared.TestWorkflow, + }, + })) + reg.Register( + httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows/456"), + httpmock.StatusStringResponse(404, "not found"), + ) + }, + wantOut: heredoc.Doc(` + STATUS TITLE WORKFLOW BRANCH EVENT ID ELAPSED AGE + X cool commit trunk push 1 4m34s about 4 minutes ago + * cool commit trunk push 2 4m34s about 4 minutes ago + ✓ cool commit trunk push 3 4m34s about 4 minutes ago + X cool commit trunk push 4 4m34s about 4 minutes ago + X cool commit CI trunk push 5 4m34s about 4 minutes ago + - cool commit CI trunk push 6 4m34s about 4 minutes ago + - cool commit CI trunk push 7 4m34s about 4 minutes ago + * cool commit CI trunk push 8 4m34s about 4 minutes ago + * cool commit CI trunk push 9 4m34s about 4 minutes ago + `), + }, { name: "pagination", opts: &ListOptions{ diff --git a/pkg/cmd/run/shared/shared.go b/pkg/cmd/run/shared/shared.go index 040888ab5..b35f0c462 100644 --- a/pkg/cmd/run/shared/shared.go +++ b/pkg/cmd/run/shared/shared.go @@ -446,6 +446,16 @@ func preloadWorkflowNames(client *api.Client, repo ghrepo.Interface, runs []Run) if _, ok := workflowMap[run.WorkflowID]; !ok { // Look up workflow by ID because it may have been deleted workflow, err := workflowShared.GetWorkflow(client, repo, run.WorkflowID) + // If the error is an httpError and it is a 404, this is likely a + // organization-level "required workflow" ruleset. The user does not + // have permissions to view the details of the workflow, so we cannot + // look it up directly without receiving a 404, but it is nonetheless + // in the workflow run list. To handle this, we set the workflow name + // to an empty string. + if httpErr, ok := err.(api.HTTPError); ok && httpErr.StatusCode == 404 { + workflowMap[run.WorkflowID] = "" + continue + } if err != nil { return err } diff --git a/pkg/cmd/run/shared/test.go b/pkg/cmd/run/shared/test.go index 7caa37039..0619541a4 100644 --- a/pkg/cmd/run/shared/test.go +++ b/pkg/cmd/run/shared/test.go @@ -18,6 +18,10 @@ func TestRunWithCommit(id int64, s Status, c Conclusion, commit string) Run { return TestRunWithWorkflowAndCommit(123, id, s, c, commit) } +func TestRunWithOrgRequiredWorkflow(id int64, s Status, c Conclusion, commit string) Run { + return TestRunWithWorkflowAndCommit(456, id, s, c, commit) +} + func TestRunWithWorkflowAndCommit(workflowId, runId int64, s Status, c Conclusion, commit string) Run { return Run{ WorkflowID: workflowId, @@ -57,6 +61,18 @@ var TestRuns []Run = []Run{ TestRun(10, Completed, Stale), } +var TestRunsWithOrgRequiredWorkflows []Run = []Run{ + TestRunWithOrgRequiredWorkflow(1, Completed, TimedOut, "cool commit"), + TestRunWithOrgRequiredWorkflow(2, InProgress, "", "cool commit"), + TestRunWithOrgRequiredWorkflow(3, Completed, Success, "cool commit"), + TestRunWithOrgRequiredWorkflow(4, Completed, Cancelled, "cool commit"), + TestRun(5, Completed, Failure), + TestRun(6, Completed, Neutral), + TestRun(7, Completed, Skipped), + TestRun(8, Requested, ""), + TestRun(9, Queued, ""), +} + var WorkflowRuns []Run = []Run{ TestRun(2, InProgress, ""), SuccessfulRun,