-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathjobs.go
319 lines (255 loc) · 7.67 KB
/
jobs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package gitlab
import (
"context"
"reflect"
"strconv"
"strings"
log "github.com/sirupsen/logrus"
goGitlab "github.com/xanzy/go-gitlab"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"github.com/mvisonneau/gitlab-ci-pipelines-exporter/pkg/schemas"
)
// ListRefPipelineJobs ..
func (c *Client) ListRefPipelineJobs(ctx context.Context, ref schemas.Ref) (jobs []schemas.Job, err error) {
ctx, span := otel.Tracer(tracerName).Start(ctx, "gitlab:ListRefPipelineJobs")
defer span.End()
span.SetAttributes(attribute.String("project_name", ref.Project.Name))
span.SetAttributes(attribute.String("ref_name", ref.Name))
if reflect.DeepEqual(ref.LatestPipeline, (schemas.Pipeline{})) {
log.WithFields(
log.Fields{
"project-name": ref.Project.Name,
"ref": ref.Name,
},
).Debug("most recent pipeline not defined, exiting..")
return
}
jobs, err = c.ListPipelineJobs(ctx, ref.Project.Name, ref.LatestPipeline.ID)
if err != nil {
return
}
if ref.Project.Pull.Pipeline.Jobs.FromChildPipelines.Enabled {
var childJobs []schemas.Job
childJobs, err = c.ListPipelineChildJobs(ctx, ref.Project.Name, ref.LatestPipeline.ID)
if err != nil {
return
}
jobs = append(jobs, childJobs...)
}
return
}
// ListPipelineJobs ..
func (c *Client) ListPipelineJobs(ctx context.Context, projectNameOrID string, pipelineID int) (jobs []schemas.Job, err error) {
ctx, span := otel.Tracer(tracerName).Start(ctx, "gitlab:ListPipelineJobs")
defer span.End()
span.SetAttributes(attribute.String("project_name_or_id", projectNameOrID))
span.SetAttributes(attribute.Int("pipeline_id", pipelineID))
var (
foundJobs []*goGitlab.Job
resp *goGitlab.Response
)
options := &goGitlab.ListJobsOptions{
ListOptions: goGitlab.ListOptions{
Page: 1,
PerPage: 100,
},
}
for {
c.rateLimit(ctx)
foundJobs, resp, err = c.Jobs.ListPipelineJobs(projectNameOrID, pipelineID, options, goGitlab.WithContext(ctx))
if err != nil {
return
}
c.requestsRemaining(resp)
for _, job := range foundJobs {
jobs = append(jobs, schemas.NewJob(*job))
}
if resp.CurrentPage >= resp.NextPage {
log.WithFields(
log.Fields{
"project-name-or-id": projectNameOrID,
"pipeline-id": pipelineID,
"jobs-count": resp.TotalItems,
},
).Debug("found pipeline jobs")
break
}
options.Page = resp.NextPage
}
return
}
// ListPipelineBridges ..
func (c *Client) ListPipelineBridges(ctx context.Context, projectNameOrID string, pipelineID int) (bridges []*goGitlab.Bridge, err error) {
ctx, span := otel.Tracer(tracerName).Start(ctx, "gitlab:ListPipelineBridges")
defer span.End()
span.SetAttributes(attribute.String("project_name_or_id", projectNameOrID))
span.SetAttributes(attribute.Int("pipeline_id", pipelineID))
var (
foundBridges []*goGitlab.Bridge
resp *goGitlab.Response
)
options := &goGitlab.ListJobsOptions{
ListOptions: goGitlab.ListOptions{
Page: 1,
PerPage: 100,
},
}
for {
c.rateLimit(ctx)
foundBridges, resp, err = c.Jobs.ListPipelineBridges(projectNameOrID, pipelineID, options, goGitlab.WithContext(ctx))
if err != nil {
return
}
c.requestsRemaining(resp)
bridges = append(bridges, foundBridges...)
if resp.CurrentPage >= resp.NextPage {
log.WithFields(
log.Fields{
"project-name-or-id": projectNameOrID,
"pipeline-id": pipelineID,
"bridges-count": resp.TotalItems,
},
).Debug("found pipeline bridges")
break
}
options.Page = resp.NextPage
}
return
}
// ListPipelineChildJobs ..
func (c *Client) ListPipelineChildJobs(ctx context.Context, projectNameOrID string, parentPipelineID int) (jobs []schemas.Job, err error) {
ctx, span := otel.Tracer(tracerName).Start(ctx, "gitlab:ListPipelineChildJobs")
defer span.End()
span.SetAttributes(attribute.String("project_name_or_id", projectNameOrID))
span.SetAttributes(attribute.Int("parent_pipeline_id", parentPipelineID))
type pipelineDef struct {
projectNameOrID string
pipelineID int
}
pipelines := []pipelineDef{{projectNameOrID, parentPipelineID}}
for {
if len(pipelines) == 0 {
return
}
var (
foundBridges []*goGitlab.Bridge
pipeline = pipelines[len(pipelines)-1]
)
pipelines = pipelines[:len(pipelines)-1]
foundBridges, err = c.ListPipelineBridges(ctx, pipeline.projectNameOrID, pipeline.pipelineID)
if err != nil {
return
}
for _, foundBridge := range foundBridges {
// Trigger job was created but not yet executed
// so downstream pipeline is not yet scheduled to start.
// Therefore no pipeline is available and bridge could be skipped.
if foundBridge.DownstreamPipeline == nil {
continue
}
pipelines = append(pipelines, pipelineDef{strconv.Itoa(foundBridge.DownstreamPipeline.ProjectID), foundBridge.DownstreamPipeline.ID})
var foundJobs []schemas.Job
foundJobs, err = c.ListPipelineJobs(ctx, strconv.Itoa(foundBridge.DownstreamPipeline.ProjectID), foundBridge.DownstreamPipeline.ID)
if err != nil {
return
}
jobs = append(jobs, foundJobs...)
}
}
}
// ListRefMostRecentJobs ..
func (c *Client) ListRefMostRecentJobs(ctx context.Context, ref schemas.Ref) (jobs []schemas.Job, err error) {
ctx, span := otel.Tracer(tracerName).Start(ctx, "gitlab:ListRefMostRecentJobs")
defer span.End()
span.SetAttributes(attribute.String("project_name", ref.Project.Name))
span.SetAttributes(attribute.String("ref_name", ref.Name))
if len(ref.LatestJobs) == 0 {
log.WithFields(
log.Fields{
"project-name": ref.Project.Name,
"ref": ref.Name,
},
).Debug("no jobs are currently held in memory, exiting..")
return
}
// Deep copy of the ref.Jobs
jobsToRefresh := make(schemas.Jobs)
for k, v := range ref.LatestJobs {
jobsToRefresh[k] = v
}
var (
foundJobs []*goGitlab.Job
resp *goGitlab.Response
opt *goGitlab.ListJobsOptions
)
keysetPagination := c.Version().PipelineJobsKeysetPaginationSupported()
if keysetPagination {
opt = &goGitlab.ListJobsOptions{
ListOptions: goGitlab.ListOptions{
Pagination: "keyset",
PerPage: 100,
},
}
} else {
opt = &goGitlab.ListJobsOptions{
ListOptions: goGitlab.ListOptions{
Page: 1,
PerPage: 100,
},
}
}
options := []goGitlab.RequestOptionFunc{goGitlab.WithContext(ctx)}
for {
c.rateLimit(ctx)
foundJobs, resp, err = c.Jobs.ListProjectJobs(ref.Project.Name, opt, options...)
if err != nil {
return
}
c.requestsRemaining(resp)
for _, job := range foundJobs {
if _, ok := jobsToRefresh[job.Name]; ok {
jobRefName, _ := schemas.GetMergeRequestIIDFromRefName(job.Ref)
if ref.Name == jobRefName {
jobs = append(jobs, schemas.NewJob(*job))
delete(jobsToRefresh, job.Name)
}
}
if len(jobsToRefresh) == 0 {
log.WithFields(
log.Fields{
"project-name": ref.Project.Name,
"ref": ref.Name,
"jobs-count": len(ref.LatestJobs),
},
).Debug("found all jobs to refresh")
return
}
}
if keysetPagination && resp.NextLink == "" ||
(!keysetPagination && resp.CurrentPage >= resp.NextPage) {
var notFoundJobs []string
for k := range jobsToRefresh {
notFoundJobs = append(notFoundJobs, k)
}
log.WithContext(ctx).
WithFields(
log.Fields{
"project-name": ref.Project.Name,
"ref": ref.Name,
"jobs-count": resp.TotalItems,
"not-found-jobs": strings.Join(notFoundJobs, ","),
},
).
Warn("found some ref jobs but did not manage to refresh all jobs which were in memory")
break
}
if keysetPagination {
options = []goGitlab.RequestOptionFunc{
goGitlab.WithContext(ctx),
goGitlab.WithKeysetPaginationParameters(resp.NextLink),
}
}
}
return
}