]> source.dussan.org Git - gitea.git/commitdiff
Fixing the issue when status check per rule matches multiple actions (#29631) (#29655)
authorGiteabot <teabot@gitea.io>
Fri, 8 Mar 2024 05:24:32 +0000 (13:24 +0800)
committerGitHub <noreply@github.com>
Fri, 8 Mar 2024 05:24:32 +0000 (13:24 +0800)
Backport #29631 by @charles7668

Close #29628
rule
```
Test / Build*
Test / Build *
Test / Build 2*
Test / Build 1*
```

![image](https://github.com/go-gitea/gitea/assets/30816317/19bef0a9-fa97-43c5-887b-dece76064aa8)
rule2
```
Test / Build*
Test / Build 1*
```

![image](https://github.com/go-gitea/gitea/assets/30816317/19bef0a9-fa97-43c5-887b-dece76064aa8)

rule3
```
Test / Build*
Test / Build 1*
NotExist*
```

![image](https://github.com/go-gitea/gitea/assets/30816317/f6a5e832-2e1b-4049-915b-45bec5ef070c)

Co-authored-by: charles <30816317+charles7668@users.noreply.github.com>
Co-authored-by: Zettat123 <zettat123@gmail.com>
services/pull/commit_status.go
services/pull/commit_status_test.go [new file with mode: 0644]

index 965cfa0df1b2e3d7417a45ed87c91095732515a9..9504bcf60568abd61e3ba8e7e883bddac27b6c37 100644 (file)
@@ -34,9 +34,9 @@ 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++
@@ -44,16 +44,20 @@ func MergeRequiredContextsCommitStatus(commitStatuses []*git_model.CommitStatus,
                                }
                        }
 
-                       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 {
diff --git a/services/pull/commit_status_test.go b/services/pull/commit_status_test.go
new file mode 100644 (file)
index 0000000..592acdd
--- /dev/null
@@ -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)
+               }
+       }
+}