/
main.go
186 lines (150 loc) · 4.04 KB
/
main.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"encoding/json"
"flag"
"fmt"
"net/url"
"os"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
var (
githubHost = ""
accessToken = ""
owner = ""
repository = ""
sha = ""
context = ""
targeturl = ""
description = ""
status = ""
display = true
)
func init() {
flag.StringVar(&githubHost, "host", "", "Github host. (api endpoint)")
flag.StringVar(&accessToken, "token", "", "Required. Access token")
flag.StringVar(&owner, "owner", "", "Required. Owner. (or Organaization)")
flag.StringVar(&repository, "repo", "", "Required. Repository name.")
flag.StringVar(&sha, "sha", "", "Required. Commit SHA.")
flag.StringVar(&context, "context", "", "A string label to differentiate this status from the status of other systems.")
flag.StringVar(&targeturl, "targeturl", "", "The target URL to associate with this status.")
flag.StringVar(&description, "desc", "", "A short description of the status.")
flag.StringVar(&status, "status", "success", `Required. The state of the status. Can be one of pending, success, error, or failure.`)
flag.BoolVar(&display, "display", true, `Display commit status json.`)
}
func main() {
flag.Parse()
if display {
doDisplay()
return
}
doUpdate()
}
func doDisplay() {
if accessToken == "" || owner == "" || repository == "" || sha == "" {
flag.Usage()
return
}
client := buildClient()
result, _, err := GitCommitStatus(client, owner, repository, sha)
if err != nil {
fmt.Fprintf(os.Stderr, "gh: %v\n", err)
os.Exit(1)
}
printjson(result)
}
func doUpdate() {
if accessToken == "" || owner == "" ||
repository == "" || sha == "" || status == "" {
flag.Usage()
return
}
client := buildClient()
err := updateBuildStatus(client)
if err != nil {
fmt.Fprintf(os.Stderr, "gh: %v\n", err)
os.Exit(1)
}
fmt.Println("gh: successfully updated")
}
func buildClient() *github.Client {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: accessToken},
)
tc := oauth2.NewClient(oauth2.NoContext, ts)
client := github.NewClient(tc)
if githubHost != "" {
var err error
client.BaseURL, err = url.Parse(githubHost)
if err != nil {
fmt.Fprintf(os.Stderr, "gh: %v\n", err)
os.Exit(1)
}
}
return client
}
type CommitStatus struct {
State string `json:"state,omitempty"`
TargetURL string `json:"target_url,omitempty"`
Description string `json:"description,omitempty"`
Context string `json:"context,omitempty"`
}
func updateBuildStatus(client *github.Client) error {
s := &CommitStatus{
State: status,
TargetURL: targeturl,
Description: description,
Context: context,
}
_, _, err := PutGitCommitStatus(client, owner, repository, sha, s)
if err != nil {
return err
}
return nil
}
func PutGitCommitStatus(client *github.Client, owner, repo, sha string, body *CommitStatus) (interface{}, *github.Response, error) {
u := fmt.Sprintf("repos/%v/%v/statuses/%v", owner, repo, sha)
if body == nil {
body = &CommitStatus{}
}
req, err := client.NewRequest("POST", u, body)
if err != nil {
return nil, nil, err
}
var c interface{}
resp, err := client.Do(req, c)
if err != nil {
return nil, resp, err
}
return c, resp, err
}
func GitCommitStatus(client *github.Client, owner, repo, sha string) (interface{}, *github.Response, error) {
u := fmt.Sprintf("repos/%v/%v/commits/%v/status", owner, repo, sha)
req, err := client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
var c interface{}
resp, err := client.Do(req, &c)
if err != nil {
return nil, resp, err
}
return c, resp, err
}
func GitCommitStatuses(client *github.Client, owner, repo, sha string) (interface{}, *github.Response, error) {
u := fmt.Sprintf("repos/%v/%v/commits/%v/statuses", owner, repo, sha)
req, err := client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
var c interface{}
resp, err := client.Do(req, &c)
if err != nil {
return nil, resp, err
}
return c, resp, err
}
func printjson(v interface{}) {
j, _ := json.MarshalIndent(v, "", " ")
fmt.Println(string(j))
}