-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.go
83 lines (73 loc) · 2.39 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
package main
import (
"fmt"
"strings"
"time"
"log"
"github.com/google/go-github/github"
"github.com/pkg/errors"
gitconfig "github.com/tcnksm/go-gitconfig"
"github.com/tidwall/gjson"
"golang.org/x/oauth2"
)
// NewGitHubClient go-github のクライアント作成
func NewGitHubClient() *github.Client {
token, err := gitconfig.Global("github.token")
if err != nil {
log.Fatalln(errors.Wrap(err, "get github token failed"))
}
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(oauth2.NoContext, ts)
return github.NewClient(tc)
}
// GetEvents GitHub API から自分のイベントを取得
func GetEvents(client *github.Client, org *string) {
options := github.ListOptions{Page: 1, PerPage: 50}
user, _, err := client.Users.Get(oauth2.NoContext, "")
if err != nil {
log.Fatalln(errors.Wrap(err, "get users failed"))
}
events, _, err := client.Activity.ListEventsPerformedByUser(oauth2.NoContext, user.GetLogin(), false, &options)
if err == nil {
SieveOutEvents(events, org)
} else {
log.Fatalln(errors.Wrap(err, "get events failed"))
}
}
// SieveOutEvents flag によって出すイベントを絞る
func SieveOutEvents(events []*github.Event, org *string) {
jst, _ := time.LoadLocation("Asia/Tokyo")
today := time.Now()
const layout = "2006-01-02"
for _, value := range events {
// API から取ってきた CreatedAt の文字列に、コマンド叩いた日付が含まれていれば表示
if strings.Contains(value.CreatedAt.In(jst).String(), string(today.Format(layout))) {
// organization が指定されていたらその organization のイベントだけ出力
if *org != "" && !strings.Contains(*value.Repo.Name, *org) {
continue
}
payload, _ := value.ParsePayload()
// 特定のイベントだけタイトルを表示
switch *value.Type {
case "PullRequestEvent":
pr, ok := payload.(*github.PullRequestEvent)
if !ok {
log.Fatalln("Failed type assertion")
}
fmt.Println(*value.Repo.Name, *value.Type, *pr.PullRequest.Title)
case "IssuesEvent":
issue, ok := payload.(*github.IssuesEvent)
if !ok {
log.Fatalln("Failed type assertion")
}
fmt.Println(*value.Repo.Name, *value.Type, *issue.Issue.Title)
default:
json, _ := value.RawPayload.MarshalJSON()
action := gjson.Get(string(json), "action")
fmt.Println(*value.Repo.Name, *value.Type, action)
}
}
}
}