diff --git a/app/serializers/diff_file_entity.rb b/app/serializers/diff_file_entity.rb index d575fbf32c1cf20def49ac1bb50cb7aa825413aa..fe9b22ad0d965e4b93614a12409ea0bef1dd6853 100644 --- a/app/serializers/diff_file_entity.rb +++ b/app/serializers/diff_file_entity.rb @@ -8,6 +8,7 @@ class DiffFileEntity < DiffFileBaseEntity expose :added_lines expose :removed_lines + expose :code_review_id expose :load_collapsed_diff_url, if: ->(diff_file, options) { options[:merge_request] } do |diff_file| merge_request = options[:merge_request] diff --git a/app/serializers/diff_file_metadata_entity.rb b/app/serializers/diff_file_metadata_entity.rb index 772b549314e149540eb92250a25db9dc30edef58..ac22fadfb502302582b5444390fce1ecca859683 100644 --- a/app/serializers/diff_file_metadata_entity.rb +++ b/app/serializers/diff_file_metadata_entity.rb @@ -12,4 +12,5 @@ class DiffFileMetadataEntity < Grape::Entity expose :submodule?, as: :submodule expose :file_identifier_hash expose :file_hash + expose :code_review_id end diff --git a/lib/gitlab/diff/file.rb b/lib/gitlab/diff/file.rb index 3f6ada6e60b23a1ab858a5c56c2790d714ffda9e..2d46c4bc829370bdde68011701861dfb8dd84b5a 100644 --- a/lib/gitlab/diff/file.rb +++ b/lib/gitlab/diff/file.rb @@ -262,6 +262,11 @@ def file_identifier_hash Digest::SHA1.hexdigest(file_identifier) end + def code_review_id + Digest::SHA1.hexdigest("#{file_identifier}-#{blob&.id}") + end + strong_memoize_attr :code_review_id + def diffable? diffable_by_attribute? && !text_with_binary_notice? end diff --git a/spec/lib/gitlab/diff/file_spec.rb b/spec/lib/gitlab/diff/file_spec.rb index 4c44779a4bd3269d25bd7a00b283a2a6141ee57d..64fe394cdb3a28abd6b9dcfcd3bb4ad3b2eefc3f 100644 --- a/spec/lib/gitlab/diff/file_spec.rb +++ b/spec/lib/gitlab/diff/file_spec.rb @@ -455,6 +455,12 @@ def delete_file(file_name) end end + describe '#code_review_id' do + it 'returns a hash' do + expect(diff_file.code_review_id).to match(/\A[0-9a-f]{40}\z/) + end + end + context 'diff file stats' do let(:diff_file) do described_class.new(diff, diff_refs: commit.diff_refs, repository: project.repository, stats: stats) diff --git a/spec/serializers/diff_file_entity_spec.rb b/spec/serializers/diff_file_entity_spec.rb index 2aeffecbcc828eafac364bb1ef83ad1f464a9f7c..01b455c08b40ec008d9c79623c1ca50cde6ef33f 100644 --- a/spec/serializers/diff_file_entity_spec.rb +++ b/spec/serializers/diff_file_entity_spec.rb @@ -90,5 +90,9 @@ end end + it 'exposes code_review_id' do + expect(subject[:code_review_id]).to match(/\A[0-9a-f]{40}\z/) + end + it_behaves_like 'diff file with conflict_type' end diff --git a/spec/serializers/diff_file_metadata_entity_spec.rb b/spec/serializers/diff_file_metadata_entity_spec.rb index 7e8695b10f0d0809a1bb1d24059481c879b3d5b9..d4a55a8a4260d051956d3bdab53b452e791be894 100644 --- a/spec/serializers/diff_file_metadata_entity_spec.rb +++ b/spec/serializers/diff_file_metadata_entity_spec.rb @@ -22,6 +22,7 @@ :submodule, :file_identifier_hash, :file_hash, + :code_review_id, :conflict_type ) end