2015-10-06 06:01:16 -04:00
|
|
|
require 'mime/types'
|
|
|
|
|
|
|
|
module API
|
|
|
|
# Project commit statuses API
|
2016-04-15 11:35:40 -04:00
|
|
|
class CommitStatuses < Grape::API
|
2015-10-06 06:01:16 -04:00
|
|
|
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
|
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)
|
2015-10-06 06:01:16 -04:00
|
|
|
statuses = statuses.latest unless parse_boolean(params[:all])
|
|
|
|
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
|
|
|
|
|
|
|
|
# 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 06:15:35 -04:00
|
|
|
# POST /projects/:id/statuses/:sha
|
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
|
2015-10-06 06:01:16 -04:00
|
|
|
required_attributes! [:state]
|
|
|
|
attrs = attributes_for_keys [:ref, :target_url, :description, :context, :name]
|
|
|
|
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]
|
|
|
|
unless ref
|
|
|
|
branches = @project.repository.branch_names_contains(commit.sha)
|
|
|
|
not_found! 'References for commit' if branches.none?
|
|
|
|
ref = branches.first
|
|
|
|
end
|
2016-04-11 10:55:40 -04:00
|
|
|
|
2016-06-03 10:27:50 -04:00
|
|
|
pipeline = @project.ensure_pipeline(commit.sha, ref)
|
2015-10-06 06:01:16 -04:00
|
|
|
|
|
|
|
name = params[:name] || params[:context]
|
2016-06-03 10:27:50 -04:00
|
|
|
status = GenericCommitStatus.running_or_pending.find_by(pipeline: pipeline, name: name, ref: params[:ref])
|
|
|
|
status ||= GenericCommitStatus.new(project: @project, pipeline: pipeline, user: current_user)
|
2015-10-06 06:01:16 -04:00
|
|
|
status.update(attrs)
|
|
|
|
|
|
|
|
case params[:state].to_s
|
2015-10-12 06:16:00 -04: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 06:01:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if status.save
|
|
|
|
present status, with: Entities::CommitStatus
|
|
|
|
else
|
|
|
|
render_validation_error!(status)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|