/
github.go
123 lines (108 loc) · 2.2 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
package main
import (
"context"
"net/http"
"os"
"sync"
"github.com/Ken2mer/knmr/logger"
"github.com/google/go-github/github"
"github.com/tcnksm/go-gitconfig"
"github.com/urfave/cli"
"golang.org/x/oauth2"
)
var (
username = "Ken2mer"
reponame = "knmr"
)
var githubCommand = cli.Command{
Name: "github",
Usage: "github",
Action: githubCmd,
}
func githubCmd(ctx *cli.Context) error {
gh := newGithubClient()
fns := []func(){
gh.user,
gh.follows,
gh.code,
gh.events,
gh.subscription,
}
var wg sync.WaitGroup
for _, fn := range fns {
wg.Add(1)
go func(f func()) {
f()
wg.Done()
}(fn)
}
wg.Wait()
return ghServe()
}
type ghClient struct {
ctx context.Context
client *github.Client
}
func newGithubClient() ghClient {
context := context.Background()
return ghClient{
ctx: context,
client: oauth2Client(context),
}
}
func (c *ghClient) subscription() {
subscription, err := getRepositorySubscription(c.ctx, c.client)
if err != nil {
return
}
logger.DumpJSON(subscription)
}
func (c *ghClient) events() {
events, err := getEvents(c.ctx, c.client)
if err != nil {
return
}
dumpEvents(events)
}
func (c *ghClient) code() {
code, err := getCodeSearchResult(c.ctx, c.client)
if err != nil {
return
}
dumpCodeSearchResult(code)
}
func (c *ghClient) follows() {
follows, err := getFollowingUsers(c.ctx, c.client)
if err != nil {
return
}
dumpFollowUsers(follows)
}
func (c *ghClient) user() {
user, err := getUser(c.ctx, c.client)
if err != nil {
return
}
dumpUser(user)
}
// cf. https://github.com/mackerelio/mkr/blob/af4f89ae6fac2290b9fe642de37f84a25de67d62/plugin/github.go
// Get github client having github token.
func oauth2Client(ctx context.Context) *github.Client {
var oauthClient *http.Client
if token := getGithubToken(); token != "" {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: getGithubToken()},
)
oauthClient = oauth2.NewClient(ctx, ts)
}
return github.NewClient(oauthClient)
}
// Get github token from environment variables, or github.token in gitconfig file
func getGithubToken() string {
token := os.Getenv("GITHUB_TOKEN")
if token != "" {
return token
}
token, _ = gitconfig.GithubToken()
return token
}