forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
55 lines (45 loc) · 1.7 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package drivers
import (
"fmt"
"net/http"
"github.com/rancher/rancher/pkg/pipeline/providers"
"github.com/rancher/rancher/pkg/pipeline/remote/model"
"github.com/rancher/rancher/pkg/pipeline/utils"
"github.com/rancher/types/apis/project.cattle.io/v3"
)
const (
RefsBranchPrefix = "refs/heads/"
RefsTagPrefix = "refs/tags/"
)
func isEventActivated(info *model.BuildInfo, pipeline *v3.Pipeline) bool {
if (info.Event == utils.WebhookEventPush && pipeline.Spec.TriggerWebhookPush) ||
(info.Event == utils.WebhookEventTag && pipeline.Spec.TriggerWebhookTag) ||
(info.Event == utils.WebhookEventPullRequest && pipeline.Spec.TriggerWebhookPr) {
return true
}
return false
}
func validateAndGeneratePipelineExecution(pipelineExecutions v3.PipelineExecutionInterface,
sourceCodeCredentials v3.SourceCodeCredentialInterface,
sourceCodeCredentialLister v3.SourceCodeCredentialLister,
info *model.BuildInfo,
pipeline *v3.Pipeline) (int, error) {
if !isEventActivated(info, pipeline) {
return http.StatusUnavailableForLegalReasons, fmt.Errorf("trigger for event '%s' is disabled", info.Event)
}
pipelineConfig, err := providers.GetPipelineConfigByBranch(sourceCodeCredentials, sourceCodeCredentialLister, pipeline, info.Branch)
if err != nil {
return http.StatusInternalServerError, err
}
if pipelineConfig == nil {
//no pipeline config to run
return http.StatusOK, nil
}
if !utils.Match(pipelineConfig.Branch, info.Branch) {
return http.StatusUnavailableForLegalReasons, fmt.Errorf("skipped branch '%s'", info.Branch)
}
if _, err := utils.GenerateExecution(pipelineExecutions, pipeline, pipelineConfig, info); err != nil {
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}