diff options
-rw-r--r-- | app/helpers/ci_status_helper.rb | 5 | ||||
-rw-r--r-- | app/serializers/build_details_entity.rb | 18 | ||||
-rw-r--r-- | app/views/projects/jobs/show.html.haml | 2 | ||||
-rw-r--r-- | changelogs/unreleased/51273-expose-runners-for-build-in-job-api.yml | 5 | ||||
-rw-r--r-- | spec/controllers/projects/jobs_controller_spec.rb | 49 | ||||
-rw-r--r-- | spec/fixtures/api/schemas/job/job_details.json | 3 | ||||
-rw-r--r-- | spec/fixtures/api/schemas/job/runners.json | 13 |
7 files changed, 88 insertions, 7 deletions
diff --git a/app/helpers/ci_status_helper.rb b/app/helpers/ci_status_helper.rb index f8d36dce45d..136772e1ec3 100644 --- a/app/helpers/ci_status_helper.rb +++ b/app/helpers/ci_status_helper.rb @@ -123,11 +123,6 @@ module CiStatusHelper render_status_with_link('pipeline', pipeline.status, path, tooltip_placement: tooltip_placement) end - def no_runners_for_project?(project) - project.runners.blank? && - Ci::Runner.instance_type.blank? - end - def render_status_with_link(type, status, path = nil, tooltip_placement: 'left', cssclass: '', container: 'body', icon_size: 16) klass = "ci-status-link ci-status-icon-#{status.dasherize} #{cssclass}" title = "#{type.titleize}: #{ci_label_for_status(status)}" diff --git a/app/serializers/build_details_entity.rb b/app/serializers/build_details_entity.rb index ab9861c58c4..00a441a9a1e 100644 --- a/app/serializers/build_details_entity.rb +++ b/app/serializers/build_details_entity.rb @@ -79,6 +79,20 @@ class BuildDetailsEntity < JobEntity expose :trigger_variables, as: :variables, using: TriggerVariableEntity end + expose :runners do + expose :online do |build| + build.any_runners_online? + end + + expose :available do |build| + project.any_runners? + end + + expose :settings_path, if: -> (*) { can_admin_build? } do |build| + project_runners_path(project) + end + end + private def build_failed_issue_options @@ -97,4 +111,8 @@ class BuildDetailsEntity < JobEntity def can_create_build_terminal? can?(current_user, :create_build_terminal, build) && build.has_terminal? end + + def can_admin_build? + can?(request.current_user, :admin_build, project) + end end diff --git a/app/views/projects/jobs/show.html.haml b/app/views/projects/jobs/show.html.haml index 078f40c4477..cf8d42976f8 100644 --- a/app/views/projects/jobs/show.html.haml +++ b/app/views/projects/jobs/show.html.haml @@ -10,7 +10,7 @@ - unless @build.any_runners_online? .bs-callout.bs-callout-warning.js-build-stuck %p - - if no_runners_for_project?(@build.project) + - if @project.any_runners? This job is stuck, because the project doesn't have any runners online assigned to it. - elsif @build.tags.any? This job is stuck, because you don't have any active runners online with any of these tags assigned to them: diff --git a/changelogs/unreleased/51273-expose-runners-for-build-in-job-api.yml b/changelogs/unreleased/51273-expose-runners-for-build-in-job-api.yml new file mode 100644 index 00000000000..df43f1dfbae --- /dev/null +++ b/changelogs/unreleased/51273-expose-runners-for-build-in-job-api.yml @@ -0,0 +1,5 @@ +--- +title: Expose project runners in job API +merge_request: 21618 +author: +type: other diff --git a/spec/controllers/projects/jobs_controller_spec.rb b/spec/controllers/projects/jobs_controller_spec.rb index 19c46e2f309..c82c85970dc 100644 --- a/spec/controllers/projects/jobs_controller_spec.rb +++ b/spec/controllers/projects/jobs_controller_spec.rb @@ -288,6 +288,55 @@ describe Projects::JobsController, :clean_gitlab_redis_shared_state do end end end + + context 'when no runners are available' do + let(:runner) { create(:ci_runner, :instance, active: false) } + let(:job) { create(:ci_build, :pending, pipeline: pipeline, runner: runner) } + + it 'exposes needed information' do + expect(response).to have_gitlab_http_status(:ok) + expect(response).to match_response_schema('job/job_details') + expect(json_response['runners']['online']).to be false + expect(json_response['runners']['available']).to be false + end + end + + context 'when no runner is online' do + let(:runner) { create(:ci_runner, :instance) } + let(:job) { create(:ci_build, :pending, pipeline: pipeline, runner: runner) } + + it 'exposes needed information' do + expect(response).to have_gitlab_http_status(:ok) + expect(response).to match_response_schema('job/job_details') + expect(json_response['runners']['online']).to be false + expect(json_response['runners']['available']).to be true + end + end + + context 'settings_path' do + context 'when user is developer' do + it 'settings_path is not available' do + expect(response).to have_gitlab_http_status(:ok) + expect(response).to match_response_schema('job/job_details') + expect(json_response['runners']).not_to have_key('settings_path') + end + end + + context 'when user is maintainer' do + let(:user) { create(:user, :admin) } + + before do + project.add_maintainer(user) + sign_in(user) + end + + it 'settings_path is available' do + expect(response).to have_gitlab_http_status(:ok) + expect(response).to match_response_schema('job/job_details') + expect(json_response['runners']['settings_path']).to match(/runners/) + end + end + end end context 'when requesting JSON job is triggered' do diff --git a/spec/fixtures/api/schemas/job/job_details.json b/spec/fixtures/api/schemas/job/job_details.json index 78c0fdf83b9..cd67d3e4160 100644 --- a/spec/fixtures/api/schemas/job/job_details.json +++ b/spec/fixtures/api/schemas/job/job_details.json @@ -8,6 +8,7 @@ "terminal_path": { "type": "string" }, "trigger": { "$ref": "trigger.json" }, "deployment_status": { "$ref": "deployment_status.json" }, - "runner": { "$ref": "runner.json" } + "runner": { "$ref": "runner.json" }, + "runners": { "type": "runners.json" } } } diff --git a/spec/fixtures/api/schemas/job/runners.json b/spec/fixtures/api/schemas/job/runners.json new file mode 100644 index 00000000000..bebb0c88652 --- /dev/null +++ b/spec/fixtures/api/schemas/job/runners.json @@ -0,0 +1,13 @@ +{ + "type": "object", + "required": [ + "online", + "available" + ], + "properties": { + "online": { "type": "boolean" }, + "available": { "type": "boolean" }, + "settings_path": { "type": "string" } + }, + "additionalProperties": false +} |