This repository was archived by the owner on Nov 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhttp.go
More file actions
130 lines (112 loc) · 2.91 KB
/
http.go
File metadata and controls
130 lines (112 loc) · 2.91 KB
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
package dreck
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/miekg/dreck/auth"
"github.com/miekg/dreck/log"
"github.com/caddyserver/caddy"
)
func init() {
caddy.RegisterPlugin("dreck", caddy.Plugin{
ServerType: "http",
Action: setup,
})
}
func (d Dreck) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
// Give up if we can't find this header in the event.
event := r.Header.Get("X-GitHub-Event")
if event == "" {
return d.Next.ServeHTTP(w, r)
}
// Not the correct path.
if !strings.HasPrefix(r.URL.Path, d.path) {
return d.Next.ServeHTTP(w, r)
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return 0, err
}
r.Body.Close()
hubSignature := r.Header.Get("X-Hub-Signature")
if d.hmac {
if hubSignature == "" {
return 0, fmt.Errorf("must provide X-Hub-Signature")
}
if d.secret == "" {
return 0, fmt.Errorf("must provide a secret")
}
err := auth.ValidateHMAC(d.secret, body, hubSignature)
if err != nil {
return 0, err
}
}
err = d.handleEvent(event, body)
return 0, err
}
func parseEvent(event string, body []byte) (IssueCommentOuter, error) {
req := IssueCommentOuter{}
if err := json.Unmarshal(body, &req); err != nil {
if e, ok := err.(*json.SyntaxError); ok {
return req, fmt.Errorf("Syntax error at byte offset %d", e.Offset)
}
return req, fmt.Errorf("parse error %s: %s", string(body), err.Error())
}
// If event is issues or pull_request; we copy some elements, so we can treat it as an issue comment.
// But only when the issue/pr is created.
switch event {
case "issues":
if req.Action == "opened" {
req.Comment.Body = req.Issue.Body
}
req.Comment.User.Login = req.Issue.User.Login
case "pull_request":
if req.Action == "opened" {
req.Comment.Body = req.PullRequest.Body
}
req.Issue.Number = req.PullRequest.Number
req.Comment.User.Login = req.PullRequest.User.Login
}
return req, nil
}
func (d Dreck) handleEvent(event string, body []byte) error {
switch event {
case "issue_comment", "issues", "pull_request_review", "pull_request":
req, err := parseEvent(event, body)
if err != nil {
return err
}
log.Infof("Action: %q for: %s", req.Action, req.Comment.User.Login)
if strings.HasSuffix(req.Comment.User.Login, "[bot]") {
return nil
}
// Do nothing on these actions
switch req.Action {
case "deleted":
fallthrough
case "synchronize":
fallthrough
case "locked":
fallthrough
case "labeled":
return nil
}
conf, err := d.getConfig(req.Repository.Owner.Login, req.Repository.Name)
if err != nil {
return fmt.Errorf("unable to access maintainers file at %s/%s: %s", req.Repository.Owner.Login, req.Repository.Name, err)
}
if _, err := d.comment(req, conf); err != nil {
return err
}
case "ping":
fallthrough
case "status":
log.Infof("Seen %s", event)
return nil
default:
return fmt.Errorf("unsupported event: %s", event)
}
return nil
}