-
Notifications
You must be signed in to change notification settings - Fork 79
/
github.go
62 lines (45 loc) · 1.11 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
package github
import (
"encoding/json"
"errors"
"fmt"
"net/http"
)
var (
name = "Github"
)
func HandleWebhook(header http.Header, msg string) (string, error) {
hookType := header.Get("X-GitHub-Event")
switch hookType {
case "push":
return githubPushEventHandler(msg)
case "ping":
return githubPingEventHandler(msg)
}
return fmt.Sprintf("收到来自 Github 的 Webhook,暂不支持的类型: %s", hookType), nil
}
func githubPingEventHandler(msg string) (string, error) {
data := &GithubPingEvent{}
err := json.Unmarshal([]byte(msg), &data)
if err != nil {
return "", errors.New("解析Github Ping事件数据失败")
}
return fmt.Sprintf(TemplatePing,
name,
data.Repository.FullName,
data.Repository.HtmlUrl,
), nil
}
func githubPushEventHandler(msg string) (string, error) {
data := &GithubPushEvent{}
err := json.Unmarshal([]byte(msg), &data)
if err != nil {
return "", errors.New("解析Github Push事件数据失败")
}
return fmt.Sprintf(TemplatePush,
name,
data.Pusher.Name, data.Pusher.Email,
data.Repository.Name, len(data.Commits),
data.Compare,
), nil
}