Skip to content

Commit 1335958

Browse files
CaiCandongyp05327puni9869
authored
refactor improve NoBetterThan (#26126)
- The `NoBetterThan` function can only handle comparisons between "pending," "success," "error," and "failure." For any other comparison, we directly return false. This prevents logic errors like the one in #26121. - The callers of the `NoBetterThan` function should also avoid making incomparable calls. --------- Co-authored-by: yp05327 <576951401@qq.com> Co-authored-by: puni9869 <80308335+puni9869@users.noreply.github.com>
1 parent df9afe3 commit 1335958

File tree

4 files changed

+193
-8
lines changed

4 files changed

+193
-8
lines changed

modules/structs/commit_status.go

+17-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
package structs
55

66
// CommitStatusState holds the state of a CommitStatus
7-
// It can be "pending", "success", "error", "failure", and "warning"
7+
// It can be "pending", "success", "error" and "failure"
88
type CommitStatusState string
99

1010
const (
@@ -18,14 +18,25 @@ const (
1818
CommitStatusFailure CommitStatusState = "failure"
1919
)
2020

21+
var commitStatusPriorities = map[CommitStatusState]int{
22+
CommitStatusError: 0,
23+
CommitStatusFailure: 1,
24+
CommitStatusPending: 2,
25+
CommitStatusSuccess: 3,
26+
}
27+
2128
// NoBetterThan returns true if this State is no better than the given State
29+
// This function only handles the states defined in CommitStatusPriorities
2230
func (css CommitStatusState) NoBetterThan(css2 CommitStatusState) bool {
23-
commitStatusPriorities := map[CommitStatusState]int{
24-
CommitStatusError: 0,
25-
CommitStatusFailure: 1,
26-
CommitStatusPending: 2,
27-
CommitStatusSuccess: 3,
31+
// NoBetterThan only handles the 4 states above
32+
if _, exist := commitStatusPriorities[css]; !exist {
33+
return false
2834
}
35+
36+
if _, exist := commitStatusPriorities[css2]; !exist {
37+
return false
38+
}
39+
2940
return commitStatusPriorities[css] <= commitStatusPriorities[css2]
3041
}
3142

modules/structs/commit_status_test.go

+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package structs
5+
6+
import (
7+
"testing"
8+
)
9+
10+
func TestNoBetterThan(t *testing.T) {
11+
type args struct {
12+
css CommitStatusState
13+
css2 CommitStatusState
14+
}
15+
var unExpectedState CommitStatusState
16+
tests := []struct {
17+
name string
18+
args args
19+
want bool
20+
}{
21+
{
22+
name: "success is no better than success",
23+
args: args{
24+
css: CommitStatusSuccess,
25+
css2: CommitStatusSuccess,
26+
},
27+
want: true,
28+
},
29+
{
30+
name: "success is no better than pending",
31+
args: args{
32+
css: CommitStatusSuccess,
33+
css2: CommitStatusPending,
34+
},
35+
want: false,
36+
},
37+
{
38+
name: "success is no better than failure",
39+
args: args{
40+
css: CommitStatusSuccess,
41+
css2: CommitStatusFailure,
42+
},
43+
want: false,
44+
},
45+
{
46+
name: "success is no better than error",
47+
args: args{
48+
css: CommitStatusSuccess,
49+
css2: CommitStatusError,
50+
},
51+
want: false,
52+
},
53+
{
54+
name: "pending is no better than success",
55+
args: args{
56+
css: CommitStatusPending,
57+
css2: CommitStatusSuccess,
58+
},
59+
want: true,
60+
},
61+
{
62+
name: "pending is no better than pending",
63+
args: args{
64+
css: CommitStatusPending,
65+
css2: CommitStatusPending,
66+
},
67+
want: true,
68+
},
69+
{
70+
name: "pending is no better than failure",
71+
args: args{
72+
css: CommitStatusPending,
73+
css2: CommitStatusFailure,
74+
},
75+
want: false,
76+
},
77+
{
78+
name: "pending is no better than error",
79+
args: args{
80+
css: CommitStatusPending,
81+
css2: CommitStatusError,
82+
},
83+
want: false,
84+
},
85+
{
86+
name: "failure is no better than success",
87+
args: args{
88+
css: CommitStatusFailure,
89+
css2: CommitStatusSuccess,
90+
},
91+
want: true,
92+
},
93+
{
94+
name: "failure is no better than pending",
95+
args: args{
96+
css: CommitStatusFailure,
97+
css2: CommitStatusPending,
98+
},
99+
want: true,
100+
},
101+
{
102+
name: "failure is no better than failure",
103+
args: args{
104+
css: CommitStatusFailure,
105+
css2: CommitStatusFailure,
106+
},
107+
want: true,
108+
},
109+
{
110+
name: "failure is no better than error",
111+
args: args{
112+
css: CommitStatusFailure,
113+
css2: CommitStatusError,
114+
},
115+
want: false,
116+
},
117+
{
118+
name: "error is no better than success",
119+
args: args{
120+
css: CommitStatusError,
121+
css2: CommitStatusSuccess,
122+
},
123+
want: true,
124+
},
125+
{
126+
name: "error is no better than pending",
127+
args: args{
128+
css: CommitStatusError,
129+
css2: CommitStatusPending,
130+
},
131+
want: true,
132+
},
133+
{
134+
name: "error is no better than failure",
135+
args: args{
136+
css: CommitStatusError,
137+
css2: CommitStatusFailure,
138+
},
139+
want: true,
140+
},
141+
{
142+
name: "error is no better than error",
143+
args: args{
144+
css: CommitStatusError,
145+
css2: CommitStatusError,
146+
},
147+
want: true,
148+
},
149+
{
150+
name: "unExpectedState is no better than success",
151+
args: args{
152+
css: unExpectedState,
153+
css2: CommitStatusSuccess,
154+
},
155+
want: false,
156+
},
157+
{
158+
name: "unExpectedState is no better than unExpectedState",
159+
args: args{
160+
css: unExpectedState,
161+
css2: unExpectedState,
162+
},
163+
want: false,
164+
},
165+
}
166+
for _, tt := range tests {
167+
t.Run(tt.name, func(t *testing.T) {
168+
result := tt.args.css.NoBetterThan(tt.args.css2)
169+
if result != tt.want {
170+
t.Errorf("NoBetterThan() = %v, want %v", result, tt.want)
171+
}
172+
})
173+
}
174+
}

services/convert/status.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func ToCombinedStatus(ctx context.Context, statuses []*git_model.CommitStatus, r
4848
retStatus.Statuses = make([]*api.CommitStatus, 0, len(statuses))
4949
for _, status := range statuses {
5050
retStatus.Statuses = append(retStatus.Statuses, ToCommitStatus(ctx, status))
51-
if status.State.NoBetterThan(retStatus.State) {
51+
if retStatus.State == "" || status.State.NoBetterThan(retStatus.State) {
5252
retStatus.State = status.State
5353
}
5454
}

templates/swagger/v1_json.tmpl

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)