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

Remove commit status running and warning to align GitHub #25839

Merged
Merged
28 changes: 25 additions & 3 deletions models/git/commit_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,18 @@ type CommitStatusOptions struct {
SortType string
}

// compatibleCommitStatus compatible with old version status
// and convert old version status to new version status
CaiCandong marked this conversation as resolved.
Show resolved Hide resolved
func compatibleCommitStatus(statuses []*CommitStatus) {
for i := range statuses {
if statuses[i].State == "warning" {
statuses[i].State = api.CommitStatusFailure
} else if statuses[i].State == "running" {
statuses[i].State = api.CommitStatusPending
}
}
}

// GetCommitStatuses returns all statuses for a given commit.
func GetCommitStatuses(ctx context.Context, repo *repo_model.Repository, sha string, opts *CommitStatusOptions) ([]*CommitStatus, int64, error) {
if opts.Page <= 0 {
Expand All @@ -239,13 +251,16 @@ func GetCommitStatuses(ctx context.Context, repo *repo_model.Repository, sha str
findSession := listCommitStatusesStatement(ctx, repo, sha, opts)
findSession = db.SetSessionPagination(findSession, opts)
sortCommitStatusesSession(findSession, opts.SortType)
return statuses, maxResults, findSession.Find(&statuses)
err = findSession.Find(&statuses)
// TODO: compatible with old version status,it will be removed in the future
compatibleCommitStatus(statuses)
return statuses, maxResults, err
}

func listCommitStatusesStatement(ctx context.Context, repo *repo_model.Repository, sha string, opts *CommitStatusOptions) *xorm.Session {
sess := db.GetEngine(ctx).Where("repo_id = ?", repo.ID).And("sha = ?", sha)
switch opts.State {
case "pending", "success", "error", "failure", "warning":
case "pending", "success", "error", "failure":
sess.And("state = ?", opts.State)
}
return sess
Expand Down Expand Up @@ -294,7 +309,10 @@ func GetLatestCommitStatus(ctx context.Context, repoID int64, sha string, listOp
if len(ids) == 0 {
return statuses, count, nil
}
return statuses, count, db.GetEngine(ctx).In("id", ids).Find(&statuses)
err = db.GetEngine(ctx).In("id", ids).Find(&statuses)
// TODO: compatible with old version status,it will be removed in the future
compatibleCommitStatus(statuses)
return statuses, count, err
}

// GetLatestCommitStatusForPairs returns all statuses with a unique context for a given list of repo-sha pairs
Expand Down Expand Up @@ -333,6 +351,8 @@ func GetLatestCommitStatusForPairs(ctx context.Context, repoIDsToLatestCommitSHA
statuses := make([]*CommitStatus, 0, len(ids))
if len(ids) > 0 {
err = db.GetEngine(ctx).In("id", ids).Find(&statuses)
// TODO: compatible with old version status,it will be removed in the future
compatibleCommitStatus(statuses)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -380,6 +400,8 @@ func GetLatestCommitStatusForRepoCommitIDs(ctx context.Context, repoID int64, co
statuses := make([]*CommitStatus, 0, len(ids))
if len(ids) > 0 {
err = db.GetEngine(ctx).In("id", ids).Find(&statuses)
// TODO: compatible with old version status,it will be removed in the future
compatibleCommitStatus(statuses)
if err != nil {
return nil, err
}
Expand Down
4 changes: 0 additions & 4 deletions models/git/commit_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ func TestGetCommitStatuses(t *testing.T) {
assert.Equal(t, structs.CommitStatusPending, statuses[0].State)
assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/statuses/1234123412341234123412341234123412341234", statuses[0].APIURL(db.DefaultContext))

assert.Equal(t, "cov/awesomeness", statuses[1].Context)
assert.Equal(t, structs.CommitStatusWarning, statuses[1].State)
assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/statuses/1234123412341234123412341234123412341234", statuses[1].APIURL(db.DefaultContext))

assert.Equal(t, "cov/awesomeness", statuses[2].Context)
assert.Equal(t, structs.CommitStatusSuccess, statuses[2].State)
assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/statuses/1234123412341234123412341234123412341234", statuses[2].APIURL(db.DefaultContext))
Expand Down
26 changes: 6 additions & 20 deletions modules/structs/commit_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,17 @@ const (
CommitStatusError CommitStatusState = "error"
// CommitStatusFailure is for when the CommitStatus is Failure
CommitStatusFailure CommitStatusState = "failure"
// CommitStatusWarning is for when the CommitStatus is Warning
CommitStatusWarning CommitStatusState = "warning"
// CommitStatusRunning is for when the CommitStatus is Running
CommitStatusRunning CommitStatusState = "running"
)

// NoBetterThan returns true if this State is no better than the given State
func (css CommitStatusState) NoBetterThan(css2 CommitStatusState) bool {
switch css {
case CommitStatusError:
return true
case CommitStatusFailure:
return css2 != CommitStatusError
case CommitStatusWarning:
return css2 != CommitStatusError && css2 != CommitStatusFailure
case CommitStatusPending:
return css2 != CommitStatusError && css2 != CommitStatusFailure && css2 != CommitStatusWarning
default:
return css2 != CommitStatusError && css2 != CommitStatusFailure && css2 != CommitStatusWarning && css2 != CommitStatusPending
commitStatusPriorities := map[CommitStatusState]int{
CommitStatusError: 0,
CommitStatusFailure: 1,
CommitStatusPending: 2,
CommitStatusSuccess: 3,
}
return commitStatusPriorities[css] <= commitStatusPriorities[css2]
}

// IsPending represents if commit status state is pending
Expand All @@ -57,8 +48,3 @@ func (css CommitStatusState) IsError() bool {
func (css CommitStatusState) IsFailure() bool {
return css == CommitStatusFailure
}

// IsWarning represents if commit status state is warning
func (css CommitStatusState) IsWarning() bool {
return css == CommitStatusWarning
}
8 changes: 2 additions & 6 deletions services/actions/commit_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,10 @@ func toCommitStatus(status actions_model.Status) api.CommitStatusState {
switch status {
case actions_model.StatusSuccess, actions_model.StatusSkipped:
return api.CommitStatusSuccess
case actions_model.StatusFailure:
case actions_model.StatusFailure, actions_model.StatusCancelled:
return api.CommitStatusFailure
case actions_model.StatusCancelled:
return api.CommitStatusWarning
case actions_model.StatusWaiting, actions_model.StatusBlocked:
case actions_model.StatusWaiting, actions_model.StatusBlocked, actions_model.StatusRunning:
return api.CommitStatusPending
case actions_model.StatusRunning:
return api.CommitStatusRunning
default:
return api.CommitStatusError
}
Expand Down
8 changes: 8 additions & 0 deletions services/convert/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ func ToCombinedStatus(ctx context.Context, statuses []*git_model.CommitStatus, r
retStatus.State = status.State
}
}
// According to https://docs.github.com/en/rest/commits/statuses?apiVersion=2022-11-28#get-the-combined-status-for-a-specific-reference
// > Additionally, a combined state is returned. The state is one of:
// > failure if any of the contexts report as error or failure
// > pending if there are no statuses or a context is pending
// > success if the latest status for all contexts is success
if retStatus.State.IsError() {
retStatus.State = api.CommitStatusFailure
}

return retStatus
}
6 changes: 0 additions & 6 deletions templates/repo/commit_status.tmpl
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
{{if eq .State "pending"}}
{{svg "octicon-dot-fill" 18 "commit-status icon text grey"}}
{{end}}
{{if eq .State "running"}}
{{svg "octicon-dot-fill" 18 "commit-status icon text yellow"}}
{{end}}
{{if eq .State "success"}}
{{svg "octicon-check" 18 "commit-status icon text green"}}
{{end}}
Expand All @@ -13,6 +10,3 @@
{{if eq .State "failure"}}
{{svg "octicon-x" 18 "commit-status icon text red"}}
{{end}}
{{if eq .State "warning"}}
{{svg "gitea-exclamation" 18 "commit-status icon text yellow"}}
{{end}}
2 changes: 0 additions & 2 deletions tests/integration/pull_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ func TestPullCreate_CommitStatus(t *testing.T) {
api.CommitStatusPending,
api.CommitStatusError,
api.CommitStatusFailure,
api.CommitStatusWarning,
api.CommitStatusSuccess,
}

Expand All @@ -61,7 +60,6 @@ func TestPullCreate_CommitStatus(t *testing.T) {
api.CommitStatusSuccess: "octicon-check",
api.CommitStatusError: "gitea-exclamation",
api.CommitStatusFailure: "octicon-x",
api.CommitStatusWarning: "gitea-exclamation",
}

testCtx := NewAPITestContext(t, "user1", "repo1", auth_model.AccessTokenScopeWriteRepository)
Expand Down
8 changes: 0 additions & 8 deletions tests/integration/repo_commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,6 @@ func TestRepoCommitsWithStatusFailure(t *testing.T) {
doTestRepoCommitWithStatus(t, "failure", "octicon-x", "red")
}

func TestRepoCommitsWithStatusWarning(t *testing.T) {
doTestRepoCommitWithStatus(t, "warning", "gitea-exclamation", "yellow")
}

func TestRepoCommitsWithStatusRunning(t *testing.T) {
doTestRepoCommitWithStatus(t, "running", "octicon-dot-fill", "yellow")
}

func TestRepoCommitsStatusParallel(t *testing.T) {
defer tests.PrepareTestEnv(t)()

Expand Down