-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcommitfest_import.rb
More file actions
316 lines (265 loc) · 9.27 KB
/
commitfest_import.rb
File metadata and controls
316 lines (265 loc) · 9.27 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
#!/usr/bin/env ruby
# frozen_string_literal: true
require_relative "../config/environment"
require "net/http"
require "nokogiri"
require "cgi"
class CommitfestImporter
BASE_URL = "https://commitfest.postgresql.org"
def initialize(commitfest_ids:, include_closed:, limit:)
@commitfest_ids = commitfest_ids
@include_closed = include_closed
@limit = limit
end
def run!
history = fetch_commitfest_history
selected = select_commitfests(history)
puts "Commitfests selected: #{selected.map { |cf| cf[:id] }.join(', ')}"
patches_by_cf = {}
patch_ids = []
selected.each do |entry|
commitfest = upsert_commitfest(entry)
patches = fetch_commitfest_patches(commitfest.external_id)
patches_by_cf[commitfest.external_id] = patches
patch_ids.concat(patches.map { |p| p[:patch_id] })
puts "Commitfest #{commitfest.external_id}: #{patches.length} patches"
end
unique_patch_ids = patch_ids.uniq
total_patches = unique_patch_ids.size
unique_patch_ids.each_with_index do |patch_id, idx|
puts "Syncing patch #{idx + 1}/#{total_patches} (#{patch_id})"
sync_patch(patch_id)
end
patches_by_cf.each do |cfid, patches|
commitfest = Commitfest.find_by!(external_id: cfid)
patches.each do |patch_data|
patch = CommitfestPatch.find_by!(external_id: patch_data[:patch_id])
join = CommitfestPatchCommitfest.find_or_initialize_by(
commitfest: commitfest,
commitfest_patch: patch
)
join.status = patch_data[:status]
join.ci_status = patch_data[:ci_status]
join.ci_score = patch_data[:ci_score]
join.last_synced_at = Time.current
join.save!
end
end
end
private
def fetch_commitfest_history
html = fetch_html("#{BASE_URL}/commitfest_history/")
doc = Nokogiri::HTML(html)
commitfests = []
doc.xpath("//h1[contains(normalize-space(.), 'Commitfest history')]/following-sibling::ul[1]/li").each do |li|
link = li.at_css("a[href^='/']")
next unless link
id = link["href"]&.match(%r{^/(\d+)/})&.captures&.first
next unless id
name = link.text.strip
text = li.text.strip
text = text.sub(name, "").strip
next unless text.match?(/\d{4}-\d{2}-\d{2}/)
status, start_date, end_date = parse_history_text(text)
next unless status && start_date && end_date
commitfests << {
id: id.to_i,
name: name,
status: status,
start_date: start_date,
end_date: end_date
}
end
commitfests
end
def parse_history_text(text)
normalized = text.tr("–", "-").tr("—", "-").gsub("â", "-").gsub("–", "-").gsub(/\s+/, " ")
match = normalized.match(/\(?\s*([A-Za-z ]+)\s*-\s*(\d{4}-\d{2}-\d{2})\s*-\s*(\d{4}-\d{2}-\d{2})\s*\)?/)
return [ nil, nil, nil ] unless match
status = match[1].strip
[ status, Date.parse(match[2]), Date.parse(match[3]) ]
end
def select_commitfests(history)
selected = history
if @commitfest_ids.any?
selected = history.select { |entry| @commitfest_ids.include?(entry[:id]) }
elsif !@include_closed
selected = history.reject { |entry| entry[:status].casecmp("Closed").zero? }
end
selected = selected.first(@limit) if @limit
selected
end
def upsert_commitfest(entry)
commitfest = Commitfest.find_or_initialize_by(external_id: entry[:id])
commitfest.assign_attributes(
name: entry[:name],
status: entry[:status],
start_date: entry[:start_date],
end_date: entry[:end_date],
last_synced_at: Time.current
)
commitfest.save!
commitfest
end
def fetch_commitfest_patches(commitfest_id)
html = fetch_html("#{BASE_URL}/#{commitfest_id}/")
doc = Nokogiri::HTML(html)
patches = []
doc.css("table tbody tr").each do |row|
link = row.at_css("a[href^='/patch/']")
next unless link
patch_id = link["href"]&.match(%r{/patch/(\d+)/})&.captures&.first
next unless patch_id
status = row.css("td")[2]&.text&.strip.to_s
ci_status, ci_score = parse_ci(row.at_css("td.cfbot-summary"))
patches << {
patch_id: patch_id.to_i,
title: link.text.strip,
status: status,
ci_status: ci_status,
ci_score: ci_score
}
end
patches
end
def parse_ci(ci_td)
return [ nil, nil ] unless ci_td
text = ci_td.text.strip
if text.include?("Not processed")
return [ "not_processed", nil ]
end
if text.include?("Needs rebase")
return [ "needs_rebase", nil ]
end
counters = ci_td.at_css("span.run-counters")&.text&.strip
return [ nil, nil ] if counters.nil? || counters.empty?
completed, total = counters.split("/").map(&:to_i)
return [ "score", nil ] if total <= 0
score = ((completed.to_f / total) * 10).round
score = [ [ score, 0 ].max, 10 ].min
[ "score", score ]
end
def sync_patch(patch_id)
html = fetch_html("#{BASE_URL}/patch/#{patch_id}/")
doc = Nokogiri::HTML(html)
title = extract_cell_text(doc, "Title")
topic = extract_cell_text(doc, "Topic")
target_version = extract_cell_text(doc, "Target version")
reviewers = extract_cell_text(doc, "Reviewers")
committer = extract_cell_text(doc, "Committer")
links = extract_links(doc)
tags = extract_tags(doc)
message_ids = extract_message_ids(doc)
patch = CommitfestPatch.find_or_initialize_by(external_id: patch_id)
patch.assign_attributes(
title: title.presence || "Patch #{patch_id}",
topic: topic.presence,
target_version: target_version.presence,
reviewers: reviewers.presence,
committer: committer.presence,
wikilink: links[:wiki],
gitlink: links[:git],
last_synced_at: Time.current
)
patch.save!
update_tags(patch, tags)
update_messages_and_topics(patch, message_ids)
rescue StandardError => e
puts "Patch #{patch_id} failed: #{e.class} #{e.message}"
end
def extract_cell_text(doc, label)
cell = doc.at_xpath("//th[normalize-space(text())='#{label}']/following-sibling::td[1]")
return "" unless cell
cell_text = cell.text.gsub(/\s+/, " ").strip
cell_text.gsub(/\b(Remove from reviewers|Become reviewer|Claim as committer|Unclaim as committer)\b/, "").strip
end
def extract_links(doc)
cell = doc.at_xpath("//th[normalize-space(text())='Links']/following-sibling::td[1]")
return { wiki: nil, git: nil } unless cell
links = cell.css("a").map { |a| [ a.text.strip.downcase, a["href"] ] }.to_h
{ wiki: links["wiki"], git: links["git"] }
end
def extract_tags(doc)
cell = doc.at_xpath("//th[normalize-space(text())='Tags']/following-sibling::td[1]")
return [] unless cell
cell.css("span.badge").map do |tag|
{
name: tag.text.strip,
description: tag["title"].to_s.strip,
color: tag["style"].to_s[/background-color:\s*([^;]+);?/, 1]
}
end
end
def extract_message_ids(doc)
ids = []
doc.css("a[href^='https://www.postgresql.org/message-id/']").each do |link|
href = link["href"].to_s
next if href.include?("/attachment/")
message_id = href.split("/message-id/").last
next unless message_id
message_id = message_id.sub(/\Aflat\//, "")
message_id = CGI.unescape(message_id)
normalized = MessageIdNormalizer.normalize(message_id)
ids << normalized if normalized.present?
end
ids.uniq
end
def update_tags(patch, tags)
tag_records = tags.map do |tag|
CommitfestTag.find_or_initialize_by(name: tag[:name]).tap do |record|
record.description = tag[:description] if tag[:description].present?
record.color = tag[:color] if tag[:color].present?
record.save!
end
end
patch.commitfest_tags = tag_records
end
def update_messages_and_topics(patch, message_ids)
patch.commitfest_patch_messages.where.not(message_id: message_ids).delete_all
patch.commitfest_patch_topics.delete_all
topic_ids = []
message_ids.each do |message_id|
message = Message.find_by(message_id: message_id)
record = patch.commitfest_patch_messages.find_or_initialize_by(message_id: message_id)
record.message = message
record.last_synced_at = Time.current
record.save!
topic_ids << message.topic_id if message
end
topic_ids.uniq.each do |topic_id|
CommitfestPatchTopic.find_or_create_by!(commitfest_patch: patch, topic_id: topic_id) do |link|
link.last_synced_at = Time.current
end
end
end
def fetch_html(url)
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri.request_uri)
request["User-Agent"] = "Hackorum Commitfest Importer"
response = http.request(request)
raise "HTTP #{response.code} for #{url}" unless response.is_a?(Net::HTTPSuccess)
response.body
end
end
commitfest_ids = []
include_closed = false
limit = nil
ARGV.each_with_index do |arg, idx|
case arg
when "--all"
include_closed = true
when "--include-closed"
include_closed = true
when "--commitfest"
commitfest_ids << ARGV[idx + 1].to_i if ARGV[idx + 1]
when "--limit"
limit = ARGV[idx + 1].to_i if ARGV[idx + 1]
end
end
CommitfestImporter.new(
commitfest_ids: commitfest_ids,
include_closed: include_closed,
limit: limit
).run!