Commit 9b641b7b authored by Gary Holtz's avatar Gary Holtz

Adds a `prev_commit_id` and a `next_commit_id`

This should allow for a fronted change to include "Previous" and
"Next" buttons on the commit viewer.
parent 4000aba0
......@@ -3,6 +3,7 @@
class DiffsEntity < Grape::Entity
include DiffHelper
include RequestAwareEntity
include Gitlab::Utils::StrongMemoize
expose :real_size
expose :size
......@@ -16,11 +17,19 @@ class DiffsEntity < Grape::Entity
end
expose :commit do |diffs, options|
if options[:commit]
neighbors = commits(options[:commit].id) if options[:commit]
prev_commit_id = neighbors[:prev_commit_id]
next_commit_id = neighbors[:next_commit_id]
end
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
pipeline_project: merge_request&.source_project,
prev_commit_id: prev_commit_id,
next_commit_id: next_commit_id
)
end
......@@ -80,4 +89,16 @@ class DiffsEntity < Grape::Entity
def merge_request
options[:merge_request]
end
def commit_ids
strong_memoize(:commit_ids) do
[nil] + merge_request.commits.collect(&:id) + [nil]
end
end
def commits(id)
commit_ids.each_cons(3) do |prev_commit, commit, next_commit|
{ prev_commit_id: prev_commit, next_commit_id: next_commit } if commit == id
end
end
end
......@@ -32,6 +32,18 @@ module API
render('projects/commit/_signature', signature: commit.signature) if commit.has_signature?
end
expose :prev_commit_id, if: { type: :full } do |commit|
next unless options[:prev_commit_id]
options[:prev_commit_id]
end
expose :next_commit_id, if: { type: :full } do |commit|
next unless options[:next_commit_id]
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,24 @@ describe DiffsEntity do
:merge_request_diffs
)
end
context "when a commit_id is passed" do
let(:commits) { merge_request.commits }
let(:commit) { commits.sample }
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
it 'includes commit references for previous and next' do
expect(subject[:commit]).to include(:prev_commit_id, :next_commit_id)
index = commits.index(commit)
prev_commit = commits[index - 1]&.id
next_commit = commits[index + 1]&.id
expect(subject[:commit][:prev_commit_id]).to eq(prev_commit)
expect(subject[:commit][:next_commit_id]).to eq(next_commit)
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