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

130 lines
4.9 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2015-10-24 18:46:33 +05:30
require 'mime/types'
module API
2016-06-02 11:05:42 +05:30
class CommitStatuses < Grape::API
2017-08-17 22:00:37 +05:30
params do
requires :id, type: String, desc: 'The ID of a project'
end
2019-01-03 12:48:30 +05:30
resource :projects, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS do
2017-08-17 22:00:37 +05:30
include PaginationParams
2015-10-24 18:46:33 +05:30
before { authenticate! }
2017-08-17 22:00:37 +05:30
desc "Get a commit's statuses" do
success Entities::CommitStatus
end
params do
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'
use :pagination
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2015-10-24 18:46:33 +05:30
get ':id/repository/commits/:sha/statuses' do
2016-06-02 11:05:42 +05:30
authorize!(:read_commit_status, user_project)
not_found!('Commit') unless user_project.commit(params[:sha])
2019-01-03 12:48:30 +05:30
pipelines = user_project.pipelines.where(sha: params[:sha])
statuses = ::CommitStatus.where(pipeline: pipelines)
2016-09-13 17:45:13 +05:30
statuses = statuses.latest unless to_boolean(params[:all])
2015-10-24 18:46:33 +05:30
statuses = statuses.where(ref: params[:ref]) if params[:ref].present?
statuses = statuses.where(stage: params[:stage]) if params[:stage].present?
statuses = statuses.where(name: params[:name]) if params[:name].present?
present paginate(statuses), with: Entities::CommitStatus
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2015-10-24 18:46:33 +05:30
2017-08-17 22:00:37 +05:30
desc 'Post status to a commit' do
success Entities::CommitStatus
end
params do
requires :sha, type: String, desc: 'The commit hash'
requires :state, type: String, desc: 'The state of the status',
values: %w(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"'
optional :coverage, type: Float, desc: 'The total code coverage'
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2015-10-24 18:46:33 +05:30
post ':id/statuses/:sha' do
authorize! :create_commit_status, user_project
2017-08-17 22:00:37 +05:30
2015-10-24 18:46:33 +05:30
commit = @project.commit(params[:sha])
not_found! 'Commit' unless commit
# Since the CommitStatus is attached to Ci::Pipeline (in the future Pipeline)
2016-06-02 11:05:42 +05:30
# 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-09-29 09:46:39 +05:30
ref ||= @project.repository.branch_names_contains(commit.sha).first
not_found! 'References for commit' unless ref
2016-06-02 11:05:42 +05:30
2016-09-29 09:46:39 +05:30
name = params[:name] || params[:context] || 'default'
2015-10-24 18:46:33 +05:30
2017-09-10 17:25:29 +05:30
pipeline = @project.pipeline_for(ref, commit.sha)
unless pipeline
2019-01-03 12:48:30 +05:30
pipeline = @project.pipelines.create!(
2017-09-10 17:25:29 +05:30
source: :external,
sha: commit.sha,
ref: ref,
2018-03-17 18:26:18 +05:30
user: current_user,
protected: @project.protected_for?(ref))
2017-09-10 17:25:29 +05:30
end
2015-10-24 18:46:33 +05:30
2016-09-29 09:46:39 +05:30
status = GenericCommitStatus.running_or_pending.find_or_initialize_by(
2017-08-17 22:00:37 +05:30
project: @project,
pipeline: pipeline,
name: name,
ref: ref,
2018-03-17 18:26:18 +05:30
user: current_user,
protected: @project.protected_for?(ref)
2017-08-17 22:00:37 +05:30
)
optional_attributes =
attributes_for_keys(%w[target_url description coverage])
status.update(optional_attributes) if optional_attributes.any?
render_validation_error!(status) if status.invalid?
2016-09-29 09:46:39 +05:30
begin
2017-08-17 22:00:37 +05:30
case params[:state]
2016-09-29 09:46:39 +05:30
when 'pending'
status.enqueue!
when 'running'
status.enqueue
status.run!
when 'success'
status.success!
when 'failed'
2018-03-17 18:26:18 +05:30
status.drop!(:api_failure)
2016-09-29 09:46:39 +05:30
when 'canceled'
status.cancel!
else
render_api_error!('invalid state', 400)
end
2015-10-24 18:46:33 +05:30
2017-09-10 17:25:29 +05:30
MergeRequest.where(source_project: @project, source_branch: ref)
.update_all(head_pipeline_id: pipeline) if pipeline.latest?
2015-10-24 18:46:33 +05:30
present status, with: Entities::CommitStatus
2016-09-29 09:46:39 +05:30
rescue StateMachines::InvalidTransition => e
render_api_error!(e.message, 400)
2015-10-24 18:46:33 +05:30
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2015-10-24 18:46:33 +05:30
end
end
end