forked from smfsh/snyk-onboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitlab.go
50 lines (40 loc) · 1.21 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
// File contains functions used to create repositories on GitLab.
package main
import (
"fmt"
"path"
"strings"
"github.com/xanzy/go-gitlab"
"github.com/spf13/viper"
)
func createGitLabRepo(name string) error {
glUser := viper.Get("glUser").(string)
glToken := viper.Get("glKey").(string)
private := gitlab.PublicVisibility // Public by default, or false
urlpath := path.Join(glUser, name)
fmt.Printf("Attempting to create %s in %s's GitLab\n", name, glUser)
client, err := gitlab.NewClient(glToken)
if err != nil {
return err
}
project, _, err := client.Projects.CreateProject(&gitlab.CreateProjectOptions{
Name: gitlab.String(name),
Visibility: gitlab.Visibility(private),
})
if err != nil && strings.Contains(err.Error(), "name: [has already been taken]") {
fmt.Printf("%s already exists in this GitLab account\n", name)
project, _, err = client.Projects.GetProject(urlpath, &gitlab.GetProjectOptions{})
if err != nil {
return err
}
} else if err != nil {
return err
} else {
fmt.Printf("%s created successfully at %s\n", project.Name, project.WebURL)
}
err = pushUpstream(name, "gitlab", project.HTTPURLToRepo, glUser, glToken)
if err != nil {
return err
}
return nil
}