Skip to content

Commit 4863a57

Browse files
committed
add support for a release event type - issue #7
Signed-off-by: thecreed <[email protected]>
1 parent 27d3fe9 commit 4863a57

File tree

3 files changed

+58
-21
lines changed

3 files changed

+58
-21
lines changed

httpinterface/api.go

+4-21
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package httpinterface
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"html/template"
76
"net/http"
@@ -106,28 +105,12 @@ func WebHookListener(w http.ResponseWriter, r *http.Request) {
106105
}
107106
}
108107

109-
var payload WebhookPayload
110-
err = json.Unmarshal(bodyInBytes, &payload)
111-
if err != nil {
112-
Respond(w, 400, map[string]any{
113-
"error": err.Error(),
114-
})
115-
return
116-
}
108+
eventType := r.Header.Get("X-GitHub-Event")
109+
err = VerifyEvent(eventType, bodyInBytes, project.Branch)
117110

118-
if payload.Ref == "" {
111+
if err != nil {
119112
Respond(w, 400, map[string]interface{}{
120-
"error": "invalid payload: cannot find ref inside given payload",
121-
})
122-
return
123-
}
124-
125-
branchStringArr := strings.Split(payload.Ref, "/")
126-
branchString := branchStringArr[len(branchStringArr)-1]
127-
128-
if project.Branch != branchString {
129-
Respond(w, 200, map[string]interface{}{
130-
"message": "request recieved but the push event is not for the configured branch",
113+
"error": fmt.Sprintf("error: %v.", err),
131114
})
132115
return
133116
}

httpinterface/util.go

+50
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"crypto/sha256"
77
"encoding/hex"
88
"encoding/json"
9+
"fmt"
910
"io"
1011
"net/http"
1112
"strings"
@@ -32,6 +33,55 @@ func VerifySignature(payload []byte, hash, key string) (bool, error) {
3233

3334
}
3435

36+
// VirifyType - check if a given event type is supported
37+
func VerifyEvent(eventType string, bodyInBytes []byte, configBranchName string) error {
38+
switch eventType {
39+
40+
case "push":
41+
42+
var payload WebhookPayload
43+
err := json.Unmarshal(bodyInBytes, &payload)
44+
if err != nil {
45+
return fmt.Errorf("Couldnt unmarshal push event - %v", err)
46+
}
47+
48+
if payload.Ref == "" {
49+
return fmt.Errorf("invalid payload: cannot find ref inside given payload")
50+
}
51+
52+
branchStringArr := strings.Split(payload.Ref, "/")
53+
branchString := branchStringArr[len(branchStringArr)-1]
54+
55+
if configBranchName != branchString {
56+
return fmt.Errorf("request recieved but the push event is not for the configured branch")
57+
}
58+
59+
return nil
60+
61+
case "release":
62+
supportedReleaseActions := map[string]bool{
63+
"published": true,
64+
"created": false,
65+
"released": false,
66+
}
67+
68+
var payload ReleaseWebhookPayload
69+
err := json.Unmarshal(bodyInBytes, &payload)
70+
71+
if err != nil {
72+
return fmt.Errorf("couldnt unmarshal release event - %v", err)
73+
}
74+
supported, exists := supportedReleaseActions[payload.Action]
75+
if exists && supported {
76+
return nil
77+
}
78+
return fmt.Errorf("release event action %s is not enabled", payload.Action)
79+
80+
default:
81+
return fmt.Errorf("event type %s: is not supported", eventType)
82+
}
83+
}
84+
3585
func StreamToByte(stream io.Reader) ([]byte, error) {
3686
buf := new(bytes.Buffer)
3787
_, err := buf.ReadFrom(stream)

httpinterface/webhookPayload.go

+4
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,7 @@ type WebhookPayload struct {
169169
Repository RepositoryT `json:"repository"`
170170
Sender SenderT `json:"sender"`
171171
}
172+
173+
type ReleaseWebhookPayload struct {
174+
Action string `json:"action"`
175+
}

0 commit comments

Comments
 (0)