-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.go
289 lines (250 loc) · 6.8 KB
/
api.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package httpinterface
import (
"fmt"
"html/template"
"net/http"
"strings"
"time"
"ghhooks.com/hook/core"
"ghhooks.com/hook/jobqueue"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
)
// DONE: verify signature before accepting webhook
// DONE: verify if branch is correct
// TODO: log with levels and cli flags
// TODO: think about error stack traces probably
// TODO: brainstorm about proxy server that will send to all the agents running on different servers (woold need CORS)
// TODO: golang ssh client is also an option
// DONE: create global resultmap in core package for keeping track of build results
// DONE: status route
// TODO: github commit status
// TODO: blocking build run
// DONE: html page for status
// TODO: maybe put password on status page to prevent from builds being cancelled by just anyone
// DONE: update progressbar using websockets,
// DONE: individual step results on statuspage
// TODO: investigate what happens if websocket events come faster than the time it takes for event to process
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
const (
SUCCESS_MARK = "✓"
PENDING_MARK = "❍"
FAILED_MARK = "✗"
)
type Step struct {
Command string `json:"command"`
Status string `json:"status"`
CommandOutput string `json:"commandOutput"`
Description string `json:"description"`
}
type StatusResponse struct {
core.JobState
ProjectName string `json:"projectName"`
DateTimeString string `json:"dateTimeString"`
Coverage float64 `json:"coverage"`
WebSocketRoute template.URL `json:"websocketRoute"`
Steps []Step `json:"steps"`
}
type WebsocketResponse struct {
core.JobState
Coverage float64 `json:"coverage"`
ProjectName string `json:"projectName"`
}
func WebHookListener(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
projectID, ok := vars["project"]
if !ok {
Respond(w, 400, map[string]interface{}{
"error": "no vars found",
})
return
}
project, ok := core.ServerConf.Project[projectID]
if !ok {
Respond(w, 400, map[string]interface{}{
"error": "no project found with given project name",
})
return
}
bodyInBytes, err := StreamToByte(r.Body)
if err != nil {
Respond(w, 400, map[string]interface{}{
"error": err.Error(),
})
return
}
hash := r.Header.Get("X-Hub-Signature-256")
if hash != "" {
verified, err := VerifySignature(bodyInBytes, hash, project.Secret)
if err != nil {
Respond(w, 500, map[string]any{
"error": err.Error(),
})
return
}
if !verified {
Respond(w, 412, map[string]any{
"error": "signauture could not be verified",
})
return
}
}
eventType := r.Header.Get("X-GitHub-Event")
err = VerifyEvent(eventType, bodyInBytes, project.Branch)
if err != nil {
Respond(w, 400, map[string]interface{}{
"error": fmt.Sprintf("error: %v.", err),
})
return
}
// enqueing job
enqueueStatus := core.Queues.Enqueue(projectID, jobqueue.Job{
Name: "build",
Action: core.Job,
Args: []any{
projectID,
project,
},
})
if !enqueueStatus {
Respond(
w, 429, map[string]interface{}{
"error": "build queue is full",
},
)
return
}
Respond(w, 201, map[string]interface{}{
"message": "build queued successfully",
})
}
func BuildStatus(w http.ResponseWriter, r *http.Request) {
//NOTE: to debug script on statuspage add //# sourceURL=statuspage at end of script above closing tag
vars := mux.Vars(r)
projectID, ok := vars["project"]
if !ok {
Respond(w, 400, map[string]interface{}{
"error": "no vars found",
})
return
}
project, ok := core.ServerConf.Project[projectID]
if !ok {
Respond(w, 400, map[string]interface{}{
"error": "no project found with given project name",
})
return
}
totalSteps := len(project.Steps)
if totalSteps == 0 {
Respond(w, http.StatusBadRequest, map[string]interface{}{
"error": "no build steps configured",
})
return
}
core.ResultMap.Mu.RLock()
result, ok := core.ResultMap.Map[projectID]
core.ResultMap.Mu.RUnlock()
if !ok {
Respond(w, http.StatusBadRequest, map[string]interface{}{
"error": "no build have been run yet, or nothing to report on the project",
})
return
}
var successfullSteps int
for _, v := range result.StepResults {
if v.Error == nil {
successfullSteps = successfullSteps + 1
}
}
coverage := float64(successfullSteps * 100 / totalSteps)
format := r.URL.Query().Get("format")
if format == "json" {
Respond(w, 200, result)
return
}
projectSteps := make([]Step, 0)
for i, step := range project.Steps {
step_ := Step{
Command: strings.Join(step, " "),
Status: PENDING_MARK,
}
if i <= len(result.StepResults)-1 {
res := result.StepResults[i]
step_.CommandOutput = res.Output
step_.Description = res.Description
if res.Error != nil {
step_.Status = FAILED_MARK
} else {
step_.Status = SUCCESS_MARK
}
}
projectSteps = append(projectSteps, step_)
}
templateResponse := StatusResponse{
JobState: result,
ProjectName: projectID,
DateTimeString: result.LastBuildStart.Format(time.RFC3339),
Coverage: coverage,
WebSocketRoute: template.URL(fmt.Sprintf("ws://%s/%s/livestatus", r.Host, projectID)),
Steps: projectSteps,
}
tmpl := template.Must(template.ParseFiles("statuspage.html"))
tmpl.Execute(w, templateResponse)
}
func LiveStatusUpdate(w http.ResponseWriter, r *http.Request) {
//NOTE: to debug script on statuspage add //# sourceURL=statuspage at end of script above closing tag
vars := mux.Vars(r)
projectID, ok := vars["project"]
if !ok {
Respond(w, 400, map[string]interface{}{
"error": "no vars found",
})
return
}
project, ok := core.ServerConf.Project[projectID]
if !ok {
Respond(w, 400, map[string]interface{}{
"error": "no project found with given project name",
})
return
}
totalSteps := len(project.Steps)
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
Respond(w, 500, map[string]interface{}{
"error": err.Error(),
})
return
}
defer conn.Close()
for jobState := range core.LiveResultUpdates[projectID] {
var successfullSteps int
for _, v := range jobState.StepResults {
if v.Error == nil {
successfullSteps = successfullSteps + 1
}
}
coverage := float64(successfullSteps * 100 / totalSteps)
res := WebsocketResponse{
JobState: jobState,
ProjectName: projectID,
Coverage: coverage,
}
err := conn.WriteJSON(res)
if err != nil {
break
}
}
}
func RouterInit(r *mux.Router) {
r.HandleFunc("/{project}", WebHookListener).Methods("POST")
r.HandleFunc("/{project}/", WebHookListener).Methods("POST")
r.HandleFunc("/{project}/status", BuildStatus).Methods("GET")
r.HandleFunc("/{project}/status/", BuildStatus).Methods("GET")
r.HandleFunc("/{project}/livestatus", LiveStatusUpdate)
r.HandleFunc("/{project}/livestatus/", LiveStatusUpdate)
}