Skip to content

Commit f4f9d7a

Browse files
authored
Merge pull request #135 from ruby-no-kai/yaml-notify
GitHubPusher: move summary building to job class
2 parents 11bdf65 + 40347ec commit f4f9d7a

1 file changed

Lines changed: 46 additions & 40 deletions

File tree

app/jobs/generate_sponsors_yaml_file_job.rb

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,50 @@ def push_to_github
137137
last_editing_history_id: @last_id,
138138
last_event_editing_history_id: @last_event_editing_history_id,
139139
push_id:,
140+
build_summary: method(:build_edit_summary),
140141
).push
141142
end
142143

144+
private
145+
146+
def build_edit_summary(from_last_id, from_last_event_id)
147+
last_id = @last_id || 0
148+
last_event_id = @last_event_editing_history_id || 0
149+
sections = []
150+
151+
if from_last_id < last_id
152+
edits = SponsorshipEditingHistory
153+
.where(id: (from_last_id + 1)..last_id)
154+
.includes(:sponsorship, :staff)
155+
.order(id: :asc)
156+
if edits.any?
157+
lines = edits.map do |edit|
158+
actor = edit.staff ? "#{edit.staff.login} (staff)" : "sponsor"
159+
fields = edit.diff_summary.map { |s| "`#{s}`" }.join(", ")
160+
"- **#{edit.sponsorship.name}** (#{fields}) — by #{actor}"
161+
end
162+
sections << "**Sponsorship changes:**\n#{lines.join("\n")}"
163+
end
164+
end
165+
166+
if from_last_event_id < last_event_id
167+
event_edits = SponsorEventEditingHistory
168+
.where(id: (from_last_event_id + 1)..last_event_id)
169+
.includes(:sponsor_event, :staff)
170+
.order(id: :asc)
171+
if event_edits.any?
172+
lines = event_edits.map do |edit|
173+
actor = edit.staff ? "#{edit.staff.login} (staff)" : "sponsor"
174+
fields = edit.diff_summary.map { |s| "`#{s}`" }.join(", ")
175+
"- **#{edit.sponsor_event.title}** (#{fields}) — by #{actor}"
176+
end
177+
sections << "**Event changes:**\n#{lines.join("\n")}"
178+
end
179+
end
180+
181+
sections.any? ? sections.join("\n\n") : nil
182+
end
183+
143184
# For debugging
144185
def self.get_octokit(repo)
145186
GithubInstallation.new(repo).octokit
@@ -150,7 +191,7 @@ class GitHubPusher
150191

151192
delegate :octokit, :base_branch, to: :github_installation
152193

153-
def initialize(conference:, filepath:, content:, last_editing_history_id:, last_event_editing_history_id:, push_id:)
194+
def initialize(conference:, filepath:, content:, last_editing_history_id:, last_event_editing_history_id:, push_id:, build_summary: nil)
154195
@conference = conference
155196
@repo = conference.github_repo
156197
@filepath = filepath
@@ -159,6 +200,7 @@ def initialize(conference:, filepath:, content:, last_editing_history_id:, last_
159200
@last_event_id = last_event_editing_history_id || 0
160201
@branch_name = "sponsor-app/#{conference.slug}"
161202
@pr_title = "Update sponsors.yml for #{conference.slug} (#{push_id})"
203+
@build_summary = build_summary
162204
end
163205

164206
def push
@@ -240,50 +282,14 @@ def branch_has_newer_data?
240282
false # Branch or file doesn't exist
241283
end
242284

243-
def build_summary(from_last_id:, from_last_event_id:)
244-
sections = []
245-
246-
if from_last_id < @last_id
247-
edits = SponsorshipEditingHistory
248-
.where(id: (from_last_id + 1)..@last_id)
249-
.includes(:sponsorship, :staff)
250-
.order(id: :asc)
251-
if edits.any?
252-
lines = edits.map do |edit|
253-
actor = edit.staff ? "#{edit.staff.login} (staff)" : "sponsor"
254-
fields = edit.diff_summary.map { |s| "`#{s}`" }.join(", ")
255-
"- **#{edit.sponsorship.name}** (#{fields}) — by #{actor}"
256-
end
257-
sections << "**Sponsorship changes:**\n#{lines.join("\n")}"
258-
end
259-
end
260-
261-
if from_last_event_id < @last_event_id
262-
event_edits = SponsorEventEditingHistory
263-
.where(id: (from_last_event_id + 1)..@last_event_id)
264-
.includes(:sponsor_event, :staff)
265-
.order(id: :asc)
266-
if event_edits.any?
267-
lines = event_edits.map do |edit|
268-
actor = edit.staff ? "#{edit.staff.login} (staff)" : "sponsor"
269-
fields = edit.diff_summary.map { |s| "`#{s}`" }.join(", ")
270-
"- **#{edit.sponsor_event.title}** (#{fields}) — by #{actor}"
271-
end
272-
sections << "**Event changes:**\n#{lines.join("\n")}"
273-
end
274-
end
275-
276-
sections.any? ? sections.join("\n\n") : nil
277-
end
278-
279285
def create_or_update_pull_request
280-
full_summary = build_summary(from_last_id: @base_last_id, from_last_event_id: @base_last_event_id)
286+
full_summary = @build_summary&.call(@base_last_id, @base_last_event_id)
281287
owner = @repo.name.split('/')[0]
282288
existing_prs = octokit.pull_requests(@repo.name, state: 'open', head: "#{owner}:#{@branch_name}")
283289
if existing_prs.any?
284290
pr_number = existing_prs[0][:number]
285291
octokit.update_pull_request(@repo.name, pr_number, title: @pr_title, body: full_summary)
286-
incremental = build_summary(from_last_id: @prev_last_id, from_last_event_id: @prev_last_event_id)
292+
incremental = @build_summary&.call(@prev_last_id, @prev_last_event_id)
287293
octokit.add_comment(@repo.name, pr_number, incremental) if incremental
288294
else
289295
begin
@@ -294,7 +300,7 @@ def create_or_update_pull_request
294300
if existing_prs.any?
295301
pr_number = existing_prs[0][:number]
296302
octokit.update_pull_request(@repo.name, pr_number, title: @pr_title, body: full_summary)
297-
incremental = build_summary(from_last_id: @prev_last_id, from_last_event_id: @prev_last_event_id)
303+
incremental = @build_summary&.call(@prev_last_id, @prev_last_event_id)
298304
octokit.add_comment(@repo.name, pr_number, incremental) if incremental
299305
end
300306
end

0 commit comments

Comments
 (0)