forked from tsoding/snitch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_credentials.go
More file actions
69 lines (56 loc) · 1.61 KB
/
Copy pathgithub_credentials.go
File metadata and controls
69 lines (56 loc) · 1.61 KB
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"gopkg.in/go-ini/ini.v1"
"net/http"
)
// GithubCredentials contains PersonalToken for GitHub API authorization
type GithubCredentials struct {
PersonalToken string
}
// QueryGithubAPI makes a GitHub API query
func (creds GithubCredentials) QueryGithubAPI(method, url string, jsonBody map[string]interface{}) (map[string]interface{}, error) {
client := &http.Client{}
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")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
return nil, fmt.Errorf("GitHub API error: %s", buf.String())
}
var v map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
return nil, err
}
return v, err
}
// 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,
}
}