forked from alexellis/derek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
102 lines (82 loc) · 2.68 KB
/
main.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
log "github.com/Sirupsen/logrus"
"github.com/alexellis/derek/auth"
"github.com/alexellis/derek/types"
)
const dcoCheck = "dco_check"
const comments = "comments"
func hmacValidation() bool {
val := os.Getenv("validate_hmac")
return len(val) > 0 && (val == "1" || val == "true")
}
func main() {
bytesIn, _ := ioutil.ReadAll(os.Stdin)
xHubSignature := os.Getenv("Http_X_Hub_Signature")
if hmacValidation() && len(xHubSignature) == 0 {
log.Fatal("must provide X_Hub_Signature")
return
}
if len(xHubSignature) > 0 {
secretKey := os.Getenv("secret_key")
err := auth.ValidateHMAC(bytesIn, xHubSignature, secretKey)
if err != nil {
log.Fatal(err.Error())
return
}
}
// HMAC Validated or not turned on.
eventType := os.Getenv("Http_X_Github_Event")
if err := handleEvent(eventType, bytesIn); err != nil {
log.Fatal(err)
}
}
func handleEvent(eventType string, bytesIn []byte) error {
switch eventType {
case "pull_request":
req := types.PullRequestOuter{}
if err := json.Unmarshal(bytesIn, &req); err != nil {
return fmt.Errorf("Cannot parse input %s", err.Error())
}
customer, err := auth.IsCustomer(req.Repository)
if err != nil {
return fmt.Errorf("Unable to verify customer: %s/%s", req.Repository.Owner.Login, req.Repository.Name)
} else if customer == false {
return fmt.Errorf("No customer found for: %s/%s", req.Repository.Owner.Login, req.Repository.Name)
}
derekConfig, err := getConfig(req.Repository.Owner.Login, req.Repository.Name)
if err != nil {
return fmt.Errorf("Unable to access maintainers file at: %s/%s", req.Repository.Owner.Login, req.Repository.Name)
}
if enabledFeature(dcoCheck, derekConfig) {
handlePullRequest(req)
}
break
case "issue_comment":
req := types.IssueCommentOuter{}
if err := json.Unmarshal(bytesIn, &req); err != nil {
return fmt.Errorf("Cannot parse input %s", err.Error())
}
customer, err := auth.IsCustomer(req.Repository)
if err != nil {
return fmt.Errorf("Unable to verify customer: %s/%s", req.Repository.Owner.Login, req.Repository.Name)
} else if customer == false {
return fmt.Errorf("No customer found for: %s/%s", req.Repository.Owner.Login, req.Repository.Name)
}
derekConfig, err := getConfig(req.Repository.Owner.Login, req.Repository.Name)
if err != nil {
return fmt.Errorf("Unable to access maintainers file at: %s/%s", req.Repository.Owner.Login, req.Repository.Name)
}
if permittedUserFeature(comments, derekConfig, req.Comment.User.Login) {
handleComment(req)
}
break
default:
return fmt.Errorf("X_Github_Event want: ['pull_request', 'issue_comment'], got: " + eventType)
}
return nil
}