@@ -236,48 +236,50 @@ def get_app_options(full_repo_name, head_sha)
236236
237237 # (4) Retrieves all changes in a pull request from the GitHub API and formats them to be usable by the app
238238 def get_pull_request_changes ( full_repo_name , pull_number , ignore_files_regex )
239- diff = @installation_client . pull_request ( full_repo_name , pull_number , accept : 'application/vnd.github.diff' )
240-
241239 ignore_files_regex . map! do |pattern |
242240 pattern . gsub! ( '.' , '\.' )
243241 pattern . gsub! ( '*' , '.*' )
244242 pattern . gsub! ( '/' , '\/' )
245243 Regexp . new ( pattern )
246244 end
247245
248- current_file = ''
249- line_number = 0
250246 changes = { }
251247 ignored_files = [ ]
252248
253- diff_enum = diff . each_line
254- line = diff_enum . next rescue nil
249+ # Paginate through all files in the pull request
250+ page = 1
251+ per_page = 100
255252 loop do
256- break if line . nil?
257-
258- if line . start_with? ( '+++' )
259- while line &.start_with? ( '+++' )
260- current_file = line [ 6 ..] . strip
261- if ignore_files_regex . any? { |pattern | pattern . match? ( current_file ) }
262- ignored_files << current_file
263- loop do
264- line = diff_enum . next rescue nil
265- break if line . nil? || line . start_with? ( '+++' )
266- end
267- else
268- changes [ current_file ] = [ ]
269- break
253+ files = @installation_client . pull_request_files ( full_repo_name , pull_number , per_page : per_page , page : page )
254+ break if files . empty?
255+
256+ files . each do |file |
257+ filename = file . filename
258+ if ignore_files_regex . any? { |pattern | pattern . match? ( filename ) }
259+ ignored_files << filename
260+ next
261+ end
262+
263+ changes [ filename ] = [ ]
264+ # Parse the patch to get added lines and their line numbers
265+ next unless file . patch
266+ line_number = 0
267+ file . patch . each_line do |line |
268+ if line . start_with? ( '@@' )
269+ # Extract the new file's starting line number from the hunk header
270+ # Example: @@ -1,6 +1,7 @@
271+ m = line . match ( /\+ ([0-9]+)/ )
272+ line_number = m ? m [ 1 ] . to_i - 1 : line_number
273+ elsif line . start_with? ( '+' ) && !line . start_with? ( '+++' )
274+ changes [ filename ] << { line : line_number , text : line [ 1 ..] }
275+ line_number += 1
276+ elsif !line . start_with? ( '-' ) && !line . chomp . eql? ( '\ No newline at end of file' )
277+ line_number += 1
270278 end
271279 end
272- break if line . nil?
273- elsif line . start_with? ( '+' )
274- changes [ current_file ] << { line : line_number , text : line [ 1 ..] }
275- elsif line . start_with? ( '@@' )
276- line_number = line . split ( ) [ 2 ] . split ( ',' ) [ 0 ] . to_i - 1
277280 end
278281
279- line_number += 1 unless line . start_with? ( '-' ) || line . chomp == '\ No newline at end of file'
280- line = diff_enum . next rescue nil
282+ page += 1
281283 end
282284
283285 [ changes , ignored_files ]
0 commit comments