forked from parrish/whedon-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkers.rb
410 lines (331 loc) · 13.3 KB
/
workers.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
class ReviewReminderWorker
require_relative 'github'
require_relative 'config_helper'
require 'sidekiq'
include Sidekiq::Worker
# Sets the Whedon environment
include ConfigHelper
# Including this means we can talk to GitHub from the background worker.
include GitHub
# Need to respond with different message if author (not a reviewer)
def perform(human, nwo, issue_id, config)
# Make sure we're working with GitHub handles with '@' at the start
unless human.start_with?('@')
human = "@#{human}"
end
issue = github_client.issue(nwo, issue_id)
return false if issue.state == 'closed'
author = issue.body.match(/\*\*Submitting author:\*\*\s*.(@\S*)/)[1]
# If the reminder is for the author then send the reminder, regardless
# of the state of the reviewer checklists. Otherwise, check if they
# need a reminder.
if human.strip == author
bg_respond(nwo, issue_id, ":wave: #{human}, please update us on how things are progressing here.")
else
if needs_reminder?(nwo, issue.body, human)
bg_respond(nwo, issue_id, ":wave: #{human}, please update us on how your review is going.")
end
end
end
end
class DOIWorker
require_relative 'github'
require_relative 'config_helper'
require 'faraday'
require 'ostruct'
require 'serrano'
require 'sidekiq'
require 'uri'
require 'whedon'
require 'yaml'
include Sidekiq::Worker
# Sets the Whedon environment
include ConfigHelper
# Including this means we can talk to GitHub from the background worker.
include GitHub
def perform(nwo, issue_id, config)
config = OpenStruct.new(config)
set_env(nwo, issue_id, config)
# Download the paper
stdout, stderr, status = download(issue_id)
if status.success?
paper_path = find_paper(issue_id)
bibtex_filename = YAML.load_file(paper_path)['bibliography']
bibtex_path = "#{File.dirname(paper_path)}/#{bibtex_filename}"
if bibtex_path
doi_summary = check_dois(bibtex_path)
if doi_summary.any?
message = "```Reference check summary:\n"
doi_summary.each do |type, messages|
message << "\n#{type.to_s.upcase} DOIs\n\n"
if messages.empty?
message << "- None\n"
else
messages.each {|m| message << "- #{m}\n"}
end
end
message << "```"
bg_respond(nwo, issue_id, message)
else
bg_respond(nwo, issue_id, "No immediate problems found with references.")
end
else
bg_respond(nwo, issue_id, "Can't find a bibtex file for this submission")
end
else
bg_respond(nwo, issue_id, "Downloading of the repository (to check the bibtex) failed for issue ##{issue_id} failed with the following error: \n\n #{stderr}") and return
end
end
# How different are two strings?
# https://en.wikipedia.org/wiki/Levenshtein_distance
def levenshtein_distance(s, t)
m = s.length
n = t.length
return m if n == 0
return n if m == 0
d = Array.new(m+1) {Array.new(n+1)}
(0..m).each {|i| d[i][0] = i}
(0..n).each {|j| d[0][j] = j}
(1..n).each do |j|
(1..m).each do |i|
d[i][j] = if s[i-1] == t[j-1] # adjust index into string
d[i-1][j-1] # no operation required
else
[ d[i-1][j]+1, # deletion
d[i][j-1]+1, # insertion
d[i-1][j-1]+1, # substitution
].min
end
end
end
d[m][n]
end
def crossref_lookup(query_value)
works = Serrano.works(:query => query_value)
if works['message']['items'].any?
if works['message']['items'].first.has_key?('DOI')
candidate = works['message']['items'].first
candidate_title = candidate['title'].first.downcase
candidate_doi = candidate['DOI']
distance = levenshtein_distance(candidate_title, query_value.downcase)
if distance < 3
return candidate_doi
else
return nil
end
end
end
end
# TODO: refactor this monster. Soon...
def check_dois(bibtex_path)
doi_summary = {:ok => [], :missing => [], :invalid => []}
entries = BibTeX.open(bibtex_path, :filter => :latex)
if entries.any?
entries.each do |entry|
next if entry.comment?
if entry.has_field?('doi') && !entry.doi.empty?
if invalid_doi?(entry.doi)
doi_summary[:invalid].push("#{entry.doi} is INVALID")
else
doi_summary[:ok].push("#{entry.doi} is OK")
end
# If there's no DOI present, check Crossref to see if we can find a candidate DOI for this entry.
else
if entry.has_field?('title')
if candidate_doi = crossref_lookup(entry.title.value)
doi_summary[:missing].push("https://doi.org/#{candidate_doi} may be missing for title: #{entry.title}")
end
end
end
end
end
return doi_summary
end
# Return true if the DOI doesn't resolve properly
def invalid_doi?(doi_string)
doi = doi_string.to_s[/\b(10[.][0-9]{4,}(?:[.][0-9]+)*\/(?:(?!["&\'])\S)+)\b/]
return true if doi.nil?
url = "https://doi.org/#{doi.strip}"
escaped_url = URI.escape(url)
begin
status_code = Faraday.head(escaped_url).status
if [301, 302].include? status_code
return false
else
return true
end
rescue Faraday::ConnectionFailed
return true
end
end
def find_paper(issue_id)
search_path ||= "tmp/#{issue_id}"
paper_paths = []
Find.find(search_path) do |path|
paper_paths << path if path =~ /paper\.md$/
end
return paper_paths.first
end
def download(issue_id)
FileUtils.rm_rf("tmp/#{issue_id}") if Dir.exist?("tmp/#{issue_id}")
Open3.capture3("whedon download #{issue_id}")
end
end
class RepoWorker
require_relative 'github'
require_relative 'config_helper'
require 'ostruct'
require 'rugged'
require 'licensee'
require 'linguist'
require 'sidekiq'
include Sidekiq::Worker
# Sets the Whedon environment
include ConfigHelper
# Including this means we can talk to GitHub from the background worker.
include GitHub
def perform(nwo, issue_id, config)
config = OpenStruct.new(config)
set_env(nwo, issue_id, config)
# Download the paper
stdout, stderr, status = download(issue_id)
if status.success?
languages = detect_languages(issue_id)
license = detect_license(issue_id)
label_issue(nwo, issue_id, languages) if languages.any?
bg_respond(nwo, issue_id, "Failed to discover a valid open source license.") if license.nil?
else
bg_respond(nwo, issue_id, "Downloading of the repository (to analyze the language) for issue ##{issue_id} failed with the following error: \n\n #{stderr}") and return
end
end
def detect_license(issue_id)
return Licensee.project("tmp/#{issue_id}").license
end
def detect_languages(issue_id)
repo = Rugged::Repository.new("tmp/#{issue_id}")
project = Linguist::Repository.new(repo, repo.head.target_id)
# Take top three languages from Linguist
project.languages.keys.take(3)
end
def download(issue_id)
FileUtils.rm_rf("tmp/#{issue_id}") if Dir.exist?("tmp/#{issue_id}")
Open3.capture3("whedon download #{issue_id}")
end
end
# This is the Sidekiq worker that processes PDFs. It leverages the Whedon gem to
# carry out the majority of its actions. Where possible, we try and capture
# errors from any of the executed tasks and report them back to the review issue
class PDFWorker
require_relative 'github'
require_relative 'config_helper'
require 'open3'
require 'ostruct'
require 'sidekiq'
include Sidekiq::Worker
# Sets the Whedon environment
include ConfigHelper
# Including this means we can talk to GitHub from the background worker.
include GitHub
def perform(nwo, issue_id, config, custom_branch)
config = OpenStruct.new(config)
set_env(nwo, issue_id, config)
# Download the paper
stdout, stderr, status = download(issue_id)
# Whedon often can't find a paper in the repository he's downloaded even
# though it's definitely there (e.g. https://github.com/openjournals/joss-reviews/issues/776#issuecomment-397714563)
# Not sure if this is because the repository hasn't downloaded yet.
# Adding in a sleep statement to see if this helps.
sleep(5)
if !status.success?
bg_respond(nwo, issue_id, "Downloading of the repository for issue ##{issue_id} failed with the following error: \n\n #{stderr}") and return
end
# Compile the paper
pdf_path, stderr, status = compile(issue_id, custom_branch)
if !status.success?
bg_respond(nwo, issue_id, "PDF failed to compile for issue ##{issue_id} with the following error: \n\n #{stderr}") and return
end
# If we've got this far then push a copy of the PDF to the papers repository
create_or_update_git_branch(issue_id, config.papers_repo, config.journal_alias)
pdf_url = create_git_pdf(pdf_path, issue_id, config.papers_repo, config.journal_alias)
pdf_response = "[ :point_right: Check article proof :page_facing_up: :point_left: ](#{pdf_url})"
# Finally, respond in the review issue with the PDF URL
bg_respond(nwo, issue_id, pdf_response)
end
# Use the Whedon gem to download the software to a local tmp directory
def download(issue_id)
FileUtils.rm_rf("tmp/#{issue_id}") if Dir.exist?("tmp/#{issue_id}")
Open3.capture3("whedon download #{issue_id}")
end
# Use the Whedon gem to compile the paper
def compile(issue_id, custom_branch=nil)
if custom_branch
Open3.capture3("whedon prepare #{issue_id} #{custom_branch}")
else
Open3.capture3("whedon prepare #{issue_id}")
end
end
end
# This is the Sidekiq worker that processes PDFs. It leverages the Whedon gem to
# carry out the majority of its actions. Where possible, we try and capture
# errors from any of the executed tasks and report them back to the review issue
class DepositWorker
require_relative 'github'
require_relative 'config_helper'
require 'open3'
require 'ostruct'
require 'sidekiq'
include Sidekiq::Worker
# Sets the Whedon environment
include ConfigHelper
# Include to communicate from background worker to GitHub
include GitHub
def perform(nwo, issue_id, config, dry_run)
config = OpenStruct.new(config)
set_env(nwo, issue_id, config)
# Download the paper
stdout, stderr, status = download(issue_id)
# Whedon often can't find a paper in the repository he's downloaded even
# though it's definitely there (e.g. https://github.com/openjournals/joss-reviews/issues/776#issuecomment-397714563)
# Not sure if this is because the repository hasn't downloaded yet.
# Adding in a sleep statement to see if this helps.
sleep(5)
if !status.success?
bg_respond(nwo, issue_id, "Downloading of the repository for issue ##{issue_id} failed with the following error: \n\n #{stderr}") and return
end
# Compile the paper
pdf_path, stderr, status = compile(issue_id)
if !status.success?
bg_respond(nwo, issue_id, "PDF failed to compile for issue ##{issue_id} with the following error: \n\n #{stderr}") and return
end
# If we've got this far then push a copy of the PDF to the papers repository
create_or_update_git_branch(issue_id, config.papers_repo, config.journal_alias)
pdf_url = create_git_pdf(pdf_path, issue_id, config.papers_repo, config.journal_alias)
crossref_xml_path = pdf_path.gsub('.pdf', '.crossref.xml')
crossref_url = create_git_xml(crossref_xml_path, issue_id, config.papers_repo, config.journal_alias)
if dry_run == true
pr_url = create_deposit_pr(issue_id, config.papers_repo, config.journal_alias, dry_run)
pr_response = "Check final proof :point_right: #{pr_url}\n\nIf the paper PDF and Crossref deposit XML look good in #{pr_url}, then you can now move forward with accepting the submission by compiling again with the flag `deposit=true` e.g.\n ```\n@whedon accept deposit=true\n```"
else
pr_url = create_deposit_pr(issue_id, config.papers_repo, config.journal_alias, dry_run)
# Deposit with journal and Crossref
deposit(issue_id)
id = "%05d" % issue_id
doi = "https://doi.org/#{config.doi_prefix}/#{config.journal_alias}.#{id}"
pr_response = "🚨🚨🚨 **THIS IS NOT A DRILL, YOU HAVE JUST ACCEPTED A PAPER INTO #{config.journal_alias.upcase}!** 🚨🚨🚨\n\n Here's what you must now do:\n\n0. Check final PDF and Crossref metadata that was deposited :point_right: #{pr_url}\n1. Wait a couple of minutes to verify that the paper DOI resolves [#{doi}](#{doi})\n2. If everything looks good, then close this review issue.\n3. Party like you just published a paper! 🎉🌈🦄💃👻🤘\n\n Any issues? notify your editorial technical team..."
end
# Finally, respond in the review issue with the PDF URL
bg_respond(nwo, issue_id, pr_response)
end
# Use the Whedon gem to download the software to a local tmp directory
def download(issue_id)
FileUtils.rm_rf("tmp/#{issue_id}") if Dir.exist?("tmp/#{issue_id}")
Open3.capture3("whedon download #{issue_id}")
end
# Use the Whedon gem to compile the paper
def compile(issue_id)
Open3.capture3("whedon compile #{issue_id}")
end
def deposit(issue_id)
Open3.capture3("whedon deposit #{issue_id}")
end
end