• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / integration-test-runner / 1583018314

10 Dec 2024 11:26PM UTC coverage: 68.703%. Remained the same
1583018314

Pull #336

gitlab-ci

danielskinstad
ci: pin docker to v27.3

Fixes runc errors in the hetzner runner
Use dependency proxy

Ticket: QA-823

Signed-off-by: Daniel Skinstad Drabitzius <daniel.drabitzius@northern.tech>
Pull Request #336: ci: pin docker to v27.3

1732 of 2521 relevant lines covered (68.7%)

2.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.91
/pullreq_pipeline.go
1
package main
2

3
import (
4
        "fmt"
5
        "io/ioutil"
6
        "os"
7
        "strconv"
8
        "strings"
9

10
        "github.com/google/go-github/v28/github"
11
        "github.com/sirupsen/logrus"
12
        "github.com/xanzy/go-gitlab"
13

14
        clientgitlab "github.com/mendersoftware/integration-test-runner/client/gitlab"
15
        "github.com/mendersoftware/integration-test-runner/git"
16
)
17

18
func startPRPipeline(
19
        log *logrus.Entry,
20
        ref string,
21
        event *github.PullRequestEvent,
22
        conf *config,
23
        isOrgMember func() bool,
24
) error {
1✔
25
        client, err := clientgitlab.NewGitLabClient(
1✔
26
                conf.gitlabToken,
1✔
27
                conf.gitlabBaseURL,
1✔
28
                conf.dryRunMode,
1✔
29
        )
1✔
30
        if err != nil {
1✔
31
                return err
×
32
        }
×
33
        pr := event.GetPullRequest()
1✔
34
        org := event.GetOrganization().GetLogin()
1✔
35
        head := pr.GetHead()
1✔
36
        base := pr.GetBase()
1✔
37
        repo := event.GetRepo()
1✔
38
        if repo.GetName() == "mender-qa" {
2✔
39
                // Verify that the pipe is started by a member of the organization
1✔
40
                if isOrgMember() {
2✔
41
                        log.Warnf(
1✔
42
                                "%s is making a pullrequest, but he/she is not a member of our organization, "+
1✔
43
                                        "ignoring",
1✔
44
                                pr.GetUser().GetLogin(),
1✔
45
                        )
1✔
46
                        return nil
1✔
47
                }
1✔
48
        }
49
        repoURL, err := getRemoteURLGitLab(org, repo.GetName())
1✔
50
        if err != nil {
1✔
51
                return err
×
52
        }
×
53
        repoHostURI := strings.SplitN(repoURL, ":", 2)
1✔
54
        if len(repoHostURI) != 2 {
1✔
55
                return fmt.Errorf("invalid GitLab URL '%s': failed to start GitLab pipeline", repoURL)
×
56
        }
×
57
        gitlabPath := repoHostURI[1]
1✔
58

1✔
59
        ciIIDKey := "CI_EXTERNAL_PULL_REQUEST_IID"
1✔
60
        ciIID := strconv.Itoa(event.GetNumber())
1✔
61
        ciSourceRepoKey := "CI_EXTERNAL_PULL_REQUEST_SOURCE_REPOSITORY"
1✔
62
        ciSourceRepo := head.GetRepo().GetFullName()
1✔
63
        ciTargetRepoKey := "CI_EXTERNAL_PULL_REQUEST_TARGET_REPOSITORY"
1✔
64
        ciTargetRepo := repo.GetFullName()
1✔
65
        ciSourceBranchNameKey := "CI_EXTERNAL_PULL_REQUEST_SOURCE_BRANCH_NAME"
1✔
66
        ciSourceBranchName := head.GetRef()
1✔
67
        ciSourceBranchSHAKey := "CI_EXTERNAL_PULL_REQUEST_SOURCE_BRANCH_SHA"
1✔
68
        ciSourceBranchSHA := head.GetSHA()
1✔
69
        ciTargetBranchNameKey := "CI_EXTERNAL_PULL_REQUEST_TARGET_BRANCH_NAME"
1✔
70
        ciTargetBranchName := base.GetRef()
1✔
71
        ciTargetBranchShaKey := "CI_EXTERNAL_PULL_REQUEST_TARGET_BRANCH_SHA"
1✔
72
        ciTargetBranchSha := base.GetSHA()
1✔
73

1✔
74
        pipeline, err := client.CreatePipeline(gitlabPath, &gitlab.CreatePipelineOptions{
1✔
75
                Ref: &ref,
1✔
76
                Variables: &[]*gitlab.PipelineVariableOptions{
1✔
77
                        {Key: &ciIIDKey, Value: &ciIID},
1✔
78
                        {Key: &ciSourceRepoKey, Value: &ciSourceRepo},
1✔
79
                        {Key: &ciTargetRepoKey, Value: &ciTargetRepo},
1✔
80
                        {Key: &ciSourceBranchNameKey, Value: &ciSourceBranchName},
1✔
81
                        {Key: &ciSourceBranchSHAKey, Value: &ciSourceBranchSHA},
1✔
82
                        {Key: &ciTargetBranchNameKey, Value: &ciTargetBranchName},
1✔
83
                        {Key: &ciTargetBranchShaKey, Value: &ciTargetBranchSha},
1✔
84
                },
1✔
85
        })
1✔
86
        if err != nil {
1✔
87
                return err
×
88
        } else {
1✔
89
                log.Debugf("started pipeline for PR: %s", pipeline.WebURL)
1✔
90
        }
1✔
91

92
        return nil
1✔
93
}
94

