-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgitlab.go
138 lines (124 loc) · 3.13 KB
/
gitlab.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
package git_backup
import (
"log"
"net/url"
"slices"
"strings"
"github.com/xanzy/go-gitlab"
)
type GitLabConfig struct {
URL string `yaml:"url,omitempty"`
JobName string `yaml:"job_name"`
AccessToken string `yaml:"access_token"`
Starred *bool `yaml:"starred,omitempty"`
Member *bool `yaml:"member,omitempty"`
Owned *bool `yaml:"owned,omitempty"`
Exclude []string `yaml:"exclude,omitempty"`
client *gitlab.Client
}
func (g *GitLabConfig) GetName() string {
return g.JobName
}
func (g *GitLabConfig) Test() error {
user, _, err := g.client.Users.CurrentUser()
if err != nil {
return err
}
log.Printf("Authenticated as with gitlab as: %s", user.Username)
return nil
}
func (g *GitLabConfig) ListRepositories() ([]*Repository, error) {
out := make(map[string]*Repository, 0)
if *g.Starred {
if repos, err := g.getAllRepos(&gitlab.ListProjectsOptions{Starred: boolPointer(true)}); err != nil {
return nil, err
} else {
for _, repo := range repos {
out[repo.FullName] = repo
}
}
}
if *g.Owned {
if repos, err := g.getAllRepos(&gitlab.ListProjectsOptions{Owned: boolPointer(true)}); err != nil {
return nil, err
} else {
for _, repo := range repos {
out[repo.FullName] = repo
}
}
}
if *g.Member {
if repos, err := g.getAllRepos(&gitlab.ListProjectsOptions{Membership: boolPointer(true)}); err != nil {
return nil, err
} else {
for _, repo := range repos {
out[repo.FullName] = repo
}
}
}
outSlice := make([]*Repository, 0, len(out))
for _, repository := range out {
isExcluded := slices.ContainsFunc(g.Exclude, func(s string) bool {
return strings.EqualFold(s, repository.FullName)
})
if isExcluded {
log.Printf("Skipping excluded repository: %s", repository.FullName)
} else {
outSlice = append(outSlice, repository)
}
}
return outSlice, nil
}
func (g *GitLabConfig) getAllRepos(opts *gitlab.ListProjectsOptions) ([]*Repository, error) {
out := make([]*Repository, 0)
for i := 1; true; i++ {
opts.ListOptions.Page = i
repos, _, err := g.getRepos(opts)
if err != nil {
return out, err
}
for _, repo := range repos {
gitUrl, err := url.Parse(repo.HTTPURLToRepo)
if err != nil {
return out, err
}
gitUrl.User = url.UserPassword("git", g.AccessToken)
out = append(out, &Repository{
GitURL: *gitUrl,
FullName: repo.PathWithNamespace,
})
}
if len(repos) == 0 {
break
}
}
return out, nil
}
func (g *GitLabConfig) getRepos(opts *gitlab.ListProjectsOptions) ([]*gitlab.Project, *gitlab.Response, error) {
opts.ListOptions.PerPage = 100
opts.Simple = boolPointer(true)
return g.client.Projects.ListProjects(opts)
}
func (g *GitLabConfig) setDefaults() {
if g.Member == nil {
g.Member = boolPointer(true)
}
if g.Owned == nil {
g.Owned = boolPointer(true)
}
if g.Starred == nil {
g.Starred = boolPointer(true)
}
if g.JobName == "" {
g.JobName = "GitLab"
}
if g.URL == "" {
g.client, _ = gitlab.NewClient(g.AccessToken)
} else {
client, err := gitlab.NewClient(g.AccessToken, gitlab.WithBaseURL(g.URL))
if err != nil {
panic(err)
}
g.client = client
}
}