gitlab-org--gitlab-foss/lib/api/commit_statuses.rb

83 lines
3.0 KiB
Ruby
Raw Normal View History

2015-10-06 10:01:16 +00:00
require 'mime/types'
module API
# Project commit statuses API
class CommitStatus < Grape::API
resource :projects do
before { authenticate! }
# Get a commit's statuses
#
# Parameters:
# id (required) - The ID of a project
# sha (required) - The commit hash
# ref (optional) - The ref
# stage (optional) - The stage
# name (optional) - The name
# all (optional) - Show all statuses, default: false
# Examples:
# GET /projects/:id/repository/commits/:sha/statuses
get ':id/repository/commits/:sha/statuses' do
authorize!(:read_commit_status, user_project)
not_found!('Commit') unless user_project.commit(params[:sha])
2016-03-31 17:51:28 +00:00
ci_commit = user_project.ci_commit(params[:sha], params[:ref])
return [] unless ci_commit
2015-10-06 10:01:16 +00:00
statuses = ci_commit.statuses
statuses = statuses.latest unless parse_boolean(params[:all])
statuses = statuses.where(ref: params[:ref]) if params[:ref].present?
2015-10-12 13:45:46 +00:00
statuses = statuses.where(stage: params[:stage]) if params[:stage].present?
2015-10-06 10:01:16 +00:00
statuses = statuses.where(name: params[:name]) if params[:name].present?
present paginate(statuses), with: Entities::CommitStatus
end
# Post status to commit
#
# Parameters:
# id (required) - The ID of a project
# sha (required) - The commit hash
# ref (optional) - The ref
# state (required) - The state of the status. Can be: pending, running, success, error or failure
# target_url (optional) - The target URL to associate with this status
# description (optional) - A short description of the status
# name or context (optional) - A string label to differentiate this status from the status of other systems. Default: "default"
# Examples:
2015-10-12 10:15:35 +00:00
# POST /projects/:id/statuses/:sha
2015-10-06 10:01:16 +00:00
post ':id/statuses/:sha' do
2015-10-12 19:35:52 +00:00
authorize! :create_commit_status, user_project
2015-10-06 10:01:16 +00:00
required_attributes! [:state]
attrs = attributes_for_keys [:ref, :target_url, :description, :context, :name]
commit = @project.commit(params[:sha])
not_found! 'Commit' unless commit
ci_commit = @project.ensure_ci_commit(commit.sha)
name = params[:name] || params[:context]
status = GenericCommitStatus.running_or_pending.find_by(commit: ci_commit, name: name, ref: params[:ref])
2015-12-04 11:55:23 +00:00
status ||= GenericCommitStatus.new(project: @project, commit: ci_commit, user: current_user)
2015-10-06 10:01:16 +00:00
status.update(attrs)
case params[:state].to_s
2015-10-12 10:16:00 +00:00
when 'running'
status.run
when 'success'
status.success
when 'failed'
status.drop
when 'canceled'
status.cancel
else
status.status = params[:state].to_s
2015-10-06 10:01:16 +00:00
end
if status.save
present status, with: Entities::CommitStatus
else
render_validation_error!(status)
end
end
end
end
end