From 68f6c61cf621db82ac98d561782590b1866fcf6f Mon Sep 17 00:00:00 2001 From: Shinya Maeda Date: Fri, 1 Sep 2017 16:52:11 +0900 Subject: - Allow runner API to pass failure_reason - Fix spec --- app/models/commit_status.rb | 2 +- .../feature-sm-37239-implement-failure_reason-on-ci_builds.yml | 5 +++++ lib/api/runner.rb | 8 +++++++- spec/lib/gitlab/import_export/safe_model_attributes.yml | 1 + spec/models/commit_status_spec.rb | 10 ++++------ spec/requests/api/commit_statuses_spec.rb | 2 +- spec/requests/api/runner_spec.rb | 9 +++++++++ spec/services/ci/retry_build_service_spec.rb | 2 +- 8 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 changelogs/unreleased/feature-sm-37239-implement-failure_reason-on-ci_builds.yml diff --git a/app/models/commit_status.rb b/app/models/commit_status.rb index 1c4088d5af9..1e8f28b6466 100644 --- a/app/models/commit_status.rb +++ b/app/models/commit_status.rb @@ -42,7 +42,7 @@ class CommitStatus < ActiveRecord::Base unknown_failure: nil, job_failure: 1, api_failure: 2, - stuck_or_timeout_failure: 3, + stuck_or_timeout_failure: 3 } state_machine :status do diff --git a/changelogs/unreleased/feature-sm-37239-implement-failure_reason-on-ci_builds.yml b/changelogs/unreleased/feature-sm-37239-implement-failure_reason-on-ci_builds.yml new file mode 100644 index 00000000000..006b0b45844 --- /dev/null +++ b/changelogs/unreleased/feature-sm-37239-implement-failure_reason-on-ci_builds.yml @@ -0,0 +1,5 @@ +--- +title: Implement `failure_reason` on `ci_builds` +merge_request: 13937 +author: +type: added diff --git a/lib/api/runner.rb b/lib/api/runner.rb index 701c1bff1e0..44ca42fef68 100644 --- a/lib/api/runner.rb +++ b/lib/api/runner.rb @@ -114,6 +114,8 @@ module API requires :id, type: Integer, desc: %q(Job's ID) optional :trace, type: String, desc: %q(Job's full trace) optional :state, type: String, desc: %q(Job's status: success, failed) + optional :failure_reason, type: String, values: CommitStatus.failure_reasons.keys, + desc: %q(Job's failure_reason) end put '/:id' do job = authenticate_job! @@ -127,7 +129,11 @@ module API when 'success' job.success when 'failed' - job.drop(:job_failure) + if params[:failure_reason] + job.drop(params[:failure_reason].to_sym) + else + job.drop(:job_failure) + end end end diff --git a/spec/lib/gitlab/import_export/safe_model_attributes.yml b/spec/lib/gitlab/import_export/safe_model_attributes.yml index 27f2ce60084..b852ac570a3 100644 --- a/spec/lib/gitlab/import_export/safe_model_attributes.yml +++ b/spec/lib/gitlab/import_export/safe_model_attributes.yml @@ -278,6 +278,7 @@ CommitStatus: - auto_canceled_by_id - retried - protected +- failure_reason Ci::Variable: - id - project_id diff --git a/spec/models/commit_status_spec.rb b/spec/models/commit_status_spec.rb index 4fd330ab7dc..f5c1db79823 100644 --- a/spec/models/commit_status_spec.rb +++ b/spec/models/commit_status_spec.rb @@ -445,22 +445,20 @@ describe CommitStatus do end describe 'set failure_reason when drop' do - let(:build) { create(:ci_build, :created) } + let(:commit_status) { create(:commit_status, :created) } - before do - build.drop!(reason) - end + subject { commit_status.drop!(reason); commit_status } context 'when failure_reason is nil' do let(:reason) { } - it { expect(build).to be_unknown_failure } + it { is_expected.to be_unknown_failure } end context 'when failure_reason is job_failure' do let(:reason) { :job_failure } - it { expect(build).to be_job_failure } + it { is_expected.to be_job_failure } end end end diff --git a/spec/requests/api/commit_statuses_spec.rb b/spec/requests/api/commit_statuses_spec.rb index e4c73583545..ed1962ede42 100644 --- a/spec/requests/api/commit_statuses_spec.rb +++ b/spec/requests/api/commit_statuses_spec.rb @@ -143,7 +143,7 @@ describe API::CommitStatuses do expect(json_response['target_url']).to be_nil expect(json_response['description']).to be_nil if status == 'failed' - expect(CommitStatus.find(json_response['id'])).to be_api_failure + expect(json_response['failure_reason']).to eq('api_failure') end end end diff --git a/spec/requests/api/runner_spec.rb b/spec/requests/api/runner_spec.rb index 386e76c6300..f9dcd9d07cb 100644 --- a/spec/requests/api/runner_spec.rb +++ b/spec/requests/api/runner_spec.rb @@ -636,6 +636,15 @@ describe API::Runner do expect(job.reload.status).to eq 'failed' expect(job).to be_job_failure end + + context 'when failure_reason is given' do + it 'mark job as failed' do + update_job(state: 'failed', failure_reason: 'stuck_or_timeout_failure') + + expect(job.reload.status).to eq 'failed' + expect(job).to be_stuck_or_timeout_failure + end + end end context 'when tace is given' do diff --git a/spec/services/ci/retry_build_service_spec.rb b/spec/services/ci/retry_build_service_spec.rb index 7c9c117bf71..f5ed9ff608f 100644 --- a/spec/services/ci/retry_build_service_spec.rb +++ b/spec/services/ci/retry_build_service_spec.rb @@ -22,7 +22,7 @@ describe Ci::RetryBuildService do %i[type lock_version target_url base_tags commit_id deployments erased_by_id last_deployment project_id runner_id tag_taggings taggings tags trigger_request_id - user_id auto_canceled_by_id retried].freeze + user_id auto_canceled_by_id retried failure_reason].freeze shared_examples 'build duplication' do let(:stage) do -- cgit v1.2.1