Skip to content

Commit

Permalink
Merge branch 'main' into feature/filter-branches
Browse files Browse the repository at this point in the history
  • Loading branch information
GiteaBot authored Mar 8, 2024
2 parents c32722e + 7cf7a49 commit 64181ca
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 7 deletions.
18 changes: 11 additions & 7 deletions services/pull/commit_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,30 @@ func MergeRequiredContextsCommitStatus(commitStatuses []*git_model.CommitStatus,
}
}

for _, commitStatus := range commitStatuses {
for _, gp := range requiredContextsGlob {
var targetStatus structs.CommitStatusState
for _, gp := range requiredContextsGlob {
for _, commitStatus := range commitStatuses {
if gp.Match(commitStatus.Context) {
targetStatus = commitStatus.State
matchedCount++
break
}
}

if targetStatus != "" && targetStatus.NoBetterThan(returnedStatus) {
// If required rule not match any action, then it is pending
if targetStatus == "" {
if structs.CommitStatusPending.NoBetterThan(returnedStatus) {
returnedStatus = structs.CommitStatusPending
}
break
}

if targetStatus.NoBetterThan(returnedStatus) {
returnedStatus = targetStatus
}
}
}

if matchedCount != len(requiredContexts) {
return structs.CommitStatusPending
}

if matchedCount == 0 {
status := git_model.CalcCommitStatus(commitStatuses)
if status != nil {
Expand Down
65 changes: 65 additions & 0 deletions services/pull/commit_status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2024 The Gitea Authors.
// All rights reserved.
// SPDX-License-Identifier: MIT

package pull

import (
"testing"

git_model "code.gitea.io/gitea/models/git"
"code.gitea.io/gitea/modules/structs"

"github.com/stretchr/testify/assert"
)

func TestMergeRequiredContextsCommitStatus(t *testing.T) {
testCases := [][]*git_model.CommitStatus{
{
{Context: "Build 1", State: structs.CommitStatusSuccess},
{Context: "Build 2", State: structs.CommitStatusSuccess},
{Context: "Build 3", State: structs.CommitStatusSuccess},
},
{
{Context: "Build 1", State: structs.CommitStatusSuccess},
{Context: "Build 2", State: structs.CommitStatusSuccess},
{Context: "Build 2t", State: structs.CommitStatusPending},
},
{
{Context: "Build 1", State: structs.CommitStatusSuccess},
{Context: "Build 2", State: structs.CommitStatusSuccess},
{Context: "Build 2t", State: structs.CommitStatusFailure},
},
{
{Context: "Build 1", State: structs.CommitStatusSuccess},
{Context: "Build 2", State: structs.CommitStatusSuccess},
{Context: "Build 2t", State: structs.CommitStatusSuccess},
},
{
{Context: "Build 1", State: structs.CommitStatusSuccess},
{Context: "Build 2", State: structs.CommitStatusSuccess},
{Context: "Build 2t", State: structs.CommitStatusSuccess},
},
}
testCasesRequiredContexts := [][]string{
{"Build*"},
{"Build*", "Build 2t*"},
{"Build*", "Build 2t*"},
{"Build*", "Build 2t*", "Build 3*"},
{"Build*", "Build *", "Build 2t*", "Build 1*"},
}

testCasesExpected := []structs.CommitStatusState{
structs.CommitStatusSuccess,
structs.CommitStatusPending,
structs.CommitStatusFailure,
structs.CommitStatusPending,
structs.CommitStatusSuccess,
}

for i, commitStatuses := range testCases {
if MergeRequiredContextsCommitStatus(commitStatuses, testCasesRequiredContexts[i]) != testCasesExpected[i] {
assert.Fail(t, "Test case failed", "Test case %d failed", i+1)
}
}
}

0 comments on commit 64181ca

Please sign in to comment.