-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
47 lines (41 loc) · 1.37 KB
/
server.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
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
)
var (
httpAddr = flag.String("http", "127.0.0.1", "gocd address")
authentication = flag.String("auth", "", "gocd authentication")
pipelineName = flag.String("pipeline", "", "pipeline name to trigger build")
materialName = flag.String("material", "", "material name to trigger build")
)
func webhookHandler(w http.ResponseWriter, r *http.Request) {
ghPayload := GitHubPullRequestPayload{}
err := json.NewDecoder(r.Body).Decode(&ghPayload)
if err != nil {
http.Error(w, "Please send a valid request body", 400)
return
}
fmt.Println(ghPayload)
statusCheckURL := fmt.Sprintf("http://%s:8153/go/api/pipelines/%s/status", *httpAddr, *pipelineName)
notifyURL := fmt.Sprintf("http://%s:8153/go/api/pipelines/%s/schedule", *httpAddr, *pipelineName)
if ghPayload.Action == "synchronize" || ghPayload.Action == "opened" {
if err := notifyGoCDOfChangeInPR(*pipelineName, *materialName, notifyURL, statusCheckURL, *authentication, ghPayload); err != nil {
http.Error(w, err.Error(), 500)
}
} else {
http.Error(w, fmt.Errorf("Action: %s is not supported", ghPayload.Action).Error(), 400)
}
}
func main() {
flag.Parse()
StartDispatcher(100)
http.HandleFunc("/github-webhook", webhookHandler)
err := http.ListenAndServe(":9090", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}