-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgitlab.go
289 lines (241 loc) · 8.06 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package main
import(
"os"
"fmt"
"path"
"log"
"github.com/forj-oss/goforjj"
"github.com/xanzy/go-gitlab"
)
//gitlabConnect connect user to gitlab (TODO)
func (gls *GitlabPlugin) gitlabConnect(server string, ret *goforjj.PluginData) *gitlab.Client {
//
gls.Client = gitlab.NewClient(nil, gls.token)
//Set url
if err := gls.gitlabSetUrl(server); err != nil{
ret.Errorf("Invalid url. %s", err)
return nil
}
//check connected
_, _, err := gls.Client.Users.CurrentUser()
if err != nil {
ret.Errorf("Unable to get the owner of the token given.", err)
return nil
} else {
ret.StatusAdd("Connection successful.")
//g.user = ...
}
return gls.Client
}
//InitGroup TODO production group
func (req *CreateReq) InitGroup(gls *GitlabPlugin) (ret bool) {
if app, found := req.Objects.App[req.Forj.ForjjInstanceName]; found{
gls.SetGroup(app)
ret = true
}
return
}
//InitGroup Same for now, TODO production group
func (req *UpdateReq) InitGroup(gls *GitlabPlugin) (ret bool) {
if app, found := req.Objects.App[req.Forj.ForjjInstanceName]; found{
gls.SetGroup(app)
ret = true
}
return
}
//SetGroup ...
func (gls *GitlabPlugin) SetGroup(fromApp AppInstanceStruct) {
if group := fromApp.Group; group == ""{
gls.gitlabDeploy.Group = fromApp.ForjjGroup
} else {
gls.gitlabDeploy.Group = group
}
if group := fromApp.ProductionGroup; group == ""{
gls.gitlabDeploy.ProdGroup = fromApp.ForjjGroup
} else {
gls.gitlabDeploy.ProdGroup = group
}
gls.gitlabSource.ProdGroup = gls.gitlabDeploy.ProdGroup
}
//ensureGroupExists (TODO)
func (gls *GitlabPlugin) ensureGroupExists(ret *goforjj.PluginData) (s bool){
//Ensure Group exist, todo: if not it is created.
//Ensure user is owner (or same).
if gls.gitlabDeploy.Group == "" {
ret.Errorf("Invalid group. The group is empty")
return
}
s = false
//Try to get group
groups, _, err := gls.Client.Groups.SearchGroup(gls.gitlabDeploy.Group)
if err != nil{
log.Printf(ret.Errorf("Unable to get '%s' group information. %s", gls.gitlabDeploy.Group, err))
return
}
for _, group := range groups{
if group.Name == gls.gitlabDeploy.Group {
//Set GroupID
gls.gitlabDeploy.GroupId = group.ID
//Ensure user is owner (todo)
log.Printf(ret.StatusAdd("'%s' group access verified", gls.gitlabDeploy.Group))
return true
}
}
//Need to create the group (todo --> create for user)
log.Printf(ret.Errorf("'%s' group need to be created. "))
return
}
//IsNewForge ...
func (gls *GitlabPlugin) IsNewForge(ret *goforjj.PluginData) (_ bool){
ClientProjects := gls.Client.Projects
for name, project := range gls.gitlabDeploy.Projects{
if !project.Infra{
continue
}
client, _, _ := gls.Client.Users.CurrentUser() //!\\ manage err
URLEncPathProject := client.Username + "/" + name
if _, resp, e := ClientProjects.GetProject(URLEncPathProject); e!= nil && resp == nil {
ret.Errorf("Unable to identify the infra project. Unknown issue: %s",e)
return
} else {
gls.newForge = (resp.StatusCode != 200)
}
return true
}
ret.Errorf("Unable to identify the infra repository. At least, one repo must be identified with "+"`%s` in %s. You can use Forjj update to fix this.","Infra: true", "github")
return
}
//gitlabSetUrl (TODO: server)
func (gls *GitlabPlugin) gitlabSetUrl(server string) (err error) {
glUrl := ""
if gls.gitlabSource.Urls == nil {
gls.gitlabSource.Urls = make(map[string]string)
}
if !gls.maintainCtxt {
if server == "" { // || ?
gls.gitlabSource.Urls["gitlab-base-url"] = "https://gitlab.com/"
gls.gitlabSource.Urls["gitlab-url"] = "https://gitlab.com"
gls.gitlabSource.Urls["gitlab-ssh"] = "[email protected]:"
} else {
//set from serveur // ! \\ TODO
server = "gitlab.com"
glUrl = "https://" + server + "/api/v4/"
gls.gitlabSource.Urls["gitlab-url"] = "https://gitlab.com"
gls.gitlabSource.Urls["gitlab-ssh"] = "[email protected]:"
}
} else {
//maintain context
gls.gitlabSource.Urls = gls.gitlabDeploy.Urls
glUrl = gls.gitlabSource.Urls["gitlab-base-url"]
}
if glUrl == ""{
return
}
err = gls.Client.SetBaseURL(glUrl)
if err != nil{
return
}
return
}
//ensureExists (TODO UPDATE and group management)
func (r *ProjectStruct) ensureExists(gls *GitlabPlugin, ret *goforjj.PluginData) error {
//test existence
clientProjects := gls.Client.Projects
//client, _, err := gls.Client.Users.CurrentUser() // Get current user
URLEncPathProject := gls.gitlabDeploy.Group + "/" + r.Name // UserName/ProjectName or Group/ProjectName
_, _, err := clientProjects.GetProject(URLEncPathProject)
if err != nil {
//if does'nt exists --> Create
ABM := 0
projectOptions := &gitlab.CreateProjectOptions{
Name: &r.Name,
NamespaceID: &gls.gitlabDeploy.GroupId,
ApprovalsBeforeMerge: &ABM, //without: request error because is set to null (restriction SQL: not null)
}
_, _, e := gls.Client.Projects.CreateProject(projectOptions)
if e != nil{
ret.Errorf("Unable to create '%s'. %s.", r.Name, e)
return e
}
log.Printf(ret.StatusAdd("Repo '%s': created", r.Name))
} else {
//Update TODO
}
//...
return nil
}
//projectExists (TODO)
func (gls *GitlabPlugin) projectsExists(ret *goforjj.PluginData) (err error) {
clientProjects := gls.Client.Projects // Projects of user
//client, _, err := gls.Client.Users.CurrentUser() // Get current user
//loop
for name, projectData := range gls.gitlabDeploy.Projects{
URLEncPathProject := gls.gitlabDeploy.Group + "/" + name // client.Username = UserName/ProjectName or gls... = Group/ProjectName
//Get X repo, if find --> err
if foundProject, _, e := clientProjects.GetProject(URLEncPathProject); e == nil{
if err == nil && name == foundProject.Name {
err = fmt.Errorf("Infra projects '%s' already exist in gitlab server.", name)
}
projectData.exist = true
if projectData.remotes == nil{
projectData.remotes = make(map[string]goforjj.PluginRepoRemoteUrl)
projectData.branchConnect = make(map[string]string)
}
projectData.remotes["origin"] = goforjj.PluginRepoRemoteUrl{
Ssh: foundProject.SSHURLToRepo,
Url: foundProject.HTTPURLToRepo,
}
projectData.branchConnect["master"] = "origin/master"
}
ret.Repos[name] = goforjj.PluginRepo{ //Project
Name: projectData.Name,
Exist: projectData.exist,
Remotes: projectData.remotes,
BranchConnect: projectData.branchConnect,
//Owner: projectData.Owner,
}
}
return
}
//checkSourcesExistence (TODO UPDATE)
func (gls *GitlabPlugin) checkSourcesExistence(when string) (err error){
log.Print("Checking Infrastructure code existence.")
sourceProject := gls.sourcePath
sourcePath := path.Join(sourceProject, gls.instance)
gls.sourceFile = path.Join(sourcePath, gitlabFile)
deployProject := path.Join(gls.deployMount, gls.deployTo)
deployBase := path.Join(deployProject, gls.instance)
gls.deployFile = path.Join(deployBase, gitlabFile)
gls.gitFile = path.Join(gls.instance, gitlabFile)
switch when {
case "create":
if _, err := os.Stat(sourcePath); err != nil{
if err = os.MkdirAll(sourcePath, 0755); err != nil{
return fmt.Errorf("Unable to create '%s'. %s", sourcePath, err)
}
}
if _, err := os.Stat(deployProject); err != nil{
return fmt.Errorf("Unable to create '%s'. Forjj must create it. %s", deployProject, err)
}
if _, err := os.Stat(gls.sourceFile); err == nil{
return fmt.Errorf("Unable to create the gitlab configuration which already exist.\nUse 'update' to update it "+"(or update %s), and 'maintain' to update your github service according to his configuration.",path.Join(gls.instance, gitlabFile))
}
if _, err := os.Stat(deployBase); err != nil{
if err = os.Mkdir(deployBase, 0755); err != nil{
return fmt.Errorf("Unable to create '%s'. %s", deployBase, err)
}
}
return
case "update":
if _, err := os.Stat(deployBase); err != nil {
if err = os.Mkdir(deployBase, 0755); err != nil {
return fmt.Errorf("Unable to create '%s'. %s", deployBase, err)
}
}
if _, err := os.Stat(gls.sourceFile); err != nil {
return fmt.Errorf("Unable to update the gitlab configuration which doesn't exist.\n" + "Use 'create' to create it or clone it")
}
return
}
return
}