Add trigger information for job API

Create new entity called TriggerVariablesEnitity for trigger variables,
to aid reuseablity in the future.

Update JSON schema to include trigger information in the response.

Refactor rspec tests a bit to reduce duplication and for the `context`
to make sense.

closes https://gitlab.com/gitlab-org/gitlab-ce/issues/50989
This commit is contained in:
Steve Azzopardi 2018-09-04 09:51:00 +02:00
parent f87809f78d
commit 1b388c798b
No known key found for this signature in database
GPG key ID: 605BD4706E7DB47D
8 changed files with 99 additions and 2 deletions

View file

@ -40,6 +40,7 @@ module Ci
delegate :url, to: :runner_session, prefix: true, allow_nil: true
delegate :terminal_specification, to: :runner_session, allow_nil: true
delegate :gitlab_deploy_token, to: :project
delegate :trigger_short_token, to: :trigger_request, allow_nil: true
##
# The "environment" field for builds is a String, and is the unexpanded name!

View file

@ -8,6 +8,8 @@ module Ci
belongs_to :pipeline, foreign_key: :commit_id
has_many :builds
delegate :short_token, to: :trigger, prefix: true, allow_nil: true
# We switched to Ci::PipelineVariable from Ci::TriggerRequest.variables.
# Ci::TriggerRequest doesn't save variables anymore.
validates :variables, absence: true

View file

@ -59,6 +59,12 @@ class BuildDetailsEntity < JobEntity
raw_project_job_path(project, build)
end
expose :trigger, if: -> (*) { build.trigger_request } do
expose :trigger_short_token, as: :short_token
expose :trigger_variables, as: :variables, using: TriggerVariableEntity
end
private
def build_failed_issue_options

View file

@ -0,0 +1,7 @@
# frozen_string_literal: true
class TriggerVariableEntity < Grape::Entity
include RequestAwareEntity
expose :key, :value, :public
end

View file

@ -0,0 +1,5 @@
---
title: Add trigger information in job API
merge_request: 21495
author:
type: other

View file

@ -208,6 +208,51 @@ describe Projects::JobsController, :clean_gitlab_redis_shared_state do
end
end
context 'when requesting JSON job is triggered' do
let!(:merge_request) { create(:merge_request, source_project: project) }
let(:trigger) { create(:ci_trigger, project: project) }
let(:trigger_request) { create(:ci_trigger_request, pipeline: pipeline, trigger: trigger) }
let(:job) { create(:ci_build, pipeline: pipeline, trigger_request: trigger_request) }
before do
project.add_developer(user)
sign_in(user)
allow_any_instance_of(Ci::Build).to receive(:merge_request).and_return(merge_request)
end
context 'with no variables' do
before do
get_show(id: job.id, format: :json)
end
it 'exposes trigger information' do
expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema('job/job_details')
expect(json_response['trigger']['short_token']).to eq 'toke'
expect(json_response['trigger']['variables'].length).to eq 0
end
end
context 'with variables' do
before do
create(:ci_pipeline_variable, pipeline: pipeline, key: :TRIGGER_KEY_1, value: 'TRIGGER_VALUE_1')
get_show(id: job.id, format: :json)
end
it 'exposes trigger information and variables' do
expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema('job/job_details')
expect(json_response['trigger']['short_token']).to eq 'toke'
expect(json_response['trigger']['variables'].length).to eq 1
expect(json_response['trigger']['variables'].first['key']).to eq "TRIGGER_KEY_1"
expect(json_response['trigger']['variables'].first['value']).to eq "TRIGGER_VALUE_1"
expect(json_response['trigger']['variables'].first['public']).to eq false
end
end
end
def get_show(**extra_params)
params = {
namespace_id: project.namespace.to_param,

View file

@ -1,8 +1,11 @@
{
"allOf": [{ "$ref": "job.json" }],
"allOf": [
{ "$ref": "job.json" }
],
"description": "An extension of job.json with more detailed information",
"properties": {
"artifact": { "$ref": "artifact.json" },
"terminal_path": { "type": "string" }
"terminal_path": { "type": "string" },
"trigger": { "$ref": "trigger.json" }
}
}

View file

@ -0,0 +1,28 @@
{
"type": "object",
"required": [
"short_token",
"variables"
],
"properties": {
"short_token": { "type": "string" },
"variables": {
"type": "array",
"items": {
"type": "object",
"required": [
"key",
"value",
"public"
],
"properties": {
"key": { "type": "string" },
"value": { "type": "string" },
"public": { "type": "boolean" }
},
"additionalProperties": false
}
}
},
"additionalProperties": false
}