This repository has been archived by the owner on Oct 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
crhub.rb
executable file
·656 lines (574 loc) · 18.1 KB
/
crhub.rb
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
#!/usr/bin/env ruby
# Copyright 2015-present Antonin Bas
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# Antonin Bas ([email protected])
#
#
require 'sinatra/base'
require 'json'
require 'octokit'
require 'sqlite3'
require 'thread'
require 'set'
require 'parseconfig'
require 'git'
if not ENV.include?("GITHUB_PERSONAL_TOKEN")
puts "You need to define the 'GITHUB_PERSONAL_TOKEN' env variable"
puts "See README.md for more information"
exit(1)
end
$ACCESS_TOKEN = ENV["GITHUB_PERSONAL_TOKEN"]
if ARGV.length != 1
puts "Error, crhub.rb takes exactly one argument (.conf file)"
puts "Usage: ruby crhub.rb [crhub.conf]"
exit(1)
end
config_file = ARGV[0]
if not File.file?(config_file)
puts "File '#{config_file}' does not exist"
exit(1)
end
puts "Using #{config_file} as configuration file"
config = ParseConfig.new(config_file)
class RepoConfig
attr_reader :with_self_assign
attr_reader :users_with_self_assign
attr_reader :users_bypass
attr_reader :checker_cmd
attr_reader :access_mode
def initialize()
@with_self_assign = true
@users_with_self_assign = Set.new
@users_bypass = Set.new
@checker_cmd = nil
@access_mode = nil
end
def load_config(config)
if not config["with_self_assign"].nil?
@with_self_assign = (config["with_self_assign"] == "true")
end
if not config["users_with_self_assign"].nil?
@users_with_self_assign = config["users_with_self_assign"].split()
@users_with_self_assign = @users_with_self_assign.to_set
end
if not config["users_bypass"].nil?
@users_bypass = config["users_bypass"].split()
@users_bypass = @users_bypass.to_set
end
if not config["checker_cmd"].nil?
@checker_cmd = config["checker_cmd"]
end
if not config["access_mode"].nil?
@access_mode = config["access_mode"]
if not Set.new(["https", "ssh"]).include?(@access_mode)
puts "'access_mode' needs to be one of 'https', 'ssh'"
exit(1)
end
end
if @access_mode.nil? and not @checker_cmd.nil?
puts "If you provide a 'checker_cmd' in the conf, you also need to " +
"provide an 'access_mode'"
exit(1)
end
end
end
class CrhubConfig
def initialize()
@default_repo_config = RepoConfig.new()
@repos_raw = []
@sinatra_config = {
:bind => '0.0.0.0',
:port => 4567,
:environment => 'production'
}
@general_config = {
:db_path => 'test.db'
}
@repo_configs = {}
end
def get_sinatra_config()
return @sinatra_config
end
def get_repo_configs()
return @repo_configs
end
def get_raw_repo_names()
return @repos_raw
end
def get_db_path()
return @general_config[:db_path]
end
def easy_load(config, name, target)
if not config[name].nil?
config[name].each do |key, value|
target[key.to_sym] = value
end
end
end
def load_config(config)
if not config['repo-default'].nil?
@default_repo_config.load_config(config['repo-default'])
end
easy_load(config, 'general', @general_config)
easy_load(config, 'sinatra', @sinatra_config)
config.get_groups().each do |group_name|
if group_name.start_with?('repo:')
repo_name = group_name.slice(5..-1)
@repos_raw.push(repo_name)
# repo_name = sanitize_repo_name(repo_name)
repo_config = @default_repo_config.clone()
repo_config.load_config(config[group_name])
@repo_configs[repo_name] = repo_config
end
end
end
end
$config = CrhubConfig.new()
$config.load_config(config)
class CrhubDB
def initialize(repos, db_name)
@repos = repos
@db = SQLite3::Database.new db_name
create_tables()
end
def sanitize_repo_name(repo)
return repo.sub('/', '---').gsub("-", "_")
end
def create_tables()
@repos.each do |repo|
repo = sanitize_repo_name(repo)
# sha used to be "unique", but 2 PRs can be created with exactly the same diff / sha
s = "create table if not exists #{repo} (
number integer primary key,
id int unique,
title text,
user_id int,
user_login text,
assignee_id int,
assignee_login text,
sha text,
target_branch text
)"
rows = @db.execute(s)
s = "create table if not exists #{repo}__reviews_ (
number int,
user_id int,
score int
)"
rows = @db.execute(s)
s = "create unique index if not exists #{repo}__reviews_idx_
on #{repo}__reviews_ (number, user_id)"
rows = @db.execute(s)
end
end
def update_pr(repo, number, id, title, user_id, user_login,
assignee_id, assignee_login, sha, target_branch)
repo = sanitize_repo_name(repo)
query = "select * from #{repo} where number = ?"
rows = @db.execute(query, number)
if rows.length == 0
query = "insert into #{repo} (number, id, title, user_id, user_login, assignee_id, assignee_login, sha, target_branch)
values (?, ?, ?, ?, ?, ?, ?, ?, ?)"
@db.execute(query, [number, id, title, user_id, user_login, assignee_id, assignee_login, sha, target_branch])
return true
end
raise "Corrupted DB" unless rows.length == 1
row = rows[0]
raise "Corrupted DB" unless row[1] == id
raise "Corrupted DB" unless row[2] == title
raise "Corrupted DB" unless row[3] == user_id
raise "Corrupted DB" unless row[4] == user_login
# TODO(antonin): is this sanity checking really necessary?
# maybe I should jut do an insert_or_replace...
if row[5] != assignee_id or row[7] != sha
query = "replace into #{repo} (number, id, title, user_id, user_login, assignee_id, assignee_login, sha, target_branch)
values (?, ?, ?, ?, ?, ?, ?, ?, ?)"
@db.execute(query, [number, id, title, user_id, user_login, assignee_id, assignee_login, sha, target_branch])
return true
end
return false
end
def add_review(repo, number, user_id, score)
repo = sanitize_repo_name(repo)
query = "insert or replace into #{repo}__reviews_ (number, user_id, score)
values (?, ?, ?)"
@db.execute(query, [number, user_id, score])
end
def get_status(repo, number)
repo = sanitize_repo_name(repo)
repo_reviews = "#{repo}__reviews_"
query = "select score from #{repo}, #{repo_reviews}
where #{repo}.number = ? and #{repo_reviews}.number = ? and #{repo}.assignee_id = #{repo_reviews}.user_id"
rows = @db.execute(query, [number, number])
if rows.length == 0
return 0
end
return rows[0][0]
end
def get_by_attr(repo, number, attr)
repo = sanitize_repo_name(repo)
query = "select #{attr} from #{repo}
where #{repo}.number = ?"
rows = @db.execute(query, number)
if rows.length == 0
return nil
end
return rows[0][0]
end
def get_branch(repo, number)
return get_by_attr(repo, number, 'target_branch')
end
def get_user_login(repo, number)
return get_by_attr(repo, number, 'user_login')
end
def is_self_assigned(repo, number)
repo = sanitize_repo_name(repo)
query = "select user_id, assignee_id from #{repo}
where #{repo}.number = ?"
rows = @db.execute(query, number)
if rows.length == 0
return false
end
return rows[0][0] == rows[0][1]
end
def get_pr_entry(repo, number)
repo = sanitize_repo_name(repo)
query = "select * from #{repo} where number = ?"
rows = @db.execute(query, number)
if rows.length == 0
return nil
end
return rows[0]
end
def self.pr_entry_get_sha(entry)
return entry[7]
end
def show_prs(repo)
repo = sanitize_repo_name(repo)
query = "select * from #{repo}"
@db.execute(query) do |row|
puts row
end
end
private :sanitize_repo_name, :create_tables, :get_by_attr
end
class StyleChecker
def initialize()
end
def sanitize_repo_name(repo)
return repo.sub('/', '---').gsub("-", "_")
end
def check_uri(uri, repo, sha, check_cmd)
# creating a new process
puts "Spawning child process"
pid = Process.fork do
puts "Cheking style for #{repo} @ #{sha}"
Dir.mktmpdir do |dir|
repo_path = File.join(dir, "repo")
puts "Cloning repo #{uri} under #{repo_path}"
g = Git.clone(uri, repo_path)
Dir.chdir(repo_path) do
g.checkout(sha)
cmd = `#{check_cmd}`
status = $?.exitstatus
if status != 0
fname = sanitize_repo_name(repo) + "_" + sha
puts "Status check failed, writing log to #{fname}"
File.open(File.join(Dir.tmpdir(), fname), 'w') do |logf|
logf.write(cmd)
end
else
puts "Style check is a success"
end
exit $?.exitstatus
end
end
end # end of child process code
Process.wait
status = $?.exitstatus
puts "Child process returned status #{status}"
return (status == 0)
end
def check(access_mode, repo, sha, check_cmd)
if access_mode == "ssh"
uri = "[email protected]:#{repo}.git"
else
uri = "https://github.com/#{repo}.git"
end
return check_uri(uri, repo, sha, check_cmd)
end
private :check_uri, :sanitize_repo_name
end
class CrhubState
def initialize(config)
@config = config
@repo_configs = config.get_repo_configs()
@repos = config.get_raw_repo_names()
@the_db = CrhubDB.new(@repos, config.get_db_path())
@style_checker = StyleChecker.new()
@sync = {}
@repos.each do |repo|
repo_state = {}
repo_state[:sema] = Mutex.new
repo_state[:busy] = Set.new
@sync[repo] = repo_state
end
end
def get_score(comment_body)
score = nil
case comment_body
when "+1"
score = 1
when "-1"
score = -1
end
return score
end
def set_pr_status(repo, number, sha, status, options)
target_branch = @the_db.get_branch(repo, number)
if target_branch != "master"
return
end
client = Octokit::Client.new(:access_token => $ACCESS_TOKEN)
client.create_status(repo, sha, status, options)
end
def set_pr_review_status(repo, number, sha, status)
options = {}
options[:context] = "crhub"
options[:description] = "checks code review status"
set_pr_status(repo, number, sha, status, options)
end
def set_pr_style_status(repo, number, sha, status)
puts "Setting style status to '#{status}'"
options = {}
options[:context] = "crhub-clang"
options[:description] = "checks code style status with clang-format"
set_pr_status(repo, number, sha, status, options)
puts "Style status set"
end
def repo_name_from_pr(pr)
return pr['base']['repo']['full_name']
end
def push_status(repo, number, sha)
user_login = @the_db.get_user_login(repo, number)
users_bypass = @repo_configs[repo].users_bypass
users_with_self_assign = @repo_configs[repo].users_with_self_assign
if users_bypass.include?(user_login)
puts "User #{user_login} is a bypass user for repo #{repo}"
puts "Status is therefore 'success'"
set_pr_review_status(repo, number, sha, "success")
return
end
with_self_assign = @repo_configs[repo].with_self_assign
if not with_self_assign and
not users_with_self_assign.include?(user_login) and
@the_db.is_self_assigned(repo, number)
puts "Self-assign is disabled for repo #{repo} and user " +
"#{user_login}, status is 'failure'"
set_pr_review_status(repo, number, sha, "failure")
return
end
score = @the_db.get_status(repo, number)
if score > 0
set_pr_review_status(repo, number, sha, "success")
else
set_pr_review_status(repo, number, sha, "failure")
end
end
def request_access(repo, pr_number)
repo_state = @sync[repo]
repo_state[:sema].lock()
while repo_state[:busy].include?(pr_number)
repo_state[:sema].unlock()
sleep(0)
repo_state[:sema].lock()
end
repo_state[:busy].add(pr_number)
repo_state[:sema].unlock()
end
def release_access(repo, pr_number)
repo_state = @sync[repo]
repo_state[:sema].synchronize {
repo_state[:busy].delete(pr_number)
}
end
def has_style_check(repo)
checker_cmd = @repo_configs[repo].checker_cmd
return !checker_cmd.nil?
end
def check_style(repo, number, sha)
if not has_style_check(repo)
return
end
access_mode = @repo_configs[repo].access_mode
checker_cmd = @repo_configs[repo].checker_cmd
success = @style_checker.check(access_mode, repo, sha, checker_cmd)
if success
set_pr_style_status(repo, number, sha, "success")
else
set_pr_style_status(repo, number, sha, "failure")
end
end
def process_pr(pr)
repo = repo_name_from_pr(pr)
set_pr_review_status(repo, pr['number'], pr['head']['sha'], "pending")
if has_style_check(repo)
set_pr_style_status(repo, pr['number'], pr['head']['sha'], "pending")
end
entry = [pr['number'], pr['id'], pr['title'],
pr['user']['id'], pr['user']['login']]
score = 0
if pr['assignee']
entry += [pr['assignee']['id'], pr['assignee']['login']]
else
# no assignee yet
entry += [nil, nil]
end
entry += [pr['head']['sha']]
entry += [pr['base']['ref']] # target branch
# puts entry.join(", ")
request_access(repo, pr['number'])
changed = @the_db.update_pr(repo, *entry)
push_status(repo, pr['number'], pr['head']['sha'])
release_access(repo, pr['number'])
check_style(repo, pr['number'], pr['head']['sha'])
puts "PR has been processed"
end
def process_pr_comment(repo, issue, comment)
# necessary?
request_access(repo, issue['number'])
db_entry = @the_db.get_pr_entry(repo, issue['number'])
if not db_entry
puts "No PR matching comment, ignoring"
release_access(repo, issue['number'])
return
end
sha = CrhubDB.pr_entry_get_sha(db_entry)
set_pr_review_status(repo, issue['number'], sha, "pending")
score = get_score(comment['body'])
if score
@the_db.add_review(repo, issue['number'], comment["user"]["id"], score)
end
push_status(repo, issue['number'], sha)
release_access(repo, issue['number'])
end
end
class Crhub < Sinatra::Base
def initialize()
super()
# TODO(antonin): put this in configure (as setting) and remove this method?
@state = CrhubState.new($config)
end
configure do
set :environment, "production"
$config.get_sinatra_config().each do |key, value|
set key, value
end
# set :bind, '0.0.0.0'
end
# for testing only
get '/' do
"Hello World!"
end
post '/codereview' do
@payload = JSON.parse(request.body.read)
case request.env['HTTP_X_GITHUB_EVENT']
when "pull_request"
action = @payload["action"]
case action
when "opened"
process_pull_request_opened(@payload["pull_request"])
when "reopened"
process_pull_request_opened(@payload["pull_request"])
when "closed"
process_pull_request_closed(@payload["pull_request"])
when "labeled"
process_pull_request_labeled(@payload["pull_request"],
@payload["label"])
when "unlabeled"
process_pull_request_unlabeled(@payload["pull_request"],
@payload["label"])
when "assigned"
process_pull_request_assigned(@payload["pull_request"])
when "unassigned"
process_pull_request_unassigned(@payload["pull_request"])
when "synchronize"
process_pull_request_synchronize(@payload["pull_request"])
end
when "status"
process_status()
when "commit_comment"
process_commit_comment()
when "issue_comment"
process_issue_comment(@payload["repository"]["full_name"],
@payload["issue"], @payload["comment"])
when "pull_request_review_comment"
process_pull_request_review_comment()
end
end
helpers do
def process_pull_request_opened(pull_request)
puts "Opened PR #{pull_request['title']}"
@state.process_pr(pull_request)
end
def process_pull_request_closed(pull_request)
puts "Closed PR #{pull_request['title']}"
# do nothing for now
end
def process_pull_request_labeled(pull_request, label)
puts "Labeled PR #{pull_request['title']} with #{label['name']}"
# do nothing for now
end
def process_pull_request_unlabeled(pull_request, label)
puts "Unlabeled PR #{pull_request['title']}: removed #{label['name']}"
# do nothing for now
end
def process_pull_request_assigned(pull_request)
puts "Assigned PR #{pull_request['title']}"
@state.process_pr(pull_request)
end
def process_pull_request_unassigned(pull_request)
puts "Unassigned PR #{pull_request['title']}"
@state.process_pr(pull_request)
end
def process_pull_request_synchronize(pull_request)
puts "Synchronize PR #{pull_request['title']}"
@state.process_pr(pull_request)
end
def process_status()
puts "A status!"
# do nothing for now
end
def process_commit_comment()
puts "A commit comment!"
# do nothing for now
end
def process_issue_comment(repo, issue, comment)
puts "An issue comment for issue #{issue['title']}"
if not issue.key?("pull_request")
return
end
puts "Issue is a pull request"
@state.process_pr_comment(repo, issue, comment)
end
def process_pull_request_review_comment()
puts "A PR review comment!"
# do nothing for now
end
end
# start the server if ruby file executed directly (or through daemon)
run! if app_file == $0 or "crhub_daemon" == $0
end