summaryrefslogtreecommitdiff
path: root/lib/api/commit_statuses.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/api/commit_statuses.rb')
-rw-r--r--lib/api/commit_statuses.rb75
1 files changed, 38 insertions, 37 deletions
diff --git a/lib/api/commit_statuses.rb b/lib/api/commit_statuses.rb
index 08b4f8db8b0..205c8e0ff6a 100644
--- a/lib/api/commit_statuses.rb
+++ b/lib/api/commit_statuses.rb
@@ -1,11 +1,11 @@
# frozen_string_literal: true
-require 'mime/types'
+require "mime/types"
module API
class CommitStatuses < Grape::API
params do
- requires :id, type: String, desc: 'The ID of a project'
+ requires :id, type: String, desc: "The ID of a project"
end
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
include PaginationParams
@@ -16,18 +16,18 @@ module API
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'
+ 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
# rubocop: disable CodeReuse/ActiveRecord
- get ':id/repository/commits/:sha/statuses' do
+ get ":id/repository/commits/:sha/statuses" do
authorize!(:read_commit_status, user_project)
- not_found!('Commit') unless user_project.commit(params[:sha])
+ not_found!("Commit") unless user_project.commit(params[:sha])
pipelines = user_project.ci_pipelines.where(sha: params[:sha])
statuses = ::CommitStatus.where(pipeline: pipelines)
@@ -39,26 +39,26 @@ module API
end
# rubocop: enable CodeReuse/ActiveRecord
- desc 'Post status to a commit' do
+ 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'
+ 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'
+ optional :coverage, type: Float, desc: "The total code coverage"
end
# rubocop: disable CodeReuse/ActiveRecord
- post ':id/statuses/:sha' do
+ post ":id/statuses/:sha" do
authorize! :create_commit_status, user_project
commit = @project.commit(params[:sha])
- not_found! 'Commit' unless commit
+ not_found! "Commit" unless commit
# Since the CommitStatus is attached to Ci::Pipeline (in the future Pipeline)
# We need to always have the pipeline object
@@ -69,19 +69,18 @@ module API
ref = params[:ref]
ref ||= @project.repository.branch_names_contains(commit.sha).first
- not_found! 'References for commit' unless ref
+ not_found! "References for commit" unless ref
- name = params[:name] || params[:context] || 'default'
+ name = params[:name] || params[:context] || "default"
pipeline = @project.pipeline_for(ref, commit.sha)
- unless pipeline
- pipeline = @project.ci_pipelines.create!(
- source: :external,
- sha: commit.sha,
- ref: ref,
- user: current_user,
- protected: @project.protected_for?(ref))
- end
+ pipeline ||= @project.ci_pipelines.create!(
+ source: :external,
+ sha: commit.sha,
+ ref: ref,
+ user: current_user,
+ protected: @project.protected_for?(ref)
+ )
status = GenericCommitStatus.running_or_pending.find_or_initialize_by(
project: @project,
@@ -100,23 +99,25 @@ module API
begin
case params[:state]
- when 'pending'
+ when "pending"
status.enqueue!
- when 'running'
+ when "running"
status.enqueue
status.run!
- when 'success'
+ when "success"
status.success!
- when 'failed'
+ when "failed"
status.drop!(:api_failure)
- when 'canceled'
+ when "canceled"
status.cancel!
else
- render_api_error!('invalid state', 400)
+ render_api_error!("invalid state", 400)
end
- MergeRequest.where(source_project: @project, source_branch: ref)
- .update_all(head_pipeline_id: pipeline.id) if pipeline.latest?
+ if pipeline.latest?
+ MergeRequest.where(source_project: @project, source_branch: ref)
+ .update_all(head_pipeline_id: pipeline.id)
+ end
present status, with: Entities::CommitStatus
rescue StateMachines::InvalidTransition => e