Skip to content

Commit 31e8115

Browse files
committed
fine tune
1 parent e843ac5 commit 31e8115

File tree

5 files changed

+22
-14
lines changed

5 files changed

+22
-14
lines changed

models/git/commit_status.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ func CalcCommitStatus(statuses []*CommitStatus) *CommitStatus {
247247
lastStatus = statuses[0]
248248
} else {
249249
// FIXME: another bad case: if the "statuses" slice is empty, the returned value is an invalid CommitStatus, all its fields are empty.
250+
// Frontend code (tmpl&vue) sometimes depend on the empty fields to skip rendering commit status elements (need to double check in the future)
250251
lastStatus = &CommitStatus{}
251252
}
252253
}

models/git/commit_status_summary.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ func UpdateCommitStatusSummary(ctx context.Context, repoID int64, sha string) er
5959
if err != nil {
6060
return err
6161
}
62-
state := CalcCommitStatus(commitStatuses)
62+
// it guarantees that commitStatuses is not empty because this function is always called after a commit status is created
63+
if len(commitStatuses) == 0 {
64+
setting.PanicInDevOrTesting("no commit statuses found for repo %d and sha %s", repoID, sha)
65+
}
66+
state := CalcCommitStatus(commitStatuses) // non-empty commitStatuses is guaranteed
6367
// mysql will return 0 when update a record which state hasn't been changed which behaviour is different from other database,
6468
// so we need to use insert in on duplicate
6569
if setting.Database.Type.IsMySQL() {

services/convert/status.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func ToCombinedStatus(ctx context.Context, statuses []*git_model.CommitStatus, r
4242
SHA: statuses[0].SHA,
4343
TotalCount: len(statuses),
4444
Repository: repo,
45-
URL: "",
45+
URL: "", // never set or used?
4646
State: api.CommitStatusSuccess,
4747
}
4848

@@ -58,9 +58,13 @@ func ToCombinedStatus(ctx context.Context, statuses []*git_model.CommitStatus, r
5858
// > failure if any of the contexts report as error or failure
5959
// > pending if there are no statuses or a context is pending
6060
// > success if the latest status for all contexts is success
61-
if retStatus.State.IsError() {
62-
retStatus.State = api.CommitStatusFailure
61+
switch retStatus.State {
62+
case api.CommitStatusSkipped:
63+
retStatus.State = api.CommitStatusSuccess // all skipped means success
64+
case api.CommitStatusPending, api.CommitStatusSuccess:
65+
// use the current state for pending or success
66+
default:
67+
retStatus.State = api.CommitStatusFailure // otherwise, it is a failure
6368
}
64-
6569
return retStatus
6670
}

services/pull/commit_status.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,15 @@ func MergeRequiredContextsCommitStatus(commitStatuses []*git_model.CommitStatus,
5959
}
6060

6161
if matchedCount == 0 && returnedStatus == structs.CommitStatusSuccess {
62+
if len(commitStatuses) == 0 {
63+
// "no statuses" should mean "pending"
64+
return structs.CommitStatusPending
65+
}
6266
status := git_model.CalcCommitStatus(commitStatuses)
63-
if status != nil {
64-
// FIXME: this check is not right, "status" can never be nil, but its fields can be empty if commitStatuses is empty
65-
// here is just a quick patch to make it overall right.
66-
if status.State == "" || status.State == structs.CommitStatusSkipped {
67-
return structs.CommitStatusSuccess
68-
}
69-
return status.State
67+
if status.State == structs.CommitStatusSkipped {
68+
return structs.CommitStatusSuccess // if all statuses are skipped, return success
7069
}
71-
return structs.CommitStatusSuccess
70+
return status.State
7271
}
7372

7473
return returnedStatus

services/pull/commit_status_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestMergeRequiredContextsCommitStatus(t *testing.T) {
2222
{
2323
commitStatuses: []*git_model.CommitStatus{},
2424
requiredContexts: []string{},
25-
expected: structs.CommitStatusSuccess,
25+
expected: structs.CommitStatusPending,
2626
},
2727
{
2828
commitStatuses: []*git_model.CommitStatus{

0 commit comments

Comments
 (0)