debian-mirror-gitlab/lib/api/commits.rb

205 lines
7.3 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
require 'mime/types'
module API
class Commits < Grape::API
2017-08-17 22:00:37 +05:30
include PaginationParams
2014-09-02 18:07:02 +05:30
before { authenticate! }
before { authorize! :download_code, user_project }
2016-11-03 12:29:30 +05:30
params do
requires :id, type: String, desc: 'The ID of a project'
end
2017-08-17 22:00:37 +05:30
resource :projects, requirements: { id: %r{[^/]+} } do
2016-11-03 12:29:30 +05:30
desc 'Get a project repository commits' do
success Entities::RepoCommit
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-08-17 22:00:37 +05:30
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'
optional :path, type: String, desc: 'The file path'
use :pagination
2016-11-03 12:29:30 +05:30
end
2014-09-02 18:07:02 +05:30
get ":id/repository/commits" do
2017-08-17 22:00:37 +05:30
path = params[:path]
before = params[:until]
after = params[:since]
ref = params[:ref_name] || user_project.try(:default_branch) || 'master'
offset = (params[:page] - 1) * params[:per_page]
2016-11-03 12:29:30 +05:30
commits = user_project.repository.commits(ref,
2017-08-17 22:00:37 +05:30
path: path,
2016-11-03 12:29:30 +05:30
limit: params[:per_page],
offset: offset,
2017-08-17 22:00:37 +05:30
before: before,
after: after)
commit_count =
if path || before || after
user_project.repository.count_commits(ref: ref, path: path, before: before, after: after)
else
# Cacheable commit count.
user_project.repository.commit_count_for_ref(ref)
end
2014-09-02 18:07:02 +05:30
2017-08-17 22:00:37 +05:30
paginated_commits = Kaminari.paginate_array(commits, total_count: commit_count)
present paginate(paginated_commits), with: Entities::RepoCommit
2014-09-02 18:07:02 +05:30
end
2016-11-03 12:29:30 +05:30
desc 'Commit multiple file changes as one commit' do
success Entities::RepoCommitDetail
detail 'This feature was introduced in GitLab 8.13'
end
params do
2017-08-17 22:00:37 +05:30
requires :branch, type: String, desc: 'The name of branch'
2016-11-03 12:29:30 +05:30
requires :commit_message, type: String, desc: 'Commit message'
2017-08-17 22:00:37 +05:30
requires :actions, type: Array[Hash], desc: 'Actions to perform in commit'
2016-11-03 12:29:30 +05:30
optional :author_email, type: String, desc: 'Author email for commit'
optional :author_name, type: String, desc: 'Author name for commit'
end
post ":id/repository/commits" do
authorize! :push_code, user_project
2017-08-17 22:00:37 +05:30
attrs = declared_params.merge(start_branch: declared_params[:branch], branch_name: declared_params[:branch])
2016-11-03 12:29:30 +05:30
result = ::Files::MultiService.new(user_project, current_user, attrs).execute
if result[:status] == :success
commit_detail = user_project.repository.commits(result[:result], limit: 1).first
present commit_detail, with: Entities::RepoCommitDetail
else
render_api_error!(result[:message], 400)
end
end
desc 'Get a specific commit of a project' do
success Entities::RepoCommitDetail
failure [[404, 'Not Found']]
end
params do
requires :sha, type: String, desc: 'A commit sha, or the name of a branch or tag'
end
2014-09-02 18:07:02 +05:30
get ":id/repository/commits/:sha" do
2016-11-03 12:29:30 +05:30
commit = user_project.commit(params[:sha])
2014-09-02 18:07:02 +05:30
not_found! "Commit" unless commit
2016-11-03 12:29:30 +05:30
2014-09-02 18:07:02 +05:30
present commit, with: Entities::RepoCommitDetail
end
2016-11-03 12:29:30 +05:30
desc 'Get the diff for a specific commit of a project' do
failure [[404, 'Not Found']]
end
params do
requires :sha, type: String, desc: 'A commit sha, or the name of a branch or tag'
end
2014-09-02 18:07:02 +05:30
get ":id/repository/commits/:sha/diff" do
2016-11-03 12:29:30 +05:30
commit = user_project.commit(params[:sha])
2014-09-02 18:07:02 +05:30
not_found! "Commit" unless commit
2016-11-03 12:29:30 +05:30
2016-09-13 17:45:13 +05:30
commit.raw_diffs.to_a
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
2016-11-03 12:29:30 +05:30
desc "Get a commit's comments" do
success Entities::CommitNote
failure [[404, 'Not Found']]
end
params do
2017-08-17 22:00:37 +05:30
use :pagination
2016-11-03 12:29:30 +05:30
requires :sha, type: String, desc: 'A commit sha, or the name of a branch or tag'
end
2015-04-26 12:48:37 +05:30
get ':id/repository/commits/:sha/comments' do
2016-11-03 12:29:30 +05:30
commit = user_project.commit(params[:sha])
2015-04-26 12:48:37 +05:30
not_found! 'Commit' unless commit
2017-08-17 22:00:37 +05:30
notes = user_project.notes.where(commit_id: commit.id).order(:created_at)
2016-11-03 12:29:30 +05:30
2015-04-26 12:48:37 +05:30
present paginate(notes), with: Entities::CommitNote
end
2017-08-17 22:00:37 +05:30
desc 'Cherry pick commit into a branch' do
detail 'This feature was introduced in GitLab 8.15'
success Entities::RepoCommit
end
params do
requires :sha, type: String, desc: 'A commit sha to be cherry picked'
requires :branch, type: String, desc: 'The name of the branch'
end
post ':id/repository/commits/:sha/cherry_pick' do
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,
start_branch: params[:branch],
branch_name: params[:branch]
}
result = ::Commits::CherryPickService.new(user_project, current_user, commit_params).execute
if result[:status] == :success
branch = user_project.repository.find_branch(params[:branch])
present user_project.repository.commit(branch.dereferenced_target), with: Entities::RepoCommit
else
render_api_error!(result[:message], 400)
end
end
2016-11-03 12:29:30 +05:30
desc 'Post comment to commit' do
success Entities::CommitNote
end
params do
requires :sha, type: String, regexp: /\A\h{6,40}\z/, desc: "The commit's SHA"
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'
2017-08-17 22:00:37 +05:30
requires :line_type, type: String, values: %w(new old), default: 'new', desc: 'The type of the line'
2016-11-03 12:29:30 +05:30
end
end
2015-04-26 12:48:37 +05:30
post ':id/repository/commits/:sha/comments' do
2016-11-03 12:29:30 +05:30
commit = user_project.commit(params[:sha])
2015-04-26 12:48:37 +05:30
not_found! 'Commit' unless commit
2016-11-03 12:29:30 +05:30
2015-04-26 12:48:37 +05:30
opts = {
note: params[:note],
noteable_type: 'Commit',
commit_id: commit.id
}
2016-11-03 12:29:30 +05:30
if params[:path]
2016-09-13 17:45:13 +05:30
commit.raw_diffs(all_diffs: true).each do |diff|
2015-04-26 12:48:37 +05:30
next unless diff.new_path == params[:path]
2016-06-02 11:05:42 +05:30
lines = Gitlab::Diff::Parser.new.parse(diff.diff.each_line)
2015-04-26 12:48:37 +05:30
lines.each do |line|
2016-11-03 12:29:30 +05:30
next unless line.new_pos == params[:line] && line.type == params[:line_type]
2015-04-26 12:48:37 +05:30
break opts[:line_code] = Gitlab::Diff::LineCode.generate(diff.new_path, line.new_pos, line.old_pos)
end
break if opts[:line_code]
end
2016-06-02 11:05:42 +05:30
opts[:type] = LegacyDiffNote.name if opts[:line_code]
2015-04-26 12:48:37 +05:30
end
note = ::Notes::CreateService.new(user_project, current_user, opts).execute
if note.save
present note, with: Entities::CommitNote
else
render_api_error!("Failed to save note #{note.errors.messages}", 400)
end
end
2014-09-02 18:07:02 +05:30
end
end
end