Commit ce38c97b authored by Igor Drozdov's avatar Igor Drozdov

Merge branch...

Merge branch '18140-add-to-next-and-previous-commit-buttons-when-viewing-commits-in-merge-request' into 'master'

Add previous and next commit IDs to the diffs JSON response

Closes #18140

See merge request gitlab-org/gitlab!27528
parents fcf2ee20 8cd83a82
......@@ -16,12 +16,7 @@ class DiffsEntity < Grape::Entity
end
expose :commit do |diffs, options|
CommitEntity.represent options[:commit], options.merge(
type: :full,
commit_url_params: { merge_request_iid: merge_request&.iid },
pipeline_ref: merge_request&.source_branch,
pipeline_project: merge_request&.source_project
)
CommitEntity.represent(options[:commit], commit_options(options))
end
expose :context_commits, using: API::Entities::Commit, if: -> (diffs, options) { merge_request&.project&.context_commits_enabled? } do |diffs|
......@@ -88,4 +83,31 @@ class DiffsEntity < Grape::Entity
def merge_request
options[:merge_request]
end
private
def commit_ids
@commit_ids ||= merge_request.recent_commits.map(&:id)
end
def commit_neighbors(commit_id)
index = commit_ids.index(commit_id)
return [] unless index
[(index > 0 ? commit_ids[index - 1] : nil), commit_ids[index + 1]]
end
def commit_options(options)
prev_commit_id, next_commit_id = *commit_neighbors(options[:commit]&.id)
options.merge(
type: :full,
commit_url_params: { merge_request_iid: merge_request&.iid },
pipeline_ref: merge_request&.source_branch,
pipeline_project: merge_request&.source_project,
prev_commit_id: prev_commit_id,
next_commit_id: next_commit_id
)
end
end
......@@ -32,6 +32,14 @@ module API
render('projects/commit/_signature', signature: commit.signature) if commit.has_signature?
end
expose :prev_commit_id, if: { type: :full } do |commit|
options[:prev_commit_id]
end
expose :next_commit_id, if: { type: :full } do |commit|
options[:next_commit_id]
end
expose :pipeline_status_path, if: { type: :full } do |commit, options|
pipeline_ref = options[:pipeline_ref]
pipeline_project = options[:pipeline_project] || commit.project
......
......@@ -26,5 +26,47 @@ describe DiffsEntity do
:merge_request_diffs, :definition_path_prefix
)
end
context "when a commit_id is passed" do
let(:commits) { merge_request.commits }
let(:entity) do
described_class.new(
merge_request_diffs.first.diffs,
request: request,
merge_request: merge_request,
merge_request_diffs: merge_request_diffs,
commit: commit
)
end
subject { entity.as_json }
context "when the passed commit is not the first or last in the group" do
let(:commit) { commits.third }
it 'includes commit references for previous and next' do
expect(subject[:commit][:prev_commit_id]).to eq(commits.second.id)
expect(subject[:commit][:next_commit_id]).to eq(commits.fourth.id)
end
end
context "when the passed commit is the first in the group" do
let(:commit) { commits.first }
it 'includes commit references for nil and next' do
expect(subject[:commit][:prev_commit_id]).to be_nil
expect(subject[:commit][:next_commit_id]).to eq(commits.second.id)
end
end
context "when the passed commit is the last in the group" do
let(:commit) { commits.last }
it 'includes commit references for previous and nil' do
expect(subject[:commit][:prev_commit_id]).to eq(commits[-2].id)
expect(subject[:commit][:next_commit_id]).to be_nil
end
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment