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

mendersoftware / integration-test-runner / 1681953125

21 Feb 2025 08:24AM UTC coverage: 59.965% (-4.4%) from 64.328%
1681953125

Pull #357

gitlab-ci

danielskinstad
feat: run integration pipeline on demand

Allow you to run the full integration pipeline on a protected branch
when a trusted member runs `start integration pipeline`.

As of now, this will only run with main - and you cannot specify PRs to
run the pipeline with.

Ticket: QA-863

Signed-off-by: Daniel Skinstad Drabitzius <daniel.drabitzius@northern.tech>
Pull Request #357: wip: integration tests on demand

77 of 311 new or added lines in 7 files covered. (24.76%)

2 existing lines in 1 file now uncovered.

1736 of 2895 relevant lines covered (59.97%)

2.19 hits per line

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

68.06
/pipeline.go
1
package main
2

3
import (
4
        "bytes"
5
        "context"
6
        "reflect"
7
        "sort"
8
        "text/template"
9

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

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

17
func say(
18
        ctx context.Context,
19
        tmplString string,
20
        data interface{},
21
        log *logrus.Entry,
22
        conf *config,
23
        pr *github.PullRequestEvent,
24
) error {
1✔
25
        tmpl, err := template.New("Main").Parse(tmplString)
1✔
26
        if err != nil {
1✔
NEW
27
                log.Errorf(
×
NEW
28
                        "Failed to parse the build matrix template. Should never happen! Error: %s\n",
×
NEW
29
                        err.Error(),
×
NEW
30
                )
×
NEW
31
        }
×
32
        var buf bytes.Buffer
1✔
33
        if err = tmpl.Execute(&buf, data); err != nil {
1✔
NEW
34
                log.Errorf("Failed to execute the build matrix template. Error: %s\n", err.Error())
×
NEW
35
        }
×
36

37
        // Comment with a pipeline-link on the PR
38
        commentBody := buf.String()
1✔
39
        comment := github.IssueComment{
1✔
40
                Body: &commentBody,
1✔
41
        }
1✔
42

1✔
43
        err = githubClient.CreateComment(ctx,
1✔
44
                conf.githubOrganization, pr.GetRepo().GetName(), pr.GetNumber(), &comment)
1✔
45
        if err != nil {
1✔
NEW
46
                log.Infof("Failed to comment on the pr: %v, Error: %s", pr, err.Error())
×
NEW
47
        }
×
48

49
        return err
1✔
50
}
51

52
func filterOutEmptyVariables(
53
        optionsIn []*gitlab.PipelineVariableOptions,
54
) []*gitlab.PipelineVariableOptions {
1✔
55
        var optionsOut []*gitlab.PipelineVariableOptions
1✔
56
        for _, option := range optionsIn {
2✔
57
                if *option.Value != "" {
2✔
58
                        optionsOut = append(optionsOut, option)
1✔
59
                }
1✔
60
        }
61
        return optionsOut
1✔
62
}
63

64
func stopStalePipelines(
65
        pipelinePath string,
66
        log *logrus.Entry,
67
        client clientgitlab.Client,
68
        vars []*gitlab.PipelineVariableOptions,
69
) {
1✔
70

1✔
71
        sort.SliceStable(vars, func(i, j int) bool {
2✔
72
                return *vars[i].Key < *vars[j].Key
1✔
73
        })
1✔
74

75
        username := githubBotName
1✔
76
        status := gitlab.Pending
1✔
77
        opt := &gitlab.ListProjectPipelinesOptions{
1✔
78
                Username: &username,
1✔
79
                Status:   &status,
1✔
80
        }
1✔
81

1✔
82
        pipelinesPending, err := client.ListProjectPipelines(clientPipelinePath, opt)
1✔
83
        if err != nil {
1✔
NEW
84
                log.Errorf("stopStalePipelines: Could not list pending pipelines: %s", err.Error())
×
NEW
85
        }
×
86

87
        status = gitlab.Running
1✔
88
        opt = &gitlab.ListProjectPipelinesOptions{
1✔
89
                Username: &username,
1✔
90
                Status:   &status,
1✔
91
        }
1✔
92

1✔
93
        pipelinesRunning, err := client.ListProjectPipelines(clientPipelinePath, opt)
1✔
94
        if err != nil {
1✔
NEW
95
                log.Errorf("stopStalePipelines: Could not list running pipelines: %s", err.Error())
×
NEW
96
        }
×
97

98
        for _, pipeline := range append(pipelinesPending, pipelinesRunning...) {
2✔
99

1✔
100
                variables, err := client.GetPipelineVariables(clientPipelinePath, pipeline.ID)
1✔
101
                if err != nil {
1✔
NEW
102
                        log.Errorf("stopStalePipelines: Could not get variables for pipeline: %s", err.Error())
×
NEW
103
                        continue
×
104
                }
105

106
                sort.SliceStable(variables, func(i, j int) bool {
1✔
NEW
107
                        return variables[i].Key < variables[j].Key
×
NEW
108
                })
×
109

110
                if reflect.DeepEqual(vars, variables) {
1✔
NEW
111
                        log.Infof("Cancelling stale pipeline %d, url: %s", pipeline.ID, pipeline.WebURL)
×
NEW
112

×
NEW
113
                        err := client.CancelPipelineBuild(clientPipelinePath, pipeline.ID)
×
NEW
114
                        if err != nil {
×
NEW
115
                                log.Errorf("stopStalePipelines: Could not cancel pipeline: %s", err.Error())
×
NEW
116
                        }
×
117

118
                }
119

120
        }
121
}
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