-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathprojects.go
110 lines (96 loc) · 2.67 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
package gitlab
import (
"fmt"
"github.com/mvisonneau/gitlab-ci-pipelines-exporter/pkg/schemas"
"github.com/openlyinc/pointy"
log "github.com/sirupsen/logrus"
"github.com/xanzy/go-gitlab"
goGitlab "github.com/xanzy/go-gitlab"
)
// GetProject ..
func (c *Client) GetProject(name string) (*goGitlab.Project, error) {
log.WithFields(log.Fields{
"project-name": name,
}).Debug("reading project")
c.rateLimit()
p, _, err := c.Projects.GetProject(name, &goGitlab.GetProjectOptions{})
return p, err
}
// ListProjects ..
func (c *Client) ListProjects(w schemas.Wildcard) ([]schemas.Project, error) {
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 := gitlab.ListOptions{
Page: 1,
PerPage: 100,
}
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,
},
)
case "group":
gps, resp, err = c.Groups.ListGroupProjects(
w.Owner.Name,
&gitlab.ListGroupProjectsOptions{
Archived: &w.Archived,
WithShared: pointy.Bool(false),
IncludeSubgroups: &w.Owner.IncludeSubgroups,
ListOptions: listOptions,
Search: &w.Search,
},
)
default:
// List all visible projects
gps, resp, err = c.Projects.ListProjects(
&gitlab.ListProjectsOptions{
ListOptions: listOptions,
Archived: &w.Archived,
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 {
if !gp.JobsEnabled {
log.WithFields(logFields).WithFields(log.Fields{
"project-id": gp.ID,
"project-name": gp.PathWithNamespace,
}).Debug("jobs/pipelines not enabled on project, ignoring")
continue
}
projects = append(
projects,
schemas.Project{
ProjectParameters: w.ProjectParameters,
Name: gp.PathWithNamespace,
},
)
}
if resp.CurrentPage >= resp.TotalPages {
break
}
listOptions.Page = resp.NextPage
}
return projects, nil
}