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

Move "skip ci" logic into global pipeline conditions #2216

Merged
merged 11 commits into from
Aug 17, 2023
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"prettier.configPath": "./web/.prettierrc.js",
"prettier.ignorePath": "./web/.prettierignore",
"cSpell.words": [
"Curr",
"doublestar",
"multierr"
]
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ test-server-datastore-coverage: ## Test server datastore with coverage report

test-ui: ui-dependencies ## Test UI code
(cd web/; pnpm run lint)
(cd web/; pnpm run formatcheck)
(cd web/; pnpm run format:check)
(cd web/; pnpm run typecheck)
(cd web/; pnpm run test)

Expand Down
14 changes: 14 additions & 0 deletions pipeline/frontend/yaml/constraint/constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,21 @@ import (
"errors"
"fmt"
"path"
"regexp"
"strings"

"github.com/antonmedv/expr"
"github.com/bmatcuk/doublestar/v4"
"github.com/rs/zerolog/log"
"golang.org/x/exp/maps"
"gopkg.in/yaml.v3"

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

var skipRe = regexp.MustCompile(`\[(?i:ci *skip|skip *ci)\]`)

type (
// When defines a set of runtime constraints.
When struct {
Expand Down Expand Up @@ -78,6 +82,16 @@ func (when *When) IsEmpty() bool {

// Returns true if at least one of the internal constraints is true.
func (when *When) Match(metadata metadata.Metadata, global bool, env map[string]string) (bool, error) {
if global {
// skip the whole workflow if any case-insensitive combination of the words "skip" and "ci"
// wrapped in square brackets appear in the commit message
skipMatch := skipRe.FindString(metadata.Curr.Commit.Message)
if len(skipMatch) > 0 {
log.Debug().Msgf("skip workflow as keyword to do so was detected in commit message '%s'", metadata.Curr.Commit.Message)
return false, nil
}
}

for _, c := range when.Constraints {
match, err := c.Match(metadata, global, env)
if err != nil {
Expand Down
18 changes: 0 additions & 18 deletions server/api/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"errors"
"fmt"
"net/http"
"regexp"

"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
Expand All @@ -35,8 +34,6 @@ import (
"github.com/woodpecker-ci/woodpecker/shared/token"
)

var skipRe = regexp.MustCompile(`\[(?i:ci *skip|skip *ci)\]`)

// GetQueueInfo
//
// @Summary Get pipeline queue information
Expand Down Expand Up @@ -140,21 +137,6 @@ func PostHook(c *gin.Context) {
return
}

//
// Skip if commit message contains skip-ci
// TODO: move into global pipeline conditions logic
//

// skip the tmpPipeline if any case-insensitive combination of the words "skip" and "ci"
// wrapped in square brackets appear in the commit message
skipMatch := skipRe.FindString(tmpPipeline.Message)
if len(skipMatch) > 0 {
msg := fmt.Sprintf("ignoring hook: %s found in %s", skipMatch, tmpPipeline.Commit)
log.Debug().Msg(msg)
c.String(http.StatusNoContent, msg)
return
}

//
// 2. Get related repo from store and take repo renaming into account
//
Expand Down
2 changes: 1 addition & 1 deletion server/pipeline/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func Create(ctx context.Context, _store store.Store, repo *model.Repo, pipeline
filtered, parseErr = checkIfFiltered(repo, pipeline, forgeYamlConfigs)
if parseErr == nil {
if filtered {
err := ErrFiltered{Msg: "branch does not match restrictions defined in yaml"}
err := ErrFiltered{Msg: "global when filter of all workflows do skip this pipeline"}
log.Debug().Str("repo", repo.FullName).Msgf("%v", err)
return nil, err
}
Expand Down
24 changes: 24 additions & 0 deletions server/pipeline/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ func (e ErrNotFound) Error() string {
return e.Msg
}

func (e ErrNotFound) Is(target error) bool {
_, ok := target.(ErrNotFound) //nolint:errorlint
if !ok {
_, ok = target.(*ErrNotFound) //nolint:errorlint
}
return ok
}

type ErrBadRequest struct {
Msg string
}
Expand All @@ -30,10 +38,26 @@ func (e ErrBadRequest) Error() string {
return e.Msg
}

func (e ErrBadRequest) Is(target error) bool {
_, ok := target.(ErrBadRequest) //nolint:errorlint
if !ok {
_, ok = target.(*ErrBadRequest) //nolint:errorlint
}
return ok
}

type ErrFiltered struct {
Msg string
}

func (e ErrFiltered) Error() string {
return "ignoring hook: " + e.Msg
}

func (e *ErrFiltered) Is(target error) bool {
_, ok := target.(ErrFiltered) //nolint:errorlint
if !ok {
_, ok = target.(*ErrFiltered) //nolint:errorlint
}
return ok
}