2014-02-18 05:41:21 -05:00
|
|
|
require 'mime/types'
|
|
|
|
|
|
|
|
module API
|
|
|
|
class Commits < Grape::API
|
2016-11-21 14:15:46 -05:00
|
|
|
include PaginationParams
|
|
|
|
|
2014-02-18 05:41:21 -05:00
|
|
|
before { authorize! :download_code, user_project }
|
|
|
|
|
2016-10-15 06:09:02 -04:00
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
end
|
2017-07-26 15:39:37 -04:00
|
|
|
resource :projects, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS do
|
2016-10-15 06:09:02 -04:00
|
|
|
desc 'Get a project repository commits' do
|
2017-10-05 04:48:05 -04:00
|
|
|
success Entities::Commit
|
2016-10-15 06:09:02 -04:00
|
|
|
end
|
|
|
|
params do
|
|
|
|
optional :ref_name, type: String, desc: 'The name of a repository branch or tag, if not given the default branch is used'
|
2017-02-20 09:54:37 -05:00
|
|
|
optional :since, type: DateTime, desc: 'Only commits after or on this date will be returned'
|
|
|
|
optional :until, type: DateTime, desc: 'Only commits before or on this date will be returned'
|
2016-06-20 21:37:40 -04:00
|
|
|
optional :path, type: String, desc: 'The file path'
|
2018-02-16 11:39:23 -05:00
|
|
|
optional :all, type: Boolean, desc: 'Every commit will be returned'
|
2017-03-02 21:06:06 -05:00
|
|
|
use :pagination
|
2016-10-15 06:09:02 -04:00
|
|
|
end
|
2017-07-26 15:39:37 -04:00
|
|
|
get ':id/repository/commits' do
|
2017-03-02 21:06:06 -05:00
|
|
|
path = params[:path]
|
|
|
|
before = params[:until]
|
|
|
|
after = params[:since]
|
2018-02-16 11:39:23 -05:00
|
|
|
ref = params[:ref_name] || user_project.try(:default_branch) || 'master' unless params[:all]
|
2017-02-05 04:30:36 -05:00
|
|
|
offset = (params[:page] - 1) * params[:per_page]
|
2018-02-16 11:39:23 -05:00
|
|
|
all = params[:all]
|
2016-10-15 06:09:02 -04:00
|
|
|
|
|
|
|
commits = user_project.repository.commits(ref,
|
2017-03-02 21:06:06 -05:00
|
|
|
path: path,
|
2016-10-15 06:09:02 -04:00
|
|
|
limit: params[:per_page],
|
|
|
|
offset: offset,
|
2017-03-02 21:06:06 -05:00
|
|
|
before: before,
|
2018-02-16 11:39:23 -05:00
|
|
|
after: after,
|
|
|
|
all: all)
|
2014-02-18 05:41:21 -05:00
|
|
|
|
2017-03-02 21:06:06 -05:00
|
|
|
commit_count =
|
2018-02-19 09:42:00 -05:00
|
|
|
if all || path || before || after
|
2018-02-16 11:39:23 -05:00
|
|
|
user_project.repository.count_commits(ref: ref, path: path, before: before, after: after, all: all)
|
2017-03-02 21:06:06 -05:00
|
|
|
else
|
|
|
|
# Cacheable commit count.
|
|
|
|
user_project.repository.commit_count_for_ref(ref)
|
|
|
|
end
|
|
|
|
|
|
|
|
paginated_commits = Kaminari.paginate_array(commits, total_count: commit_count)
|
2017-01-20 10:04:16 -05:00
|
|
|
|
2017-10-05 04:48:05 -04:00
|
|
|
present paginate(paginated_commits), with: Entities::Commit
|
2014-02-18 05:41:21 -05:00
|
|
|
end
|
|
|
|
|
2016-08-29 19:58:32 -04:00
|
|
|
desc 'Commit multiple file changes as one commit' do
|
2017-10-05 04:48:05 -04:00
|
|
|
success Entities::CommitDetail
|
2016-08-29 19:58:32 -04:00
|
|
|
detail 'This feature was introduced in GitLab 8.13'
|
|
|
|
end
|
|
|
|
params do
|
2017-08-04 13:18:07 -04:00
|
|
|
requires :branch, type: String, desc: 'Name of the branch to commit into. To create a new branch, also provide `start_branch`.'
|
2016-08-29 19:58:32 -04:00
|
|
|
requires :commit_message, type: String, desc: 'Commit message'
|
2016-11-24 07:30:53 -05:00
|
|
|
requires :actions, type: Array[Hash], desc: 'Actions to perform in commit'
|
2017-08-04 13:18:07 -04:00
|
|
|
optional :start_branch, type: String, desc: 'Name of the branch to start the new commit from'
|
2016-08-29 19:58:32 -04:00
|
|
|
optional :author_email, type: String, desc: 'Author email for commit'
|
|
|
|
optional :author_name, type: String, desc: 'Author name for commit'
|
|
|
|
end
|
2017-07-26 15:39:37 -04:00
|
|
|
post ':id/repository/commits' do
|
2016-08-29 19:58:32 -04:00
|
|
|
authorize! :push_code, user_project
|
|
|
|
|
2017-08-04 13:25:35 -04:00
|
|
|
attrs = declared_params
|
|
|
|
attrs[:branch_name] = attrs.delete(:branch)
|
2017-08-04 13:18:07 -04:00
|
|
|
attrs[:start_branch] ||= attrs[:branch_name]
|
2017-02-02 09:24:30 -05:00
|
|
|
|
2016-08-29 19:58:32 -04:00
|
|
|
result = ::Files::MultiService.new(user_project, current_user, attrs).execute
|
|
|
|
|
|
|
|
if result[:status] == :success
|
2017-07-03 18:46:52 -04:00
|
|
|
commit_detail = user_project.repository.commit(result[:result])
|
2017-10-05 04:48:05 -04:00
|
|
|
present commit_detail, with: Entities::CommitDetail
|
2016-08-29 19:58:32 -04:00
|
|
|
else
|
|
|
|
render_api_error!(result[:message], 400)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-15 06:09:02 -04:00
|
|
|
desc 'Get a specific commit of a project' do
|
2017-10-05 04:48:05 -04:00
|
|
|
success Entities::CommitDetail
|
2017-07-26 15:39:37 -04:00
|
|
|
failure [[404, 'Commit Not Found']]
|
2016-10-15 06:09:02 -04:00
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :sha, type: String, desc: 'A commit sha, or the name of a branch or tag'
|
2018-01-09 06:36:12 -05:00
|
|
|
optional :stats, type: Boolean, default: true, desc: 'Include commit stats'
|
2016-10-15 06:09:02 -04:00
|
|
|
end
|
2017-09-23 09:21:32 -04:00
|
|
|
get ':id/repository/commits/:sha', requirements: API::COMMIT_ENDPOINT_REQUIREMENTS do
|
2016-10-15 06:09:02 -04:00
|
|
|
commit = user_project.commit(params[:sha])
|
|
|
|
|
2017-07-26 15:39:37 -04:00
|
|
|
not_found! 'Commit' unless commit
|
2016-10-15 06:09:02 -04:00
|
|
|
|
2018-01-09 06:36:12 -05:00
|
|
|
present commit, with: Entities::CommitDetail, stats: params[:stats]
|
2014-02-18 05:41:21 -05:00
|
|
|
end
|
|
|
|
|
2016-10-15 06:09:02 -04:00
|
|
|
desc 'Get the diff for a specific commit of a project' do
|
2017-07-26 15:39:37 -04:00
|
|
|
failure [[404, 'Commit Not Found']]
|
2016-10-15 06:09:02 -04:00
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :sha, type: String, desc: 'A commit sha, or the name of a branch or tag'
|
2018-02-18 22:17:23 -05:00
|
|
|
use :pagination
|
2016-10-15 06:09:02 -04:00
|
|
|
end
|
2017-09-23 09:21:32 -04:00
|
|
|
get ':id/repository/commits/:sha/diff', requirements: API::COMMIT_ENDPOINT_REQUIREMENTS do
|
2016-10-15 06:09:02 -04:00
|
|
|
commit = user_project.commit(params[:sha])
|
|
|
|
|
2017-07-26 15:39:37 -04:00
|
|
|
not_found! 'Commit' unless commit
|
2016-10-15 06:09:02 -04:00
|
|
|
|
2018-02-18 22:17:23 -05:00
|
|
|
raw_diffs = ::Kaminari.paginate_array(commit.raw_diffs.to_a)
|
|
|
|
|
|
|
|
present paginate(raw_diffs), with: Entities::Diff
|
2014-02-18 05:41:21 -05:00
|
|
|
end
|
2014-06-27 10:48:30 -04:00
|
|
|
|
2016-10-15 06:09:02 -04:00
|
|
|
desc "Get a commit's comments" do
|
|
|
|
success Entities::CommitNote
|
2017-07-26 15:39:37 -04:00
|
|
|
failure [[404, 'Commit Not Found']]
|
2016-10-15 06:09:02 -04:00
|
|
|
end
|
|
|
|
params do
|
2016-11-21 14:15:46 -05:00
|
|
|
use :pagination
|
2016-10-15 06:09:02 -04:00
|
|
|
requires :sha, type: String, desc: 'A commit sha, or the name of a branch or tag'
|
|
|
|
end
|
2017-09-23 09:21:32 -04:00
|
|
|
get ':id/repository/commits/:sha/comments', requirements: API::COMMIT_ENDPOINT_REQUIREMENTS do
|
2016-10-15 06:09:02 -04:00
|
|
|
commit = user_project.commit(params[:sha])
|
|
|
|
|
2014-06-27 10:48:30 -04:00
|
|
|
not_found! 'Commit' unless commit
|
2017-11-07 11:11:37 -05:00
|
|
|
notes = commit.notes.order(:created_at)
|
2016-10-15 06:09:02 -04:00
|
|
|
|
2014-06-27 10:48:30 -04:00
|
|
|
present paginate(notes), with: Entities::CommitNote
|
|
|
|
end
|
|
|
|
|
2016-12-12 07:04:13 -05:00
|
|
|
desc 'Cherry pick commit into a branch' do
|
|
|
|
detail 'This feature was introduced in GitLab 8.15'
|
2017-10-05 04:48:05 -04:00
|
|
|
success Entities::Commit
|
2016-12-12 07:04:13 -05:00
|
|
|
end
|
|
|
|
params do
|
2017-07-26 15:39:37 -04:00
|
|
|
requires :sha, type: String, desc: 'A commit sha, or the name of a branch or tag to be cherry picked'
|
2016-12-12 07:04:13 -05:00
|
|
|
requires :branch, type: String, desc: 'The name of the branch'
|
|
|
|
end
|
2017-09-23 09:21:32 -04:00
|
|
|
post ':id/repository/commits/:sha/cherry_pick', requirements: API::COMMIT_ENDPOINT_REQUIREMENTS do
|
2016-12-12 07:04:13 -05:00
|
|
|
authorize! :push_code, user_project
|
|
|
|
|
|
|
|
commit = user_project.commit(params[:sha])
|
|
|
|
not_found!('Commit') unless commit
|
|
|
|
|
|
|
|
branch = user_project.repository.find_branch(params[:branch])
|
|
|
|
not_found!('Branch') unless branch
|
|
|
|
|
|
|
|
commit_params = {
|
|
|
|
commit: commit,
|
2017-02-16 19:24:56 -05:00
|
|
|
start_branch: params[:branch],
|
2017-04-19 20:37:44 -04:00
|
|
|
branch_name: params[:branch]
|
2016-12-12 07:04:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
result = ::Commits::CherryPickService.new(user_project, current_user, commit_params).execute
|
|
|
|
|
|
|
|
if result[:status] == :success
|
|
|
|
branch = user_project.repository.find_branch(params[:branch])
|
2017-10-05 04:48:05 -04:00
|
|
|
present user_project.repository.commit(branch.dereferenced_target), with: Entities::Commit
|
2016-12-12 07:04:13 -05:00
|
|
|
else
|
|
|
|
render_api_error!(result[:message], 400)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-09 08:30:27 -05:00
|
|
|
desc 'Get all references a commit is pushed to' do
|
2017-10-24 06:28:06 -04:00
|
|
|
detail 'This feature was introduced in GitLab 10.6'
|
2018-02-09 08:30:27 -05:00
|
|
|
success Entities::BasicRef
|
2017-10-24 06:28:06 -04:00
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :sha, type: String, desc: 'A commit sha'
|
2018-02-13 14:22:37 -05:00
|
|
|
optional :type, type: String, values: %w[branch tag all], default: 'all', desc: 'Scope'
|
|
|
|
use :pagination
|
2017-10-24 06:28:06 -04:00
|
|
|
end
|
|
|
|
get ':id/repository/commits/:sha/refs', requirements: API::COMMIT_ENDPOINT_REQUIREMENTS do
|
|
|
|
commit = user_project.commit(params[:sha])
|
|
|
|
not_found!('Commit') unless commit
|
|
|
|
|
2018-02-13 14:22:37 -05:00
|
|
|
refs = []
|
|
|
|
refs.concat(user_project.repository.branch_names_contains(commit.id).map {|name| { type: 'branch', name: name }}) unless params[:type] == 'tag'
|
|
|
|
refs.concat(user_project.repository.tag_names_contains(commit.id).map {|name| { type: 'tag', name: name }}) unless params[:type] == 'branch'
|
|
|
|
refs = Kaminari.paginate_array(refs)
|
2017-10-24 06:28:06 -04:00
|
|
|
|
2018-02-13 14:22:37 -05:00
|
|
|
present paginate(refs), with: Entities::BasicRef
|
2017-10-24 06:28:06 -04:00
|
|
|
end
|
|
|
|
|
2016-10-15 06:09:02 -04:00
|
|
|
desc 'Post comment to commit' do
|
|
|
|
success Entities::CommitNote
|
|
|
|
end
|
|
|
|
params do
|
2017-07-26 15:39:37 -04:00
|
|
|
requires :sha, type: String, desc: 'A commit sha, or the name of a branch or tag on which to post a comment'
|
2016-10-15 06:09:02 -04:00
|
|
|
requires :note, type: String, desc: 'The text of the comment'
|
|
|
|
optional :path, type: String, desc: 'The file path'
|
|
|
|
given :path do
|
|
|
|
requires :line, type: Integer, desc: 'The line number'
|
2018-02-09 08:30:27 -05:00
|
|
|
requires :line_type, type: String, values: %w[new old], default: 'new', desc: 'The type of the line'
|
2016-10-15 06:09:02 -04:00
|
|
|
end
|
|
|
|
end
|
2017-09-23 09:21:32 -04:00
|
|
|
post ':id/repository/commits/:sha/comments', requirements: API::COMMIT_ENDPOINT_REQUIREMENTS do
|
2016-10-15 06:09:02 -04:00
|
|
|
commit = user_project.commit(params[:sha])
|
2014-06-27 10:48:30 -04:00
|
|
|
not_found! 'Commit' unless commit
|
2016-10-15 06:09:02 -04:00
|
|
|
|
2014-06-27 10:48:30 -04:00
|
|
|
opts = {
|
|
|
|
note: params[:note],
|
|
|
|
noteable_type: 'Commit',
|
|
|
|
commit_id: commit.id
|
|
|
|
}
|
|
|
|
|
2016-10-15 06:09:02 -04:00
|
|
|
if params[:path]
|
2017-05-31 15:41:25 -04:00
|
|
|
commit.raw_diffs(limits: false).each do |diff|
|
2014-06-27 10:48:30 -04:00
|
|
|
next unless diff.new_path == params[:path]
|
2017-11-14 04:02:39 -05:00
|
|
|
|
2016-03-03 12:38:44 -05:00
|
|
|
lines = Gitlab::Diff::Parser.new.parse(diff.diff.each_line)
|
2014-06-27 10:48:30 -04:00
|
|
|
|
|
|
|
lines.each do |line|
|
2016-10-15 06:09:02 -04:00
|
|
|
next unless line.new_pos == params[:line] && line.type == params[:line_type]
|
2017-11-14 04:02:39 -05:00
|
|
|
|
2017-10-10 13:44:14 -04:00
|
|
|
break opts[:line_code] = Gitlab::Git.diff_line_code(diff.new_path, line.new_pos, line.old_pos)
|
2014-06-27 10:48:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
break if opts[:line_code]
|
|
|
|
end
|
2016-05-10 18:41:46 -04:00
|
|
|
|
|
|
|
opts[:type] = LegacyDiffNote.name if opts[:line_code]
|
2014-06-27 10:48:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
note = ::Notes::CreateService.new(user_project, current_user, opts).execute
|
|
|
|
|
|
|
|
if note.save
|
|
|
|
present note, with: Entities::CommitNote
|
|
|
|
else
|
2015-01-07 04:46:00 -05:00
|
|
|
render_api_error!("Failed to save note #{note.errors.messages}", 400)
|
2014-06-27 10:48:30 -04:00
|
|
|
end
|
|
|
|
end
|
2014-02-18 05:41:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|