Preserved diff file sometimes differs from actual diff file in repository
While investigation "undefined method index for nil:NilClass" error for Importer: Investigate and fix failed relations
we discovered that there is a case when importing, that it fails to create a Diff File, because it can't find the line in the persisted diff with the old_line
and new_line
of original_position
.
If we fallback to the diff file in the repository it can find the file and line of original_position
.
The expected behavior would be that noteable.diffs(original_position.diff_options).diff_files.first
have the same content as original_position.diff_file(repository)
.
Method where this is happening is DiffNote#fetch_diff_file
.
all MRs from the failed import have the external_diff
column populated (some of them diff-<number>
)
For actual example of the old_line
and new_line
of each line in repo and persisted file: check this comment.
original_position: { new_line: 128, old_line: nil }
- Line found in persisted file
{
new_line:128
old_line:137
text: " if (value != null) {"
}
- If we fallback to repository, and retrieve file, it contains actual line that we are looking for:
{
new_line:128
old_line: nil
text: "+ if (value != null) {"
}
It seems that when merge request diffs are persisted, that in some rare cases, something is off with persisted lines (maybe because it's expanded). It happened just 13 times in 102.000 imported merge requests in customer project.
Note: When this issue is resolved, it would make sense to remove fallback in case line is not found, introduced in this MR