@@ -30,13 +30,15 @@ func VerifyEvent(req *http.Request, body []byte, secret string) bool { |
30 | 30 | } |
31 | 31 |
|
32 | 32 | // Do whatever logic we need for the incoming event |
33 | | -func ProcessEvent(eventType string, eventBody []byte) { |
| 33 | +func ProcessEvent(eventType string, eventBody []byte) int { |
34 | 34 | log.Printf("Event Type: %s", eventType) |
35 | 35 |
|
36 | | - // Only care about "push" events |
37 | | - if eventType != "push" { |
| 36 | + // Only care about "push" events and "ping" for webhook creation |
| 37 | + if eventType == "ping" { |
| 38 | + return http.StatusOK |
| 39 | + } else if eventType != "push" { |
38 | 40 | log.Print("Not a 'push' event so exit") |
39 | | - return |
| 41 | + return http.StatusBadRequest |
40 | 42 | } |
41 | 43 |
|
42 | 44 | pretty, _ := json.MarshalIndent(json.RawMessage(eventBody), "", " ") |
@@ -79,13 +81,7 @@ func ProcessEvent(eventType string, eventBody []byte) { |
79 | 81 | err := json.Unmarshal(eventBody, &PushEvent) |
80 | 82 | if err != nil { |
81 | 83 | log.Printf("Error parsing:\n%s\n%s", err, string(eventBody)) |
82 | | - return |
83 | | - } |
84 | | - |
85 | | - // Only care about pushes to the "main" branch |
86 | | - if PushEvent.Ref != "refs/heads/main" { |
87 | | - log.Printf("Skipping branch: %s", PushEvent.Ref) |
88 | | - return |
| 84 | + return http.StatusBadRequest |
89 | 85 | } |
90 | 86 |
|
91 | 87 | log.Printf("%s committed %q to %q branch", |
@@ -95,6 +91,7 @@ func ProcessEvent(eventType string, eventBody []byte) { |
95 | 91 | // To see how to kick off CodeEngine CLI commands from inside of an app |
96 | 92 | // see the "cecli" sample. |
97 | 93 | log.Print("ibmcloud ce buildrun submit ...") |
| 94 | + return http.StatusOK |
98 | 95 | } |
99 | 96 |
|
100 | 97 | // Simple web server that waits for incoming events, verifies they're |
@@ -110,7 +107,8 @@ func main() { |
110 | 107 | return |
111 | 108 | } |
112 | 109 |
|
113 | | - ProcessEvent(r.Header.Get("X-Github-Event"), body) |
| 110 | + status := ProcessEvent(r.Header.Get("X-Github-Event"), body) |
| 111 | + w.WriteHeader(status) |
114 | 112 | }) |
115 | 113 |
|
116 | 114 | log.Printf("Listening on port 8080") |
|