Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/gitlab/client/pipelines.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,20 @@ def pipeline(project, id)
#
# @param [Integer, String] project The ID or name of a project.
# @param [String] ref Reference to commit.
# @param [Hash] variables Variables passed to pipelines
# @return [Gitlab::ObjectifiedHash] The pipelines changes.
def create_pipeline(project, ref)
post("/projects/#{url_encode project}/pipeline?ref=#{ref}")
def create_pipeline(project, ref, variables = {})
body = {}

# This mapping is necessary, cause the API expects an array with objects (with `key` and `value` keys)
# See: https://docs.gitlab.com/ee/api/pipelines.html#create-a-new-pipeline
body[:variables] = variables.map { |(key, value)| { key: key, value: value } } if variables.any?

post(
"/projects/#{url_encode project}/pipeline",
query: { ref: ref },
body: body
)
end

# Cancels a pipeline.
Expand Down
19 changes: 17 additions & 2 deletions spec/gitlab/client/pipelines_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@
end

describe '.create_pipeline' do
let(:pipeline_path) { '/projects/3/pipeline?ref=master' }

before do
stub_post('/projects/3/pipeline?ref=master', 'pipeline_create')
stub_post(pipeline_path, 'pipeline_create')
@pipeline_create = Gitlab.create_pipeline(3, 'master')
end

it 'gets the correct resource' do
expect(a_post('/projects/3/pipeline?ref=master')).to have_been_made
expect(a_post(pipeline_path)).to have_been_made
end

it 'returns a single pipeline' do
Expand All @@ -55,6 +57,19 @@
it 'returns information about a pipeline' do
expect(@pipeline_create.user.name).to eq('Administrator')
end

context 'when variables are passed' do
before do
stub_post(pipeline_path, 'pipeline_create')
variables = { 'VAR1' => 'value', VAR2: :value }
@pipeline_create = Gitlab.create_pipeline(3, 'master', variables)
end

it 'calls with the correct body' do
expected_body = 'variables[][key]=VAR1&variables[][value]=value&variables[][key]=VAR2&variables[][value]=value'
expect(a_post(pipeline_path).with(body: expected_body)).to have_been_made
end
end
end

describe '.cancel_pipeline' do
Expand Down