This repository was archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandler.go
More file actions
80 lines (69 loc) · 1.8 KB
/
handler.go
File metadata and controls
80 lines (69 loc) · 1.8 KB
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
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/google/go-github/github"
"github.com/spf13/viper"
)
func handleHealth(w http.ResponseWriter, r *http.Request) {
_, err := fmt.Fprintf(w, "Good to Serve")
if err != nil {
log.Println(err)
} else {
log.Println("Good to Serve")
}
}
func handleWebhook(w http.ResponseWriter, r *http.Request) {
defer func() {
err := r.Body.Close()
if err != nil {
log.Printf("Error closing body: %s", err)
}
}()
payload, err := github.ValidatePayload(r, []byte(viper.GetString("GITHUB_SECRET")))
if err != nil {
log.Printf("error validating request body: err=%s\n", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
event, err := github.ParseWebHook(github.WebHookType(r), payload)
if err != nil {
log.Printf("could not parse webhook: err=%s\n", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
switch e := event.(type) {
case *github.PingEvent:
log.Println("GitHub Pinged the Webhook")
case *github.PushEvent:
env, err := filterEnvironment(os.Environ())
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
jobArgs := NomadJobData{
GitRepoName: e.Repo.GetName(),
GitRepoURL: e.Repo.GetSSHURL(),
HeadSHA: e.GetAfter(),
DeployKey: viper.GetString("SSH_PRIV_KEY"),
Environment: env,
}
job, err := templateToJob(jobArgs)
if err != nil {
log.Println("template to job failed:", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = submitNomadJob(job)
if err != nil {
log.Println("submit job failed:", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
default:
log.Printf("WARN: unknown event type %s\n", github.WebHookType(r))
return
}
}