/
go-gitlab-notifer.go
362 lines (312 loc) · 9.67 KB
/
go-gitlab-notifer.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package main
import (
"encoding/json"
"flag"
"os"
"log"
"os/user"
"io/ioutil"
"github.com/kyokomi/go-gitlab-client/gogitlab"
"github.com/codegangsta/cli"
"time"
"github.com/wsxiaoys/terminal/color"
"github.com/kyokomi/emoji"
"fmt"
"strings"
"os/exec"
)
type GitlabAccessConfig struct {
Host string `json:"host"`
ApiPath string `json:"api_path"`
Token string `json:"token"`
IconPath string `json:"icon_path"`
}
func main() {
app := cli.NewApp()
app.Version = "0.0.3"
app.Name = "go-gitlab-notifer"
app.Usage = "make an explosive entrance"
app.Flags = []cli.Flag {
cli.BoolFlag{"gitlab.skip-cert-check", "If set to true, gitlab client will skip certificate checking for https, possibly exposing your system to MITM attack."},
}
gitlab := createGitlab()
app.Commands = []cli.Command{
{
Name: "issue",
ShortName: "i",
Usage: "my issue list",
Action: func(_ *cli.Context) {
printGitlabIssues(gitlab)
},
},
{
Name: "activity",
ShortName: "a",
Usage: "my activiy list",
Action: func(_ *cli.Context) {
printActivity(gitlab)
},
},
{
Name: "project",
ShortName: "p",
Usage: "my project list",
Action: func(_ *cli.Context) {
printGitlabProjects(gitlab)
},
},
{
Name: "tick",
ShortName: "t",
Usage: "my activity list N seconds tick",
Flags: []cli.Flag{
cli.IntFlag{"second", 60, "second N."},
},
Action: func(c *cli.Context) {
tickGitlabActivity(gitlab, c.Int("second"))
},
},
{
// add v0.0.3
Name: "events",
ShortName: "e",
Usage: "chice project events",
Flags: []cli.Flag{
cli.IntFlag{"project-id", 1, "projectId."},
},
Action: func(c *cli.Context) {
getProjectIssues(gitlab, c.Int("project-id"))
},
},
{
Name: "project_issue",
ShortName: "pi",
Usage: "project issue list",
Flags: []cli.Flag{
cli.IntFlag{"project-id", 1, "projectId."},
},
Action: func(c *cli.Context) {
printGitlabProjectIssues(gitlab, c.Int("project-id"))
},
},
}
app.Run(os.Args)
}
func getProjectIssues(gitlab *gogitlab.Gitlab, projectId int) {
events := gitlab.ProjectEvents(projectId)
for _, event := range events {
var iconName string
switch (event.TargetType) {
case "Issue":
iconName = ":beer:"
default:
iconName = ":punch:"
}
//fmt.Printf("ProjectID[%d] action[%s] targetId[%d] targetType[%s] targetTitle[%s]\n", event.ProductId, event.ActionName,event.TargetId, event.TargetType, event.TargetTitle)
if event.TargetId != 0 {
actionText := color.Sprintf("@y[%s]", event.ActionName)
repositoriesText := color.Sprintf("@c%s(%d)", event.TargetType, event.TargetId)
userText := color.Sprintf("@c%s", event.Data.UserName)
titleText := color.Sprintf("@g%s", event.TargetTitle)
emoji.Println("@{"+iconName+"}", actionText, repositoriesText, userText, titleText)
} else if event.TargetId == 0 {
actionText := color.Sprintf("@y[%s]", event.ActionName)
repositoriesText := color.Sprintf("@c%s", event.Data.Repository.Name)
userText := color.Sprintf("@c%s", event.Data.UserName)
var titleText string
if event.Data.TotalCommitsCount > 0 {
commitMessage := event.Data.Commits[0].Message
commitMessage = strings.Replace(commitMessage, "\n\n", "\t", -1)
titleText = color.Sprintf("@g%s", commitMessage)
} else if event.Data.Before == "0000000000000000000000000000000000000000" {
titleText = color.Sprintf("@g%s %s", emoji.Sprint("@{:fire:}"), "create New branch")
}
emoji.Println("@{"+iconName+"}", actionText, repositoriesText, userText, titleText)
// fmt.Println(" \t user -> ", event.Data.UserName, event.Data.UserId)
// fmt.Println(" \t author -> ", event.Data.AuthorId)
//
// fmt.Println(" \t\t name -> ", event.Data.Repository.Name)
// fmt.Println(" \t\t description -> ", event.Data.Repository.Description)
// fmt.Println(" \t\t gitUrl -> ", event.Data.Repository.GitUrl)
// fmt.Println(" \t\t pageUrl -> ", event.Data.Repository.PageUrl)
//
// fmt.Println(" \t\t totalCount -> ", event.Data.TotalCommitsCount)
//
// if event.Data.TotalCommitsCount > 0 {
// fmt.Println(" \t\t message -> ", event.Data.Commits[0].Message)
// fmt.Println(" \t\t time -> ", event.Data.Commits[0].Timestamp)
// }
}
}
//
// for _, event := range events {
//
// }
}
func tickGitlabActivity(gitlab *gogitlab.Gitlab, tickSecond int) {
ch := time.Tick(time.Second * time.Duration(10))
lastedFeed, err := gitlab.Activity()
if err != nil {
log.Fatal(err.Error())
return
}
currentUser, err := gitlab.CurrentUser()
if err != nil {
log.Fatal(err.Error())
return
}
for i := len(lastedFeed.Entries) - 1; i >= 0; i-- {
feedCommentText := createFeedCommentText(¤tUser, lastedFeed.Entries[i])
fmt.Println(feedCommentText)
}
for now := range ch {
emoji.Printf("@{:beer:}[%v] tick \n", now)
feed, err := gitlab.Activity()
if err != nil {
log.Fatal(err.Error())
return
}
for i := len(feed.Entries) - 1; i >= 0; i-- {
feedComment := feed.Entries[i]
if feedComment.Updated.After(lastedFeed.Entries[0].Updated) {
feedCommentText := createFeedShortCommentText(feedComment)
fmt.Println(feedCommentText)
Notifier(feedCommentText, feedComment.Id, feedComment.Summary, "")
}
}
lastedFeed = feed
}
}
// 自分の関わってるプロジェクトを取得して一覧表示する
func printGitlabProjects(gitlab *gogitlab.Gitlab) {
projects, err := gitlab.Projects()
if err != nil {
log.Fatal(err.Error())
return
}
for _, project := range projects {
fmt.Printf("[%4d] [%20s] (%s)\n", project.Id, project.Name, project.HttpRepoUrl)
}
}
// 自分のIssueを取得して一覧表示する
func printGitlabIssues(gitlab *gogitlab.Gitlab) {
issues, err := gitlab.Issues()
if err != nil {
log.Fatal(err.Error())
return
}
for _, issue := range issues {
fmt.Printf("[%4d] [%s]\n", issue.Id, issue.Title)
}
}
func printGitlabProjectIssues(gitlab *gogitlab.Gitlab, projectId int) {
page := 1
for {
issues, err := gitlab.ProjectIssues(projectId, page)
if err != nil {
log.Fatal(err.Error())
return
}
if len(issues) == 0 {
break
}
for _, issue := range issues {
if issue.State == "closed" {
color.Printf("@r[%4d(%d)] [%s] %s state=%s\n", issue.Id, issue.LocalId, issue.Title, issue.Description, issue.State)
} else {
color.Printf("@g[%4d(%d)] [%s] %s state=%s\n", issue.Id, issue.LocalId, issue.Title, issue.Description, issue.State)
}
}
page++
}
}
// Activity(dashboard.atpm)の表示。XMLパースなので情報欠落が辛い感じ。廃止予定
func printActivity(gitlab *gogitlab.Gitlab) {
feed, err := gitlab.Activity()
if err != nil {
log.Fatal(err.Error())
return
}
currentUser, err := gitlab.CurrentUser()
if err != nil {
log.Fatal(err.Error())
return
}
for _, feedCommit := range feed.Entries {
feedCommentText := createFeedCommentText(¤tUser, feedCommit)
fmt.Println(feedCommentText)
}
}
// 絵文字付きターミナル用のコメント作成(Activiyベース)
func createFeedCommentText(currentUser *gogitlab.User, feedCommit *gogitlab.FeedCommit) string {
var iconName string
if strings.Contains(feedCommit.Title, "commented") {
iconName = ":speech_balloon:"
} else if strings.Contains(feedCommit.Title, "pushed") {
iconName = ":punch:"
} else if strings.Contains(feedCommit.Title, "closed") {
iconName = ":white_check_mark:"
} else if strings.Contains(feedCommit.Title, "opened") {
iconName = ":fire:"
} else if strings.Contains(feedCommit.Title, "accepted") {
iconName = ":atm:"
} else {
iconName = ":beer:"
}
if currentUser.Name == feedCommit.Author.Name {
return emoji.Sprint("@{"+iconName+"}", color.Sprintf("@y[%s] %s", feedCommit.Updated.Format(time.ANSIC), feedCommit.Title))
} else {
return emoji.Sprint("@{"+iconName+"}", fmt.Sprintf("[%s] %s", feedCommit.Updated.Format(time.ANSIC), feedCommit.Title))
}
}
// 絵文字付き通知用の簡易コメント作成(Activiyベース)
func createFeedShortCommentText(feedCommit *gogitlab.FeedCommit) string {
var iconName string
if strings.Contains(feedCommit.Title, "commented") {
iconName = ":speech_balloon:"
} else if strings.Contains(feedCommit.Title, "pushed") {
iconName = ":punch:"
} else if strings.Contains(feedCommit.Title, "closed") {
iconName = ":white_check_mark:"
} else if strings.Contains(feedCommit.Title, "opened") {
iconName = ":fire:"
} else if strings.Contains(feedCommit.Title, "accepted") {
iconName = ":atm:"
} else {
iconName = ":beer:"
}
return emoji.Sprint("@{"+iconName+"}", fmt.Sprintf("%s", feedCommit.Title))
}
// Gitlabクライアントを作成する
func createGitlab() *gogitlab.Gitlab {
config := readGitlabAccessTokenJson()
// --gitlab.skip-cert-checkを読み込む
flag.Parse()
return gogitlab.NewGitlab(config.Host, config.ApiPath, config.Token)
}
// アクセストークンを保存してるローカルファイルを読み込んで返却
func readGitlabAccessTokenJson() GitlabAccessConfig {
usr, err := user.Current()
if err != nil {
log.Fatal( err )
os.Exit(1)
}
file, err := ioutil.ReadFile(usr.HomeDir + "/.ggn/config.json")
if err != nil {
log.Fatalf("Config file error: %v\n", err)
os.Exit(1)
}
var config GitlabAccessConfig
json.Unmarshal(file, &config)
config.IconPath = usr.HomeDir + "/.ggn/logo.png"
return config
}
// 通知のやつ $ brew install terminal-notiferが必要
func Notifier(title, message, subTitle, openUrl string) {
config := readGitlabAccessTokenJson()
cmd := exec.Command("terminal-notifier", "-title", title, "-message", message, "-subtitle", subTitle, "-appIcon", config.IconPath, "-open", openUrl)
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
}