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

mendersoftware / integration-test-runner / 1689195873

26 Feb 2025 10:00AM UTC coverage: 65.892% (+0.02%) from 65.869%
1689195873

push

gitlab-ci

web-flow
Merge pull request #361 from danielskinstad/fix-delete

Fix delete

5 of 6 new or added lines in 2 files covered. (83.33%)

1 existing line in 1 file now uncovered.

1928 of 2926 relevant lines covered (65.89%)

2.23 hits per line

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

71.51
/pullreq_pipeline.go
1
package main
2

3
import (
4
        "fmt"
5
        "os"
6
        "strconv"
7
        "strings"
8

9
        "github.com/google/go-github/v28/github"
10
        "github.com/sirupsen/logrus"
11
        gitlab "gitlab.com/gitlab-org/api/client-go"
12

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

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

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

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

91
        return nil
1✔
92
}
93

94
func syncPullRequestBranch(
95
        log *logrus.Entry,
96
        pr *github.PullRequestEvent,
97
        conf *config,
98
) (string, error) {
1✔
99
        prBranchName := "pr_" + strconv.Itoa(pr.GetNumber())
1✔
100
        if err := syncBranch(prBranchName, log, pr, conf); err != nil {
1✔
101
                mainErrMsg := "There was an error syncing branches"
×
102
                return "", fmt.Errorf("%v returned error: %s: %s", err, mainErrMsg, err.Error())
×
103
        }
×
104
        return prBranchName, nil
1✔
105
}
106

107
func syncBranch(
108
        prBranchName string,
109
        log *logrus.Entry,
110
        pr *github.PullRequestEvent,
111
        conf *config,
112
) error {
1✔
113

1✔
114
        repo := pr.GetRepo().GetName()
1✔
115
        prNum := strconv.Itoa(pr.GetNumber())
1✔
116

1✔
117
        tmpdir, err := os.MkdirTemp("", repo)
1✔
118
        if err != nil {
1✔
119
                return err
×
120
        }
×
121
        defer os.RemoveAll(tmpdir)
1✔
122

1✔
123
        gitcmd := git.Command("init", ".")
1✔
124
        gitcmd.Dir = tmpdir
1✔
125
        out, err := gitcmd.CombinedOutput()
1✔
126
        if err != nil {
1✔
127
                return fmt.Errorf("%v returned error: %s: %s", gitcmd.Args, out, err.Error())
×
128
        }
×
129

130
        repoURL := getRemoteURLGitHub(conf.githubProtocol, conf.githubOrganization, repo)
1✔
131
        gitcmd = git.Command("remote", "add", "github", repoURL)
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
        remoteURL, err := getRemoteURLGitLab(conf.githubOrganization, repo)
1✔
139
        if err != nil {
1✔
140
                return fmt.Errorf("getRemoteURLGitLab returned error: %s", err.Error())
×
141
        }
×
142

143
        gitcmd = git.Command("remote", "add", "gitlab", remoteURL)
1✔
144
        gitcmd.Dir = tmpdir
1✔
145
        out, err = gitcmd.CombinedOutput()
1✔
146
        if err != nil {
1✔
147
                return fmt.Errorf("%v returned error: %s: %s", gitcmd.Args, out, err.Error())
×
148
        }
×
149

150
        gitcmd = git.Command("fetch", "github", "pull/"+prNum+"/head:"+prBranchName)
1✔
151
        gitcmd.Dir = tmpdir
1✔
152
        out, err = gitcmd.CombinedOutput()
1✔
153
        if err != nil {
1✔
154
                return fmt.Errorf("%v returned error: %s: %s", gitcmd.Args, out, err.Error())
×
155
        }
×
156

157
        // Push but not don't trigger CI (yet)
158
        gitcmd = git.Command("push", "-f", "-o", "ci.skip", "--set-upstream", "gitlab", prBranchName)
1✔
159
        gitcmd.Dir = tmpdir
1✔
160
        out, err = gitcmd.CombinedOutput()
1✔
161
        if err != nil {
1✔
162
                return fmt.Errorf("%v returned error: %s: %s", gitcmd.Args, out, err.Error())
×
163
        }
×
164

165
        log.Infof("Created branch: %s:%s", repo, prBranchName)
1✔
166
        return nil
1✔
167
}
168

169
func deleteStaleGitlabPRBranch(log *logrus.Entry, pr *github.PullRequestEvent, conf *config) error {
1✔
170
        // If the action is "closed", the pull request was merged or just closed,
1✔
171
        // stop builds in both cases.
1✔
172
        if pr.GetAction() != "closed" {
1✔
173
                log.Debugf("deleteStaleGitlabPRBranch: PR not closed, therefore not stopping it's pipeline")
×
174
                return nil
×
175
        }
×
176

177
        // Call the DeletePRBranch function to delete the PR branch on GitLab
178
        response, err := deletePRBranch(pr, conf, fmt.Sprintf("pr_%d", pr.GetNumber()), log)
1✔
179
        if err != nil {
1✔
180
                return fmt.Errorf("Got response: %d. Failed to delete PR branch: %s",
×
181
                        response.StatusCode,
×
182
                        err.Error(),
×
183
                )
×
184
        }
×
185

186
        if pr.GetRepo().GetName() == "integration" {
1✔
187
                // check if we have a protected branch and try to delete it
×
188
                response, err := deletePRBranch(pr, conf,
×
189
                        fmt.Sprintf("pr_%d_protected", pr.GetNumber()), log)
×
190
                if err != nil {
×
191
                        // Don't return error if the branch doesn't exist
×
192
                        if response.StatusCode != 404 {
×
193
                                return fmt.Errorf("Got response: %d. Failed to delete PR branch: %s",
×
194
                                        response.StatusCode,
×
195
                                        err.Error(),
×
196
                                )
×
197
                        }
×
198
                }
199
        }
200

201
        return nil
1✔
202
}
203

204
func deletePRBranch(
205
        pr *github.PullRequestEvent,
206
        conf *config,
207
        prBranchName string,
208
        log *logrus.Entry,
209
) (*gitlab.Response, error) {
1✔
210

1✔
211
        repoName := pr.GetRepo().GetName()
1✔
212

1✔
213
        group, ok := gitHubOrganizationToGitLabGroup[conf.githubOrganization]
1✔
214
        if !ok {
1✔
NEW
215
                return nil, fmt.Errorf("Unrecognized organization %q", conf.githubOrganization)
×
UNCOV
216
        }
×
217
        path := "Northern.tech/" + group + "/" + repoName
1✔
218

1✔
219
        client, err := clientgitlab.NewGitLabClient(
1✔
220
                conf.gitlabToken,
1✔
221
                conf.gitlabBaseURL,
1✔
222
                conf.dryRunMode,
1✔
223
        )
1✔
224
        if err != nil {
1✔
225
                return nil, err
×
226
        }
×
227

228
        response, err := client.DeleteBranch(path, prBranchName, nil)
1✔
229
        if err != nil {
1✔
230
                return response, err
×
231
        }
×
232
        return response, nil
1✔
233
}
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