This repository has been archived by the owner on Jan 4, 2018. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
webhooks.go
196 lines (164 loc) · 5.15 KB
/
webhooks.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strings"
"github.com/Luzifer/gobuilder/buildjob"
"github.com/Sirupsen/logrus"
)
func webhookGitHub(res http.ResponseWriter, r *http.Request) {
data, err := ioutil.ReadAll(r.Body)
if err != nil {
log.WithFields(logrus.Fields{
"error": fmt.Sprintf("%v", err),
}).Error("GitHub Hook Error")
http.Error(res, "GitHub request could not be read.", http.StatusInternalServerError)
return
}
var tmp interface{}
json.Unmarshal([]byte(data), &tmp)
repoName := tmp.(map[string]interface{})["repository"].(map[string]interface{})["full_name"].(string)
commit := tmp.(map[string]interface{})["after"].(string)
ref := tmp.(map[string]interface{})["ref"].(string)
if ref != "refs/heads/master" {
http.Error(res, "OK, got your message, will not take action.", http.StatusOK)
return
}
if r.URL.Query().Get("override") != "" {
repoName = r.URL.Query().Get("override")
}
repo := fmt.Sprintf("github.com/%s", repoName)
err = sendToQueue(repo, commit)
if err != nil {
http.Error(res, "Could not submit build job", http.StatusInternalServerError)
} else {
http.Error(res, "OK", http.StatusOK)
}
}
func webhookBitBucket(res http.ResponseWriter, r *http.Request) {
data := r.FormValue("payload")
var tmp interface{}
json.Unmarshal([]byte(data), &tmp)
repoName := tmp.(map[string]interface{})["repository"].(map[string]interface{})["absolute_url"].(string)
repoName = strings.Trim(repoName, "/")
repo := fmt.Sprintf("bitbucket.org/%s", repoName)
err := sendToQueue(parseRepoCommit(repo))
if err != nil {
http.Error(res, "Could not submit build job", http.StatusInternalServerError)
} else {
http.Error(res, "OK", http.StatusOK)
}
}
func webhookInterface(res http.ResponseWriter, r *http.Request) {
sess, _ := sessionStore.Get(r, "GoBuilderSession")
repo, commit := parseRepoCommit(r.FormValue("repository"))
// No repository was given, just submitted
if len(repo) == 0 {
sess.AddFlash("Please provide a repository.", "alert_error")
sess.Save(r, res)
http.Redirect(res, r, "/", http.StatusFound)
return
}
// Repository contained characters not being allowed
if !isValidRepositorySource(repo) {
log.WithFields(logrus.Fields{
"repository": repo,
}).Warn("Refused to build repo")
sess.AddFlash("Sorry, that does not look like a valid package. Not building that.", "alert_error")
sess.Save(r, res)
http.Redirect(res, r, "/", http.StatusFound)
return
}
addGithubWebhook(res, r, repo)
err := sendToQueue(repo, commit)
if err != nil {
sess.AddFlash("An unknown error occured while queueing the repository.", "alert_error")
sess.Save(r, res)
http.Redirect(res, r, "/", http.StatusFound)
return
}
sess.AddFlash(flashContext{
"success": "Your build job has been submitted.",
"repo": repo,
}, "context")
sess.Save(r, res)
http.Redirect(res, r, "/", http.StatusFound)
}
func webhookCLI(res http.ResponseWriter, r *http.Request) {
repo, commit := parseRepoCommit(r.FormValue("repository"))
// No repository was given, just submitted
if len(repo) == 0 {
http.Error(res, "Please provide a repository", http.StatusNoContent)
return
}
// Repository contained characters not being allowed
if !isValidRepositorySource(repo) {
log.WithFields(logrus.Fields{
"repository": repo,
}).Warn("Refused to build repo")
http.Error(res, "Sorry, that does not look like a valid package. Not building that.", http.StatusNotAcceptable)
return
}
err := sendToQueue(repo, commit)
if err != nil {
http.Error(res, "An unknown error occured while queueing the repository.", http.StatusInternalServerError)
return
}
http.Error(res, "OK", http.StatusOK)
}
func isValidRepositorySource(repository string) bool {
regex := regexp.MustCompile(`^[a-zA-Z0-9/_\.-]+/[a-zA-Z0-9/_\.-]+[^/]$`)
return regex.Match([]byte(repository))
}
func sendToQueue(repository, commit string) error {
if blocked, reason := blockedRepos.IsBlocked(repository); blocked {
return errors.New("This repository is blocked: " + reason)
}
job := buildjob.BuildJob{
Repository: repository,
Commit: commit,
NumberOfExecutions: 0,
}
queueEntry, err := job.ToByte()
if err != nil {
log.Error(fmt.Sprintf("%q", err))
return err
}
// Check the queue does not contain this job already
queueLength, err := redisClient.LLen("build-queue")
if err != nil {
return err
}
queueItems, err := redisClient.LRange("build-queue", 0, int(queueLength))
if err != nil {
return err
}
for _, item := range queueItems {
j, err := buildjob.FromBytes([]byte(item))
if err != nil {
return err
}
if j.Repository == job.Repository && j.Commit == job.Commit {
// We have a matching entry in the queue, will not add!
return nil
}
}
// Put the job into the queue and give it a time to run of 900 secs
redisClient.RPush("build-queue", string(queueEntry))
err = redisClient.Set(fmt.Sprintf("project::%s::build-status", repository), "queued", 0, 0, false, false)
if err != nil {
fmt.Printf("%+v", err)
}
return nil
}
func parseRepoCommit(repo string) (string, string) {
t := strings.Split(repo, "@")
if len(t) == 1 {
return repo, ""
}
return t[0], t[1]
}