-
Notifications
You must be signed in to change notification settings - Fork 41
/
gitea.go
164 lines (137 loc) · 3.86 KB
/
gitea.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"os/user"
"path"
"strconv"
"strings"
"gopkg.in/ini.v1"
)
// GiteaCredentials contains PersonalToken for gitea API authorization
// and Host for possibly implementing support for self-hosted instances
type GiteaCredentials struct {
Host string
PersonalToken string
}
func (creds GiteaCredentials) query(method, url string, jsonBody map[string]interface{}) (map[string]interface{}, error) {
bodyBuffer := new(bytes.Buffer)
err := json.NewEncoder(bodyBuffer).Encode(jsonBody)
req, err := http.NewRequest(method, url, bodyBuffer)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", "token "+creds.PersonalToken)
req.Header.Add("Content-Type", "application/json")
return QueryHTTP(req)
}
func (creds GiteaCredentials) getIssue(repo string, todo Todo) (map[string]interface{}, error) {
// FIXME(#187): gitea integration does not support http instances.
json, err := creds.query(
"GET",
"https://"+creds.Host+"/api/v1/repos/"+repo+"/issues/"+(*todo.ID)[1:],
nil) // self-hosted
if err != nil {
return nil, err
}
return json, nil
}
func (creds GiteaCredentials) postIssue(repo string, todo Todo, body string) (Todo, error) {
json, err := creds.query(
"POST",
"https://"+creds.Host+"/api/v1/repos/"+repo+"/issues",
map[string]interface{}{
"title": todo.Title,
"body": body,
}) // self-hosted
if err != nil {
return todo, err
}
id := "#" + strconv.Itoa(int(json["number"].(float64)))
todo.ID = &id
return todo, err
}
func (creds GiteaCredentials) getHost() string {
return creds.Host
}
// GiteaCredentialsFromFile gets GiteaCredentials from a filepath
func GiteaCredentialsFromFile(filepath string) []GiteaCredentials {
credentials := []GiteaCredentials{}
cfg, err := ini.Load(filepath)
if err != nil {
return credentials
}
for _, section := range cfg.Sections()[1:] {
credentials = append(credentials, GiteaCredentials{
Host: section.Name(),
PersonalToken: section.Key("access_token").String(),
})
}
return credentials
}
// GiteaCredentialsFromToken returns a GiteaCredentials from a string token
func GiteaCredentialsFromToken(token string) (GiteaCredentials, error) {
credentials := strings.Split(token, ":")
switch len(credentials) {
case 2:
return GiteaCredentials{
Host: credentials[0],
PersonalToken: credentials[1],
}, nil
case 3:
return GiteaCredentials{
Host: credentials[0] + ":" + credentials[1],
PersonalToken: credentials[2],
}, nil
default:
return GiteaCredentials{},
fmt.Errorf("Couldn't parse gitea credentials from ENV: %s", token)
}
}
func getGiteaCredentials(creds []IssueAPI) []IssueAPI {
tokenEnvar := os.Getenv("GITEA_ACCESS_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 := GiteaCredentialsFromToken(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/gitea.ini")
if _, err := os.Stat(filePath); err == nil {
for _, cred := range GiteaCredentialsFromFile(filePath) {
creds = append(creds, cred)
}
}
}
// default XDG_CONFIG_HOME
if len(xdgEnvar) == 0 {
filePath := path.Join(usr.HomeDir, ".config/snitch/gitea.ini")
if _, err := os.Stat(filePath); err == nil {
for _, cred := range GiteaCredentialsFromFile(filePath) {
creds = append(creds, cred)
}
}
}
filePath := path.Join(usr.HomeDir, ".snitch/gitea.ini")
if _, err := os.Stat(filePath); err == nil {
for _, cred := range GiteaCredentialsFromFile(filePath) {
creds = append(creds, cred)
}
}
return creds
}