-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.go
165 lines (150 loc) · 4.57 KB
/
webhook.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
//
// TheFederation Github Integration Server
// Copyright (C) 2018 Lukas Matt <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
package main
import (
"fmt"
"net/http"
"github.com/google/go-github/github"
"io/ioutil"
"encoding/json"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"strings"
"context"
)
func webhook(w http.ResponseWriter, r *http.Request) {
db, err := gorm.Open(databaseDriver, databaseDSN)
if err != nil {
logger.Println(err)
fmt.Fprintf(w, `{"error":"database error"}`)
return
}
defer db.Close()
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
if err != nil {
logger.Println(err)
fmt.Fprintf(w, `{"error":"invalid body"}`)
return
}
var event github.PullRequestReviewEvent
err = json.Unmarshal(b, &event)
pr := event.PullRequest
if err != nil || pr == nil {
logger.Println("Not supported event type", string(b))
fmt.Fprintf(w, `{"error":"unsupported event type"}`)
return
}
// skip all events except for open PRs
if pr.GetState() != "open" {
logger.Println("Ignore closed pull request")
fmt.Fprintf(w, `{}`)
return
}
var repo Repo
err = db.Where("slug = ?",
pr.GetBase().GetRepo().GetFullName(),
).Find(&repo).Error
if err != nil {
logger.Println(err, pr.GetBase().GetRepo().GetFullName())
fmt.Fprintf(w, `{"error":"repo not registered"}`)
return
}
// XXX validate payload
//_, err = github.ValidatePayload(r, []byte(repo.Secret))
//if err != nil {
// logger.Println(err, repo.Secret)
// fmt.Fprintf(w, `{"error":"invalid signature"}`)
// return
//}
var flagExists = false
var buildFlag = repo.OptOutFlag
if repo.OptIn {
buildFlag = repo.OptInFlag
}
// check PR title and body for [ci] or [ci skip] flag
if pr.Title != nil && strings.Contains(pr.GetTitle(),
fmt.Sprintf("[%s]", buildFlag)) {
flagExists = true
} else if pr.Body != nil && strings.Contains(pr.GetBody(),
fmt.Sprintf("[%s]", buildFlag)) {
flagExists = true
} else {
// check labels for build flag if we haven't already found it
for _, label := range pr.Labels {
if label.Name != nil && strings.Contains(label.GetName(), buildFlag) {
flagExists = true
break
}
}
if !flagExists {
// last but not least check the commit message for flags
client := github.NewClient(nil)
commits, _, err := client.PullRequests.ListCommits(
context.Background(),
pr.GetHead().GetUser().GetLogin(),
pr.GetHead().GetRepo().GetName(),
pr.GetNumber(), &github.ListOptions{})
if err == nil && len(commits) > 0 {
// check only last commit since older ones are irrelevant
commitMsg := commits[len(commits)-1].GetCommit().GetMessage()
flagExists = strings.Contains(
commitMsg, fmt.Sprintf("[%s]", buildFlag))
} else {
logger.Printf("Commits is empty or an error occurred: %+v\n", err)
}
}
}
// ignoring pull-request! Repository is set
// to opt-in and no build flag was found
if repo.OptIn && !flagExists {
logger.Printf(
"Ignore optin=%t buildFlag=%s flagExists=%t\n",
repo.OptIn, buildFlag, flagExists)
fmt.Fprintf(w, `{}`)
return
}
// ignoring pull-request! Repository is set
// to opt-out and a skip flag was found
if !repo.OptIn && flagExists {
logger.Printf(
"Ignore optin=%t buildFlag=%s flagExists=%t\n",
repo.OptIn, buildFlag, flagExists)
fmt.Fprintf(w, `{}`)
return
}
build := Build{
RepoID: repo.ID,
Matrix: fmt.Sprintf(
`"PROJECT=%s PRREPO=%s PRSHA=%s"`,
repo.Project,
pr.GetHead().GetRepo().GetCloneURL(),
pr.GetHead().GetSHA(),
),
PRUser: pr.GetHead().GetUser().GetLogin(),
PRRepo: pr.GetHead().GetRepo().GetName(),
PRSha: pr.GetHead().GetSHA(),
}
err = db.Create(&build).Error
if err != nil {
logger.Println(err)
fmt.Fprintf(w, `{"error":"database error"}`)
return
}
fmt.Fprintf(w, `{}`)
}