-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook-handler.go
70 lines (62 loc) · 1.8 KB
/
webhook-handler.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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)
func webhookHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}
eventType := r.Header.Get("X-GitHub-Event")
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Failed to read request body", http.StatusInternalServerError)
return
}
responseStruct, eventName := GetEventDetails(eventType, body)
if responseStruct == nil {
http.Error(w, "Failed to process event", http.StatusBadRequest)
return
}
determineQueryType(responseStruct, eventName)
w.WriteHeader(http.StatusOK)
w.Write([]byte("Webhook received"))
}
func GetEventDetails(event string, body []byte) (interface{}, string) {
if strings.Contains(event, "push") {
fmt.Printf("Received a push event\n")
var response PushResponse
fillStruct(body, &response)
return response, "push"
} else if strings.Contains(event, "issue") {
fmt.Printf("Received an issue event\n")
var response IssueResponse
fillStruct(body, &response)
return response, "issue"
} else if strings.Contains(event, "pull_request") {
fmt.Printf("Received a pull request event\n")
var response PullRequestResponse
fillStruct(body, &response)
return response, "pull request"
} else if strings.Contains(event, "create") {
fmt.Printf("Received a create event\n")
var response CreateResponse
fillStruct(body, &response)
return response, "create"
} else if strings.Contains(event, "delete") {
fmt.Printf("Received a delete event\n")
var response DeleteResponse
fillStruct(body, &response)
return response, "delete"
} else {
return nil, "nil"
}
}
func fillStruct(body []byte, response interface{}) error {
err := json.Unmarshal(body, response)
return err
}