• 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

80.25
/main_comment.go
1
package main
2

3
import (
4
        "errors"
5
        "strconv"
6
        "strings"
7

8
        "github.com/davecgh/go-spew/spew"
9
        "github.com/gin-gonic/gin"
10
        "github.com/google/go-github/v28/github"
11
        "github.com/sirupsen/logrus"
12

13
        clientgithub "github.com/mendersoftware/integration-test-runner/client/github"
14
)
15

16
func processGitHubComment(
17
        ctx *gin.Context,
18
        comment *github.IssueCommentEvent,
19
        githubClient clientgithub.Client,
20
        conf *config,
21
) error {
11✔
22
        log := getCustomLoggerFromContext(ctx)
11✔
23

11✔
24
        // process created actions only, ignore the others
11✔
25
        action := comment.GetAction()
11✔
26
        if action != "created" {
12✔
27
                log.Infof("Ignoring action %s on comment", action)
1✔
28
                return nil
1✔
29
        }
1✔
30

31
        // accept commands only from organization members
32
        if !githubClient.IsOrganizationMember(ctx, conf.githubOrganization, comment.Sender.GetLogin()) {
13✔
33
                log.Warnf(
3✔
34
                        "%s commented, but he/she is not a member of our organization, ignoring",
3✔
35
                        comment.Sender.GetLogin(),
3✔
36
                )
3✔
37
                return nil
3✔
38
        }
3✔
39

40
        // but ignore comments from myself
41
        if comment.Sender.GetLogin() == githubBotName {
7✔
42
                log.Warnf("%s commented, probably giving instructions, ignoring", comment.Sender.GetLogin())
×
43
                return nil
×
44
        }
×
45

46
        // filter comments mentioning the bot
47
        commentBody := comment.Comment.GetBody()
7✔
48
        if !strings.Contains(commentBody, "@"+githubBotName) {
7✔
49
                log.Info("ignoring comment not mentioning me")
×
50
                return nil
×
51
        }
×
52

53
        // retrieve the pull request
54
        prLink := comment.Issue.GetPullRequestLinks().GetURL()
7✔
55
        if prLink == "" {
8✔
56
                log.Warnf("ignoring comment not on a pull request")
1✔
57
                return nil
1✔
58
        }
1✔
59

60
        prLinkParts := strings.Split(prLink, "/")
6✔
61
        prNumber, err := strconv.Atoi(prLinkParts[len(prLinkParts)-1])
6✔
62
        if err != nil {
7✔
63
                log.Errorf("Unable to retrieve the pull request: %s", err.Error())
1✔
64
                return err
1✔
65
        }
1✔
66

67
        pr, err := githubClient.GetPullRequest(
5✔
68
                ctx,
5✔
69
                conf.githubOrganization,
5✔
70
                comment.GetRepo().GetName(),
5✔
71
                prNumber,
5✔
72
        )
5✔
73
        if err != nil {
6✔
74
                log.Errorf("Unable to retrieve the pull request: %s", err.Error())
1✔
75
                return err
1✔
76
        }
1✔
77

78
        // extract the command and check it is valid
79
        switch {
4✔
80
        case strings.Contains(commentBody, commandStartPipeline):
4✔
81
                buildOptions, err := parseBuildOptions(commentBody)
4✔
82
                // get the list of builds
4✔
83
                prRequest := &github.PullRequestEvent{
4✔
84
                        Repo:        comment.GetRepo(),
4✔
85
                        Number:      github.Int(pr.GetNumber()),
4✔
86
                        PullRequest: pr,
4✔
87
                }
4✔
88
                if err != nil {
5✔
89
                        _ = say(ctx, "There was an error while parsing arguments: {{.ErrorMessage}}",
1✔
90
                                struct {
1✔
91
                                        ErrorMessage string
1✔
92
                                }{
1✔
93
                                        ErrorMessage: err.Error(),
1✔
94
                                },
1✔
95
                                log,
1✔
96
                                conf,
1✔
97
                                prRequest)
1✔
98
                        return err
1✔
99
                }
1✔
100
                builds := parsePullRequest(log, conf, "opened", prRequest)
3✔
101
                log.Infof(
3✔
102
                        "%s:%d will trigger %d builds",
3✔
103
                        comment.GetRepo().GetName(),
3✔
104
                        pr.GetNumber(),
3✔
105
                        len(builds),
3✔
106
                )
3✔
107

3✔
108
                // start the builds
3✔
109
                for idx, build := range builds {
4✔
110
                        log.Infof("%d: "+spew.Sdump(build)+"\n", idx+1)
1✔
111
                        if build.repo == "meta-mender" && build.baseBranch == "master-next" {
1✔
112
                                log.Info("Skipping build targeting meta-mender:master-next")
×
113
                                continue
×
114
                        }
115
                        if err := triggerBuild(log, conf, &build, prRequest, buildOptions); err != nil {
1✔
116
                                log.Errorf("Could not start build: %s", err.Error())
×
117
                        }
×
118
                }
119
        case strings.Contains(commentBody, commandCherryPickBranch):
1✔
120
                log.Infof("Attempting to cherry-pick the changes in PR: %s/%d",
1✔
121
                        comment.GetRepo().GetName(),
1✔
122
                        pr.GetNumber(),
1✔
123
                )
1✔
124
                err = cherryPickPR(log, comment, pr, conf, commentBody, githubClient)
1✔
125
                if err != nil {
1✔
126
                        log.Error(err)
×
127
                }
×
128
        case strings.Contains(commentBody, commandConventionalCommit) &&
129
                strings.Contains(pr.GetUser().GetLogin(), "dependabot"):
1✔
130
                log.Infof(
1✔
131
                        "Attempting to make the PR: %s/%d and commit: %s a conventional commit",
1✔
132
                        comment.GetRepo().GetName(),
1✔
133
                        pr.GetNumber(),
1✔
134
                        pr.GetHead().GetSHA(),
1✔
135
                )
1✔
136
                err = conventionalComittifyDependabotPr(log, comment, pr, conf, commentBody, githubClient)
1✔
137
                if err != nil {
1✔
138
                        log.Error(err)
×
139
                }
×
140
        case strings.Contains(commentBody, commandSyncRepos):
×
141
                syncPRBranch(ctx, comment, pr, log, conf)
×
142
        default:
×
143
                log.Warnf("no command found: %s", commentBody)
×
144
                return nil
×
145
        }
146

147
        return nil
3✔
148
}
149

