@@ -169,13 +169,29 @@ def push
169169 return
170170 end
171171
172+ read_previous_ids
172173 ensure_branch
173174 commit_content
174175 create_or_update_pull_request
175176 end
176177
177178 private
178179
180+ def read_previous_ids
181+ @base_last_id , @base_last_event_id = read_ids_from_ref ( base_branch )
182+ @prev_last_id , @prev_last_event_id = read_ids_from_ref ( @branch_name )
183+ end
184+
185+ def read_ids_from_ref ( ref )
186+ existing = octokit . contents ( @repo . name , path : @filepath , ref : ref )
187+ content = Base64 . decode64 ( existing [ :content ] )
188+ last_id = content =~ /\b last_editing_history: (\d +)/ ? $1. to_i : 0
189+ last_event_id = content =~ /\b last_event_editing_history: (\d +)/ ? $1. to_i : 0
190+ [ last_id , last_event_id ]
191+ rescue Octokit ::NotFound
192+ [ 0 , 0 ]
193+ end
194+
179195 def ensure_branch
180196 octokit . branch ( @repo . name , @branch_name )
181197 rescue Octokit ::NotFound
@@ -224,18 +240,63 @@ def branch_has_newer_data?
224240 false # Branch or file doesn't exist
225241 end
226242
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+
227279 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 )
228281 owner = @repo . name . split ( '/' ) [ 0 ]
229282 existing_prs = octokit . pull_requests ( @repo . name , state : 'open' , head : "#{ owner } :#{ @branch_name } " )
230283 if existing_prs . any?
231- octokit . update_pull_request ( @repo . name , existing_prs [ 0 ] [ :number ] , title : @pr_title )
284+ pr_number = existing_prs [ 0 ] [ :number ]
285+ 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 )
287+ octokit . add_comment ( @repo . name , pr_number , incremental ) if incremental
232288 else
233289 begin
234- octokit . create_pull_request ( @repo . name , base_branch , @branch_name , @pr_title , nil )
290+ octokit . create_pull_request ( @repo . name , base_branch , @branch_name , @pr_title , full_summary )
235291 rescue Octokit ::UnprocessableEntity
236292 # Concurrent job already created PR
237293 existing_prs = octokit . pull_requests ( @repo . name , state : 'open' , head : "#{ owner } :#{ @branch_name } " )
238- octokit . update_pull_request ( @repo . name , existing_prs [ 0 ] [ :number ] , title : @pr_title ) if existing_prs . any?
294+ if existing_prs . any?
295+ pr_number = existing_prs [ 0 ] [ :number ]
296+ 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 )
298+ octokit . add_comment ( @repo . name , pr_number , incremental ) if incremental
299+ end
239300 end
240301 end
241302 end
0 commit comments