Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve job status check handling #28

Merged
merged 6 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions internal/validators/status/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,20 @@ func (sv *statusValidator) listGhaStatuses(ctx context.Context) ([]*ghaStatus, e
return nil, err
}

// Because multiple jobs with the same name may exist when jobs are created dynamically by third-party tools, etc.,
// only the latest job should be managed.
currentJobs := make(map[string]struct{})

ghaStatuses := make([]*ghaStatus, 0, len(combined.Statuses))
for _, s := range combined.Statuses {
if s.Context == nil || s.State == nil {
return nil, fmt.Errorf("%w context: %v, status: %v", ErrInvalidCombinedStatusResponse, s.Context, s.State)
}
if _, ok := currentJobs[*s.Context]; ok {
continue
}
currentJobs[*s.Context] = struct{}{}

ghaStatuses = append(ghaStatuses, &ghaStatus{
Job: *s.Context,
State: *s.State,
Expand All @@ -155,6 +164,11 @@ func (sv *statusValidator) listGhaStatuses(ctx context.Context) ([]*ghaStatus, e
if run.Name == nil || run.Status == nil {
return nil, fmt.Errorf("%w name: %v, status: %v", ErrInvalidCheckRunResponse, run.Name, run.Status)
}
if _, ok := currentJobs[*run.Name]; ok {
continue
}
currentJobs[*run.Name] = struct{}{}

ghaStatus := &ghaStatus{
Job: *run.Name,
}
Expand Down
87 changes: 87 additions & 0 deletions internal/validators/status/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,93 @@ func Test_statusValidator_listStatues(t *testing.T) {
want []*ghaStatus
}
tests := map[string]test{
"succeeds to get job statuses even if the same job exists": func() test {
c := &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{
// The first element here is the latest state.
{
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
Context: stringPtr("job-01"),
State: stringPtr(successState),
},
{
Context: stringPtr("job-01"), // Same as above job name, and thus should be disregarded as old job status.
State: stringPtr(errorState),
},
},
}, nil, nil
},
ListCheckRunsForRefFunc: func(ctx context.Context, owner, repo, ref string, opts *github.ListCheckRunsOptions) (*github.ListCheckRunsResults, *github.Response, error) {
return &github.ListCheckRunsResults{
CheckRuns: []*github.CheckRun{
// The first element here is the latest state.
{
Name: stringPtr("job-02"),
Status: stringPtr("failure"),
},
{
Name: stringPtr("job-02"), // Same as above job name, and thus should be disregarded as old job status.
Status: stringPtr(checkRunCompletedStatus),
Conclusion: stringPtr(checkRunNeutralConclusion),
},
{
Name: stringPtr("job-03"),
Status: stringPtr(checkRunCompletedStatus),
Conclusion: stringPtr(checkRunNeutralConclusion),
},
{
Name: stringPtr("job-04"),
Status: stringPtr(checkRunCompletedStatus),
Conclusion: stringPtr(checkRunSuccessConclusion),
},
{
Name: stringPtr("job-05"),
Status: stringPtr(checkRunCompletedStatus),
Conclusion: stringPtr("failure"),
},
{
Name: stringPtr("job-06"),
Status: stringPtr(checkRunCompletedStatus),
Conclusion: stringPtr(checkRunSkipConclusion),
},
},
}, nil, nil
},
}
return test{
fields: fields{
client: c,
selfJobName: "self-job",
owner: "test-owner",
repo: "test-repo",
ref: "main",
},
wantErr: false,
want: []*ghaStatus{
{
Job: "job-01",
State: successState,
},
{
Job: "job-02",
State: pendingState,
},
{
Job: "job-03",
State: successState,
},
{
Job: "job-04",
State: successState,
},
{
Job: "job-05",
State: errorState,
},
},
}
}(),
"returns error when the GetCombinedStatus returns an error": func() test {
c := &mock.Client{
GetCombinedStatusFunc: func(ctx context.Context, owner, repo, ref string, opts *github.ListOptions) (*github.CombinedStatus, *github.Response, error) {
Expand Down