-
Notifications
You must be signed in to change notification settings - Fork 41
/
github.go
126 lines (103 loc) · 2.93 KB
/
github.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"gopkg.in/ini.v1"
"net/http"
"os"
"os/user"
"path"
"strconv"
)
// GithubCredentials contains PersonalToken for GitHub API authorization
type GithubCredentials struct {
PersonalToken string
}
func (creds GithubCredentials) 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 GithubCredentials) getIssue(repo string, todo Todo) (map[string]interface{}, error) {
json, err := creds.query(
"GET",
// FIXME(#59): possible GitHub API injection attack
"https://api.github.com/repos/"+repo+"/issues/"+(*todo.ID)[1:],
nil)
if err != nil {
return nil, err
}
return json, nil
}
func (creds GithubCredentials) postIssue(repo string, todo Todo, body string) (Todo, error) {
json, err := creds.query(
"POST",
"https://api.github.com/repos/"+repo+"/issues",
map[string]interface{}{
"title": todo.Title,
"body": body,
})
if err != nil {
return todo, err
}
id := "#" + strconv.Itoa(int(json["number"].(float64)))
todo.ID = &id
return todo, err
}
func (creds GithubCredentials) getHost() string {
return "github.com"
}
// GithubCredentialsFromFile gets GithubCredentials from a filepath
func GithubCredentialsFromFile(filepath string) (GithubCredentials, error) {
cfg, err := ini.Load(filepath)
if err != nil {
return GithubCredentials{}, err
}
return GithubCredentials{
PersonalToken: cfg.Section("github").Key("personal_token").String(),
}, nil
}
// GithubCredentialsFromToken returns a GithubCredentials from a string token
func GithubCredentialsFromToken(token string) GithubCredentials {
return GithubCredentials{
PersonalToken: token,
}
}
func getGithubCredentials() (GithubCredentials, error) {
tokenEnvar := os.Getenv("GITHUB_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 {
return GithubCredentialsFromToken(tokenEnvar), nil
}
// custom XDG_CONFIG_HOME
if len(xdgEnvar) != 0 {
filePath := path.Join(xdgEnvar, "snitch/github.ini")
if _, err := os.Stat(filePath); err == nil {
return GithubCredentialsFromFile(filePath)
}
}
// default XDG_CONFIG_HOME
if len(xdgEnvar) == 0 {
filePath := path.Join(usr.HomeDir, ".config/snitch/github.ini")
if _, err := os.Stat(filePath); err == nil {
return GithubCredentialsFromFile(filePath)
}
}
filePath := path.Join(usr.HomeDir, ".snitch/github.ini")
if _, err := os.Stat(filePath); err == nil {
return GithubCredentialsFromFile(filePath)
}
return GithubCredentials{}, fmt.Errorf("GitHub token is missing")
}