forked from alexellis/derek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
145 lines (120 loc) · 4.35 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (c) Derek Author(s) 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"github.com/alexellis/derek/auth"
"github.com/alexellis/derek/config"
"github.com/alexellis/derek/handler"
"github.com/alexellis/derek/types"
"github.com/alexellis/hmac"
)
const (
dcoCheck = "dco_check"
comments = "comments"
deleted = "deleted"
prDescriptionRequired = "pr_description_required"
)
func main() {
validateHmac := hmacValidation()
requestRaw, _ := ioutil.ReadAll(os.Stdin)
xHubSignature := os.Getenv("Http_X_Hub_Signature")
if validateHmac && len(xHubSignature) == 0 {
os.Stderr.Write([]byte("must provide X_Hub_Signature"))
os.Exit(1)
}
config, configErr := config.NewConfig()
if configErr != nil {
os.Stderr.Write([]byte(configErr.Error()))
os.Exit(1)
}
if validateHmac {
err := hmac.Validate(requestRaw, xHubSignature, config.SecretKey)
if err != nil {
os.Stderr.Write([]byte(err.Error()))
os.Exit(1)
}
}
eventType := os.Getenv("Http_X_Github_Event")
if err := handleEvent(eventType, requestRaw, config); err != nil {
os.Stderr.Write([]byte(err.Error()))
os.Exit(1)
}
}
func handleEvent(eventType string, bytesIn []byte, config config.Config) 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.Owner.Login, &http.Client{})
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)
}
var derekConfig *types.DerekRepoConfig
if req.Repository.Private {
derekConfig, err = handler.GetPrivateRepoConfig(req.Repository.Owner.Login, req.Repository.Name, req.Installation.ID, config)
} else {
derekConfig, err = handler.GetRepoConfig(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 req.Action != handler.ClosedConstant {
contributingURL := getContributingURL(derekConfig.ContributingURL, req.Repository.Owner.Login, req.Repository.Name)
if handler.EnabledFeature(dcoCheck, derekConfig) {
handler.HandlePullRequest(req, contributingURL, config)
}
if handler.EnabledFeature(prDescriptionRequired, derekConfig) {
handler.VerifyPullRequestDescription(req, contributingURL, config)
}
}
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.Owner.Login, &http.Client{})
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)
}
var derekConfig *types.DerekRepoConfig
if req.Repository.Private {
derekConfig, err = handler.GetPrivateRepoConfig(req.Repository.Owner.Login, req.Repository.Name, req.Installation.ID, config)
} else {
derekConfig, err = handler.GetRepoConfig(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 req.Action != deleted {
if handler.PermittedUserFeature(comments, derekConfig, req.Comment.User.Login) {
handler.HandleComment(req, config)
}
}
break
default:
return fmt.Errorf("X_Github_Event want: ['pull_request', 'issue_comment'], got: " + eventType)
}
return nil
}
func getContributingURL(contributingURL, owner, repositoryName string) string {
if len(contributingURL) == 0 {
contributingURL = fmt.Sprintf("https://github.com/%s/%s/blob/master/CONTRIBUTING.md", owner, repositoryName)
}
return contributingURL
}
func hmacValidation() bool {
val := os.Getenv("validate_hmac")
return (val != "false") && (val != "0")
}