Skip to content

Commit a5ef372

Browse files
authored
Move "skip ci" logic into global pipeline conditions (woodpecker-ci#2216)
... and make custom errors follow std err conventions this fix a 500 response if the whole pipeline is filtered out
1 parent 2222638 commit a5ef372

File tree

6 files changed

+41
-20
lines changed

6 files changed

+41
-20
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"prettier.configPath": "./web/.prettierrc.js",
1717
"prettier.ignorePath": "./web/.prettierignore",
1818
"cSpell.words": [
19+
"Curr",
1920
"doublestar",
2021
"multierr"
2122
]

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ test-server-datastore-coverage: ## Test server datastore with coverage report
168168

169169
test-ui: ui-dependencies ## Test UI code
170170
(cd web/; pnpm run lint)
171-
(cd web/; pnpm run formatcheck)
171+
(cd web/; pnpm run format:check)
172172
(cd web/; pnpm run typecheck)
173173
(cd web/; pnpm run test)
174174

pipeline/frontend/yaml/constraint/constraint.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,21 @@ import (
1818
"errors"
1919
"fmt"
2020
"path"
21+
"regexp"
2122
"strings"
2223

2324
"github.com/antonmedv/expr"
2425
"github.com/bmatcuk/doublestar/v4"
26+
"github.com/rs/zerolog/log"
2527
"golang.org/x/exp/maps"
2628
"gopkg.in/yaml.v3"
2729

2830
"github.com/woodpecker-ci/woodpecker/pipeline/frontend/metadata"
2931
yaml_base_types "github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml/types/base"
3032
)
3133

34+
var skipRe = regexp.MustCompile(`\[(?i:ci *skip|skip *ci)\]`)
35+
3236
type (
3337
// When defines a set of runtime constraints.
3438
When struct {
@@ -78,6 +82,16 @@ func (when *When) IsEmpty() bool {
7882

7983
// Returns true if at least one of the internal constraints is true.
8084
func (when *When) Match(metadata metadata.Metadata, global bool, env map[string]string) (bool, error) {
85+
if global {
86+
// skip the whole workflow if any case-insensitive combination of the words "skip" and "ci"
87+
// wrapped in square brackets appear in the commit message
88+
skipMatch := skipRe.FindString(metadata.Curr.Commit.Message)
89+
if len(skipMatch) > 0 {
90+
log.Debug().Msgf("skip workflow as keyword to do so was detected in commit message '%s'", metadata.Curr.Commit.Message)
91+
return false, nil
92+
}
93+
}
94+
8195
for _, c := range when.Constraints {
8296
match, err := c.Match(metadata, global, env)
8397
if err != nil {

server/api/hook.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"errors"
2323
"fmt"
2424
"net/http"
25-
"regexp"
2625

2726
"github.com/gin-gonic/gin"
2827
"github.com/rs/zerolog/log"
@@ -35,8 +34,6 @@ import (
3534
"github.com/woodpecker-ci/woodpecker/shared/token"
3635
)
3736

38-
var skipRe = regexp.MustCompile(`\[(?i:ci *skip|skip *ci)\]`)
39-
4037
// GetQueueInfo
4138
//
4239
// @Summary Get pipeline queue information
@@ -140,21 +137,6 @@ func PostHook(c *gin.Context) {
140137
return
141138
}
142139

143-
//
144-
// Skip if commit message contains skip-ci
145-
// TODO: move into global pipeline conditions logic
146-
//
147-
148-
// skip the tmpPipeline if any case-insensitive combination of the words "skip" and "ci"
149-
// wrapped in square brackets appear in the commit message
150-
skipMatch := skipRe.FindString(tmpPipeline.Message)
151-
if len(skipMatch) > 0 {
152-
msg := fmt.Sprintf("ignoring hook: %s found in %s", skipMatch, tmpPipeline.Commit)
153-
log.Debug().Msg(msg)
154-
c.String(http.StatusNoContent, msg)
155-
return
156-
}
157-
158140
//
159141
// 2. Get related repo from store and take repo renaming into account
160142
//

server/pipeline/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func Create(ctx context.Context, _store store.Store, repo *model.Repo, pipeline
6666
filtered, parseErr = checkIfFiltered(repo, pipeline, forgeYamlConfigs)
6767
if parseErr == nil {
6868
if filtered {
69-
err := ErrFiltered{Msg: "branch does not match restrictions defined in yaml"}
69+
err := ErrFiltered{Msg: "global when filter of all workflows do skip this pipeline"}
7070
log.Debug().Str("repo", repo.FullName).Msgf("%v", err)
7171
return nil, err
7272
}

server/pipeline/errors.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ func (e ErrNotFound) Error() string {
2222
return e.Msg
2323
}
2424

25+
func (e ErrNotFound) Is(target error) bool {
26+
_, ok := target.(ErrNotFound) //nolint:errorlint
27+
if !ok {
28+
_, ok = target.(*ErrNotFound) //nolint:errorlint
29+
}
30+
return ok
31+
}
32+
2533
type ErrBadRequest struct {
2634
Msg string
2735
}
@@ -30,10 +38,26 @@ func (e ErrBadRequest) Error() string {
3038
return e.Msg
3139
}
3240

41+
func (e ErrBadRequest) Is(target error) bool {
42+
_, ok := target.(ErrBadRequest) //nolint:errorlint
43+
if !ok {
44+
_, ok = target.(*ErrBadRequest) //nolint:errorlint
45+
}
46+
return ok
47+
}
48+
3349
type ErrFiltered struct {
3450
Msg string
3551
}
3652

3753
func (e ErrFiltered) Error() string {
3854
return "ignoring hook: " + e.Msg
3955
}
56+
57+
func (e *ErrFiltered) Is(target error) bool {
58+
_, ok := target.(ErrFiltered) //nolint:errorlint
59+
if !ok {
60+
_, ok = target.(*ErrFiltered) //nolint:errorlint
61+
}
62+
return ok
63+
}

0 commit comments

Comments
 (0)