-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_hook.go
168 lines (143 loc) · 4.62 KB
/
handler_hook.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/chinglinwen/log"
"github.com/davecgh/go-spew/spew"
prettyjson "github.com/hokaccha/go-prettyjson"
"github.com/k0kubun/pp"
"github.com/labstack/echo"
"github.com/tidwall/gjson"
gitlab "github.com/xanzy/go-gitlab"
)
// save log to file, for later access?
// https://github.com/google/logger
func hookHandler(c echo.Context) (err error) {
log.Debug.Println("start hook handler")
payload, err := ioutil.ReadAll(c.Request().Body)
if err != nil {
err = fmt.Errorf("read body err: %v", err)
log.Println(err)
c.JSONPretty(http.StatusBadRequest, E(0, err.Error(), "failed"), " ")
return
}
a := make(map[string]interface{})
err = json.Unmarshal(payload, &a)
if err != nil {
err = fmt.Errorf("unmarshal body err: %v", err)
log.Println(err)
c.JSONPretty(http.StatusBadRequest, E(0, err.Error(), "failed"), " ")
return
}
out, err := prettyjson.Marshal(a)
if err != nil {
err = fmt.Errorf("marshal a err: %v", err)
log.Println(err)
c.JSONPretty(http.StatusBadRequest, E(0, err.Error(), "failed"), " ")
return
}
projectName := gjson.GetBytes(payload, "project.name").String()
if projectName == "config-deploy" || projectName == "self-release" {
log.Println("ignore config-deploy projects")
c.JSONPretty(http.StatusOK, E(0, "ignore config-deploy", "ok"), " ")
return
}
ns := gjson.GetBytes(payload, "project.namespace").String()
log.Printf("got gitlab event for project: %v/%v\n", ns, projectName)
// if ns != "wenzhenglin" && ns != "donglintong" && ns != "yuzongwei" && ns != "robot" {
// log.Println("ignore non-test projects")
// c.JSONPretty(http.StatusOK, E(0, "ignore non-test projects", "ok"), " ")
// return
// }
// eventName, _ := a["event_name"].(string)
eventName := gjson.GetBytes(payload, "event_name").String()
data, err := ParseEvent(eventName, payload)
if err != nil {
err = fmt.Errorf("parse event err: %v", err)
log.Println(err)
c.JSONPretty(http.StatusBadRequest, E(0, err.Error(), "failed"), " ")
return
}
if eventName == "repository_update" {
log.Println("ignore repository_update event")
c.JSONPretty(http.StatusOK, E(0, "ignore repository_update event", "ok"), " ")
return
}
log.Printf("out: %s\n", out)
log.Debug.Println("got event: ", eventName)
event1, ok := data.(*PushEvent)
if ok {
if event1.TotalCommitsCount == 0 {
log.Println("ignore 0 commits event")
return c.JSONPretty(http.StatusOK, E(0, "zero commits", "ok"), " ")
}
if len(event1.Commits) >= 1 {
if strings.Contains(event1.Commits[0].Message, "by self-release") {
log.Println("ignore project init commits event")
return c.JSONPretty(http.StatusOK, E(0, "project init commits", "ok"), " ")
}
}
log.Println("got push event")
log.Printf("commits: %v\n", len(event1.Commits))
err = handlePush(event1)
if err != nil {
err = fmt.Errorf("push release err: %v", err)
log.Println(err)
c.JSONPretty(http.StatusBadRequest, E(0, err.Error(), "failed"), " ")
return
}
}
// tag push event need to remove messge=empty or commitcount=0(except include force keyword? )
event2, ok := data.(*TagPushEvent)
if ok {
// if event2.TotalCommitsCount == 0 {
// log.Println("ignore 0 commits event")
// return c.JSONPretty(http.StatusOK, E(0, "zero commits", "ok"), " ")
// }
// TODO(wen): need to recheck?
// if event2.Message == "" && event2.TotalCommitsCount == 0 {
// log.Println("ignore empty message for tag event")
// return c.JSONPretty(http.StatusOK, E(0, "empty message for tag event", "ok"), " ")
// }
if strings.Contains(event2.Message, "by self-release") {
log.Println("ignore project init commits event")
return c.JSONPretty(http.StatusOK, E(0, "project init commits", "ok"), " ")
}
log.Println("got tag push event")
for _, v := range event2.Commits {
pp.Print("modified", v.Modified)
}
log.Printf("commits: %v\n", len(event2.Commits))
err = handleRelease(event2)
if err != nil {
err = fmt.Errorf("tag release err: %v", err)
log.Println(err)
c.JSONPretty(http.StatusBadRequest, E(0, err.Error(), "failed"), " ")
return
}
}
return c.JSONPretty(http.StatusOK, E(0, "endok", "ok"), " ")
}
func processPushEvent(e *gitlab.PushEvent) {
spew.Dump("event", e)
}
func E(code int, msg, status string) map[string]interface{} {
// log.Println(msg)
return map[string]interface{}{
"code": code,
"message": msg,
"status": status,
}
}
func EData(code int, msg, status string, data interface{}) map[string]interface{} {
// log.Println(msg)
return map[string]interface{}{
"code": code,
"message": msg,
"status": status,
"data": data,
}
}