-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathprojects.go
150 lines (126 loc) · 4.22 KB
/
projects.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
package gitlab
import (
"context"
"fmt"
"regexp"
log "github.com/sirupsen/logrus"
goGitlab "github.com/xanzy/go-gitlab"
"go.openly.dev/pointy"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"github.com/mvisonneau/gitlab-ci-pipelines-exporter/pkg/config"
"github.com/mvisonneau/gitlab-ci-pipelines-exporter/pkg/schemas"
)
// GetProject ..
func (c *Client) GetProject(ctx context.Context, name string) (*goGitlab.Project, error) {
ctx, span := otel.Tracer(tracerName).Start(ctx, "gitlab:GetProject")
defer span.End()
span.SetAttributes(attribute.String("project_name", name))
log.WithFields(log.Fields{
"project-name": name,
}).Debug("reading project")
c.rateLimit(ctx)
p, resp, err := c.Projects.GetProject(name, &goGitlab.GetProjectOptions{}, goGitlab.WithContext(ctx))
c.requestsRemaining(resp)
return p, err
}
// ListProjects ..
func (c *Client) ListProjects(ctx context.Context, w config.Wildcard) ([]schemas.Project, error) {
ctx, span := otel.Tracer(tracerName).Start(ctx, "gitlab:ListProjects")
defer span.End()
span.SetAttributes(attribute.String("wildcard_search", w.Search))
span.SetAttributes(attribute.String("wildcard_owner_kind", w.Owner.Kind))
span.SetAttributes(attribute.String("wildcard_owner_name", w.Owner.Name))
span.SetAttributes(attribute.Bool("wildcard_owner_include_subgroups", w.Owner.IncludeSubgroups))
span.SetAttributes(attribute.Bool("wildcard_archived", w.Archived))
logFields := log.Fields{
"wildcard-search": w.Search,
"wildcard-owner-kind": w.Owner.Kind,
"wildcard-owner-name": w.Owner.Name,
"wildcard-owner-include-subgroups": w.Owner.IncludeSubgroups,
"wildcard-archived": w.Archived,
}
log.WithFields(logFields).Debug("listing all projects from wildcard")
var projects []schemas.Project
listOptions := goGitlab.ListOptions{
Page: 1,
PerPage: 100,
}
// As a result, the API will return the projects that the owner has access onto.
// This is not necessarily what we the end-user would intend when leveraging a
// scoped wildcard. Therefore, if the wildcard owner name is set, we want to filter
// out to project actually *belonging* to the owner.
var ownerRegexp *regexp.Regexp
if len(w.Owner.Name) > 0 {
ownerRegexp = regexp.MustCompile(fmt.Sprintf(`^%s\/`, w.Owner.Name))
} else {
ownerRegexp = regexp.MustCompile(`.*`)
}
for {
var (
gps []*goGitlab.Project
resp *goGitlab.Response
err error
)
c.rateLimit(ctx)
switch w.Owner.Kind {
case "user":
gps, resp, err = c.Projects.ListUserProjects(
w.Owner.Name,
&goGitlab.ListProjectsOptions{
Archived: &w.Archived,
ListOptions: listOptions,
Search: &w.Search,
Simple: pointy.Bool(true),
},
goGitlab.WithContext(ctx),
)
case "group":
gps, resp, err = c.Groups.ListGroupProjects(
w.Owner.Name,
&goGitlab.ListGroupProjectsOptions{
Archived: &w.Archived,
WithShared: pointy.Bool(false),
IncludeSubGroups: &w.Owner.IncludeSubgroups,
ListOptions: listOptions,
Search: &w.Search,
Simple: pointy.Bool(true),
},
goGitlab.WithContext(ctx),
)
default:
// List all visible projects
gps, resp, err = c.Projects.ListProjects(
&goGitlab.ListProjectsOptions{
ListOptions: listOptions,
Archived: &w.Archived,
Search: &w.Search,
Simple: pointy.Bool(true),
},
goGitlab.WithContext(ctx),
)
}
if err != nil {
return projects, fmt.Errorf("unable to list projects with search pattern '%s' from the GitLab API : %v", w.Search, err.Error())
}
c.requestsRemaining(resp)
// Copy relevant settings from wildcard into created project
for _, gp := range gps {
if !ownerRegexp.MatchString(gp.PathWithNamespace) {
log.WithFields(logFields).WithFields(log.Fields{
"project-id": gp.ID,
"project-name": gp.PathWithNamespace,
}).Debug("project path not matching owner's name, skipping")
continue
}
p := schemas.NewProject(gp.PathWithNamespace)
p.ProjectParameters = w.ProjectParameters
projects = append(projects, p)
}
if resp.CurrentPage >= resp.NextPage {
break
}
listOptions.Page = resp.NextPage
}
return projects, nil
}