forked from masutaka/github-nippou
/
settings.go
170 lines (131 loc) · 3.66 KB
/
settings.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
165
166
167
168
169
170
package lib
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
"gopkg.in/yaml.v2"
)
// Settings has configure
type Settings struct {
Format struct {
Subject string
Line string
}
Dictionary struct {
Status struct {
Merged string
Closed string
}
}
URL string
}
// Init initializes Settings
func (s *Settings) Init() error {
var content string
var err error
gistID := getGistID()
if gistID != "" {
ctx := context.Background()
accessToken, err := getAccessToken()
if err != nil {
return err
}
client := getClient(ctx, accessToken)
gist, _, err := client.Gists.Get(ctx, gistID)
if err != nil {
return err
}
content = *gist.Files["settings.yml"].Content
s.URL = *gist.HTMLURL
} else {
content, err = getDefaultSettingsYml()
if err != nil {
return err
}
s.URL = getDefaultSettingsURL()
}
return yaml.Unmarshal([]byte(content), s)
}
func getUser() (string, error) {
if os.Getenv("GITHUB_NIPPOU_USER") != "" {
return os.Getenv("GITHUB_NIPPOU_USER"), nil
}
output, _ := exec.Command("git", "config", "github-nippou.user").Output()
if len(output) >= 1 {
return strings.TrimRight(string(output), "\n"), nil
}
errText := `!!!! GitHub User required. Please execute the following command. !!!!
$ github-nippou init`
return "", errors.New(errText)
}
func getAccessToken() (string, error) {
if os.Getenv("GITHUB_NIPPOU_ACCESS_TOKEN") != "" {
return os.Getenv("GITHUB_NIPPOU_ACCESS_TOKEN"), nil
}
output, _ := exec.Command("git", "config", "github-nippou.token").Output()
if len(output) >= 1 {
return strings.TrimRight(string(output), "\n"), nil
}
errText := `!!!! GitHub Personal access token required. Please execute the following command. !!!!
$ github-nippou init`
return "", errors.New(errText)
}
func getGistID() string {
if os.Getenv("GITHUB_NIPPOU_SETTINGS_GIST_ID") != "" {
return os.Getenv("GITHUB_NIPPOU_SETTINGS_GIST_ID")
}
output, _ := exec.Command("git", "config", "github-nippou.settings-gist-id").Output()
if len(output) == 1 {
return ""
}
return strings.TrimRight(string(output), "\n")
}
func getParallelNum() (int, error) {
if os.Getenv("GITHUB_NIPPOU_THREAD_NUM") != "" {
return strconv.Atoi(os.Getenv("GITHUB_NIPPOU_THREAD_NUM"))
}
output, _ := exec.Command("git", "config", "github-nippou.thread-num").Output()
if len(output) >= 1 {
return strconv.Atoi(strings.TrimRight(string(output), "\n"))
}
return 5, nil
}
func getClient(ctx context.Context, accessToken string) *github.Client {
sts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: accessToken},
)
return github.NewClient(oauth2.NewClient(ctx, sts))
}
func getClientScopes(ctx context.Context, client *github.Client) ([]string, error) {
_, response, err := client.Users.Get(ctx, "")
return strings.Split(response.Header.Get("X-OAuth-Scopes"), ", "), err
}
func createGist(ctx context.Context, client *github.Client) (*github.Gist, *github.Response, error) {
content, err := getDefaultSettingsYml()
if err != nil {
return nil, nil, err
}
gistFiles := make(map[github.GistFilename]github.GistFile, 1)
gistFiles["settings.yml"] = github.GistFile{
Content: github.String(content),
}
gist := &github.Gist{
Description: github.String("github-nippou settings"),
Public: github.Bool(true),
Files: gistFiles,
}
return client.Gists.Create(ctx, gist)
}
func getDefaultSettingsURL() string {
return fmt.Sprintf("https://github.com/masutaka/github-nippou/blob/v%s/config/settings.yml", Version)
}
func getDefaultSettingsYml() (string, error) {
yml, err := Asset("config/settings.yml")
return string(yml), err
}