-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgithub.go
73 lines (59 loc) · 1.49 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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"time"
)
func githubHandler(w http.ResponseWriter, req *http.Request) {
log.Printf("received request: %s", req.URL)
decoder := json.NewDecoder(req.Body)
var api github
err := decoder.Decode(&api)
if err != nil {
panic(err)
}
if api.Zen != nil {
log.Println("received test hook request")
return
}
var commits string
for index, commit := range api.Commits {
t, _ := time.Parse(time.RFC3339, commit.Timestamp)
commitTime := fmt.Sprintf("%d-%02d-%02d %02d:%02d",
t.Year(), t.Month(), t.Day(),
t.Hour(), t.Minute())
commits += fmt.Sprintf("\n%d. [%s](%s)\n_at %s by %s_\n",
index+1, strings.Trim(commit.Message, "\n"),
commit.URL, commitTime, commit.Author.Name)
if len(commit.Added) != 0 {
commits += fmt.Sprintf("`Added:` \n")
for _, f := range commit.Added {
commits += fmt.Sprintf("`— %s`\n", f)
}
}
if len(commit.Modified) != 0 {
commits += fmt.Sprintf("`Modified:` \n")
for _, f := range commit.Modified {
commits += fmt.Sprintf("`— %s`\n", f)
}
}
if len(commit.Removed) != 0 {
commits += fmt.Sprintf("`Removed:` \n")
for _, f := range commit.Removed {
commits += fmt.Sprintf("`— %s`\n", f)
}
}
}
text := fmt.Sprintf("*%s* pushed to *%s/%s* "+
"[Compare changes](%s)\n"+
"%s\n"+
"`Total commits: %d`\n",
api.Pusher.Name, api.Repository.Name, strings.Split(api.Ref, "/")[2],
api.Compare,
commits,
len(api.Commits))
publish(text)
}