Skip to content

Commit cecbbbd

Browse files
authored
Add failure state handling (#38)
1 parent 70d8add commit cecbbbd

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

internal/validators/status/validator.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
const (
1414
successState = "success"
1515
errorState = "error"
16+
failureState = "failure"
1617
pendingState = "pending"
1718
)
1819

@@ -128,7 +129,7 @@ func (sv *statusValidator) Validate(ctx context.Context) (validators.Status, err
128129
case successState:
129130
st.completeJobs = append(st.completeJobs, ghaStatus.Job)
130131
successCnt++
131-
case errorState:
132+
case errorState, failureState:
132133
st.errJobs = append(st.errJobs, ghaStatus.Job)
133134
}
134135
}

internal/validators/status/validator_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,44 @@ func Test_statusValidator_Validate(t *testing.T) {
203203
},
204204
}).Detail(),
205205
},
206+
"returns error when there is a failed job with failure state": {
207+
selfJobName: "self-job",
208+
client: &mock.Client{
209+
GetCombinedStatusFunc: func(ctx context.Context, owner, repo, ref string, opts *github.ListOptions) (*github.CombinedStatus, *github.Response, error) {
210+
return &github.CombinedStatus{
211+
Statuses: []*github.RepoStatus{
212+
{
213+
Context: stringPtr("job-01"),
214+
State: stringPtr(successState),
215+
},
216+
{
217+
Context: stringPtr("job-02"),
218+
State: stringPtr(failureState),
219+
},
220+
{
221+
Context: stringPtr("self-job"),
222+
State: stringPtr(pendingState),
223+
},
224+
},
225+
}, nil, nil
226+
},
227+
ListCheckRunsForRefFunc: func(ctx context.Context, owner, repo, ref string, opts *github.ListCheckRunsOptions) (*github.ListCheckRunsResults, *github.Response, error) {
228+
return &github.ListCheckRunsResults{}, nil, nil
229+
},
230+
},
231+
wantErr: true,
232+
wantErrStr: (&status{
233+
totalJobs: []string{
234+
"job-01", "job-02",
235+
},
236+
completeJobs: []string{
237+
"job-01",
238+
},
239+
errJobs: []string{
240+
"job-02",
241+
},
242+
}).Detail(),
243+
},
206244
"returns failed status and nil when successful job count is less than total": {
207245
selfJobName: "self-job",
208246
client: &mock.Client{
@@ -314,6 +352,40 @@ func Test_statusValidator_Validate(t *testing.T) {
314352
errJobs: []string{},
315353
},
316354
},
355+
"returns succeeded status and nil when only an ignored job is failing, with failure state": {
356+
selfJobName: "self-job",
357+
ignoredJobs: []string{"job-02", "job-03"},
358+
client: &mock.Client{
359+
GetCombinedStatusFunc: func(ctx context.Context, owner, repo, ref string, opts *github.ListOptions) (*github.CombinedStatus, *github.Response, error) {
360+
return &github.CombinedStatus{
361+
Statuses: []*github.RepoStatus{
362+
{
363+
Context: stringPtr("job-01"),
364+
State: stringPtr(successState),
365+
},
366+
{
367+
Context: stringPtr("job-02"),
368+
State: stringPtr(failureState),
369+
},
370+
{
371+
Context: stringPtr("self-job"),
372+
State: stringPtr(pendingState),
373+
},
374+
},
375+
}, nil, nil
376+
},
377+
ListCheckRunsForRefFunc: func(ctx context.Context, owner, repo, ref string, opts *github.ListCheckRunsOptions) (*github.ListCheckRunsResults, *github.Response, error) {
378+
return &github.ListCheckRunsResults{}, nil, nil
379+
},
380+
},
381+
wantErr: false,
382+
wantStatus: &status{
383+
succeeded: true,
384+
totalJobs: []string{"job-01"},
385+
completeJobs: []string{"job-01"},
386+
errJobs: []string{},
387+
},
388+
},
317389
}
318390
for name, tt := range tests {
319391
t.Run(name, func(t *testing.T) {

0 commit comments

Comments
 (0)