Skip to content

Commit ad082d8

Browse files
authored
Refine ignored handling (#45)
* Add ignore detalis in the summary * Update string handling to remove space and empty input * Adjust test case and validation as trim logic moved to option * Move trimming logic out from validator
1 parent dd76382 commit ad082d8

File tree

5 files changed

+77
-22
lines changed

5 files changed

+77
-22
lines changed

internal/validators/status/option.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,19 @@ func WithGitHubRef(ref string) Option {
3434
func WithIgnoredJobs(names string) Option {
3535
return func(s *statusValidator) {
3636
// TODO: Add more input validation, such as "," should not be a valid input.
37-
if len(names) != 0 {
38-
s.ignoredJobs = strings.Split(names, ",")
37+
if len(names) == 0 {
38+
return // TODO: Return some clearer error
3939
}
40+
41+
jobs := []string{}
42+
ss := strings.Split(names, ",")
43+
for _, s := range ss {
44+
jobName := strings.TrimSpace(s)
45+
if len(jobName) == 0 {
46+
continue // TODO: Provide more clue to users
47+
}
48+
jobs = append(jobs, jobName)
49+
}
50+
s.ignoredJobs = jobs
4051
}
4152
}

internal/validators/status/status.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ type status struct {
66
totalJobs []string
77
completeJobs []string
88
errJobs []string
9+
ignoredJobs []string
910
succeeded bool
1011
}
1112

1213
func (s *status) Detail() string {
13-
return fmt.Sprintf(
14+
result := fmt.Sprintf(
1415
`%d out of %d
1516
1617
Total job count: %d
@@ -25,6 +26,16 @@ func (s *status) Detail() string {
2526
len(s.completeJobs), s.completeJobs,
2627
len(s.errJobs), s.errJobs,
2728
)
29+
30+
if len(s.ignoredJobs) > 0 {
31+
result = fmt.Sprintf(
32+
`%s
33+
34+
--
35+
Ignored jobs: %+q`, result, s.ignoredJobs)
36+
}
37+
38+
return result
2839
}
2940

3041
func (s *status) IsSuccess() bool {

internal/validators/status/status_test.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,38 @@ func Test_status_Detail(t *testing.T) {
3232
Failed job count: 1
3333
jobs: ["job-3"]
3434
`,
35+
},
36+
"return detail with ignored jobs input": {
37+
s: &status{
38+
totalJobs: []string{
39+
"job-1",
40+
"job-2",
41+
"job-3",
42+
"job-4",
43+
},
44+
completeJobs: []string{
45+
"job-2",
46+
"job-4",
47+
},
48+
errJobs: []string{
49+
"job-3",
50+
},
51+
ignoredJobs: []string{
52+
"job-4",
53+
},
54+
},
55+
want: `2 out of 4
56+
57+
Total job count: 4
58+
jobs: ["job-1" "job-2" "job-3" "job-4"]
59+
Completed job count: 2
60+
jobs: ["job-2" "job-4"]
61+
Failed job count: 1
62+
jobs: ["job-3"]
63+
64+
65+
--
66+
Ignored jobs: ["job-4"]`,
3567
},
3668
"return detail when totalJobs and completeJobs is empty": {
3769
s: &status{
@@ -54,7 +86,7 @@ func Test_status_Detail(t *testing.T) {
5486
t.Run(name, func(t *testing.T) {
5587
got := tt.s.Detail()
5688
if got != tt.want {
57-
t.Errorf("status.Detail() str = %s, want: %s", got, tt.want)
89+
t.Errorf("status.Detail() didn't match\n got:\n%s\n\n want:\n%s", got, tt.want)
5890
}
5991
})
6092
}

internal/validators/status/validator.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"strings"
87

98
"github.com/upsidr/merge-gatekeeper/internal/github"
109
"github.com/upsidr/merge-gatekeeper/internal/multierror"
@@ -79,11 +78,6 @@ func (sv *statusValidator) validateFields() error {
7978
if len(sv.selfJobName) == 0 {
8079
errs = append(errs, errors.New("self job name is empty"))
8180
}
82-
for _, job := range sv.ignoredJobs {
83-
if len(job) == 0 {
84-
errs = append(errs, errors.New("ignored job name is empty"))
85-
}
86-
}
8781
if sv.client == nil {
8882
errs = append(errs, errors.New("github client is empty"))
8983
}
@@ -112,7 +106,7 @@ func (sv *statusValidator) Validate(ctx context.Context) (validators.Status, err
112106
for _, ghaStatus := range ghaStatuses {
113107
var toIgnore bool
114108
for _, ignored := range sv.ignoredJobs {
115-
if ghaStatus.Job == strings.TrimSpace(ignored) {
109+
if ghaStatus.Job == ignored {
116110
toIgnore = true
117111
break
118112
}

internal/validators/status/validator_test.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,32 +58,39 @@ func TestCreateValidator(t *testing.T) {
5858
},
5959
wantErr: false,
6060
},
61-
"returns error when option is empty": {
62-
c: &mock.Client{},
63-
want: nil,
64-
wantErr: true,
65-
},
66-
"returns error when client is nil": {
67-
c: nil,
61+
"returns Validator when invalid string is provided for ignored jobs": {
62+
c: &mock.Client{},
6863
opts: []Option{
6964
WithGitHubOwnerAndRepo("test", "test-repo"),
7065
WithGitHubRef("sha"),
7166
WithGitHubRef("sha-01"),
7267
WithSelfJob("job"),
7368
WithSelfJob("job-01"),
69+
WithIgnoredJobs(","), // Malformed but handled
70+
},
71+
want: &statusValidator{
72+
client: &mock.Client{},
73+
owner: "test",
74+
repo: "test-repo",
75+
ref: "sha-01",
76+
selfJobName: "job-01",
77+
ignoredJobs: []string{}, // Not nil
7478
},
79+
wantErr: false,
80+
},
81+
"returns error when option is empty": {
82+
c: &mock.Client{},
7583
want: nil,
7684
wantErr: true,
7785
},
78-
"returns error when ignored jobs is an empty string": {
79-
c: &mock.Client{},
86+
"returns error when client is nil": {
87+
c: nil,
8088
opts: []Option{
8189
WithGitHubOwnerAndRepo("test", "test-repo"),
8290
WithGitHubRef("sha"),
8391
WithGitHubRef("sha-01"),
8492
WithSelfJob("job"),
8593
WithSelfJob("job-01"),
86-
WithIgnoredJobs(","), // Malformed, and causes the error
8794
},
8895
want: nil,
8996
wantErr: true,
@@ -376,7 +383,7 @@ func Test_statusValidator_Validate(t *testing.T) {
376383
},
377384
"returns succeeded status and nil when only an ignored job is failing": {
378385
selfJobName: "self-job",
379-
ignoredJobs: []string{"job-02 ", "job-03"}, // Some extra space will be trimmed by strings.TrimSpace
386+
ignoredJobs: []string{"job-02", "job-03"}, // String input here should be already TrimSpace'd
380387
client: &mock.Client{
381388
GetCombinedStatusFunc: func(ctx context.Context, owner, repo, ref string, opts *github.ListOptions) (*github.CombinedStatus, *github.Response, error) {
382389
return &github.CombinedStatus{

0 commit comments

Comments
 (0)