-
Notifications
You must be signed in to change notification settings - Fork 41
/
gitlab.go
158 lines (131 loc) · 3.73 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
package main
import (
"fmt"
"net/http"
"net/url"
"os"
"os/user"
"path"
"strconv"
"strings"
"gopkg.in/ini.v1"
)
// GitlabCredentials contains PersonalToken for GitLab API authorization
// and Host for possibly implementing support for self-hosted instances
type GitlabCredentials struct {
Host string
PersonalToken string
}
func (creds GitlabCredentials) query(method, url string) (map[string]interface{}, error) {
req, err := http.NewRequest(method, url, nil)
if err != nil {
return nil, err
}
req.Header.Add("PRIVATE-TOKEN", creds.PersonalToken)
return QueryHTTP(req)
}
func (creds GitlabCredentials) getIssue(repo string, todo Todo) (map[string]interface{}, error) {
json, err := creds.query(
"GET",
// FIXME(#156): possible GitLab API injection attack
"https://"+creds.Host+"/api/v4/projects/"+url.QueryEscape(repo)+"/issues/"+(*todo.ID)[1:]) // self-hosted
if err != nil {
return nil, err
}
return json, nil
}
func (creds GitlabCredentials) postIssue(repo string, todo Todo, body string) (Todo, error) {
params := url.Values{}
params.Add("title", todo.Title)
params.Add("description", body)
json, err := creds.query(
"POST",
"https://"+creds.Host+"/api/v4/projects/"+url.QueryEscape(repo)+"/issues?"+params.Encode()) // self-hosted
if err != nil {
return todo, err
}
id := "#" + strconv.Itoa(int(json["iid"].(float64)))
todo.ID = &id
return todo, err
}
func (creds GitlabCredentials) getHost() string {
return creds.Host
}
// GitlabCredentialsFromFile gets GitlabCredentials from a filepath
func GitlabCredentialsFromFile(filepath string) []GitlabCredentials {
credentials := []GitlabCredentials{}
cfg, err := ini.Load(filepath)
if err != nil {
return credentials
}
for _, section := range cfg.Sections()[1:] {
credentials = append(credentials, GitlabCredentials{
Host: section.Name(),
PersonalToken: section.Key("personal_token").String(),
})
}
return credentials
}
// GitlabCredentialsFromToken returns a GitlabCredentials from a string token
func GitlabCredentialsFromToken(token string) (GitlabCredentials, error) {
credentials := strings.Split(token, ":")
switch len(credentials) {
case 1:
return GitlabCredentials{
Host: "gitlab.com",
PersonalToken: credentials[0],
}, nil
case 2:
return GitlabCredentials{
Host: credentials[0],
PersonalToken: credentials[1],
}, nil
default:
return GitlabCredentials{},
fmt.Errorf("Couldn't parse GitLab credentials from ENV: %s", token)
}
}
func getGitlabCredentials(creds []IssueAPI) []IssueAPI {
tokenEnvar := os.Getenv("GITLAB_PERSONAL_TOKEN")
xdgEnvar := os.Getenv("XDG_CONFIG_HOME")
usr, err := user.Current()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if len(tokenEnvar) != 0 {
for _, credential := range strings.Split(tokenEnvar, ",") {
parsedCredentials, err := GitlabCredentialsFromToken(credential)
if err != nil {
fmt.Fprintln(os.Stderr, err)
continue
}
creds = append(creds, parsedCredentials)
}
}
// custom XDG_CONFIG_HOME
if len(xdgEnvar) != 0 {
filePath := path.Join(xdgEnvar, "snitch/gitlab.ini")
if _, err := os.Stat(filePath); err == nil {
for _, cred := range GitlabCredentialsFromFile(filePath) {
creds = append(creds, cred)
}
}
}
// default XDG_CONFIG_HOME
if len(xdgEnvar) == 0 {
filePath := path.Join(usr.HomeDir, ".config/snitch/gitlab.ini")
if _, err := os.Stat(filePath); err == nil {
for _, cred := range GitlabCredentialsFromFile(filePath) {
creds = append(creds, cred)
}
}
}
filePath := path.Join(usr.HomeDir, ".snitch/gitlab.ini")
if _, err := os.Stat(filePath); err == nil {
for _, cred := range GitlabCredentialsFromFile(filePath) {
creds = append(creds, cred)
}
}
return creds
}