-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
72 lines (59 loc) · 1.4 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
package main
import (
"fmt"
"log"
"os"
"strconv"
"strings"
webhooks "gopkg.in/go-playground/webhooks.v2"
"gopkg.in/go-playground/webhooks.v2/github"
gosxnotifier "github.com/deckarep/gosx-notifier"
)
const (
path = "/payload"
port = 4567
)
func main() {
hook := github.New(&github.Config{Secret: os.Getenv("GITHUB_WEBHOOK_SECRET")})
hook.RegisterEvents(HandleEvent, github.PullRequestEvent)
err := webhooks.Run(hook, ":"+strconv.Itoa(port), path)
if err != nil {
log.Println(err)
}
}
type message struct {
title string
subtitle string
body string
url string
}
func newMessage(pr github.PullRequest, r github.Repository) message {
return message{
title: r.Name,
subtitle: pr.Title,
body: pr.Body,
url: pr.HTMLURL,
}
}
// HandleEvent handles Github event
func HandleEvent(payload interface{}, header webhooks.Header) {
switch payload.(type) {
case github.PullRequestPayload:
pullRequest := payload.(github.PullRequestPayload)
if strings.Contains(pullRequest.PullRequest.Body, fmt.Sprintf("@%s", "tkbky")) {
msg := newMessage(pullRequest.PullRequest, pullRequest.Repository)
notify(msg)
}
}
}
func notify(message message) {
note := gosxnotifier.NewNotification(message.body)
note.Title = message.title
note.Subtitle = message.subtitle
note.Link = message.url
note.AppIcon = "octocat.png"
err := note.Push()
if err != nil {
log.Println(err)
}
}