2022-06-15 19:33:29 +00:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package pipeline
|
|
|
|
|
|
|
|
// TODO(770): pipeline filter should not belong here
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2022-11-06 11:44:04 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline"
|
2022-09-26 07:27:20 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/frontend"
|
2022-06-15 19:33:29 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml"
|
2023-06-04 22:15:07 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server"
|
2022-11-06 11:44:04 +00:00
|
|
|
forge_types "github.com/woodpecker-ci/woodpecker/server/forge/types"
|
2022-06-15 19:33:29 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
|
|
|
)
|
|
|
|
|
2022-11-06 11:44:04 +00:00
|
|
|
func zeroSteps(currentPipeline *model.Pipeline, forgeYamlConfigs []*forge_types.FileMeta) bool {
|
|
|
|
b := pipeline.StepBuilder{
|
2022-06-15 19:33:29 +00:00
|
|
|
Repo: &model.Repo{},
|
2022-11-06 11:44:04 +00:00
|
|
|
Curr: currentPipeline,
|
2022-10-18 01:24:12 +00:00
|
|
|
Last: &model.Pipeline{},
|
2022-06-15 19:33:29 +00:00
|
|
|
Netrc: &model.Netrc{},
|
|
|
|
Secs: []*model.Secret{},
|
|
|
|
Regs: []*model.Registry{},
|
|
|
|
Link: "",
|
2022-11-04 23:35:06 +00:00
|
|
|
Yamls: forgeYamlConfigs,
|
2023-06-04 22:15:07 +00:00
|
|
|
Forge: server.Config.Services.Forge,
|
2022-06-15 19:33:29 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
pipelineItems, err := b.Build()
|
2022-06-15 19:33:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
if len(pipelineItems) == 0 {
|
2022-06-15 19:33:29 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-06-06 07:52:08 +00:00
|
|
|
// TODO: parse yaml once and not for each filter function (-> move server/pipeline/filter* into pipeline/step_builder)
|
2022-09-26 07:27:20 +00:00
|
|
|
// Check if at least one pipeline step will be execute otherwise we will just ignore this webhook
|
2023-06-04 22:15:07 +00:00
|
|
|
func checkIfFiltered(repo *model.Repo, p *model.Pipeline, forgeYamlConfigs []*forge_types.FileMeta) (bool, error) {
|
|
|
|
log.Trace().Msgf("hook.branchFiltered(): pipeline branch: '%s' pipeline event: '%s' config count: %d", p.Branch, p.Event, len(forgeYamlConfigs))
|
2022-06-15 19:33:29 +00:00
|
|
|
|
2023-06-04 22:15:07 +00:00
|
|
|
matchMetadata := frontend.MetadataFromStruct(server.Config.Services.Forge, repo, p, nil, nil, "")
|
2022-06-15 19:33:29 +00:00
|
|
|
|
2022-11-04 23:35:06 +00:00
|
|
|
for _, forgeYamlConfig := range forgeYamlConfigs {
|
2023-06-04 22:15:07 +00:00
|
|
|
substitutedConfigData, err := frontend.EnvVarSubst(string(forgeYamlConfig.Data), matchMetadata.Environ())
|
|
|
|
if err != nil {
|
|
|
|
log.Trace().Err(err).Msgf("failed to substitute config '%s'", forgeYamlConfig.Name)
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
parsedPipelineConfig, err := yaml.ParseString(substitutedConfigData)
|
2022-06-15 19:33:29 +00:00
|
|
|
if err != nil {
|
2023-06-04 22:15:07 +00:00
|
|
|
log.Trace().Err(err).Msgf("failed to parse config '%s'", forgeYamlConfig.Name)
|
2022-06-15 19:33:29 +00:00
|
|
|
return false, err
|
|
|
|
}
|
2022-11-04 23:35:06 +00:00
|
|
|
log.Trace().Msgf("config '%s': %#v", forgeYamlConfig.Name, parsedPipelineConfig)
|
2022-06-15 19:33:29 +00:00
|
|
|
|
2022-09-26 07:27:20 +00:00
|
|
|
// ignore if the pipeline was filtered by matched constraints
|
2023-07-02 21:45:22 +00:00
|
|
|
if match, err := parsedPipelineConfig.When.Match(matchMetadata, true, p.AdditionalVariables); !match && err == nil {
|
2022-09-26 07:27:20 +00:00
|
|
|
continue
|
2022-10-05 23:49:23 +00:00
|
|
|
} else if err != nil {
|
|
|
|
return false, err
|
2022-09-26 07:27:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// at least one config yielded in a valid run.
|
|
|
|
return false, nil
|
2022-06-15 19:33:29 +00:00
|
|
|
}
|
|
|
|
|
2022-09-26 07:27:20 +00:00
|
|
|
// no configs yielded a valid run.
|
2022-06-15 19:33:29 +00:00
|
|
|
return true, nil
|
|
|
|
}
|