-
Notifications
You must be signed in to change notification settings - Fork 15
/
pullreq_pipeline.go
210 lines (183 loc) · 5.99 KB
/
pullreq_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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"github.com/google/go-github/v28/github"
"github.com/sirupsen/logrus"
"github.com/xanzy/go-gitlab"
clientgitlab "github.com/mendersoftware/integration-test-runner/client/gitlab"
"github.com/mendersoftware/integration-test-runner/git"
)
func startPRPipeline(
log *logrus.Entry,
ref string,
event *github.PullRequestEvent,
conf *config,
isOrgMember func() bool,
) error {
client, err := clientgitlab.NewGitLabClient(
conf.gitlabToken,
conf.gitlabBaseURL,
conf.dryRunMode,
)
if err != nil {
return err
}
pr := event.GetPullRequest()
org := event.GetOrganization().GetLogin()
head := pr.GetHead()
base := pr.GetBase()
repo := event.GetRepo()
if repo.GetName() == "mender-qa" {
// Verify that the pipe is started by a member of the organization
if isOrgMember() {
log.Warnf(
"%s is making a pullrequest, but he/she is not a member of our organization, "+
"ignoring",
pr.GetUser().GetLogin(),
)
return nil
}
}
repoURL, err := getRemoteURLGitLab(org, repo.GetName())
if err != nil {
return err
}
repoHostURI := strings.SplitN(repoURL, ":", 2)
if len(repoHostURI) != 2 {
return fmt.Errorf("invalid GitLab URL '%s': failed to start GitLab pipeline", repoURL)
}
gitlabPath := repoHostURI[1]
ciIIDKey := "CI_EXTERNAL_PULL_REQUEST_IID"
ciIID := strconv.Itoa(event.GetNumber())
ciSourceRepoKey := "CI_EXTERNAL_PULL_REQUEST_SOURCE_REPOSITORY"
ciSourceRepo := head.GetRepo().GetFullName()
ciTargetRepoKey := "CI_EXTERNAL_PULL_REQUEST_TARGET_REPOSITORY"
ciTargetRepo := repo.GetFullName()
ciSourceBranchNameKey := "CI_EXTERNAL_PULL_REQUEST_SOURCE_BRANCH_NAME"
ciSourceBranchName := head.GetRef()
ciSourceBranchSHAKey := "CI_EXTERNAL_PULL_REQUEST_SOURCE_BRANCH_SHA"
ciSourceBranchSHA := head.GetSHA()
ciTargetBranchNameKey := "CI_EXTERNAL_PULL_REQUEST_TARGET_BRANCH_NAME"
ciTargetBranchName := base.GetRef()
ciTargetBranchShaKey := "CI_EXTERNAL_PULL_REQUEST_TARGET_BRANCH_SHA"
ciTargetBranchSha := base.GetSHA()
pipeline, err := client.CreatePipeline(gitlabPath, &gitlab.CreatePipelineOptions{
Ref: &ref,
Variables: &[]*gitlab.PipelineVariableOptions{
{Key: &ciIIDKey, Value: &ciIID},
{Key: &ciSourceRepoKey, Value: &ciSourceRepo},
{Key: &ciTargetRepoKey, Value: &ciTargetRepo},
{Key: &ciSourceBranchNameKey, Value: &ciSourceBranchName},
{Key: &ciSourceBranchSHAKey, Value: &ciSourceBranchSHA},
{Key: &ciTargetBranchNameKey, Value: &ciTargetBranchName},
{Key: &ciTargetBranchShaKey, Value: &ciTargetBranchSha},
},
})
if err != nil {
return err
} else {
log.Debugf("started pipeline for PR: %s", pipeline.WebURL)
}
return nil
}
func syncPullRequestBranch(
log *logrus.Entry,
pr *github.PullRequestEvent,
conf *config,
) (string, error) {
repo := pr.GetRepo().GetName()
org := pr.GetOrganization().GetLogin()
prNum := strconv.Itoa(pr.GetNumber())
tmpdir, err := ioutil.TempDir("", repo)
if err != nil {
return "", err
}
defer os.RemoveAll(tmpdir)
gitcmd := git.Command("init", ".")
gitcmd.Dir = tmpdir
out, err := gitcmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("%v returned error: %s: %s", gitcmd.Args, out, err.Error())
}
repoURL := getRemoteURLGitHub(conf.githubProtocol, conf.githubOrganization, repo)
gitcmd = git.Command("remote", "add", "github", repoURL)
gitcmd.Dir = tmpdir
out, err = gitcmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("%v returned error: %s: %s", gitcmd.Args, out, err.Error())
}
remoteURL, err := getRemoteURLGitLab(org, repo)
if err != nil {
return "", fmt.Errorf("getRemoteURLGitLab returned error: %s", err.Error())
}
gitcmd = git.Command("remote", "add", "gitlab", remoteURL)
gitcmd.Dir = tmpdir
out, err = gitcmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("%v returned error: %s: %s", gitcmd.Args, out, err.Error())
}
prBranchName := "pr_" + prNum
gitcmd = git.Command("fetch", "github", "pull/"+prNum+"/head:"+prBranchName)
gitcmd.Dir = tmpdir
out, err = gitcmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("%v returned error: %s: %s", gitcmd.Args, out, err.Error())
}
// Push but not don't trigger CI (yet)
gitcmd = git.Command("push", "-f", "-o", "ci.skip", "--set-upstream", "gitlab", prBranchName)
gitcmd.Dir = tmpdir
out, err = gitcmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("%v returned error: %s: %s", gitcmd.Args, out, err.Error())
}
log.Infof("Created branch: %s:%s", repo, prBranchName)
return prBranchName, nil
}
func deleteStaleGitlabPRBranch(log *logrus.Entry, pr *github.PullRequestEvent, conf *config) error {
// If the action is "closed" the pull request was merged or just closed,
// stop builds in both cases.
if pr.GetAction() != "closed" {
log.Debugf("deleteStaleGitlabPRBranch: PR not closed, therefore not stopping it's pipeline")
return nil
}
repoName := pr.GetRepo().GetName()
repoOrg := pr.GetOrganization().GetLogin()
tmpdir, err := ioutil.TempDir("", repoName)
if err != nil {
return err
}
defer os.RemoveAll(tmpdir)
gitcmd := git.Command("init", ".")
gitcmd.Dir = tmpdir
out, err := gitcmd.CombinedOutput()
if err != nil {
return fmt.Errorf("%v returned error: %s: %s", gitcmd.Args, out, err.Error())
}
remoteURL, err := getRemoteURLGitLab(repoOrg, repoName)
if err != nil {
return fmt.Errorf("getRemoteURLGitLab returned error: %s", err.Error())
}
gitcmd = git.Command("remote", "add", "gitlab", remoteURL)
gitcmd.Dir = tmpdir
out, err = gitcmd.CombinedOutput()
if err != nil {
return fmt.Errorf("%v returned error: %s: %s", gitcmd.Args, out, err.Error())
}
gitcmd = git.Command("fetch", "gitlab")
gitcmd.Dir = tmpdir
out, err = gitcmd.CombinedOutput()
if err != nil {
return fmt.Errorf("%v returned error: %s: %s", gitcmd.Args, out, err.Error())
}
gitcmd = git.Command("push", "gitlab", "--delete", fmt.Sprintf("pr_%d", pr.GetNumber()))
gitcmd.Dir = tmpdir
out, err = gitcmd.CombinedOutput()
if err != nil {
return fmt.Errorf("%v returned error: %s: %s", gitcmd.Args, out, err.Error())
}
return nil
}