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

mendersoftware / integration-test-runner / 1687972984

25 Feb 2025 04:02PM UTC coverage: 65.959% (+1.0%) from 64.916%
1687972984

Pull #359

gitlab-ci

danielskinstad
fix: wait for `pr_xxx_protected` to be created

There is a potential race condition between syncing branches and
protecting them. Add a sleep between sync and protect in syncProtectdBranch

Ticket: None
Changelog: None

Signed-off-by: Daniel Skinstad Drabitzius <daniel.drabitzius@northern.tech>
Pull Request #359: Fix integration pipeline

26 of 47 new or added lines in 4 files covered. (55.32%)

7 existing lines in 3 files now uncovered.

1926 of 2920 relevant lines covered (65.96%)

2.23 hits per line

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

64.1
/client/gitlab/client.go
1
package gitlab
2

3
import (
4
        "encoding/json"
5
        "fmt"
6

7
        gitlab "gitlab.com/gitlab-org/api/client-go"
8

9
        "github.com/mendersoftware/integration-test-runner/logger"
10
)
11

12
// Client represents a GitLab client
13
type Client interface {
14
        CancelPipelineBuild(path string, id int) error
15
        CreatePipeline(path string, options *gitlab.CreatePipelineOptions) (*gitlab.Pipeline, error)
16
        GetPipelineVariables(path string, id int) ([]*gitlab.PipelineVariable, error)
17
        ProtectRepositoryBranches(
18
                path string,
19
                options *gitlab.ProtectRepositoryBranchesOptions,
20
        ) (*gitlab.ProtectedBranch, error)
21
        ListProjectPipelines(
22
                path string,
23
                options *gitlab.ListProjectPipelinesOptions,
24
        ) ([]*gitlab.PipelineInfo, error)
25
        DeleteBranch(path string,
26
                branch string,
27
                options *gitlab.RequestOptionFunc,
28
        ) (*gitlab.Response, error)
29
}
30

31
type gitLabClient struct {
32
        client     *gitlab.Client
33
        dryRunMode bool
34
}
35

36
// NewGitLabClient returns a new GitLabClient for the given conf
37
func NewGitLabClient(accessToken string, baseURL string, dryRunMode bool) (Client, error) {
1✔
38
        gitlabClient, err := gitlab.NewClient(accessToken, gitlab.WithBaseURL(baseURL))
1✔
39
        if err != nil {
1✔
40
                return nil, err
×
41
        }
×
42
        return &gitLabClient{
1✔
43
                client:     gitlabClient,
1✔
44
                dryRunMode: dryRunMode,
1✔
45
        }, nil
1✔
46
}
47

48
// CancelPipelineBuild cancel a pipeline
49
func (c *gitLabClient) CancelPipelineBuild(path string, id int) error {
×
50
        if c.dryRunMode {
×
51
                msg := fmt.Sprintf("gitlab.CancelPipelineBuild: path=%s,id=%d",
×
52
                        path, id,
×
53
                )
×
54
                logger.GetRequestLogger().Push(msg)
×
55
                return nil
×
56
        }
×
57
        _, _, err := c.client.Pipelines.CancelPipelineBuild(path, id, nil)
×
58
        return err
×
59
}
60

61
// CreatePipeline creates a pipeline
62
func (c *gitLabClient) CreatePipeline(
63
        path string,
64
        options *gitlab.CreatePipelineOptions,
65
) (*gitlab.Pipeline, error) {
1✔
66
        if c.dryRunMode {
2✔
67
                optionsJSON, _ := json.Marshal(options)
1✔
68
                msg := fmt.Sprintf("gitlab.CreatePipeline: path=%s,options=%s",
1✔
69
                        path, string(optionsJSON),
1✔
70
                )
1✔
71
                logger.GetRequestLogger().Push(msg)
1✔
72
                return &gitlab.Pipeline{}, nil
1✔
73
        }
1✔
74
        pipeline, _, err := c.client.Pipelines.CreatePipeline(path, options, nil)
×
75
        return pipeline, err
×
76
}
77

78
// GetPipelineVariables get the pipeline variables
79
func (c *gitLabClient) GetPipelineVariables(
80
        path string,
81
        id int,
82
) ([]*gitlab.PipelineVariable, error) {
1✔
83
        if c.dryRunMode {
2✔
84
                msg := fmt.Sprintf("gitlab.GetPipelineVariables: path=%s,id=%d",
1✔
85
                        path, id,
1✔
86
                )
1✔
87
                logger.GetRequestLogger().Push(msg)
1✔
88
                return []*gitlab.PipelineVariable{}, nil
1✔
89
        }
1✔
90
        variables, _, err := c.client.Pipelines.GetPipelineVariables(path, id, nil)
×
91
        return variables, err
×
92
}
93

94
// ListProjectPipelines list the project pipelines
95
func (c *gitLabClient) ListProjectPipelines(
96
        path string,
97
        options *gitlab.ListProjectPipelinesOptions,
98
) ([]*gitlab.PipelineInfo, error) {
1✔
99
        if c.dryRunMode {
2✔
100
                optionsJSON, _ := json.Marshal(options)
1✔
101
                msg := fmt.Sprintf("gitlab.ListProjectPipelines: path=%s,options=%s",
1✔
102
                        path, string(optionsJSON),
1✔
103
                )
1✔
104
                logger.GetRequestLogger().Push(msg)
1✔
105
                return []*gitlab.PipelineInfo{{ID: 1}}, nil
1✔
106
        }
1✔
107
        pipelines, _, err := c.client.Pipelines.ListProjectPipelines(path, options, nil)
×
108
        return pipelines, err
×
109
}
110

111
// ProtectRepositoryBranches protects branches
112
func (c *gitLabClient) ProtectRepositoryBranches(
113
        path string,
114
        options *gitlab.ProtectRepositoryBranchesOptions,
115
) (*gitlab.ProtectedBranch, error) {
1✔
116
        if c.dryRunMode {
2✔
117
                optionsJSON, _ := json.Marshal(options)
1✔
118
                msg := fmt.Sprintf("gitlab.ProtectedBranch: path=%s,options=%s",
1✔
119
                        path, string(optionsJSON),
1✔
120
                )
1✔
121
                logger.GetRequestLogger().Push(msg)
1✔
122
                return &gitlab.ProtectedBranch{}, nil
1✔
123
        }
1✔
124
        protected_branch, _, err := c.client.ProtectedBranches.ProtectRepositoryBranches(
×
125
                path,
×
126
                options,
×
127
                nil)
×
128
        return protected_branch, err
×
129
}
130

131
// DeleteBranch deletes branches
132
func (c *gitLabClient) DeleteBranch(
133
        path string,
134
        branch string,
135
        options *gitlab.RequestOptionFunc,
136
) (*gitlab.Response, error) {
1✔
137
        if c.dryRunMode {
2✔
138
                msg := fmt.Sprintf("gitlab.DeleteBranch: path=%s,branch=%s",
1✔
139
                        path, branch,
1✔
140
                )
1✔
141
                logger.GetRequestLogger().Push(msg)
1✔
142
                return nil, nil
1✔
143
        }
1✔
NEW
144
        response, err := c.client.Branches.DeleteBranch(path,
×
NEW
145
                branch,
×
NEW
146
                nil)
×
NEW
147

×
NEW
148
        return response, err
×
149
}
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