diff --git a/app/models/ci/pipeline_variable.rb b/app/models/ci/pipeline_variable.rb index 017ec0b145a8bb0122f8f4dc2a642f331b097f41..08514d6af4e9ab1c7650674850cb82c56b64ea72 100644 --- a/app/models/ci/pipeline_variable.rb +++ b/app/models/ci/pipeline_variable.rb @@ -10,5 +10,9 @@ module Ci alias_attribute :secret_value, :value validates :key, uniqueness: { scope: :pipeline_id } + + def hook_attrs + { key: key, value: value } + end end end diff --git a/changelogs/unreleased/pipeline-event-variables.yml b/changelogs/unreleased/pipeline-event-variables.yml new file mode 100644 index 0000000000000000000000000000000000000000..90fd964efd59ec21bc1e6bbf457f5427ab4ee4fa --- /dev/null +++ b/changelogs/unreleased/pipeline-event-variables.yml @@ -0,0 +1,5 @@ +--- +title: pipeline webhook event now contain pipeline variables +merge_request: 18171 +author: Pierre Tardy +type: added diff --git a/doc/user/project/integrations/webhooks.md b/doc/user/project/integrations/webhooks.md index 5a38f5d8aed513bce4f13e6b9e991d49f908d784..7d12cd8f7c2da9f8dcd1279473197822b8a3bd1e 100644 --- a/doc/user/project/integrations/webhooks.md +++ b/doc/user/project/integrations/webhooks.md @@ -943,7 +943,13 @@ X-Gitlab-Event: Pipeline Hook ], "created_at": "2016-08-12 15:23:28 UTC", "finished_at": "2016-08-12 15:26:29 UTC", - "duration": 63 + "duration": 63, + "variables": [ + { + "key": "NESTOR_PROD_ENVIRONMENT", + "value": "us-west-1" + } + ] }, "user":{ "name": "Administrator", diff --git a/lib/gitlab/data_builder/pipeline.rb b/lib/gitlab/data_builder/pipeline.rb index eb246d393a1e4e99ad1ee432f8a36625b0b056fb..f382992cb0a5ccda245ab7495b2e6e1a452af5d8 100644 --- a/lib/gitlab/data_builder/pipeline.rb +++ b/lib/gitlab/data_builder/pipeline.rb @@ -26,7 +26,8 @@ module Gitlab stages: pipeline.stages_names, created_at: pipeline.created_at, finished_at: pipeline.finished_at, - duration: pipeline.duration + duration: pipeline.duration, + variables: pipeline.variables.map(&:hook_attrs) } end diff --git a/spec/lib/gitlab/data_builder/pipeline_spec.rb b/spec/lib/gitlab/data_builder/pipeline_spec.rb index 9ca960502c8e6b454a5dd1feb8627cb16502f26b..98f1696badb31bcb126f1b030ba72bdf8c4d40ed 100644 --- a/spec/lib/gitlab/data_builder/pipeline_spec.rb +++ b/spec/lib/gitlab/data_builder/pipeline_spec.rb @@ -6,10 +6,10 @@ describe Gitlab::DataBuilder::Pipeline do let(:pipeline) do create(:ci_pipeline, - project: project, - status: 'success', - sha: project.commit.sha, - ref: project.default_branch) + project: project, + status: 'success', + sha: project.commit.sha, + ref: project.default_branch) end let!(:build) { create(:ci_build, pipeline: pipeline) } @@ -20,18 +20,35 @@ describe Gitlab::DataBuilder::Pipeline do let(:build_data) { data[:builds].first } let(:project_data) { data[:project] } - it { expect(attributes).to be_a(Hash) } - it { expect(attributes[:ref]).to eq(pipeline.ref) } - it { expect(attributes[:sha]).to eq(pipeline.sha) } - it { expect(attributes[:tag]).to eq(pipeline.tag) } - it { expect(attributes[:id]).to eq(pipeline.id) } - it { expect(attributes[:status]).to eq(pipeline.status) } - it { expect(attributes[:detailed_status]).to eq('passed') } + it 'has correct attributes' do + expect(attributes).to be_a(Hash) + expect(attributes[:ref]).to eq(pipeline.ref) + expect(attributes[:sha]).to eq(pipeline.sha) + expect(attributes[:tag]).to eq(pipeline.tag) + expect(attributes[:id]).to eq(pipeline.id) + expect(attributes[:status]).to eq(pipeline.status) + expect(attributes[:detailed_status]).to eq('passed') + expect(build_data).to be_a(Hash) + expect(build_data[:id]).to eq(build.id) + expect(build_data[:status]).to eq(build.status) + expect(project_data).to eq(project.hook_attrs(backward: false)) + end - it { expect(build_data).to be_a(Hash) } - it { expect(build_data[:id]).to eq(build.id) } - it { expect(build_data[:status]).to eq(build.status) } + context 'pipeline without variables' do + it 'has empty variables hash' do + expect(attributes[:variables]).to be_a(Array) + expect(attributes[:variables]).to be_empty() + end + end - it { expect(project_data).to eq(project.hook_attrs(backward: false)) } + context 'pipeline with variables' do + let(:build) { create(:ci_build, pipeline: pipeline) } + let(:data) { described_class.build(pipeline) } + let(:attributes) { data[:object_attributes] } + let!(:pipeline_variable) { create(:ci_pipeline_variable, pipeline: pipeline, key: 'TRIGGER_KEY_1', value: 'TRIGGER_VALUE_1') } + + it { expect(attributes[:variables]).to be_a(Array) } + it { expect(attributes[:variables]).to contain_exactly({ key: 'TRIGGER_KEY_1', value: 'TRIGGER_VALUE_1' }) } + end end end diff --git a/spec/models/ci/pipeline_variable_spec.rb b/spec/models/ci/pipeline_variable_spec.rb index 889c243c8d8fda86aebba3be4cab9f9c3acda705..03d09cb31d61f9ba01a577dda75b50b0fee409cc 100644 --- a/spec/models/ci/pipeline_variable_spec.rb +++ b/spec/models/ci/pipeline_variable_spec.rb @@ -5,4 +5,13 @@ describe Ci::PipelineVariable do it { is_expected.to include_module(HasVariable) } it { is_expected.to validate_uniqueness_of(:key).scoped_to(:pipeline_id) } + + describe '#hook_attrs' do + let(:variable) { create(:ci_pipeline_variable, key: 'foo', value: 'bar') } + + subject { variable.hook_attrs } + + it { is_expected.to be_a(Hash) } + it { is_expected.to eq({ key: 'foo', value: 'bar' }) } + end end