2015-10-06 06:01:16 -04:00
|
|
|
require 'mime/types'
|
|
|
|
|
|
|
|
module API
|
2016-04-15 11:35:40 -04:00
|
|
|
class CommitStatuses < Grape::API
|
2015-10-06 06:01:16 -04:00
|
|
|
resource :projects do
|
2016-12-04 12:11:19 -05:00
|
|
|
include PaginationParams
|
|
|
|
|
2015-10-06 06:01:16 -04:00
|
|
|
before { authenticate! }
|
|
|
|
|
2016-10-14 04:45:23 -04:00
|
|
|
desc "Get a commit's statuses" do
|
|
|
|
success Entities::CommitStatus
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
requires :sha, type: String, desc: 'The commit hash'
|
|
|
|
optional :ref, type: String, desc: 'The ref'
|
|
|
|
optional :stage, type: String, desc: 'The stage'
|
|
|
|
optional :name, type: String, desc: 'The name'
|
|
|
|
optional :all, type: String, desc: 'Show all statuses, default: false'
|
2016-12-04 12:11:19 -05:00
|
|
|
use :pagination
|
2016-10-14 04:45:23 -04:00
|
|
|
end
|
2015-10-06 06:01:16 -04:00
|
|
|
get ':id/repository/commits/:sha/statuses' do
|
2016-02-29 07:54:33 -05:00
|
|
|
authorize!(:read_commit_status, user_project)
|
|
|
|
|
|
|
|
not_found!('Commit') unless user_project.commit(params[:sha])
|
|
|
|
|
2016-06-03 10:27:50 -04:00
|
|
|
pipelines = user_project.pipelines.where(sha: params[:sha])
|
|
|
|
statuses = ::CommitStatus.where(pipeline: pipelines)
|
2016-07-20 02:55:44 -04:00
|
|
|
statuses = statuses.latest unless to_boolean(params[:all])
|
2015-10-06 06:01:16 -04:00
|
|
|
statuses = statuses.where(ref: params[:ref]) if params[:ref].present?
|
2015-10-12 09:45:46 -04:00
|
|
|
statuses = statuses.where(stage: params[:stage]) if params[:stage].present?
|
2015-10-06 06:01:16 -04:00
|
|
|
statuses = statuses.where(name: params[:name]) if params[:name].present?
|
|
|
|
present paginate(statuses), with: Entities::CommitStatus
|
|
|
|
end
|
|
|
|
|
2016-10-14 04:45:23 -04:00
|
|
|
desc 'Post status to a commit' do
|
|
|
|
success Entities::CommitStatus
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
requires :sha, type: String, desc: 'The commit hash'
|
|
|
|
requires :state, type: String, desc: 'The state of the status',
|
|
|
|
values: ['pending', 'running', 'success', 'failed', 'canceled']
|
|
|
|
optional :ref, type: String, desc: 'The ref'
|
|
|
|
optional :target_url, type: String, desc: 'The target URL to associate with this status'
|
|
|
|
optional :description, type: String, desc: 'A short description of the status'
|
|
|
|
optional :name, type: String, desc: 'A string label to differentiate this status from the status of other systems. Default: "default"'
|
|
|
|
optional :context, type: String, desc: 'A string label to differentiate this status from the status of other systems. Default: "default"'
|
2017-02-13 21:28:45 -05:00
|
|
|
optional :coverage, type: Float, desc: 'The total code coverage'
|
2016-10-14 04:45:23 -04:00
|
|
|
end
|
2015-10-06 06:01:16 -04:00
|
|
|
post ':id/statuses/:sha' do
|
2015-10-12 15:35:52 -04:00
|
|
|
authorize! :create_commit_status, user_project
|
2016-10-14 04:45:23 -04:00
|
|
|
|
2015-10-06 06:01:16 -04:00
|
|
|
commit = @project.commit(params[:sha])
|
|
|
|
not_found! 'Commit' unless commit
|
|
|
|
|
2016-06-02 10:19:18 -04:00
|
|
|
# Since the CommitStatus is attached to Ci::Pipeline (in the future Pipeline)
|
2016-04-16 16:43:40 -04:00
|
|
|
# We need to always have the pipeline object
|
|
|
|
# To have a valid pipeline object that can be attached to specific MR
|
|
|
|
# Other CI service needs to send `ref`
|
|
|
|
# If we don't receive it, we will attach the CommitStatus to
|
|
|
|
# the first found branch on that commit
|
|
|
|
|
|
|
|
ref = params[:ref]
|
2016-08-25 11:52:09 -04:00
|
|
|
ref ||= @project.repository.branch_names_contains(commit.sha).first
|
|
|
|
not_found! 'References for commit' unless ref
|
|
|
|
|
|
|
|
name = params[:name] || params[:context] || 'default'
|
2016-04-11 10:55:40 -04:00
|
|
|
|
2016-08-11 05:26:04 -04:00
|
|
|
pipeline = @project.ensure_pipeline(ref, commit.sha, current_user)
|
2015-10-06 06:01:16 -04:00
|
|
|
|
2016-08-25 11:52:09 -04:00
|
|
|
status = GenericCommitStatus.running_or_pending.find_or_initialize_by(
|
2016-10-20 12:51:03 -04:00
|
|
|
project: @project,
|
|
|
|
pipeline: pipeline,
|
|
|
|
user: current_user,
|
|
|
|
name: name,
|
|
|
|
ref: ref,
|
|
|
|
target_url: params[:target_url],
|
2017-02-13 21:28:45 -05:00
|
|
|
description: params[:description],
|
|
|
|
coverage: params[:coverage]
|
2016-10-20 12:51:03 -04:00
|
|
|
)
|
2015-10-06 06:01:16 -04:00
|
|
|
|
2017-01-18 06:24:53 -05:00
|
|
|
render_validation_error!(status) if status.invalid?
|
|
|
|
|
2016-08-25 11:52:09 -04:00
|
|
|
begin
|
2016-11-24 07:30:53 -05:00
|
|
|
case params[:state]
|
2016-08-25 11:52:09 -04:00
|
|
|
when 'pending'
|
|
|
|
status.enqueue!
|
|
|
|
when 'running'
|
|
|
|
status.enqueue
|
|
|
|
status.run!
|
|
|
|
when 'success'
|
|
|
|
status.success!
|
|
|
|
when 'failed'
|
|
|
|
status.drop!
|
|
|
|
when 'canceled'
|
|
|
|
status.cancel!
|
|
|
|
else
|
|
|
|
render_api_error!('invalid state', 400)
|
|
|
|
end
|
2015-10-06 06:01:16 -04:00
|
|
|
|
|
|
|
present status, with: Entities::CommitStatus
|
2016-08-25 11:52:09 -04:00
|
|
|
rescue StateMachines::InvalidTransition => e
|
|
|
|
render_api_error!(e.message, 400)
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|