mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-23 23:14:32 +02:00
Introduce combined status
This commit is contained in:
28 files changed
+340
-271
No files matched your search
@@ -0,0 +1,35 @@
|
||||
// Copyright 2025 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package commitstatus
|
||||
|
||||
// CombinedStatus represents the combined status of a commit.
|
||||
type CombinedStatus string
|
||||
|
||||
const (
|
||||
// CombinedStatusPending is for when the CombinedStatus is Pending
|
||||
CombinedStatusPending CombinedStatus = "pending"
|
||||
// CombinedStatusSuccess is for when the CombinedStatus is Success
|
||||
CombinedStatusSuccess CombinedStatus = "success"
|
||||
// CombinedStatusFailure is for when the CombinedStatus is Failure
|
||||
CombinedStatusFailure CombinedStatus = "failure"
|
||||
)
|
||||
|
||||
func (cs CombinedStatus) String() string {
|
||||
return string(cs)
|
||||
}
|
||||
|
||||
// IsPending represents if commit status state is pending
|
||||
func (cs CombinedStatus) IsPending() bool {
|
||||
return cs == CombinedStatusPending
|
||||
}
|
||||
|
||||
// IsSuccess represents if commit status state is success
|
||||
func (cs CombinedStatus) IsSuccess() bool {
|
||||
return cs == CombinedStatusSuccess
|
||||
}
|
||||
|
||||
// IsFailure represents if commit status state is failure
|
||||
func (cs CombinedStatus) IsFailure() bool {
|
||||
return cs == CombinedStatusFailure
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package structs
|
||||
package commitstatus
|
||||
|
||||
// CommitStatusState holds the state of a CommitStatus
|
||||
// It can be "pending", "success", "error" and "failure"
|
||||
type CommitStatusState string
|
||||
type CommitStatusState string //nolint
|
||||
|
||||
const (
|
||||
// CommitStatusPending is for when the CommitStatus is Pending
|
||||
@@ -71,3 +71,28 @@ func (css CommitStatusState) IsFailure() bool {
|
||||
func (css CommitStatusState) IsWarning() bool {
|
||||
return css == CommitStatusWarning
|
||||
}
|
||||
|
||||
type CommitStatusStates []CommitStatusState //nolint
|
||||
|
||||
// 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
|
||||
|
||||
func (css CommitStatusStates) Merge() CombinedStatus {
|
||||
successCnt := 0
|
||||
for _, state := range css {
|
||||
switch {
|
||||
case state.IsError() || state.IsFailure():
|
||||
return CombinedStatusFailure
|
||||
case state.IsPending():
|
||||
case state.IsSuccess() || state.IsWarning():
|
||||
successCnt++
|
||||
}
|
||||
}
|
||||
if successCnt > 0 && successCnt == len(css) {
|
||||
return CombinedStatusSuccess
|
||||
}
|
||||
return CombinedStatusPending
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package structs
|
||||
package commitstatus
|
||||
|
||||
import (
|
||||
"testing"
|
||||
+20
-18
@@ -5,17 +5,19 @@ package structs
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/commitstatus"
|
||||
)
|
||||
|
||||
// CommitStatus holds a single status of a single Commit
|
||||
type CommitStatus struct {
|
||||
ID int64 `json:"id"`
|
||||
State CommitStatusState `json:"status"`
|
||||
TargetURL string `json:"target_url"`
|
||||
Description string `json:"description"`
|
||||
URL string `json:"url"`
|
||||
Context string `json:"context"`
|
||||
Creator *User `json:"creator"`
|
||||
ID int64 `json:"id"`
|
||||
State commitstatus.CommitStatusState `json:"status"`
|
||||
TargetURL string `json:"target_url"`
|
||||
Description string `json:"description"`
|
||||
URL string `json:"url"`
|
||||
Context string `json:"context"`
|
||||
Creator *User `json:"creator"`
|
||||
// swagger:strfmt date-time
|
||||
Created time.Time `json:"created_at"`
|
||||
// swagger:strfmt date-time
|
||||
@@ -24,19 +26,19 @@ type CommitStatus struct {
|
||||
|
||||
// CombinedStatus holds the combined state of several statuses for a single commit
|
||||
type CombinedStatus struct {
|
||||
State CommitStatusState `json:"state"`
|
||||
SHA string `json:"sha"`
|
||||
TotalCount int `json:"total_count"`
|
||||
Statuses []*CommitStatus `json:"statuses"`
|
||||
Repository *Repository `json:"repository"`
|
||||
CommitURL string `json:"commit_url"`
|
||||
URL string `json:"url"`
|
||||
State commitstatus.CombinedStatus `json:"state"`
|
||||
SHA string `json:"sha"`
|
||||
TotalCount int `json:"total_count"`
|
||||
Statuses []*CommitStatus `json:"statuses"`
|
||||
Repository *Repository `json:"repository"`
|
||||
CommitURL string `json:"commit_url"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
// CreateStatusOption holds the information needed to create a new CommitStatus for a Commit
|
||||
type CreateStatusOption struct {
|
||||
State CommitStatusState `json:"state"`
|
||||
TargetURL string `json:"target_url"`
|
||||
Description string `json:"description"`
|
||||
Context string `json:"context"`
|
||||
State commitstatus.CommitStatusState `json:"state"`
|
||||
TargetURL string `json:"target_url"`
|
||||
Description string `json:"description"`
|
||||
Context string `json:"context"`
|
||||
}
|
||||
Reference in new issue
Block a user