2014-02-18 05:41:21 -05:00
|
|
|
require 'mime/types'
|
|
|
|
|
|
|
|
module API
|
2014-03-31 09:31:53 -04:00
|
|
|
# Projects commits API
|
2014-02-18 05:41:21 -05:00
|
|
|
class Commits < Grape::API
|
|
|
|
before { authenticate! }
|
|
|
|
before { authorize! :download_code, user_project }
|
|
|
|
|
|
|
|
resource :projects do
|
|
|
|
# Get a project repository commits
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# ref_name (optional) - The name of a repository branch or tag, if not given the default branch is used
|
2016-04-22 08:07:25 -04:00
|
|
|
# since (optional) - Only commits after or in this date will be returned
|
|
|
|
# until (optional) - Only commits before or in this date will be returned
|
2014-02-18 05:41:21 -05:00
|
|
|
# Example Request:
|
|
|
|
# GET /projects/:id/repository/commits
|
|
|
|
get ":id/repository/commits" do
|
2016-04-22 08:07:25 -04:00
|
|
|
datetime_attributes! :since, :until
|
|
|
|
|
2014-02-18 05:41:21 -05:00
|
|
|
page = (params[:page] || 0).to_i
|
|
|
|
per_page = (params[:per_page] || 20).to_i
|
|
|
|
ref = params[:ref_name] || user_project.try(:default_branch) || 'master'
|
2016-04-22 08:07:25 -04:00
|
|
|
after = params[:since]
|
|
|
|
before = params[:until]
|
2014-02-18 05:41:21 -05:00
|
|
|
|
2016-04-22 08:07:25 -04:00
|
|
|
commits = user_project.repository.commits(ref, limit: per_page, offset: page * per_page, after: after, before: before)
|
2014-02-18 05:41:21 -05:00
|
|
|
present commits, with: Entities::RepoCommit
|
|
|
|
end
|
|
|
|
|
|
|
|
# Get a specific commit of a project
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# sha (required) - The commit hash or name of a repository branch or tag
|
|
|
|
# Example Request:
|
|
|
|
# GET /projects/:id/repository/commits/:sha
|
|
|
|
get ":id/repository/commits/:sha" do
|
|
|
|
sha = params[:sha]
|
2015-04-21 09:13:40 -04:00
|
|
|
commit = user_project.commit(sha)
|
2014-02-18 05:41:21 -05:00
|
|
|
not_found! "Commit" unless commit
|
|
|
|
present commit, with: Entities::RepoCommitDetail
|
|
|
|
end
|
|
|
|
|
|
|
|
# Get the diff for a specific commit of a project
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# sha (required) - The commit or branch name
|
|
|
|
# Example Request:
|
|
|
|
# GET /projects/:id/repository/commits/:sha/diff
|
|
|
|
get ":id/repository/commits/:sha/diff" do
|
|
|
|
sha = params[:sha]
|
2015-04-21 09:13:40 -04:00
|
|
|
commit = user_project.commit(sha)
|
2014-02-18 05:41:21 -05:00
|
|
|
not_found! "Commit" unless commit
|
2016-07-27 13:00:34 -04:00
|
|
|
commit.raw_diffs.to_a
|
2014-02-18 05:41:21 -05:00
|
|
|
end
|
2014-06-27 10:48:30 -04:00
|
|
|
|
|
|
|
# Get a commit's comments
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# sha (required) - The commit hash
|
|
|
|
# Examples:
|
|
|
|
# GET /projects/:id/repository/commits/:sha/comments
|
|
|
|
get ':id/repository/commits/:sha/comments' do
|
|
|
|
sha = params[:sha]
|
2015-04-21 09:13:40 -04:00
|
|
|
commit = user_project.commit(sha)
|
2014-06-27 10:48:30 -04:00
|
|
|
not_found! 'Commit' unless commit
|
2015-05-08 08:34:10 -04:00
|
|
|
notes = Note.where(commit_id: commit.id).order(:created_at)
|
2014-06-27 10:48:30 -04:00
|
|
|
present paginate(notes), with: Entities::CommitNote
|
|
|
|
end
|
|
|
|
|
|
|
|
# Post comment to commit
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# sha (required) - The commit hash
|
|
|
|
# note (required) - Text of comment
|
|
|
|
# path (optional) - The file path
|
|
|
|
# line (optional) - The line number
|
|
|
|
# line_type (optional) - The type of line (new or old)
|
|
|
|
# Examples:
|
|
|
|
# POST /projects/:id/repository/commits/:sha/comments
|
|
|
|
post ':id/repository/commits/:sha/comments' do
|
|
|
|
required_attributes! [:note]
|
|
|
|
|
|
|
|
sha = params[:sha]
|
2015-04-21 09:13:40 -04:00
|
|
|
commit = user_project.commit(sha)
|
2014-06-27 10:48:30 -04:00
|
|
|
not_found! 'Commit' unless commit
|
|
|
|
opts = {
|
|
|
|
note: params[:note],
|
|
|
|
noteable_type: 'Commit',
|
|
|
|
commit_id: commit.id
|
|
|
|
}
|
|
|
|
|
|
|
|
if params[:path] && params[:line] && params[:line_type]
|
2016-07-27 13:00:34 -04:00
|
|
|
commit.raw_diffs(all_diffs: true).each do |diff|
|
2014-06-27 10:48:30 -04:00
|
|
|
next unless diff.new_path == params[:path]
|
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|
|
|
|
|
next unless line.new_pos == params[:line].to_i && line.type == params[:line_type]
|
|
|
|
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-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
|