diff --git a/lib/ci/gitlab_ci_yaml_processor.rb b/lib/ci/gitlab_ci_yaml_processor.rb index b06474cda7fe308fbfc0dd7c2f4e4a7724774967..17a3cdc714c72d687328b2c4928a194e022551c1 100644 --- a/lib/ci/gitlab_ci_yaml_processor.rb +++ b/lib/ci/gitlab_ci_yaml_processor.rb @@ -50,10 +50,20 @@ module Ci end end + def stages_for_ref(ref, tag = false, trigger_request = nil) + stages = @stages.map do |stage| + builds = builds_for_stage_and_ref(stage, ref, tag, trigger_request) + + { name: stage, builds_attributes: builds.to_a } if builds.any? + end + + stages.compact.sort_by { |stage| @stages.index(stage[:name]) } + end + def build_attributes(name) job = @jobs[name.to_sym] || {} - { - stage_idx: @stages.index(job[:stage]), + + { stage_idx: @stages.index(job[:stage]), stage: job[:stage], commands: job[:commands], tag_list: job[:tags] || [], diff --git a/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb b/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb index fe2c00bb2cad46545e481a7b2fbf331c0e40fb09..f98da1916b46bcc53ff7e581ecdf5f3a316fe7e9 100644 --- a/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb +++ b/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb @@ -1,7 +1,8 @@ require 'spec_helper' module Ci - describe GitlabCiYamlProcessor, lib: true do + describe GitlabCiYamlProcessor, :lib do + subject { described_class.new(config, path) } let(:path) { 'path' } describe 'our current .gitlab-ci.yml' do @@ -82,6 +83,44 @@ module Ci end end + describe '#stages_for_ref' do + context 'when no refs policy is specified' do + let(:config) do + YAML.dump(production: { stage: 'deploy', script: 'cap prod' }, + rspec: { stage: 'test', script: 'rspec' }, + spinach: { stage: 'test', script: 'spinach' }) + end + + it 'returns model attributes for stages with nested jobs' do + attributes = subject.stages_for_ref('master') + + expect(attributes.size).to eq 2 + expect(attributes.dig(0, :name)).to eq 'test' + expect(attributes.dig(1, :name)).to eq 'deploy' + expect(attributes.dig(0, :builds_attributes, 0, :name)).to eq 'rspec' + expect(attributes.dig(0, :builds_attributes, 1, :name)).to eq 'spinach' + expect(attributes.dig(1, :builds_attributes, 0, :name)).to eq 'production' + end + end + + context 'when refs policy is specified' do + let(:config) do + YAML.dump(production: { stage: 'deploy', script: 'cap prod', only: ['master'] }, + spinach: { stage: 'test', script: 'spinach', only: ['tags'] }) + end + + it 'returns stage attributes except of jobs assigned to master' do + # true flag argument means matching jobs for tags + # + attributes = subject.stages_for_ref('feature', true) + + expect(attributes.size).to eq 1 + expect(attributes.dig(0, :name)).to eq 'test' + expect(attributes.dig(0, :builds_attributes, 0, :name)).to eq 'spinach' + end + end + end + describe "#builds_for_ref" do let(:type) { 'test' } diff --git a/spec/services/ci/create_pipeline_service_spec.rb b/spec/services/ci/create_pipeline_service_spec.rb index b536103ed65ac0b2df236720677761f5d71804fa..674de2d80c11a2aed734548dca7fb2f2f9f6d2b2 100644 --- a/spec/services/ci/create_pipeline_service_spec.rb +++ b/spec/services/ci/create_pipeline_service_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Ci::CreatePipelineService, services: true do +describe Ci::CreatePipelineService, :services do let(:project) { create(:project, :repository) } let(:user) { create(:admin) }