diff options
Diffstat (limited to 'lib/api/commit_statuses.rb')
-rw-r--r-- | lib/api/commit_statuses.rb | 82 |
1 files changed, 14 insertions, 68 deletions
diff --git a/lib/api/commit_statuses.rb b/lib/api/commit_statuses.rb index 531235dc9b2..acb64cd0d3a 100644 --- a/lib/api/commit_statuses.rb +++ b/lib/api/commit_statuses.rb @@ -61,7 +61,7 @@ module API requires :sha, type: String, desc: 'The commit hash', documentation: { example: '18f3e63d05582537db6d183d9d557be09e1f90c8' } requires :state, type: String, desc: 'The state of the status', - values: %w(pending running success failed canceled), + values: %w[pending running success failed canceled], documentation: { example: 'pending' } optional :ref, type: String, desc: 'The ref', documentation: { example: 'develop' } @@ -80,75 +80,16 @@ module API post ':id/statuses/:sha' do authorize! :create_commit_status, user_project - not_found! 'Commit' unless commit + response = + ::Ci::CreateCommitStatusService + .new(user_project, current_user, params) + .execute(optional_commit_status_params: optional_commit_status_params) - # Since the CommitStatus is attached to ::Ci::Pipeline (in the future Pipeline) - # 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 - - pipeline = all_matching_pipelines.first - - ref = params[:ref] - ref ||= pipeline&.ref - ref ||= user_project.repository.branch_names_contains(commit.sha).first - not_found! 'References for commit' unless ref - - name = params[:name] || params[:context] || 'default' - - pipeline ||= user_project.ci_pipelines.build( - source: :external, - sha: commit.sha, - ref: ref, - user: current_user, - protected: user_project.protected_for?(ref)) - - pipeline.ensure_project_iid! - pipeline.save! - - authorize! :update_pipeline, pipeline - - # rubocop: disable Performance/ActiveRecordSubtransactionMethods - stage = pipeline.stages.safe_find_or_create_by!(name: 'external') do |stage| - stage.position = GenericCommitStatus::EXTERNAL_STAGE_IDX - stage.project = pipeline.project - end - # rubocop: enable Performance/ActiveRecordSubtransactionMethods - - status = GenericCommitStatus.running_or_pending.find_or_initialize_by( - project: user_project, - pipeline: pipeline, - name: name, - ref: ref, - user: current_user, - protected: user_project.protected_for?(ref), - ci_stage: stage, - stage_idx: stage.position, - stage: 'external' - ) - - updatable_optional_attributes = %w[target_url description coverage] - status.assign_attributes(attributes_for_keys(updatable_optional_attributes)) - - render_validation_error!(status) unless status.valid? - - response = ::Ci::Pipelines::AddJobService.new(pipeline).execute!(status) do |job| - apply_job_state!(job) - rescue ::StateMachines::InvalidTransition => e - render_api_error!(e.message, 400) + if response.error? + render_api_error!(response.message, response.http_status) + else + present response.payload[:job], with: Entities::CommitStatus end - - render_validation_error!(response.payload[:job]) unless response.success? - - if pipeline.latest? - MergeRequest - .where(source_project: user_project, source_branch: ref) - .update_all(head_pipeline_id: pipeline.id) - end - - present response.payload[:job], with: Entities::CommitStatus end # rubocop: enable CodeReuse/ActiveRecord @@ -183,6 +124,11 @@ module API render_api_error!('invalid state', 400) end end + + def optional_commit_status_params + updatable_optional_attributes = %w[target_url description coverage] + attributes_for_keys(updatable_optional_attributes) + end end end end |