-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathprojects.go
100 lines (88 loc) · 2.32 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
package gitlab
import (
"fmt"
"github.com/apex/log"
"github.com/mvisonneau/gitlab-ci-pipelines-exporter/pkg/schemas"
"github.com/xanzy/go-gitlab"
goGitlab "github.com/xanzy/go-gitlab"
)
// GetProject ..
func (c *Client) GetProject(name string) (*goGitlab.Project, error) {
c.rateLimit()
p, _, err := c.Projects.GetProject(name, &goGitlab.GetProjectOptions{})
return p, err
}
// ListProjects ..
func (c *Client) ListProjects(w schemas.Wildcard) ([]schemas.Project, error) {
log.WithFields(
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,
},
).Debug("listing all projects from wildcard")
var projects []schemas.Project
trueVal := true
listOptions := gitlab.ListOptions{
PerPage: 20,
Page: 1,
}
for {
var gps []*gitlab.Project
var resp *gitlab.Response
var err error
c.rateLimit()
switch w.Owner.Kind {
case "user":
gps, resp, err = c.Projects.ListUserProjects(
w.Owner.Name,
&gitlab.ListProjectsOptions{
Archived: &w.Archived,
ListOptions: listOptions,
Search: &w.Search,
Simple: &trueVal,
},
)
case "group":
gps, resp, err = c.Groups.ListGroupProjects(
w.Owner.Name,
&gitlab.ListGroupProjectsOptions{
Archived: &w.Archived,
IncludeSubgroups: &w.Owner.IncludeSubgroups,
ListOptions: listOptions,
Search: &w.Search,
Simple: &trueVal,
},
)
default:
gps, resp, err = c.Projects.ListProjects(
&gitlab.ListProjectsOptions{
ListOptions: listOptions,
Archived: &w.Archived,
Simple: &trueVal,
Search: &w.Search,
},
)
}
if err != nil {
return projects, fmt.Errorf("unable to list projects with search pattern '%s' from the GitLab API : %v", w.Search, err.Error())
}
// Copy relevant settings from wildcard into created project
for _, gp := range gps {
projects = append(
projects,
schemas.Project{
Parameters: w.Parameters,
Name: gp.PathWithNamespace,
},
)
}
if resp.CurrentPage >= resp.TotalPages {
break
}
listOptions.Page = resp.NextPage
}
return projects, nil
}