forked from tailscale/ToBeReviewedBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
345 lines (302 loc) · 9.97 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright (c) 2022 Tailscale Inc & AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// tbr-audit is a GitHub app which will walk through recently merged
// pull requests from a GitHub repository and validate that at least one
// reviewer approved them. If a Pull Request was merged without a reviewer,
// a GitHub issue will be filed requesting a followup review.
//
// This bot is intended to provide a control for SOC2 while allowing
// to-be-reviewed changes to be submitted.
package main
import (
"context"
"expvar"
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/bradleyfalzon/ghinstallation/v2"
"github.com/google/go-github/v45/github"
"go4.org/mem"
"tailscale.com/tsweb"
)
var (
reposChecked = expvar.NewInt("tbrbot_repos_checked")
totalWakeups = expvar.NewInt("tbrbot_total_wakeups")
webhookWakeups = expvar.NewInt("tbrbot_webhook_wakeups")
)
var wakeBot chan int
var gitHubWebhookSecret []byte
type githubInfo struct {
org string
bugrepo string
appname string
repos string
port int
// secrets
appId int64
appInstall int64
appPrivateKey []byte
}
// Return an HTTP client suitable to use with the GitHub API, initialized with
// our API keys and certificate.
//
// This bot expects to run as an organization-level GitHub app, as seen in
// https://github.com/organizations/<name>/settings/installations
// This gives it permission to access private repos without using an individual's
// Personal Access Token.
func getGitHubApiClient(args githubInfo) *github.Client {
itr, err := ghinstallation.New(http.DefaultTransport, args.appId,
args.appInstall, args.appPrivateKey)
if err != nil {
log.Fatal(err)
}
return github.NewClient(&http.Client{Transport: itr})
}
// check whether an issue has already been filed for the given PR, and file one if not.
func fileFollowupIssue(ctx context.Context, client *github.Client, repo string, args githubInfo,
prNum int) error {
title := fmt.Sprintf("TBR %s/%s/pull/%d followup review", args.org, repo, prNum)
// all of the followup issues are in the bugrepo, no matter the repo of the submitted PR
check := fmt.Sprintf("%s in:title repo:%s/%s author:app/%s", title, args.org,
args.bugrepo, args.appname)
followup, _, err := client.Search.Issues(ctx, check, &github.SearchOptions{
ListOptions: github.ListOptions{PerPage: 10}})
if err != nil {
return err
}
if len(followup.Issues) > 0 {
// Issue already filed, nothing more to do.
return nil
}
body := fmt.Sprintf("https://github.com/%s/%s/pull/%d was filed to-be-reviewed without any reviewer approving. Someone needs to review it, followup on any changes needed, and note completion by closing this issue.", args.org, repo, prNum)
req := github.IssueRequest{Title: &title, Body: &body}
_, _, err = client.Issues.Create(ctx, args.org, args.bugrepo, &req)
if err != nil {
return err
}
return nil
}
// The Activity API only includes State=APPROVED if the approval came within the last
// 300 events. PRs approved a long time before submission will not show as APPROVED
// in the PullRequestReviewEvent.
// To double-check, we use the API to check all comments of a Pull Request if any of them
// contained an Approval.
func wasPrEverApproved(client *github.Client, repo string, args githubInfo, prNum int) bool {
opt := &github.ListOptions{PerPage: 100}
ctx := context.Background()
for {
reviews, resp, err := client.PullRequests.ListReviews(ctx, args.org, repo, prNum, opt)
if err != nil {
log.Fatal(err.Error())
}
for _, review := range reviews {
if strings.EqualFold(*review.State, "approved") {
return true
}
if review.Body != nil {
if mem.ContainsFold(mem.S(*review.Body), mem.S("LGTM")) {
return true
}
// https://twitter.com/naomi_lgbt/status/1573462393103192064
if mem.ContainsFold(mem.S(*review.Body), mem.S("banger pr")) {
return true
}
}
}
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
return false
}
func checkForToBeReviewed(client *github.Client, repo string, args githubInfo) {
type PullRequestState struct {
Approved bool
Submitted bool
Author string
MergedBy string
}
pulls := make(map[int]*PullRequestState, 50)
opt := &github.ListOptions{PerPage: 100}
ctx := context.Background()
for {
// ListRepositoryEvents returns the most recent 300 events, but only
// those occurring in the last 90 days. It is recommended this bot be
// run at least once per day and at most every 15 minutes, depending
// on how much activity there is, in order to not miss events.
events, resp, err := client.Activity.ListRepositoryEvents(ctx, args.org, repo, opt)
if err != nil {
log.Fatal(err.Error())
}
for _, evt := range events {
typ := evt.GetType()
if typ == "PullRequestEvent" {
p, err := evt.ParsePayload()
if err != nil {
log.Printf("failed (%v) to parse: %v\n", err, evt)
continue
}
payload := p.(*github.PullRequestEvent)
prNum := *payload.PullRequest.Number
if _, ok := pulls[prNum]; !ok {
pulls[prNum] = &PullRequestState{}
}
pulls[prNum].Author = *payload.PullRequest.User.Login
if payload.PullRequest.MergedBy != nil {
pulls[prNum].MergedBy = *payload.PullRequest.MergedBy.Login
}
if strings.EqualFold(*payload.Action, "closed") &&
*payload.PullRequest.Merged {
pulls[prNum].Submitted = true
}
}
if typ == "PullRequestReviewEvent" {
p, err := evt.ParsePayload()
if err != nil {
log.Printf("failed (%v) to parse: %v\n", err, evt)
continue
}
payload := p.(*github.PullRequestReviewEvent)
prNum := *payload.PullRequest.Number
if _, ok := pulls[prNum]; !ok {
pulls[prNum] = &PullRequestState{}
}
pulls[prNum].Author = *payload.PullRequest.User.Login
if payload.PullRequest.MergedBy != nil {
pulls[prNum].MergedBy = *payload.PullRequest.MergedBy.Login
}
if strings.EqualFold(*payload.Review.State, "approved") {
pulls[prNum].Approved = true
}
}
}
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
for prNum, pr := range pulls {
if pr.Submitted && !pr.Approved {
// for dependabot, our policy is that merging the PR is Approval.
if pr.Author == "dependabot[bot]" {
continue
}
// merging someone else's PR indicates approval of that PR.
if pr.MergedBy != "" && pr.Author != pr.MergedBy {
continue
}
// Double-check if there was ever an approval.
if wasPrEverApproved(client, repo, args, prNum) {
continue
}
// This Pull Request was submitted without an Approver.
err := fileFollowupIssue(ctx, client, repo, args, prNum)
if err != nil {
log.Fatal(err)
}
}
}
}
func handleWebhook(w http.ResponseWriter, r *http.Request) {
payload, err := github.ValidatePayload(r, gitHubWebhookSecret)
if err != nil {
log.Printf("error validating request body: err=%s\n", err)
return
}
defer r.Body.Close()
event, err := github.ParseWebHook(github.WebHookType(r), payload)
if err != nil {
log.Printf("could not parse webhook: err=%s\n", err)
return
}
_ = event
switch event.(type) {
case *github.PullRequestReviewEvent:
case *github.PullRequestEvent:
default:
// not something we need to respond to
return
}
select {
case wakeBot <- 1:
default:
// Somebody else already woke the bot. Just return.
}
}
func processArgs() githubInfo {
var args githubInfo
args.org = os.Getenv("TBRBOT_ORG")
args.bugrepo = os.Getenv("TBRBOT_BUGREPO")
args.appname = os.Getenv("TBRBOT_APPNAME")
appIdString := os.Getenv("TBRBOT_APP_ID")
appInstallString := os.Getenv("TBRBOT_APP_INSTALL")
args.repos = os.Getenv("TBRBOT_REPOS")
if args.org == "" || args.bugrepo == "" || args.appname == "" || appIdString == "" || appInstallString == "" {
log.Fatal("TBRBOT_ORG, TBRBOT_BUGREPO, TBRBOT_APPNAME, TBRBOT_APP_ID, and TBRBOT_APP_INSTALL are required environment variables")
}
var err error
args.appId, err = strconv.ParseInt(appIdString, 10, 64)
if err != nil {
log.Fatal("Cannot parse TBRBOT_APP_ID as integer: %v", appIdString)
}
args.appInstall, err = strconv.ParseInt(appInstallString, 10, 64)
if err != nil {
log.Fatal("Cannot parse TBRBOT_APP_INSTALL as integer: %v", appInstallString)
}
if args.repos == "" {
log.Print("WARNING: no TBRBOT_REPOS set, no GitHub repositories are being monitored")
}
args.appPrivateKey = []byte(os.Getenv("TBRBOT_APP_PRIVATE_KEY"))
gitHubWebhookSecret = []byte(os.Getenv("WEBHOOK_SECRET"))
return args
}
func mainLoop(args githubInfo) {
ticker := time.NewTicker(1 * time.Hour)
client := getGitHubApiClient(args)
for {
totalWakeups.Add(1)
// check for to-be-reviewed PRs once at startup,
// and then whenever something wakes us up.
for _, repo := range strings.Split(args.repos, ",") {
reposChecked.Add(1)
checkForToBeReviewed(client, repo, args)
}
select {
case <-ticker.C:
log.Print("Periodic check of repositories for TBR submissions.")
case <-wakeBot:
// A webhook will wake the bot immediately after submission. We pause
// a generous amount of time before checking, for two reasons:
// 1. If they realize they didn't have an approval, give them a few
// minutes to find someone to review it. An Approval or LGTM comment
// added before the bot runs will suffice.
// 2. If someone submits and didn't even realize there was no approval,
// it is disconcerting to have the bot file an issue the instant one's
// finger lifts from the mouse button. Wait a decent interval before
// proceeding.
log.Print("Webhook notification received, pausing before checking repositories.")
time.Sleep(5 * time.Minute)
webhookWakeups.Add(1)
}
}
}
func main() {
log.Print("ToBeReviewedBot is starting")
args := processArgs()
wakeBot = make(chan int)
go mainLoop(args)
mux := http.NewServeMux()
tsweb.Debugger(mux)
mux.HandleFunc("/webhook", handleWebhook)
srv := &http.Server{
Addr: ":8080",
Handler: mux,
}
log.Fatal(srv.ListenAndServe())
}