Skip to content

Commit 8d78184

Browse files
committed
clean up
1 parent 40bcd57 commit 8d78184

File tree

5 files changed

+21
-173
lines changed

5 files changed

+21
-173
lines changed

models/git/commit_status.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func CalcCommitStatus(statuses []*CommitStatus) *CommitStatus {
233233
var lastStatus *CommitStatus
234234
state := api.CommitStatusSuccess
235235
for _, status := range statuses {
236-
if status.State.NoBetterThan(state) {
236+
if status.State.HasHigherPriorityThan(state) {
237237
state = status.State
238238
lastStatus = status
239239
}

modules/structs/commit_status.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,10 @@ func (css CommitStatusState) String() string {
3535
return string(css)
3636
}
3737

38-
// NoBetterThan returns true if this State is no better than the given State
39-
// This function only handles the states defined in CommitStatusPriorities
40-
func (css CommitStatusState) NoBetterThan(css2 CommitStatusState) bool {
41-
// only handle the states with defined priorities above, it always returns false for undefined priorities
42-
if _, exist := commitStatusPriorities[css]; !exist {
43-
return false
44-
}
45-
46-
if _, exist := commitStatusPriorities[css2]; !exist {
47-
return false
48-
}
49-
50-
return commitStatusPriorities[css] <= commitStatusPriorities[css2]
38+
// HasHigherPriorityThan returns true if this state has higher priority than the other
39+
// Undefined states are considered to have the highest priority like CommitStatusError(0)
40+
func (css CommitStatusState) HasHigherPriorityThan(other CommitStatusState) bool {
41+
return commitStatusPriorities[css] < commitStatusPriorities[other]
5142
}
5243

5344
// IsPending represents if commit status state is pending

modules/structs/commit_status_test.go

Lines changed: 12 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -10,165 +10,21 @@ import (
1010
)
1111

1212
func TestNoBetterThan(t *testing.T) {
13-
type args struct {
14-
css CommitStatusState
15-
css2 CommitStatusState
16-
}
17-
var unExpectedState CommitStatusState
1813
tests := []struct {
19-
name string
20-
args args
21-
want bool
14+
s1, s2 CommitStatusState
15+
higher bool
2216
}{
23-
{
24-
name: "success is no better than success",
25-
args: args{
26-
css: CommitStatusSuccess,
27-
css2: CommitStatusSuccess,
28-
},
29-
want: true,
30-
},
31-
{
32-
name: "success is no better than pending",
33-
args: args{
34-
css: CommitStatusSuccess,
35-
css2: CommitStatusPending,
36-
},
37-
want: false,
38-
},
39-
{
40-
name: "success is no better than failure",
41-
args: args{
42-
css: CommitStatusSuccess,
43-
css2: CommitStatusFailure,
44-
},
45-
want: false,
46-
},
47-
{
48-
name: "success is no better than error",
49-
args: args{
50-
css: CommitStatusSuccess,
51-
css2: CommitStatusError,
52-
},
53-
want: false,
54-
},
55-
{
56-
name: "pending is no better than success",
57-
args: args{
58-
css: CommitStatusPending,
59-
css2: CommitStatusSuccess,
60-
},
61-
want: true,
62-
},
63-
{
64-
name: "pending is no better than pending",
65-
args: args{
66-
css: CommitStatusPending,
67-
css2: CommitStatusPending,
68-
},
69-
want: true,
70-
},
71-
{
72-
name: "pending is no better than failure",
73-
args: args{
74-
css: CommitStatusPending,
75-
css2: CommitStatusFailure,
76-
},
77-
want: false,
78-
},
79-
{
80-
name: "pending is no better than error",
81-
args: args{
82-
css: CommitStatusPending,
83-
css2: CommitStatusError,
84-
},
85-
want: false,
86-
},
87-
{
88-
name: "failure is no better than success",
89-
args: args{
90-
css: CommitStatusFailure,
91-
css2: CommitStatusSuccess,
92-
},
93-
want: true,
94-
},
95-
{
96-
name: "failure is no better than pending",
97-
args: args{
98-
css: CommitStatusFailure,
99-
css2: CommitStatusPending,
100-
},
101-
want: true,
102-
},
103-
{
104-
name: "failure is no better than failure",
105-
args: args{
106-
css: CommitStatusFailure,
107-
css2: CommitStatusFailure,
108-
},
109-
want: true,
110-
},
111-
{
112-
name: "failure is no better than error",
113-
args: args{
114-
css: CommitStatusFailure,
115-
css2: CommitStatusError,
116-
},
117-
want: false,
118-
},
119-
{
120-
name: "error is no better than success",
121-
args: args{
122-
css: CommitStatusError,
123-
css2: CommitStatusSuccess,
124-
},
125-
want: true,
126-
},
127-
{
128-
name: "error is no better than pending",
129-
args: args{
130-
css: CommitStatusError,
131-
css2: CommitStatusPending,
132-
},
133-
want: true,
134-
},
135-
{
136-
name: "error is no better than failure",
137-
args: args{
138-
css: CommitStatusError,
139-
css2: CommitStatusFailure,
140-
},
141-
want: true,
142-
},
143-
{
144-
name: "error is no better than error",
145-
args: args{
146-
css: CommitStatusError,
147-
css2: CommitStatusError,
148-
},
149-
want: true,
150-
},
151-
{
152-
name: "unExpectedState is no better than success",
153-
args: args{
154-
css: unExpectedState,
155-
css2: CommitStatusSuccess,
156-
},
157-
want: false,
158-
},
159-
{
160-
name: "unExpectedState is no better than unExpectedState",
161-
args: args{
162-
css: unExpectedState,
163-
css2: unExpectedState,
164-
},
165-
want: false,
166-
},
17+
{CommitStatusError, CommitStatusFailure, true},
18+
{CommitStatusFailure, CommitStatusWarning, true},
19+
{CommitStatusWarning, CommitStatusPending, true},
20+
{CommitStatusPending, CommitStatusSuccess, true},
21+
{CommitStatusSuccess, CommitStatusSkipped, true},
22+
23+
{CommitStatusError, "unknown-xxx", false},
24+
{"unknown-xxx", CommitStatusFailure, true},
16725
}
16826
for _, tt := range tests {
169-
t.Run(tt.name, func(t *testing.T) {
170-
result := tt.args.css.NoBetterThan(tt.args.css2)
171-
assert.Equal(t, tt.want, result)
172-
})
27+
assert.Equal(t, tt.higher, tt.s1.HasHigherPriorityThan(tt.s2), "s1=%s, s2=%s, expected=%v", tt.s1, tt.s2, tt.higher)
17328
}
29+
assert.Equal(t, false, CommitStatusError.HasHigherPriorityThan(CommitStatusError))
17430
}

services/convert/status.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ func ToCombinedStatus(ctx context.Context, statuses []*git_model.CommitStatus, r
4343
TotalCount: len(statuses),
4444
Repository: repo,
4545
URL: "",
46+
State: api.CommitStatusSuccess,
4647
}
4748

4849
retStatus.Statuses = make([]*api.CommitStatus, 0, len(statuses))
4950
for _, status := range statuses {
5051
retStatus.Statuses = append(retStatus.Statuses, ToCommitStatus(ctx, status))
51-
if retStatus.State == "" || status.State.NoBetterThan(retStatus.State) {
52+
if status.State.HasHigherPriorityThan(retStatus.State) {
5253
retStatus.State = status.State
5354
}
5455
}

services/pull/commit_status.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ func MergeRequiredContextsCommitStatus(commitStatuses []*git_model.CommitStatus,
4646

4747
// If required rule not match any action, then it is pending
4848
if targetStatus == "" {
49-
if structs.CommitStatusPending.NoBetterThan(returnedStatus) {
49+
if structs.CommitStatusPending.HasHigherPriorityThan(returnedStatus) {
5050
returnedStatus = structs.CommitStatusPending
5151
}
5252
break
5353
}
5454

55-
if targetStatus.NoBetterThan(returnedStatus) {
55+
if targetStatus.HasHigherPriorityThan(returnedStatus) {
5656
returnedStatus = targetStatus
5757
}
5858
}

0 commit comments

Comments
 (0)