Commit b8c69a27 authored by Anwar Sadath's avatar Anwar Sadath Committed by Nick Thomas

Add arbitrary commit API backend changes

This commit moves some required entities from controller to API level, and implements a limit on the number of context commits that will be returned from the API.
parent 849b0265
...@@ -65,7 +65,7 @@ class Projects::MergeRequests::DiffsController < Projects::MergeRequests::Applic ...@@ -65,7 +65,7 @@ class Projects::MergeRequests::DiffsController < Projects::MergeRequests::Applic
options = additional_attributes.merge(diff_view: diff_view) options = additional_attributes.merge(diff_view: diff_view)
if @merge_request.project.context_commits_enabled? if @merge_request.project.context_commits_enabled?
options[:context_commits] = @merge_request.context_commits options[:context_commits] = @merge_request.recent_context_commits
end end
render json: DiffsSerializer.new(request).represent(diffs, options) render json: DiffsSerializer.new(request).represent(diffs, options)
......
...@@ -410,8 +410,16 @@ class MergeRequest < ApplicationRecord ...@@ -410,8 +410,16 @@ class MergeRequest < ApplicationRecord
"#{project.to_reference_base(from, full: full)}#{reference}" "#{project.to_reference_base(from, full: full)}#{reference}"
end end
def context_commits def context_commits(limit: nil)
@context_commits ||= merge_request_context_commits.map(&:to_commit) @context_commits ||= merge_request_context_commits.limit(limit).map(&:to_commit)
end
def recent_context_commits
context_commits(limit: MergeRequestDiff::COMMITS_SAFE_SIZE)
end
def context_commits_count
context_commits.count
end end
def commits(limit: nil) def commits(limit: nil)
......
# frozen_string_literal: true # frozen_string_literal: true
class CommitEntity < API::Entities::Commit class CommitEntity < API::Entities::CommitWithLink
include MarkupHelper
include RequestAwareEntity
expose :author, using: UserEntity
expose :author_gravatar_url do |commit|
GravatarService.new.execute(commit.author_email) # rubocop: disable CodeReuse/ServiceClass
end
expose :commit_url do |commit, options|
project_commit_url(request.project, commit, params: options.fetch(:commit_url_params, {}))
end
expose :commit_path do |commit, options|
project_commit_path(request.project, commit, params: options.fetch(:commit_url_params, {}))
end
expose :description_html, if: { type: :full } do |commit|
markdown_field(commit, :description)
end
expose :title_html, if: { type: :full } do |commit|
markdown_field(commit, :title)
end
expose :signature_html, if: { type: :full } do |commit|
render('projects/commit/_signature', signature: commit.signature) if commit.has_signature?
end
expose :pipeline_status_path, if: { type: :full } do |commit, options|
pipeline_ref = options[:pipeline_ref]
pipeline_project = options[:pipeline_project] || commit.project
next unless pipeline_ref && pipeline_project
pipeline = commit.latest_pipeline_for_project(pipeline_ref, pipeline_project)
next unless pipeline&.status
pipelines_project_commit_path(pipeline_project, commit.id, ref: pipeline_ref)
end
def render(*args)
return unless request.respond_to?(:render) && request.render.respond_to?(:call)
request.render.call(*args)
end
end end
# frozen_string_literal: true # frozen_string_literal: true
class UserEntity < API::Entities::UserBasic class UserEntity < API::Entities::UserPath
include RequestAwareEntity
include UserStatusTooltip
expose :path do |user|
user_path(user)
end
end end
# frozen_string_literal: true
module API
module Entities
class CommitWithLink < Commit
include MarkupHelper
include RequestAwareEntity
expose :author, using: Entities::UserPath
expose :author_gravatar_url do |commit|
GravatarService.new.execute(commit.author_email)
end
expose :commit_url do |commit, options|
project_commit_url(request.project, commit, params: options.fetch(:commit_url_params, {}))
end
expose :commit_path do |commit, options|
project_commit_path(request.project, commit, params: options.fetch(:commit_url_params, {}))
end
expose :description_html, if: { type: :full } do |commit|
markdown_field(commit, :description)
end
expose :title_html, if: { type: :full } do |commit|
markdown_field(commit, :title)
end
expose :signature_html, if: { type: :full } do |commit|
render('projects/commit/_signature', signature: commit.signature) if commit.has_signature?
end
expose :pipeline_status_path, if: { type: :full } do |commit, options|
pipeline_ref = options[:pipeline_ref]
pipeline_project = options[:pipeline_project] || commit.project
next unless pipeline_ref && pipeline_project
pipeline = commit.latest_pipeline_for_project(pipeline_ref, pipeline_project)
next unless pipeline&.status
pipelines_project_commit_path(pipeline_project, commit.id, ref: pipeline_ref)
end
def render(*args)
return unless request.respond_to?(:render) && request.render.respond_to?(:call)
request.render.call(*args)
end
end
end
end
# frozen_string_literal: true
module API
module Entities
class UserPath < UserBasic
include RequestAwareEntity
include UserStatusTooltip
expose :path do |user|
user_path(user)
end
end
end
end
...@@ -306,7 +306,7 @@ module API ...@@ -306,7 +306,7 @@ module API
context_commits = context_commits =
paginate(merge_request.merge_request_context_commits).map(&:to_commit) paginate(merge_request.merge_request_context_commits).map(&:to_commit)
present context_commits, with: Entities::Commit present context_commits, with: Entities::CommitWithLink, type: :full, request: merge_request
end end
params do params do
......
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