6
6
"crypto/sha256"
7
7
"encoding/hex"
8
8
"encoding/json"
9
+ "fmt"
9
10
"io"
10
11
"net/http"
11
12
"strings"
@@ -32,6 +33,55 @@ func VerifySignature(payload []byte, hash, key string) (bool, error) {
32
33
33
34
}
34
35
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
+
35
85
func StreamToByte (stream io.Reader ) ([]byte , error ) {
36
86
buf := new (bytes.Buffer )
37
87
_ , err := buf .ReadFrom (stream )
0 commit comments