150
func syncPRBranch(
151
        ctx *gin.Context,
152
        comment *github.IssueCommentEvent,
153
        pr *github.PullRequest,
154
        log *logrus.Entry,
155
        conf *config,
156
) {
×
157
        prEvent := &github.PullRequestEvent{
×
158
                Repo:        comment.GetRepo(),
×
159
                Number:      github.Int(pr.GetNumber()),
×
160
                PullRequest: pr,
×
161
        }
×
162
        if _, err := syncPullRequestBranch(log, prEvent, conf); err != nil {
×
163
                mainErrMsg := "There was an error syncing branches"
×
164
                log.Errorf(mainErrMsg+": %s", err.Error())
×
165
                msg := mainErrMsg + ", " + msgDetailsKubernetesLog
×
166
                postGitHubMessage(ctx, prEvent, log, msg)
×
167
        }
×
168
}
169

170
// parsing `start pipeline --pr mender-connect/pull/88/head --pr deviceconnect/pull/12/head
171
// --pr mender/3.1.x --fast sugar pretty please`
172
//
173
//        BuildOptions {
174
//                Fast: true,
175
//                PullRequests: map[string]string{
176
//                        "mender-connect": "pull/88/head",
177
//                        "deviceconnect": "pull/12/head",
178
//                }
179
//        }
180
func parseBuildOptions(commentBody string) (*BuildOptions, error) {
16✔
181
        buildOptions := NewBuildOptions()
16✔
182
        var err error
16✔
183
        words := strings.Fields(commentBody)
16✔
184
        tokensCount := len(words)
16✔
185
        for id, word := range words {
120✔
186
                if word == "--pr" && id < (tokensCount-1) {
136✔
187
                        userInput := strings.TrimSpace(words[id+1])
32✔
188
                        userInputParts := strings.Split(userInput, "/")
32✔
189

32✔
190
                        if len(userInput) > 0 {
64✔
191
                                var revision string
32✔
192
                                switch len(userInputParts) {
32✔
193
                                case 2: // we can have both deviceauth/1 and mender/3.1.x syntax
12✔
194
                                        // repo/<pr_number> syntax
12✔
195
                                        if _, err := strconv.Atoi(userInputParts[1]); err == nil {
15✔
196
                                                revision = "pull/" + userInputParts[1] + "/head"
3✔
197
                                        } else {
13✔
198
                                                // feature branch
10✔
199
                                                revision = userInputParts[1]
10✔
200
                                        }
10✔
201
                                case 3: // deviceconnect/pull/12 syntax
2✔
202
                                        revision = strings.Join(userInputParts[1:], "/") + "/head"
2✔
203
                                case 4: // deviceauth/pull/1/head syntax
13✔
204
                                        revision = strings.Join(userInputParts[1:], "/")
13✔
205
                                default:
6✔
206
                                        err = errors.New(
6✔
207
                                                "parse error near '" + userInput + "', I need, e.g.: start pipeline --pr" +
6✔
208
                                                        " somerepo/pull/12/head --pr somerepo/1.0.x ",
6✔
209
                                        )
6✔
210
                                }
211
                                buildOptions.PullRequests[userInputParts[0]] = revision
32✔
212
                        }
213
                } else if word == "--fast" {
74✔
214
                        buildOptions.Fast = true
1✔
215
                }
1✔
216
        }
217

218
        return buildOptions, err
16✔
219
}
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