95
func syncPullRequestBranch(
96
        log *logrus.Entry,
97
        pr *github.PullRequestEvent,
98
        conf *config,
99
) (string, error) {
1✔
100

1✔
101
        repo := pr.GetRepo().GetName()
1✔
102
        org := pr.GetOrganization().GetLogin()
1✔
103
        prNum := strconv.Itoa(pr.GetNumber())
1✔
104

1✔
105
        tmpdir, err := ioutil.TempDir("", repo)
1✔
106
        if err != nil {
1✔
107
                return "", err
×
108
        }
×
109
        defer os.RemoveAll(tmpdir)
1✔
110

1✔
111
        gitcmd := git.Command("init", ".")
1✔
112
        gitcmd.Dir = tmpdir
1✔
113
        out, err := gitcmd.CombinedOutput()
1✔
114
        if err != nil {
1✔
115
                return "", fmt.Errorf("%v returned error: %s: %s", gitcmd.Args, out, err.Error())
×
116
        }
×
117

118
        repoURL := getRemoteURLGitHub(conf.githubProtocol, conf.githubOrganization, repo)
1✔
119
        gitcmd = git.Command("remote", "add", "github", repoURL)
1✔
120
        gitcmd.Dir = tmpdir
1✔
121
        out, err = gitcmd.CombinedOutput()
1✔
122
        if err != nil {
1✔
123
                return "", fmt.Errorf("%v returned error: %s: %s", gitcmd.Args, out, err.Error())
×
124
        }
×
125

126
        remoteURL, err := getRemoteURLGitLab(org, repo)
1✔
127
        if err != nil {
1✔
128
                return "", fmt.Errorf("getRemoteURLGitLab returned error: %s", err.Error())
×
129
        }
×
130

131
        gitcmd = git.Command("remote", "add", "gitlab", remoteURL)
1✔
132
        gitcmd.Dir = tmpdir
1✔
133
        out, err = gitcmd.CombinedOutput()
1✔
134
        if err != nil {
1✔
135
                return "", fmt.Errorf("%v returned error: %s: %s", gitcmd.Args, out, err.Error())
×
136
        }
×
137

138
        prBranchName := "pr_" + prNum
1✔
139
        gitcmd = git.Command("fetch", "github", "pull/"+prNum+"/head:"+prBranchName)
1✔
140
        gitcmd.Dir = tmpdir
1✔
141
        out, err = gitcmd.CombinedOutput()
1✔
142
        if err != nil {
1✔
143
                return "", fmt.Errorf("%v returned error: %s: %s", gitcmd.Args, out, err.Error())
×
144
        }
×
145

146
        // Push but not don't trigger CI (yet)
147
        gitcmd = git.Command("push", "-f", "-o", "ci.skip", "--set-upstream", "gitlab", prBranchName)
1✔
148
        gitcmd.Dir = tmpdir
1✔
149
        out, err = gitcmd.CombinedOutput()
1✔
150
        if err != nil {
1✔
151
                return "", fmt.Errorf("%v returned error: %s: %s", gitcmd.Args, out, err.Error())
×
152
        }
×
153

154
        log.Infof("Created branch: %s:%s", repo, prBranchName)
1✔
155
        return prBranchName, nil
1✔
156
}
157

158
func deleteStaleGitlabPRBranch(log *logrus.Entry, pr *github.PullRequestEvent, conf *config) error {
1✔
159

1✔
160
        // If the action is "closed" the pull request was merged or just closed,
1✔
161
        // stop builds in both cases.
1✔
162
        if pr.GetAction() != "closed" {
1✔
163
                log.Debugf("deleteStaleGitlabPRBranch: PR not closed, therefore not stopping it's pipeline")
×
164
                return nil
×
165
        }
×
166
        repoName := pr.GetRepo().GetName()
1✔
167
        repoOrg := pr.GetOrganization().GetLogin()
1✔
168

1✔
169
        tmpdir, err := ioutil.TempDir("", repoName)
1✔
170
        if err != nil {
1✔
171
                return err
×
172
        }
×
173
        defer os.RemoveAll(tmpdir)
1✔
174

1✔
175
        gitcmd := git.Command("init", ".")
1✔
176
        gitcmd.Dir = tmpdir
1✔
177
        out, err := gitcmd.CombinedOutput()
1✔
178
        if err != nil {
1✔
179
                return fmt.Errorf("%v returned error: %s: %s", gitcmd.Args, out, err.Error())
×
180
        }
×
181

182
        remoteURL, err := getRemoteURLGitLab(repoOrg, repoName)
1✔
183
        if err != nil {
1✔
184
                return fmt.Errorf("getRemoteURLGitLab returned error: %s", err.Error())
×
185
        }
×
186

187
        gitcmd = git.Command("remote", "add", "gitlab", remoteURL)
1✔
188
        gitcmd.Dir = tmpdir
1✔
189
        out, err = gitcmd.CombinedOutput()
1✔
190
        if err != nil {
1✔
191
                return fmt.Errorf("%v returned error: %s: %s", gitcmd.Args, out, err.Error())
×
192
        }
×
193

194
        gitcmd = git.Command("fetch", "gitlab")
1✔
195
        gitcmd.Dir = tmpdir
1✔
196
        out, err = gitcmd.CombinedOutput()
1✔
197
        if err != nil {
1✔
198
                return fmt.Errorf("%v returned error: %s: %s", gitcmd.Args, out, err.Error())
×
199
        }
×
200

201
        gitcmd = git.Command("push", "gitlab", "--delete", fmt.Sprintf("pr_%d", pr.GetNumber()))
1✔
202
        gitcmd.Dir = tmpdir
1✔
203
        out, err = gitcmd.CombinedOutput()
1✔
204
        if err != nil {
1✔
205
                return fmt.Errorf("%v returned error: %s: %s", gitcmd.Args, out, err.Error())
×
206
        }
×
207

208
        return nil
1✔
209

210
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc