-
Notifications
You must be signed in to change notification settings - Fork 1
/
push.go
93 lines (84 loc) · 2.21 KB
/
push.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
package main
import (
"fmt"
"strings"
)
type githubPushHandler struct{}
func (g *githubPushHandler) tidyPushRefspecs(data *pushhook) {
data.Refspec = g.tidyRefsHeads(data.Refspec)
data.Refspec = g.tidyRefsTags(data.Refspec)
data.Baserefspec = g.tidyRefsHeads(data.Baserefspec)
data.Baserefspec = g.tidyRefsTags(data.Baserefspec)
}
func (g *githubPushHandler) tidyRefsHeads(input string) string {
if strings.HasPrefix(input, "refs/heads/") {
return fmt.Sprintf("branch %s", strings.TrimPrefix(input, "refs/heads/"))
}
return input
}
func (g *githubPushHandler) tidyRefsTags(input string) string {
if strings.HasPrefix(input, "refs/tags/") {
return fmt.Sprintf("tag %s", strings.TrimPrefix(input, "refs/tags/"))
}
return input
}
func (g *githubPushHandler) handlePushEvent(data pushhook) (messages []string) {
g.tidyPushRefspecs(&data)
if data.Created {
return g.handleCreate(data)
} else if data.Deleted {
return g.handleDelete(data)
} else {
return g.handleCommit(data)
}
}
func (g *githubPushHandler) handleDelete(data pushhook) (messages []string) {
messages = append(messages, fmt.Sprintf(
"[%s] %s deleted %s",
data.Repository.FullName,
data.Sender.Login,
data.Refspec,
))
return
}
func (g *githubPushHandler) handleCreate(data pushhook) (messages []string) {
if data.Baserefspec == "" {
messages = append(messages, fmt.Sprintf(
"[%s] %s created %s - %s",
data.Repository.FullName,
data.Pusher.Name,
data.Refspec,
data.CompareLink,
))
} else {
messages = append(messages, fmt.Sprintf(
"[%s] %s created %s from %s - %s",
data.Repository.FullName,
data.Pusher.Name,
data.Refspec,
data.Baserefspec,
data.CompareLink,
))
}
return
}
func (g *githubPushHandler) handleCommit(data pushhook) (messages []string) {
messages = append(messages, fmt.Sprintf(
"[%s] %s pushed %d commits to %s - %s",
data.Repository.FullName,
data.Pusher.Name,
len(data.Commits),
data.Refspec,
data.CompareLink,
))
for _, commit := range data.Commits {
messages = append(messages, fmt.Sprintf(
"[%s] %s committed %s - %s",
data.Repository.FullName,
commit.Author.User,
commit.ID[len(commit.ID)-6:],
strings.SplitN(commit.Message, "\n", 2)[0],
))
}
return
}