Skip to content

Commit

Permalink
Add failure state handling (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
rytswd authored Aug 12, 2022
1 parent 70d8add commit cecbbbd
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
3 changes: 2 additions & 1 deletion internal/validators/status/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
const (
successState = "success"
errorState = "error"
failureState = "failure"
pendingState = "pending"
)

Expand Down Expand Up @@ -128,7 +129,7 @@ func (sv *statusValidator) Validate(ctx context.Context) (validators.Status, err
case successState:
st.completeJobs = append(st.completeJobs, ghaStatus.Job)
successCnt++
case errorState:
case errorState, failureState:
st.errJobs = append(st.errJobs, ghaStatus.Job)
}
}
Expand Down
72 changes: 72 additions & 0 deletions internal/validators/status/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,44 @@ func Test_statusValidator_Validate(t *testing.T) {
},
}).Detail(),
},
"returns error when there is a failed job with failure state": {
selfJobName: "self-job",
client: &mock.Client{
GetCombinedStatusFunc: func(ctx context.Context, owner, repo, ref string, opts *github.ListOptions) (*github.CombinedStatus, *github.Response, error) {
return &github.CombinedStatus{
Statuses: []*github.RepoStatus{
{
Context: stringPtr("job-01"),
State: stringPtr(successState),
},
{
Context: stringPtr("job-02"),
State: stringPtr(failureState),
},
{
Context: stringPtr("self-job"),
State: stringPtr(pendingState),
},
},
}, nil, nil
},
ListCheckRunsForRefFunc: func(ctx context.Context, owner, repo, ref string, opts *github.ListCheckRunsOptions) (*github.ListCheckRunsResults, *github.Response, error) {
return &github.ListCheckRunsResults{}, nil, nil
},
},
wantErr: true,
wantErrStr: (&status{
totalJobs: []string{
"job-01", "job-02",
},
completeJobs: []string{
"job-01",
},
errJobs: []string{
"job-02",
},
}).Detail(),
},
"returns failed status and nil when successful job count is less than total": {
selfJobName: "self-job",
client: &mock.Client{
Expand Down Expand Up @@ -314,6 +352,40 @@ func Test_statusValidator_Validate(t *testing.T) {
errJobs: []string{},
},
},
"returns succeeded status and nil when only an ignored job is failing, with failure state": {
selfJobName: "self-job",
ignoredJobs: []string{"job-02", "job-03"},
client: &mock.Client{
GetCombinedStatusFunc: func(ctx context.Context, owner, repo, ref string, opts *github.ListOptions) (*github.CombinedStatus, *github.Response, error) {
return &github.CombinedStatus{
Statuses: []*github.RepoStatus{
{
Context: stringPtr("job-01"),
State: stringPtr(successState),
},
{
Context: stringPtr("job-02"),
State: stringPtr(failureState),
},
{
Context: stringPtr("self-job"),
State: stringPtr(pendingState),
},
},
}, nil, nil
},
ListCheckRunsForRefFunc: func(ctx context.Context, owner, repo, ref string, opts *github.ListCheckRunsOptions) (*github.ListCheckRunsResults, *github.Response, error) {
return &github.ListCheckRunsResults{}, nil, nil
},
},
wantErr: false,
wantStatus: &status{
succeeded: true,
totalJobs: []string{"job-01"},
completeJobs: []string{"job-01"},
errJobs: []string{},
},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
Expand Down

0 comments on commit cecbbbd

Please sign in to comment.