-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
87 lines (77 loc) · 2.04 KB
/
config.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package bitbucket
import (
"path"
"strings"
)
// see https://confluence.atlassian.com/bitbucket/configure-bitbucket-pipelines-yml-792298910.html#Configurebitbucket-pipelines.yml-ci_branches
type (
// Config defines the pipeline configuration.
Config struct {
// Image specifies the Docker image with
// which we run your builds.
Image string
// Clone defines the depth of Git clones
// for all pipelines.
Clone struct {
Depth int
}
// Pipeline defines the pipeline configuration
// which includes a list of all steps for default,
// tag, and branch-specific execution.
Pipelines struct {
Default Stage
Tags map[string]Stage
Branches map[string]Stage
}
}
// Stage contains a list of steps executed
// for a specific branch or tag.
Stage struct {
Name string
Steps []*Step
}
// Step defines a build execution unit.
Step struct {
// Image specifies the Docker image with
// which we run your builds.
Image string
// Script contains the list of bash commands
// that are executed in sequence.
Script []string
}
)
// Pipeline returns the pipeline stage that best matches the branch
// and ref. If there is no matching pipeline specific to the branch
// or tag, the default pipeline is returned.
func (c *Config) Pipeline(ref, branch string) Stage {
// match pipeline by tag name
tag := strings.TrimPrefix(ref, "refs/tags/")
for pattern, pipeline := range c.Pipelines.Tags {
if ok, _ := path.Match(pattern, tag); ok {
return pipeline
}
}
// match pipeline by branch name
for pattern, pipeline := range c.Pipelines.Branches {
if ok, _ := path.Match(pattern, branch); ok {
return pipeline
}
}
// use default
return c.Pipelines.Default
}
// UnmarshalYAML implements custom parsing for the stage section of the yaml
// to cleanup the structure a bit.
func (s *Stage) UnmarshalYAML(unmarshal func(interface{}) error) error {
in := []struct {
Step *Step
}{}
err := unmarshal(&in)
if err != nil {
return err
}
for _, step := range in {
s.Steps = append(s.Steps, step.Step)
}
return nil
}