forked from everypolitician/rebuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
306 lines (247 loc) · 7.93 KB
/
app.rb
File metadata and controls
306 lines (247 loc) · 7.93 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
# frozen_string_literal: true
require 'bundler'
Bundler.require
Dotenv.load
set :bind, '0.0.0.0'
require 'active_support/core_ext'
require 'English'
require_relative './lib/cleaned_output'
def github
@github ||= Octokit::Client.new(
access_token: github_access_token
)
end
def github_access_token
@github_access_token ||= ENV.fetch('GITHUB_ACCESS_TOKEN')
rescue KeyError
abort 'Please set GITHUB_ACCESS_TOKEN in the environment before running'
end
EVERYPOLITICIAN_DATA_REPO = ENV.fetch(
'EVERYPOLITICIAN_DATA_REPO',
'loudspeek/representa'
)
class Build
def initialize(legislature, source_name = nil)
@legislature = legislature
@source_name = source_name
end
def skip_reason
return 'No source' unless source
return unless source.morph?
return 'No github data' if source.current_data.to_s.empty?
return 'No morph data' if source.fresh_data.to_s.empty?
return 'No morph changes' if source.current_data == source.fresh_data
end
private
attr_reader :legislature, :source_name
def instructions
@instructions ||= EveryPolitician::Instructions.new(legislature)
end
def source
@source ||= instructions.source(source_name)
end
end
# Rebuild a given country's legislature information
class RebuilderJob
include SuckerPunch::Job
def perform(country_slug, legislature_slug, source = nil)
logger.info("Begin #{country_slug}/#{legislature_slug}/#{source}")
country = EveryPolitician.country(country_slug)
legislature = country.legislature(legislature_slug)
# If we're rebuilding a single source, check if we can short-circuit
# before checking out the repo, and creating a branch, etc.
# This can be done in seconds rather than minutes
if source
if skip_reason = Build.new(legislature, source).skip_reason
logger.info("Skip #{country_slug}/#{legislature_slug}/#{source}: #{skip_reason}")
return
end
end
branch = [country_slug, legislature_slug, Time.now.to_i].join('-').parameterize
source_name_with_default = source || 'all sources'
output, child_status = run(
"#{File.join(__dir__, 'bin/everypolitician-data-builder')} 2>&1",
'BRANCH_NAME' => branch,
'GIT_CLONE_URL' => clone_url.to_s,
'LEGISLATURE_DIRECTORY' => 'data/' + legislature.directory,
'SOURCE_NAME' => source,
'COUNTRY_NAME' => country.name,
'COUNTRY_SLUG' => country.slug
)
cleaned_output = CleanedOutput.new(output: output, redactions: [ENV['MORPH_API_KEY']])
unless child_status&.success?
logger.error("Failed to build #{country.name} - #{legislature.name} — #{source_name_with_default}\n\n#{cleaned_output}")
return
end
title = "#{country.name} (#{legislature.name}): refresh #{source_name_with_default}"
body = "Automated refresh of #{source_name_with_default} for #{country.name} - #{legislature.name}" \
"\n\n#### Output\n\n```\n#{cleaned_output}\n```"
CreatePullRequestJob.perform_in(1.minute, branch, title, body)
end
private
def clone_url
@clone_url ||= URI.parse(repo.clone_url).tap do |repo_clone_url|
repo_clone_url.user = github.login
repo_clone_url.password = github.access_token
end
end
def repo
@repo ||= github.repository(EVERYPOLITICIAN_DATA_REPO)
end
# Unset bundler environment variables so it uses the correct Gemfile etc.
def env
@env ||= {
'BUNDLE_GEMFILE' => nil,
'BUNDLE_BIN_PATH' => nil,
'RUBYOPT' => nil,
'RUBYLIB' => nil,
'NOKOGIRI_USE_SYSTEM_LIBRARIES' => '1',
}
end
def run(command, extra_env = {})
with_tmp_dir do |dir|
output = IO.popen(env.merge(extra_env), "cd \"#{dir}\" && #{command}", &:read)
[output, $CHILD_STATUS]
end
end
def with_tmp_dir(&block)
Dir.mktmpdir do |tmp_dir|
block.call(tmp_dir)
end
end
end
class CreatePullRequestJob
class Error < StandardError; end
include SuckerPunch::Job
# If any of these change, then we have a usable build
EXPECTED_FILES = ['ep-popolo-v1.0.json', 'unstable/positions.csv'].freeze
def perform(branch, title, body)
logger.info("Begin PR #{title}")
# The branch won't exist if there were no changes when the rebuild was run.
unless branch_exists?(branch)
warn "Couldn't find branch: #{branch}"
return
end
changes = github.compare(EVERYPOLITICIAN_DATA_REPO, 'master', branch)
changed_files = changes[:files].map { |f| f[:filename].split('/').drop(3).join('/') }
unless (changed_files & EXPECTED_FILES).any?
warn 'No usable change detected, skipping'
return
end
body ||= 'Output of build no longer available'
github.create_pull_request(
EVERYPOLITICIAN_DATA_REPO,
'master',
branch,
title,
body
)
end
def branch_exists?(branch_name)
github.branch(EVERYPOLITICIAN_DATA_REPO, branch_name)
true
rescue Octokit::NotFound
false
end
end
module EveryPolitician
class Instructions
require 'json5'
def initialize(legislature)
@legislature = legislature
end
def source(name)
stanza = stanza(name) or return
Source.new(legislature: legislature, stanza: stanza)
end
private
attr_reader :legislature
def instructions_url
legislature.popolo_url.sub('ep-popolo-v1.0.json', 'sources/instructions.json')
end
def raw_instructions
@raw_instructions ||= open(instructions_url).read
end
def instructions
JSON.parse(JSON5.parse(raw_instructions).to_json, symbolize_names: true)
end
def stanza(name)
instructions[:sources].select { |src| src[:create] }.find do |src|
src[:file].include? name
end
end
end
class Source
GITHUB = 'https://raw.githubusercontent.com/loudspeek/representa/master/data/%s/sources/%s'
def initialize(stanza:, legislature:)
@stanza = stanza
@legislature = legislature
end
def morph?
creation[:from].to_s == 'morph'
end
# TODO: handle all the other types of source
def fresh_data
return '' unless morph?
@fresh_data ||= MorphData.new(creation[:scraper]).query(creation[:query]) rescue nil
end
def current_data
@current ||= open(github_data_url).read
rescue => error
warn "Can't fetch current data from #{github_data_url}: #{error}"
nil
end
private
attr_reader :stanza, :legislature
def github_data_url
GITHUB % [legislature.directory, stanza[:file]]
end
def creation
stanza[:create] || {}
end
end
end
class MorphData
require 'rest-client'
def initialize(scraper)
@scraper = scraper
end
def query(query)
morph_api_url = 'https://api.morph.io/%s/data.csv' % scraper
morph_api_key = ENV['MORPH_API_KEY']
result = RestClient.get morph_api_url, params: {
key: morph_api_key,
query: query,
}
result.to_s
end
private
attr_reader :scraper
end
helpers do
def rebuild(country, legislature, source = nil)
RebuilderJob.perform_async(country, legislature, source)
message = "Queued rebuild for country=#{country} legislature=#{legislature} source=#{source}\n"
logger.info(message.chomp)
message
end
end
get '/' do
erb :bot_image
end
post '/:country/:legislature' do |country_path, legislature_path|
logger.warn "Legacy route used: /#{country_path}/#{legislature_path}. Please use / with params"
legislature = EveryPolitician.countries.flat_map(&:legislatures).find do |l|
l.directory == "#{country_path}/#{legislature_path}"
end
rebuild(legislature.country.slug, legislature.slug)
end
post '/' do
country = params[:country]
legislature = params[:legislature]
source = params[:source]
rebuild(country, legislature, source)
end
post '/rebuild/:country_slug/:legislature_slug/?:source_name?' do |country_slug, legislature_slug, source_name|
rebuild(country_slug, legislature_slug, source_name)
end