-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathintegration_pipeline.go
165 lines (138 loc) · 4.07 KB
/
integration_pipeline.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"bytes"
"context"
"strconv"
"strings"
"text/template"
"github.com/google/go-github/v28/github"
"github.com/sirupsen/logrus"
gitlab "gitlab.com/gitlab-org/api/client-go"
clientgitlab "github.com/mendersoftware/integration-test-runner/client/gitlab"
)
const integrationPipelinePath = "Northern.tech/Mender/integration"
func getIntegrationBuild(
log *logrus.Entry,
conf *config,
pr *github.PullRequestEvent,
) buildOptions {
repo := pr.GetRepo().GetName()
commitSHA := pr.PullRequest.Base.GetSHA()
//GetLabel returns "mendersoftware:master", we just want the branch
baseBranch := strings.Split(pr.PullRequest.Base.GetLabel(), ":")[1]
build := buildOptions{
pr: strconv.Itoa(pr.GetNumber()),
repo: repo,
baseBranch: baseBranch,
commitSHA: commitSHA,
}
return build
}
func triggerIntegrationBuild(
log *logrus.Entry,
conf *config,
build *buildOptions,
pr *github.PullRequestEvent,
buildOptions *BuildOptions,
) error {
gitlabIntegration, err := clientgitlab.NewGitLabClient(
conf.gitlabToken,
conf.gitlabBaseURL,
conf.dryRunMode,
)
if err != nil {
return err
}
buildParameters, err := getIntegrationBuildParameters(log, conf, build, buildOptions)
if err != nil {
return err
}
// first stop old pipelines with the same buildParameters
stopStalePipelines(integrationPipelinePath, log, gitlabIntegration, buildParameters)
// trigger the new pipeline
ref := "pr_" + strconv.Itoa(pr.GetNumber()) + "_protected"
opt := &gitlab.CreatePipelineOptions{
Ref: &ref,
Variables: &buildParameters,
}
variablesString := ""
for _, variable := range *opt.Variables {
variablesString += *variable.Key + ":" + *variable.Value + ", "
}
log.Infof(
"Creating pipeline in project %s:%s with variables: %s",
integrationPipelinePath,
*opt.Ref,
variablesString,
)
pipeline, err := gitlabIntegration.CreatePipeline(integrationPipelinePath, opt)
if err != nil {
log.Errorf("Could not create pipeline: %s", err.Error())
return err
}
log.Infof("Created pipeline: %s", pipeline.WebURL)
// Add the build variable matrix to the pipeline comment under a
// drop-down tab
// nolint:lll
tmplString := `
Hello :smiley_cat: I created a pipeline for you here: [Pipeline-{{.Pipeline.ID}}]({{.Pipeline.WebURL}})
<details>
<summary>Build Configuration Matrix</summary><p>
| Key | Value |
| ----- | ----- |
{{range $i, $var := .BuildVars}}{{if $var.Value}}| {{$var.Key}} | {{$var.Value}} |{{printf "\n"}}{{end}}{{end}}
</p></details>
`
tmpl, err := template.New("Main").Parse(tmplString)
if err != nil {
log.Errorf(
"Failed to parse the build matrix template. Should never happen! Error: %s\n",
err.Error(),
)
return err
}
var buf bytes.Buffer
if err = tmpl.Execute(&buf, struct {
BuildVars []*gitlab.PipelineVariableOptions
Pipeline *gitlab.Pipeline
}{
BuildVars: filterOutEmptyVariables(*opt.Variables),
Pipeline: pipeline,
}); err != nil {
log.Errorf("Failed to execute the build matrix template. Error: %s\n", err.Error())
return err
}
// Comment with a pipeline-link on the PR
commentBody := buf.String()
comment := github.IssueComment{
Body: &commentBody,
}
err = githubClient.CreateComment(context.Background(),
conf.githubOrganization, pr.GetRepo().GetName(), pr.GetNumber(), &comment)
if err != nil {
log.Infof("Failed to comment on the pr: %v, Error: %s", pr, err.Error())
}
return err
}
func getIntegrationBuildParameters(
log *logrus.Entry,
conf *config,
build *buildOptions,
buildOptions *BuildOptions,
) ([]*gitlab.PipelineVariableOptions, error) {
readHead := "pull/" + build.pr + "/head"
var buildParameters []*gitlab.PipelineVariableOptions
runIntegrationTests := "true"
runIntegrationTestsKey := "RUN_TESTS_FULL_INTEGRATION"
buildParameters = append(
buildParameters,
&gitlab.PipelineVariableOptions{Key: &runIntegrationTestsKey, Value: &runIntegrationTests},
)
buildRepoKey := repoToBuildParameter(build.repo)
buildParameters = append(buildParameters,
&gitlab.PipelineVariableOptions{
Key: &buildRepoKey,
Value: &readHead,
})
return buildParameters, nil
}