/
gitlab.go
170 lines (138 loc) · 3.58 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
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
// Copyright 2024 Daytona Platforms Inc.
// SPDX-License-Identifier: Apache-2.0
package gitprovider
import (
"log"
"strconv"
"github.com/xanzy/go-gitlab"
)
type GitLabGitProvider struct {
token string
baseApiUrl *string
}
func NewGitLabGitProvider(token string, baseApiUrl *string) *GitLabGitProvider {
return &GitLabGitProvider{
token: token,
baseApiUrl: baseApiUrl,
}
}
func (g *GitLabGitProvider) GetNamespaces() ([]*GitNamespace, error) {
client := g.getApiClient()
user, err := g.GetUser()
if err != nil {
return nil, err
}
groupList, _, err := client.Groups.ListGroups(&gitlab.ListGroupsOptions{})
if err != nil {
return nil, err
}
namespaces := []*GitNamespace{}
for _, group := range groupList {
namespaces = append(namespaces, &GitNamespace{
Id: strconv.Itoa(group.ID),
Name: group.Name,
})
}
namespaces = append([]*GitNamespace{{Id: personalNamespaceId, Name: user.Username}}, namespaces...)
return namespaces, nil
}
func (g *GitLabGitProvider) GetRepositories(namespace string) ([]*GitRepository, error) {
client := g.getApiClient()
var response []*GitRepository
var repoList []*gitlab.Project
var err error
if namespace == personalNamespaceId {
user, err := g.GetUser()
if err != nil {
return nil, err
}
repoList, _, err = client.Projects.ListUserProjects(user.Id, &gitlab.ListProjectsOptions{
ListOptions: gitlab.ListOptions{
PerPage: 100,
Page: 1,
},
})
if err != nil {
return nil, err
}
} else {
repoList, _, err = client.Groups.ListGroupProjects(namespace, &gitlab.ListGroupProjectsOptions{
ListOptions: gitlab.ListOptions{
PerPage: 100,
Page: 1,
},
})
if err != nil {
return nil, err
}
}
for _, repo := range repoList {
response = append(response, &GitRepository{
Id: strconv.Itoa(repo.ID),
Name: repo.Name,
Url: repo.WebURL,
})
}
return response, err
}
func (g *GitLabGitProvider) GetRepoBranches(repositoryId string, namespaceId string) ([]*GitBranch, error) {
client := g.getApiClient()
var response []*GitBranch
branches, _, err := client.Branches.ListBranches(repositoryId, &gitlab.ListBranchesOptions{})
if err != nil {
return nil, err
}
for _, branch := range branches {
responseBranch := &GitBranch{
Name: branch.Name,
}
if branch.Commit != nil {
responseBranch.SHA = branch.Commit.ID
}
response = append(response, responseBranch)
}
return response, nil
}
func (g *GitLabGitProvider) GetRepoPRs(repositoryId string, namespaceId string) ([]*GitPullRequest, error) {
client := g.getApiClient()
var response []*GitPullRequest
mergeRequests, _, err := client.MergeRequests.ListProjectMergeRequests(repositoryId, &gitlab.ListProjectMergeRequestsOptions{})
if err != nil {
return nil, err
}
for _, mergeRequest := range mergeRequests {
response = append(response, &GitPullRequest{
Name: mergeRequest.Title,
Branch: mergeRequest.SourceBranch,
})
}
return response, nil
}
func (g *GitLabGitProvider) GetUser() (*GitUser, error) {
client := g.getApiClient()
user, _, err := client.Users.CurrentUser()
if err != nil {
return nil, err
}
userId := strconv.Itoa(user.ID)
response := &GitUser{
Id: userId,
Username: user.Username,
Name: user.Name,
Email: user.Email,
}
return response, nil
}
func (g *GitLabGitProvider) getApiClient() *gitlab.Client {
var client *gitlab.Client
var err error
if g.baseApiUrl == nil {
client, err = gitlab.NewClient(g.token)
} else {
client, err = gitlab.NewClient(g.token, gitlab.WithBaseURL(*g.baseApiUrl))
}
if err != nil {
log.Fatal(err)
}
return client
}