Skip to content

Commit a7406e0

Browse files
authored
fix: fetch build status from jobs instead of run status (#5495)
1 parent 8a45fd7 commit a7406e0

File tree

2 files changed

+41
-44
lines changed

2 files changed

+41
-44
lines changed

pkg/orchestrator/github_actions.go

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@ type githubActionsConfigProvider struct {
3131

3232
type run struct {
3333
fetched bool
34-
Status string `json:"status"`
3534
StartedAt time.Time `json:"run_started_at"`
3635
}
3736

37+
// used to unmarshal list jobs of the current workflow run into []job
3838
type job struct {
39-
ID int64 `json:"id"`
40-
Name string `json:"name"`
41-
HtmlURL string `json:"html_url"`
39+
ID int64 `json:"id"`
40+
Name string `json:"name"`
41+
HtmlURL string `json:"html_url"`
42+
Conclusion string `json:"conclusion"`
4243
}
4344

4445
type fullLog struct {
@@ -75,19 +76,26 @@ func (g *githubActionsConfigProvider) OrchestratorType() string {
7576
return "GitHubActions"
7677
}
7778

78-
// BuildStatus returns current run status
79+
// BuildStatus returns current run status by looking at all jobs of the current workflow run
80+
// if any job has conclusion "failure" the whole run is considered failed
81+
// if any job has conclusion "cancelled" the whole run is considered aborted
82+
// otherwise the run is considered successful
7983
func (g *githubActionsConfigProvider) BuildStatus() string {
80-
g.fetchRunData()
81-
switch g.runData.Status {
82-
case "success":
83-
return BuildStatusSuccess
84-
case "cancelled":
85-
return BuildStatusAborted
86-
case "in_progress":
87-
return BuildStatusInProgress
88-
default:
84+
if err := g.fetchJobs(); err != nil {
85+
log.Entry().Debugf("fetching jobs: %s", err)
8986
return BuildStatusFailure
9087
}
88+
89+
for _, j := range g.jobs {
90+
switch j.Conclusion {
91+
case "failure":
92+
return BuildStatusFailure
93+
case "cancelled":
94+
return BuildStatusAborted
95+
}
96+
}
97+
98+
return BuildStatusSuccess
9199
}
92100

93101
// FullLogs returns the whole logfile for the current pipeline run
@@ -261,12 +269,7 @@ func (g *githubActionsConfigProvider) fetchRunData() {
261269
return
262270
}
263271

264-
runId, err := g.runIdInt64()
265-
if err != nil {
266-
log.Entry().Errorf("fetchRunData: %s", err)
267-
}
268-
269-
runData, resp, err := g.client.Actions.GetWorkflowRunByID(g.ctx, g.owner, g.repo, runId)
272+
runData, resp, err := g.client.Actions.GetWorkflowRunByID(g.ctx, g.owner, g.repo, g.runIdInt64())
270273
if err != nil || resp.StatusCode != 200 {
271274
log.Entry().Errorf("failed to get API data: %s", err)
272275
return
@@ -279,7 +282,6 @@ func (g *githubActionsConfigProvider) fetchRunData() {
279282
func convertRunData(runData *github.WorkflowRun) run {
280283
startedAtTs := piperutils.SafeDereference(runData.RunStartedAt)
281284
return run{
282-
Status: piperutils.SafeDereference(runData.Status),
283285
StartedAt: startedAtTs.Time,
284286
}
285287
}
@@ -289,12 +291,7 @@ func (g *githubActionsConfigProvider) fetchJobs() error {
289291
return nil
290292
}
291293

292-
runId, err := g.runIdInt64()
293-
if err != nil {
294-
return err
295-
}
296-
297-
jobs, resp, err := g.client.Actions.ListWorkflowJobs(g.ctx, g.owner, g.repo, runId, nil)
294+
jobs, resp, err := g.client.Actions.ListWorkflowJobs(g.ctx, g.owner, g.repo, g.runIdInt64(), nil)
298295
if err != nil || resp.StatusCode != 200 {
299296
return errors.Wrap(err, "failed to get API data")
300297
}
@@ -312,22 +309,24 @@ func convertJobs(jobs []*github.WorkflowJob) []job {
312309
result := make([]job, 0, len(jobs))
313310
for _, j := range jobs {
314311
result = append(result, job{
315-
ID: j.GetID(),
316-
Name: j.GetName(),
317-
HtmlURL: j.GetHTMLURL(),
312+
ID: j.GetID(),
313+
Name: j.GetName(),
314+
HtmlURL: j.GetHTMLURL(),
315+
Conclusion: j.GetConclusion(),
318316
})
319317
}
320318
return result
321319
}
322320

323-
func (g *githubActionsConfigProvider) runIdInt64() (int64, error) {
321+
func (g *githubActionsConfigProvider) runIdInt64() int64 {
324322
strRunId := g.BuildID()
325323
runId, err := strconv.ParseInt(strRunId, 10, 64)
326324
if err != nil {
327-
return 0, errors.Wrapf(err, "invalid GITHUB_RUN_ID value %s: %s", strRunId, err)
325+
log.Entry().Debugf("invalid GITHUB_RUN_ID value %s: %s", strRunId, err)
326+
return 0
328327
}
329328

330-
return runId, nil
329+
return runId
331330
}
332331

333332
func getOwnerAndRepoNames() (string, string) {

pkg/orchestrator/github_actions_test.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@ import (
1717

1818
func TestGitHubActionsConfigProvider_GetBuildStatus(t *testing.T) {
1919
tests := []struct {
20-
name string
21-
runData run
22-
want string
20+
name string
21+
jobs []job
22+
want string
2323
}{
24-
{"BuildStatusSuccess", run{fetched: true, Status: "success"}, BuildStatusSuccess},
25-
{"BuildStatusAborted", run{fetched: true, Status: "cancelled"}, BuildStatusAborted},
26-
{"BuildStatusInProgress", run{fetched: true, Status: "in_progress"}, BuildStatusInProgress},
27-
{"BuildStatusFailure", run{fetched: true, Status: "qwertyu"}, BuildStatusFailure},
28-
{"BuildStatusFailure", run{fetched: true, Status: ""}, BuildStatusFailure},
24+
{"BuildStatusSuccess", []job{{Conclusion: "success"}, {Conclusion: "success"}, {Conclusion: "success"}}, BuildStatusSuccess},
25+
{"BuildStatusAborted", []job{{Conclusion: "success"}, {Conclusion: "success"}, {Conclusion: "cancelled"}}, BuildStatusAborted},
26+
{"BuildStatusFailure", []job{{Conclusion: "success"}, {Conclusion: "failure"}, {Conclusion: "cancelled"}}, BuildStatusFailure},
27+
{"BuildStatusSuccess", []job{{Conclusion: "success"}, {Conclusion: "cancelled"}, {Conclusion: "failure"}}, BuildStatusAborted},
2928
}
3029
for _, tt := range tests {
3130
t.Run(tt.name, func(t *testing.T) {
3231
g := &githubActionsConfigProvider{
33-
runData: tt.runData,
32+
jobsFetched: true,
33+
jobs: tt.jobs,
3434
}
3535
assert.Equalf(t, tt.want, g.BuildStatus(), "BuildStatus()")
3636
})
@@ -113,7 +113,6 @@ func TestGitHubActionsConfigProvider_fetchRunData(t *testing.T) {
113113
startedAt, _ := time.Parse(time.RFC3339, "2023-08-11T07:28:24Z")
114114
wantRunData := run{
115115
fetched: true,
116-
Status: "completed",
117116
StartedAt: startedAt,
118117
}
119118

@@ -285,7 +284,6 @@ func TestGitHubActionsConfigProvider_Others(t *testing.T) {
285284
startedAt, _ := time.Parse(time.RFC3339, "2023-08-11T07:28:24Z")
286285
p.runData = run{
287286
fetched: true,
288-
Status: "",
289287
StartedAt: startedAt,
290288
}
291289

0 commit comments

Comments
 (0)