-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13684 from eduardoj/feature/diff_parser
Implement parser for request diffs
- Loading branch information
Showing
9 changed files
with
245 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class ActionSourcediffParser | ||
def initialize(action_sourcediff:) | ||
@action_sourcediff = action_sourcediff | ||
end | ||
|
||
def call | ||
@action_sourcediff&.each do |sourcediff| | ||
sourcediff['filenames']&.each do |filename| | ||
content = sourcediff['files'][filename].dig('diff', '_content') | ||
|
||
sourcediff['files'][filename]['diff']['parsed_lines'] = DiffParser.new(content: content).call | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
class DiffParser | ||
def initialize(content: []) | ||
@content = content | ||
@original_index = 0 | ||
@changed_index = 0 | ||
@last_original_index = 0 | ||
@last_changed_index = 0 | ||
@lines = [] | ||
end | ||
|
||
def call | ||
@content.each_line.with_index do |line, line_index| | ||
@state = parse_line(line) | ||
|
||
@lines << create_line(line, line_index) | ||
|
||
@last_original_index = @original_index | ||
@last_changed_index = @changed_index | ||
end | ||
|
||
@lines | ||
end | ||
|
||
private | ||
|
||
def parse_line(line) | ||
case line | ||
when /^ / | ||
@original_index += 1 | ||
@changed_index += 1 | ||
'unchanged' | ||
when /^@@ -(?<original_index>\d+).+\+(?<changed_index>\d+),/ | ||
@original_index = Regexp.last_match[:original_index].to_i - 1 | ||
@changed_index = Regexp.last_match[:changed_index].to_i - 1 | ||
'range' | ||
when /^-/ | ||
@original_index += 1 | ||
'removed' | ||
when /^[+]/ | ||
@changed_index += 1 | ||
'added' | ||
else | ||
'comment' | ||
end | ||
end | ||
|
||
def create_line(line, line_index) | ||
DiffParser::Line.new(content: line, state: @state, index: line_index + 1, original_index: final_original_index, changed_index: final_changed_index) | ||
end | ||
|
||
def final_original_index | ||
return if ['comment', 'range'].include?(@state) | ||
|
||
@original_index unless @last_original_index == @original_index | ||
end | ||
|
||
def final_changed_index | ||
return if ['comment', 'range'].include?(@state) | ||
|
||
@changed_index unless @last_changed_index == @changed_index | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class DiffParser | ||
class Line | ||
attr_reader :content, :state, :index, :original_index, :changed_index | ||
|
||
def initialize(content:, state:, index:, original_index:, changed_index:) | ||
@content = content | ||
@state = state | ||
@index = index | ||
@original_index = original_index | ||
@changed_index = changed_index | ||
end | ||
|
||
def ==(other) | ||
content == other.content && state == other.state && index == other.index && original_index == other.original_index && changed_index == other.changed_index | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe DiffParser, type: :service do | ||
let(:content) { Rails.root.join("spec/support/files/#{file}").expand_path } | ||
let(:parser) { described_class.new(content: content) } | ||
|
||
let(:result) { result_array.map { |line| DiffParser::Line.new(content: line[0], state: line[1], index: line[2], original_index: line[3], changed_index: line[4]) } } | ||
|
||
subject { parser.call } | ||
|
||
describe '#call' do | ||
context 'empty diff' do | ||
let(:content) { '' } | ||
|
||
let(:result_array) { [] } | ||
|
||
it { expect(subject).to eq(result) } | ||
end | ||
|
||
context 'simple diff' do | ||
let(:file) { 'diff_simple.diff' } | ||
|
||
let(:result_array) do | ||
[ | ||
["@@ -1,1 +1,1 @@\n", 'range', 1, nil, nil], | ||
["-a\n", 'removed', 2, 1, nil], | ||
["+b\n", 'added', 3, nil, 1] | ||
] | ||
end | ||
|
||
it 'parses correctly' do expect(subject).to eq(result) end | ||
end | ||
|
||
context 'diff with no newline comments' do | ||
let(:file) { 'diff_with_no_newline_comments.diff' } | ||
|
||
let(:result_array) do | ||
[ | ||
["@@ -1,1 +1,1 @@\n", 'range', 1, nil, nil], | ||
["-a\n", 'removed', 2, 1, nil], | ||
["\\ No newline at end of file\n", 'comment', 3, nil, nil], | ||
["+b\n", 'added', 4, nil, 1], | ||
["\\ No newline at end of file\n", 'comment', 5, nil, nil] | ||
] | ||
end | ||
|
||
it 'parses correctly' do expect(subject).to eq(result) end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@@ -1,1 +1,1 @@ | ||
-a | ||
+b |
5 changes: 5 additions & 0 deletions
5
src/api/spec/support/files/diff_with_no_newline_comments.diff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@@ -1,1 +1,1 @@ | ||
-a | ||
\ No newline at end of file | ||
+b | ||
\ No newline at end of file |