-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.rb
More file actions
1722 lines (1476 loc) · 60.5 KB
/
Copy pathcore.rb
File metadata and controls
1722 lines (1476 loc) · 60.5 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
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require 'octokit'
require 'yaml'
require 'json'
require 'httparty'
require 'net/smtp'
require 'sqlite3'
require 'bcrypt'
require 'securerandom'
require 'aws-sdk'
require 'stomp'
require 'open-uri'
require 'pry' # remove this when not needed
require 'open-uri'
require 'debian_control_parser'
require 'tmpdir'
require 'rgl/adjacency'
require 'rgl/traversal'
require 'rgl/connected_components'
# When testing, there should be a stomp (rabbitmq) broker running, like so:
# sudo docker run -d -e RABBITMQ_NODENAME=my-rabbit --name rabbitmq -p 61613:61613 resilva87/docker-rabbitmq-stomp
## To manually read a payload
## require 'json' ; file = File.read("./payloadOpenIssue") ; obj = JSON.parse(file)
class String
# Strip leading whitespace from each line that is the same as the
# amount of whitespace on the first line of the string.
# Leaves _additional_ indentation on later lines intact.
def unindent()
gsub(/^#{self[/\A[ \t]*/]}/,'')
end
end
class InvalidSegmentNumberError < StandardError; end
class InvalidCharacterError < StandardError; end
class BiocVersion
@x = 0
@y = 0
@z = 0
attr_reader :x, :y, :z
def initialize(version_string)
segs = version_string.strip.split('.')
unless segs.length == 3
raise InvalidSegmentNumberError
end
for seg in segs
fail = false
if seg.include? '-'
fail = true
end
begin
Integer(seg)
rescue
fail = true
end
if fail
raise InvalidCharacterError
end
end
@x = Integer(segs[0])
@y = Integer(segs[1])
@z = Integer(segs[2])
end
def compare(other)
if @x > other.x
return 1
end
if @x < other.x
return -1
end
if @x == other.x
if @y > other.y
return 1
end
if @y < other.y
return -1
end
if @y == other.y
if @z > other.z
return 1
end
if @z < other.z
return -1
end
return 0
end
end
end
end
class CoreConfig
@@request_uri = nil
@@db = nil
@@auth_config = nil
@@labels = {
AWAITING_MODERATION_LABEL: "1. awaiting moderation",
REVIEW_IN_PROGRESS_LABEL: "2. review in progress",
ACCEPTED_LABEL: "3a. accepted",
DECLINED_LABEL: "3b. declined",
INACTIVE_LABEL: "3c. inactive",
PREREVIEW_LABEL: "pre-review",
PREAPPROVED: "pre-check passed",
PENDING_CHANGES: "3e. pending pre-review changes",
NEED_INTEROP: "3d. needs interop",
VERSION_BUMP_LABEL: "VERSION BUMP REQUIRED",
ABNORMAL_LABEL: "ABNORMAL",
ERROR_LABEL: "ERROR",
OK_LABEL: "OK",
TIMEOUT_LABEL: "TIMEOUT",
WARNINGS_LABEL: "WARNINGS",
TESTING_LABEL: "TESTING"
}
def self.set_request_uri(request_uri)
@@request_uri = request_uri
end
def self.request_uri
@@request_uri
end
def self.set_db(db)
@@db = db
end
def self.db
@@db
end
def self.set_auth_config(auth_config)
@@auth_config = auth_config
end
def self.auth_config
@@auth_config
end
def self.labels
@@labels
end
end
module Core
# here, define the repo we care about
# and other stuff.
# this repo must have bioc-issue-bot as a collaborator, and
# must have a hook defined that points to this app and
# pushes (at least) the "Issues" and "Issue comments" events.
# After 'registering' their package in our issues repo,
# developers can then set up webhooks to send us push
# notifications.
# Also need to make sure that the repos has whatever
# custom labels (defined on issues) that we make use
# of in this script.
# (See CoreConfig.labels definition)
# See https://help.github.com/articles/creating-and-editing-labels-for-issues-and-pull-requests/
CoreConfig.set_auth_config(YAML::load_file(File.join(File.dirname(__FILE__), "auth.yml" )))
$LastCommitId = "initialize"
GITHUB_URL_REGEX = %r{https://github.com/[^/]+/[^ /\s]+}
NEW_ISSUE_REPO = CoreConfig.auth_config['issue_repo']
REQUIRE_PREAPPROVAL = CoreConfig.auth_config['require_preapproval']
# Version number checks
# x.99.z valid syntax check
PKG_VER_REGEX = %r{^[0-9]+[-\\.]99[-\\.][0-9]+$}
# x should be 0 unless pre-release check
PKG_VER_X_REGEX = %r{^[0]+}
# FIXME - do authentication more often (on requests?) so it doesn't go stale?
# Not sure yet if this is a problem.
# A note about OAuth. When setting up the token, it must have
# the 'public_repo' scope (and when we are testing using a private
# repos, it must be an admin-level contributor to the repos, and have
# "repo" scope).
def Core.authenticate()
Octokit.configure do |c|
c.access_token = CoreConfig.auth_config['auth_key']
end
end
Core.authenticate()
dbfile = File.join(File.dirname(__FILE__), "db.sqlite3" )
CoreConfig.set_db(SQLite3::Database.new dbfile)
if (!File.exists? dbfile) or (File.size(dbfile) == 0)
rows = CoreConfig.db.execute <<-SQL.unindent
create table repos (
id integer primary key,
name varchar(255) unique not null,
pw_hash varchar(255) not null,
issue_number integer not null,
login varchar(255)
);
SQL
end
def Core.get_repo_issue_number(repo)
rows = CoreConfig.db.execute("select issue_number from repos where name = ?",
repo.sub(/https:\/\/github.com\//i, ""))
return nil if rows.empty?
rows.first.first
end
def Core.get_repo_issue_number_git(pkgname)
call = "select issue_number from repos where name LIKE '%/" + pkgname + "'"
rows = CoreConfig.db.execute(call)
return nil if rows.empty?
rows.first.first
end
def Core.get_repo_by_issue_number(issue_number)
results_as_hash = CoreConfig.db.results_as_hash
begin
CoreConfig.db.results_as_hash = true
rows = CoreConfig.db.execute("select * from repos where issue_number = ?",
issue_number)
return nil if rows.empty?
return rows.first
ensure
CoreConfig.db.results_as_hash = results_as_hash
end
end
def Core.get_repo_by_repo_name(repo_name)
results_as_hash = CoreConfig.db.results_as_hash
begin
CoreConfig.db.results_as_hash = true
rows = CoreConfig.db.execute("select * from repos where name = ?",
repo_name)
return nil if rows.empty?
return rows.first
ensure
CoreConfig.db.results_as_hash = results_as_hash
end
end
def Core.count_ssh_keys(repos)
user_keys_url = repos.split("/")[0..3].join("/") + ".keys"
return HTTParty.get(user_keys_url).response.body.lines.count
end
# Count keys on login
def Core.count_login_keys(login)
login_keys_url = "https://github.com/" + login + ".keys"
return HTTParty.get(login_keys_url).response.body.lines.count
end
def Core.add_repos_to_db(repos, hash, issue_number, login)
CoreConfig.db.execute "insert into repos (name, pw_hash, issue_number, login) values (?,?,?,?)",
repos.sub(/https:\/\/github.com\//i, ""), hash, issue_number, login
end
def Core.is_spoof? (request)
# we should use https and basic auth as described at
# https://help.github.com/articles/what-ip-addresses-does-github-use-that-i-should-whitelist/#service-hook-ip-addresses
# although I'm not sure exactly how this is supposed to work.
# At that point, request.env['REQUEST_URI'] will start with 'https://'
# (i guess) if https is available.
# However, in development we don't have that yet. So use IP whitelisting.
# Also, this is not ipv6 compatible.
if request.env.has_key? 'HTTP_X_FORWARDED_FOR'
ip = request.env['HTTP_X_FORWARDED_FOR']
else
ip = request.env['REMOTE_ADDR']
end
return false if request.env.has_key? "HTTP_HOST" and
["127.0.0.1", "localhost"].include? request.env['HTTP_HOST']
return false if ip == "127.0.0.1" # allow local testing
regex = %r{^192\.30\.25[2345]\.}
test = ip !~ regex
if (test)
regex = %r{^185\.199\.1(0[89]|1[01])\.}
test = ip !~ regex
end
if (test)
regex = %r{^140.82.1(1[23456789]|2[01234567])\.}
test = ip !~ regex
end
test
end
def Core.handle_post(request)
if CoreConfig.request_uri.nil?
CoreConfig.set_request_uri(request.base_url)
end
if Core.is_spoof? request
puts "Unknown IP address"
return [400, "Unknown IP address"]
end
begin
json = request.body.read
obj = JSON.parse json
rescue JSON::ParserError
return [400, "Failed to parse JSON"]
end
if (obj.has_key? 'zen')
return [200, "ping received, Bioconductor/Contributions webhook ok"]
end
if (!obj.has_key? 'action') and (!obj.has_key? 'ref')
return [400, "Only push, issue, and issue comment event hooks supported"]
end
if (obj.has_key? 'ref') and (obj.has_key? 'after') and (obj['after'] != $LastCommitId)
$LastCommitId = obj['after']
return Core.handle_push(obj)
end
if (obj.has_key? 'ref') and (obj.has_key? 'after') and (obj['after'] == $LastCommitId)
return [200, "repeated action"]
end
if obj.has_key? 'action' and obj['action'] == "created"
return Core.handle_issue_comment(obj)
end
if obj.has_key? 'action' and obj['action'] == "labeled"
return Core.handle_issue_label_added(obj)
end
if obj['repository']['full_name'] != Core::NEW_ISSUE_REPO
puts "Unknown repository #{obj['repository']['full_name']}"
return [400, "Unknown repository"]
end
if obj.has_key? 'action' and obj['action'] == "opened"
return Core.handle_new_issue(obj)
end
if (obj.has_key? 'action') and (obj['action'] == "reopened")
return Core.handle_reopened_issue(obj)
end
if (obj.has_key? 'action') and (obj['action'] == "closed")
return Core.handle_closed_issue(obj)
end
[200, 'Post handled']
end
def Core.handle_issue_comment(obj)
# TODO implement
# issue comments may be used to submit additional packages
# (such as experiment data packages) to be reviewed together
# with the main package.
# Be sure and ignore all comments posted by this bot itself.
login = obj['comment']['user']['login']
if login == Octokit.user.login
return "ignoring a comment that I made myself."
end
if login != obj['issue']['user']['login']
return "ignoring comment that's not from the creator of the issue."
end
comment = obj['comment']['body']
lines = comment.split("\n")
pkg_line = lines.detect{|i| i =~ /AdditionalPackage: /}
if pkg_line.nil?
return "comment did not contain AdditionalPackage: tag"
end
match = pkg_line.scan(Core::GITHUB_URL_REGEX)
if match.empty?
return "no github URL in AdditionalPackage: line"
end
issue_state = obj['issue']['state']
labels = obj['issue']['labels'].map{|i| i['name']}
build_ok1 = (issue_state == "open" and (labels.include? CoreConfig.labels[:REVIEW_IN_PROGRESS_LABEL] or labels.include? CoreConfig.labels[:PREREVIEW_LABEL]))
build_ok2 = (issue_state == "closed" and labels.include? CoreConfig.labels[:TESTING_LABEL])
issue_number = obj['issue']['number']
unless build_ok1 or build_ok2
msg = "Can't build unless issue is open and past pre-review stage, or issue is closed and '#{CoreConfig.labels[:TESTING_LABEL]}' label is present."
Octokit.add_comment(Core::NEW_ISSUE_REPO, issue_number, msg)
return msg
end
full_repos_url = match.first.strip
repos_url = full_repos_url.sub("https://github.com/", "")
unless Core.repo_exists_in_github? (repos_url) # github url points to nonexistent repos
return Core.handle_repo_does_not_exist(repos_url, issue_number, login,
close=true)
end
github_repo_name = Core.get_repo_name(repos_url)
unless repos_url == github_repo_name
return Core.handle_caps_check_failed(repos_url, github_repo_name,
issue_number, login, close=false)
end
description = Core.get_description_file(repos_url)
if description.nil?
return Core.handle_no_description_file(full_repos_url, issue_number, login,
close=false)
end
existing_issue_number = Core.get_repo_issue_number(repos_url)
if not existing_issue_number.nil?
return Core.handle_existing_issue(existing_issue_number, issue_number, login,
close=false)
end
pkgname = repos_url.partition('/').last
existing_issue_number2 = Core.get_repo_issue_number_git(pkgname)
if not existing_issue_number2.nil?
return Core.handle_existing_issue2(existing_issue_number2, issue_number, login)
end
password = SecureRandom.hex(20)
hash = BCrypt::Password.create(password)
Core.add_repos_to_db(repos_url, hash, issue_number, login)
comment= <<-END
Hi @#{login},
Thanks for submitting your additional package: #{full_repos_url}.
We are taking a quick look at it and you will hear back from us soon.
END
comment = comment.unindent
comment = comment.encode("UTF-8", invalid: :replace, undef: :replace, replace: "")
Octokit.add_comment(Core::NEW_ISSUE_REPO, issue_number, comment)
return Core.handle_preapproval_additional_package(repos_url, issue_number, password)
end
# FIXME This checks if our DB already has a row with the same repos name.
# We should also check if GitHub already has a repo with this name.
def Core.handle_existing_issue(existing_issue_number, issue_number, login,
close=true)
comment= <<-END.unindent
Dear @#{login} ,
You (or someone) has already posted that repository to our tracker.
See https://github.com/#{Core::NEW_ISSUE_REPO}/issues/#{existing_issue_number}
You cannot post the same repository more than once.
If you would like this repository to be linked to issue number: #{issue_number},
Please contact a Bioconductor Core Member.
END
if close
comment += "I am closing this issue."
Core.close_issue(issue_number)
end
Octokit.add_comment(Core::NEW_ISSUE_REPO, issue_number, comment)
return "duplicate issue"
end
def Core.handle_existing_issue2(existing_issue_number, issue_number, login,
close=true)
comment= <<-END.unindent
Dear @#{login} ,
You (or someone) has already posted a repository with the same name to our tracker.
See https://github.com/#{Core::NEW_ISSUE_REPO}/issues/#{existing_issue_number}
You cannot post the same repository more than once and packages are not
allowed to have the same name.
If you would like this repository to be linked to issue number: #{issue_number},
Please contact a Bioconductor Core Member.
END
if close
comment += "I am closing this issue."
Core.close_issue(issue_number)
end
Octokit.add_comment(Core::NEW_ISSUE_REPO, issue_number, comment)
return "duplicate name issue"
end
def Core.handle_closed_issue(obj)
login = obj['issue']['user']['login']
body = obj['issue']['body']
issue_number = obj['issue']['number']
issue = Octokit.issue(Core::NEW_ISSUE_REPO, issue_number)
labels = Octokit.labels_for_issue(Core::NEW_ISSUE_REPO, issue_number).
map{|i| i.name}
if labels.include? CoreConfig.labels[:REVIEW_IN_PROGRESS_LABEL]
Octokit.remove_label(
CoreConfig.auth_config['issue_repo'], issue_number,
CoreConfig.labels[:REVIEW_IN_PROGRESS_LABEL])
end
if labels.include? CoreConfig.labels[:PREREVIEW_LABEL]
Octokit.remove_label(
CoreConfig.auth_config['issue_repo'], issue_number,
CoreConfig.labels[:PREREVIEW_LABEL])
end
return "handle closed issue"
end
# When you want to close an issue, use this.
def Core.close_issue(issue_number, issue=nil)
unless Core.is_authenticated?
Core.authenticate
end
if issue.nil?
issue = Octokit.issue(Core::NEW_ISSUE_REPO, issue_number)
end
title = issue['title']
title = "(inactive) " + title unless title.start_with? "(inactive) "
unless title == issue['title']
Octokit.update_issue(Core::NEW_ISSUE_REPO, issue_number,
title, issue['body'])
end
labels = Octokit.labels_for_issue(
CoreConfig.auth_config['issue_repo'], issue_number)
has_review_label = labels.find { |i| i.name == CoreConfig.labels[:REVIEW_IN_PROGRESS_LABEL]}
if has_review_label
Octokit.remove_label(
CoreConfig.auth_config['issue_repo'], issue_number,
CoreConfig.labels[:REVIEW_IN_PROGRESS_LABEL])
end
has_review_label = labels.find { |i| i.name == CoreConfig.labels[:PREREVIEW_LABEL]}
if has_review_label
Octokit.remove_label(
CoreConfig.auth_config['issue_repo'], issue_number,
CoreConfig.labels[:PREREVIEW_LABEL])
end
# This should be the only place where Octokit.close_issue is called directly.
Octokit.close_issue(Core::NEW_ISSUE_REPO, issue_number)
end
def Core.handle_issue_label_added(obj)
login = obj['issue']['user']['login']
if login == Octokit.user.login
return "ignoring a comment that i made myself."
end
issue_number = obj['issue']['number']
if obj["label"]["name"] == CoreConfig.labels[:ACCEPTED_LABEL]
package_repos =
obj['issue']['body'].split("\n").
find {|i| i.start_with? "- Repository: "}.sub("- Repository: ", "").
strip
package = obj['issue']['title']
recipient_email = CoreConfig.auth_config["email_recipient"]
recipient_name = CoreConfig.auth_config["email_recipient_name"]
from_email = "bioc-github-noreply@bioconductor.org"
from_name = "Bioconductor Issue Tracker"
subject = "Package #{package} (issue #{issue_number}) has been accepted."
message= <<-END.unindent
Dear Bioconductor package administrator,
Package '#{package}' accepted.
Issue: https://github.com/#{Core::NEW_ISSUE_REPO}/issues/#{issue_number}
Source: #{package_repos}
Thanks,
#{Octokit.user.login}
END
Core.send_email("#{from_name} <#{from_email}>",
"#{recipient_name} <#{recipient_email}>",
subject,
message)
comment= <<-END.unindent
Your package has been accepted. It will be added to the
Bioconductor nightly builds.
Thank you for contributing to Bioconductor!
Reviewers for Bioconductor packages are volunteers from the Bioconductor
community. If you are interested in becoming a Bioconductor package
reviewer, please see [Reviewers Expectations][revexp].
[revexp]: http://contributions.bioconductor.org/reviewer-resources-overview.html
END
Octokit.add_comment(Core::NEW_ISSUE_REPO, issue_number, comment)
return "ok, package accepted"
elsif obj['label']['name'] == CoreConfig.labels[:DECLINED_LABEL]
Core.close_issue(issue_number)
return "ok, package declined, issue closed"
elsif obj['label']['name'] == CoreConfig.labels[:INACTIVE_LABEL]
comment= <<-END.unindent
This issue is being closed because there has been no progress
for an extended period of time. You may reopen the issue when
you have the time to actively participate in the review /
submission process. Please also keep in mind that a package
accepted to Bioconductor requires a commitment on your part to
ongoing maintenance.
Thank you for your interest in Bioconductor.
END
Octokit.add_comment(Core::NEW_ISSUE_REPO, issue_number, comment)
Core.close_issue(issue_number)
return "ok, package inactive, issue closed"
elsif obj['label']['name'] == CoreConfig.labels[:REVIEW_IN_PROGRESS_LABEL]
comment= <<-END.unindent
A reviewer has been assigned to your package for an indepth review.
Please respond accordingly to any further comments from the reviewer.
END
Octokit.add_comment(Core::NEW_ISSUE_REPO, issue_number, comment)
assignee = Core.get_issue_assignee(issue_number)
unless assignee.nil?
Octokit.update_issue(Core::NEW_ISSUE_REPO, issue_number, assignee: assignee)
end
labels = Octokit.labels_for_issue(Core::NEW_ISSUE_REPO,
issue_number).map{|i| i.name}
if labels.include? CoreConfig.labels[:PREREVIEW_LABEL]
Octokit.remove_label(CoreConfig.auth_config['issue_repo'],issue_number,
CoreConfig.labels[:PREREVIEW_LABEL])
end
return [200, "OK; assigned reviewer"]
end
return "handle_issue_label_added"
end
def Core.handle_git_push(request)
begin
json = request.body.read
obj = JSON.parse json
rescue JSON::ParserError
return [400, "Failed to parse JSON"]
end
if (!obj.has_key? 'pkgname')
return [400, "Request must include pkgname"]
end
if (!obj.has_key? 'commit_id')
return [400, "Request must include commit_id"]
end
pkgname = obj['pkgname'].to_s
commit_id = obj['commit_id'].to_s
giturl = "https://git.bioconductor.org/packages/" + pkgname
issue_number = get_repo_issue_number_git(pkgname)
# figure out what to do...
build_ok = false
newpackage = false
if !issue_number.nil?
issue = Octokit.issue(Core::NEW_ISSUE_REPO, issue_number)
labels = Octokit.labels_for_issue(Core::NEW_ISSUE_REPO, issue_number).
map{|i| i.name}
if (issue['state'] == "open" and (labels.include? CoreConfig.labels[:REVIEW_IN_PROGRESS_LABEL] or labels.include? CoreConfig.labels[:PREREVIEW_LABEL]))
build_ok = true
newpackage = true
elsif issue['state'] == "open"
build_ok = false
newpackage = true
elsif (issue['state'] == "closed" and labels.include? CoreConfig.labels[:TESTING_LABEL])
build_ok = true
newpackage = true
elsif labels.include? CoreConfig.labels[:ACCEPTED_LABEL]
# when building ALL packges on commit
# package issue number exists and already accepted into bioc
#build_ok = true
end
# else
# # when building ALL packages on commit
# # older packages without issue_number
# build_ok = true
end
# ...now do it
if build_ok
if newpackage
comment = "Received a valid push on git.bioconductor.org; starting a build for commit id: " + commit_id
Octokit.add_comment(Core::NEW_ISSUE_REPO, issue_number, comment)
end
Core.start_build(giturl, issue_number, commit_id, newpackage=newpackage)
return [200, "OK starting build"]
else
if newpackage
comment = <<-END.unindent
cannot build unless issue is open and has the
'#{CoreConfig.labels[:PREREVIEW_LABEL]}' label or '#{CoreConfig.labels[:REVIEW_IN_PROGRESS_LABEL]}' label,
or is closed and has the '#{CoreConfig.labels[:TESTING_LABEL]}' label.
END
Octokit.add_comment(Core::NEW_ISSUE_REPO, issue_number, comment)
return [400, "Issue not in a state to allow building"]
end
return [200, "OK; Not building existing package or closed issue."]
end
end
def Core.handle_push(obj)
repos = obj['repository']['full_name']
db_record = get_repo_by_repo_name(repos)
if db_record.nil?
return "Sorry, you haven't told us about this repository, please
go to https://github.com/#{Core::NEW_ISSUE_REPO}/issues/new ."
end
issue_number = db_record['issue_number']
issue = Octokit.issue(Core::NEW_ISSUE_REPO, issue_number)
repos_obj = Octokit.repository(repos)
default_branch = repos_obj['default_branch']
push_branch = obj['ref'].sub("refs/heads/", "")
unless push_branch == default_branch
return "#{push_branch} branch ignored, only handling #{default_branch}"
end
build_ok = false
labels = Octokit.labels_for_issue(Core::NEW_ISSUE_REPO, issue_number).
map{|i| i.name}
if issue['state'] = "open" and (labels.include? CoreConfig.labels[:REVIEW_IN_PROGRESS_LABEL] or labels.include? CoreConfig.labels[:PREREVIEW_LABEL])
build_ok = true
elsif issue['state'] = "closed" and labels.include? CoreConfig.labels[:TESTING_LABEL]
build_ok = true
end
if build_ok
labels = Octokit.labels_for_issue(
CoreConfig.auth_config['issue_repo'], issue_number)
has_version_bump_label = labels.find { |i| i.name == CoreConfig.labels[:VERSION_BUMP_LABEL]}
unless Core.version_has_bumped? obj
if not has_version_bump_label
Octokit.add_labels_to_an_issue(
CoreConfig.auth_config['issue_repo'], issue_number,
[CoreConfig.labels[:VERSION_BUMP_LABEL]])
end
return "version bump required"
end
if has_version_bump_label
Octokit.remove_label(
CoreConfig.auth_config['issue_repo'], issue_number,
CoreConfig.labels[:VERSION_BUMP_LABEL])
end
comment= "Please remember to push to git.bioconductor.org to trigger a new build"
Octokit.add_comment(Core::NEW_ISSUE_REPO, issue_number, comment)
return [400,"Tried to build from github"]
else
return "can't build unless issue is open and has the '#{CoreConfig.labels[:PREREVIEW_LABEL]}'
label or '#{CoreConfig.labels[:REVIEW_IN_PROGRESS_LABEL]}' label, or is closed and has the '#{CoreConfig.labels[:TESTING_LABEL]}' label."
end
end
def Core.version_has_bumped? (push_obj)
repos = push_obj['repository']['full_name']
before = push_obj['before']
after = push_obj['after']
comp = Octokit.compare(repos, before, after)
desc = comp[:files].find{|i| i[:filename] == "DESCRIPTION"}
return false if desc.nil?
return Core.does_patch_have_version_bump? (desc[:patch])
end
def Core.does_patch_have_version_bump? (patch)
lines = patch.split /\r|\n|\r\n/
lines.reject! {|i| i.empty? }
oldversion = lines.find {|i| i.start_with? "-Version:"}
return false if oldversion.nil?
oldversion = oldversion.sub("-Version:", "").strip
newversion = lines.find {|i| i.start_with? "+Version:"}
return false if newversion.nil?
newversion = newversion.sub("+Version:", "").strip
begin
old_v = BiocVersion.new(oldversion)
new_v = BiocVersion.new(newversion)
rescue
return false
end
return (new_v.compare(old_v) == 1)
end
def Core.handle_no_repos_url(issue_number, login)
comment= <<-END.unindent
Dear @#{login} ,
I couldn't find a GitHub repository URL in your issue text!
Please include a github repository URL, it should look like this:
https://github.com/username/reponame
I am closing this issue. Please try again with a new issue.
END
Octokit.add_comment(Core::NEW_ISSUE_REPO, issue_number, comment)
Core.close_issue(issue_number)
return "no github URL in new issue comment"
end
def Core.handle_multiple_urls(issue_number, login)
comment = <<-END.unindent
Dear @#{login} ,
I found more than one GitHub URL in your issue. Please make sure there
is only one, it should look like:
https://github.com/username/reponame
I am closing this issue. Please try again with a new issue.
END
Octokit.add_comment(Core::NEW_ISSUE_REPO, issue_number, comment)
Core.close_issue(issue_number)
return "found multiple URLs in new issue comment"
end
def Core.repo_exists_in_github? (repos_url)
begin
repos = Octokit.repository(repos_url)
return true
rescue Octokit::NotFound
return false
end
end
def Core.get_repo_name(repos_url)
url = "https://api.github.com/repos/"+repos_url
response = HTTParty.get(url)
return response.parsed_response["full_name"]
end
def Core.handle_bioconductor_mirror_repo(issue_number, login)
comment = <<-END.unindent
Dear @#{login} ,
Sorry, we don't build packages in the `Bioconductor-mirror`
organization. These packages have already been accepted
into _Bioconductor_.
This issue will now be closed.
END
Core.close_issue(issue_number)
Octokit.add_comment(Core::NEW_ISSUE_REPO, issue_number, comment)
return "Won't build repos in the Bioconductor-mirror organization."
end
def Core.handle_repo_does_not_exist(repos_url, issue_number, login, close=true)
comment = <<-END.unindent
Dear @#{login} ,
There is no repository called https://github.com/#{repos_url} .
You must submit the url to a valid, public GitHub repository.
END
if close
comment += "I am closing this issue. Please try again with a new issue."
Core.close_issue(issue_number)
end
Octokit.add_comment(Core::NEW_ISSUE_REPO, issue_number, comment)
return "repos does not exist"
end
def Core.handle_caps_check_failed(repos_url, github_repo_name, issue_number, login, close=true)
comment = <<-END.unindent
Dear @#{login} ,
The github link provided https://github.com/#{repos_url} ,
does not match the capitalization of the github repository:
#{github_repo_name}.
Please update the link for the repository on this issue page.
END
if close
comment += "I am closing this issue. Please try again with a new issue."
Core.close_issue(issue_number)
end
Octokit.add_comment(Core::NEW_ISSUE_REPO, issue_number, comment)
return "repo capitalization does not match"
end
def Core.get_description_response(repos_url)
repos_url = repos_url.sub(/\.git$/, "")
repos = Octokit.repository(repos_url)
default_branch = repos['default_branch']
desc_url = "https://raw.githubusercontent.com/#{repos_url}/#{default_branch}/DESCRIPTION"
return HTTParty.get(desc_url)
end
def Core.has_description_file?(repos_url)
response = Core.get_description_response(repos_url)
return response.code == 200
end
def Core.get_description_file(repos_url)
response = Core.get_description_response(repos_url)
return nil unless response.code == 200
return response.body
end
def Core.handle_no_description_file(full_repos_url, issue_number, login, close=true)
full_repos_url = full_repos_url.sub(/\.git$/, "")
comment = <<-END.unindent
Dear @#{login} ,
I could not find a DESCRIPTION file in the default branch of the
GitHub repository at #{full_repos_url} . This repository should
contain an R package.
END
if close
comment += "I am closing this issue. Please try again with a new issue."
Core.close_issue(issue_number)
end
Octokit.add_comment(Core::NEW_ISSUE_REPO, issue_number, comment)
return "no description file found!"
end
def Core.handle_package_name_mismatch(repos_url, package_name,
issue_number, login, close=true)
repos_package_name = repos_url.split('/').last
comment = <<-END.unindent
Dear @#{login},
The package repository name, '#{repos_package_name}', differs
from the package name in the DESCRIPTION file, '#{package_name}'.
Please rename your repository, and submit a new
issue. Alternatively, change the Package: field in the
DESCRIPTION file to match the name of the repository.
END
if close
comment += "I am closing this issue. Please try again with a new issue."
Core.close_issue(issue_number)
end
Octokit.add_comment(Core::NEW_ISSUE_REPO, issue_number, comment)
return "DESCRIPTION and issue package name differ!"
end
def Core.handle_bad_version_number(package_ver, issue_number, login)
comment = <<-END.unindent
Dear @#{login},
The package version number, '#{package_ver}', is not formatted
correctly. Expecting format: 'x.99.z'.
Please fix your version number. See [Bioconductor version numbers][1]
Please also remember to run [BiocCheck::BiocCheck('new-package'=TRUE)][2] on your package
before submitting a new issue. BiocCheck will look for other
Bioconductor package requirements.
[1]: http://contributions.bioconductor.org/description.html#description-ver
[2]: https://bioconductor.org/packages/BiocCheck/
END
Core.close_issue(issue_number)
Octokit.add_comment(Core::NEW_ISSUE_REPO, issue_number, comment)
return "Invalid Version Number!"
end
def Core.handle_x_version_number(package_ver, issue_number, login)
comment = <<-END.unindent
Dear @#{login},
The package version number, '#{package_ver}', does not start
with 0. Expecting format: '0.99.z' for new packages. Starting
with non-zero x of 'x.y.z' format is generally only allowed if
the package has been pre-released.
We recommend fixing the version number. See [Bioconductor version numbers][1]
Please also consider running [BiocCheck::BiocCheck('new-package'=TRUE)][2] on your package
to look for other Bioconductor package requirements.
[1]: http://contributions.bioconductor.org/description.html#description-ver
[2]: https://bioconductor.org/packages/BiocCheck/
END
Octokit.add_comment(Core::NEW_ISSUE_REPO, issue_number, comment)
return "x of version number non-zero."
end
def Core.check_biocviews(description, issue_number, login)
parser = DebianControlParser.new(description)
desc_hash = Hash.new
parser.fields do |name,value|
desc_hash["#{name}"] = "#{value}".gsub(/\s+/,"")
end
## check if book and if so skip biocViews checks
if desc_hash.keys.include? "BiocType"
bioctype = desc_hash["BiocType"].downcase
if bioctype == "book"
return [200, "biocviews skipped"]
end
end
## check if biocViews in DESCRIPTION
if !desc_hash.keys.include? "biocViews"
return Core.handle_no_biocviews(issue_number, login)
end
views = desc_hash["biocViews"]
## check if biocViews has any terms
if views.nil?
return Core.handle_no_biocviews(issue_number, login)
end
views = views.split(/\s*,\s*/)
## check if only top level trivial view
topLevelViews = ["Software", "AnnotationData", "ExperimentData", "Workflow"]
if (views - topLevelViews).empty?
return Core.handle_no_biocviews(issue_number, login)
end
tempDir = Dir.tmpdir
call = "git archive --remote=ssh://git@git.bioconductor.org/packages/biocViews devel inst/extdata/biocViewsVocab.sqlite | tar -x --strip=2 -C #{tempDir}"
system(call)
dbfileViews = "#{tempDir}/biocViewsVocab.sqlite"
dbv = SQLite3::Database.new(dbfileViews)
rows = dbv.execute("select * from biocViews")
g = RGL::DirectedAdjacencyGraph.new()
sort_order = []
for row in rows
g.add_edge(row.first, row.last)
sort_order.push row.first
end