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

Skip to content
Closed
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
16 changes: 14 additions & 2 deletions lib/gitlab/client/pipelines.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,21 @@ def pipeline(project, id)
#
# @param [Integer, String] project The ID or name of a project.
# @param [String] ref Reference to commit.
# @param [Hash] variables A variables pased to pipeline.
# @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 = {})
normalized_variables = variables.each_with_object([]) do |(key, value), res|
res.push(
'key' => key,
'value' => value
)
end
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let variables be a string. We don't need this conversion logic in API wrapper.


post(
"/projects/#{url_encode project}/pipeline",
query: { ref: ref },
body: JSON.dump(normalized_variables)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've played around with this change and noticed that it does not work as expected. The problem is, that the API is missing the name of the parameter (variables=... see https://docs.gitlab.com/ee/api/pipelines.html#create-a-new-pipeline). Furthermore this gem sets the Content-Type header to x-www-form-urlencoded so the server does not understand the json structure which is sent in the body of the request.

The following code fixes these issues:

def create_pipeline(project, ref, variables = {})
  # This is necessary, cause the API expects an array with objects (with `key` and `value` keys) <_< 
  normalized_variables = variables.each_with_object([]) do |(key, value), res|
    res.push(key: key, value: value)
  end

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

@NARKOZ do you prefer that this MR gets updated, or should I open another MR with this fix?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@littldr please open a new one.

)
end

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

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

before do
stub_post('/projects/3/pipeline?ref=master', 'pipeline_create')
@pipeline_create = Gitlab.create_pipeline(3, 'master')
stub_post(pipeline_path, 'pipeline_create')
variables = { 'VAR1' => ':-D', 'VAR2' => ':-(' }
@pipeline_create = Gitlab.create_pipeline(3, 'master', variables)
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 'get correct params in request' do
expected_parameters = JSON.dump([
{ 'key' => 'VAR1', 'value' => ':-D' },
{ 'key' => 'VAR2', 'value' => ':-(' }
])
expect(a_post(pipeline_path).with(body: expected_parameters)).to have_been_made
end

it 'returns a single pipeline' do
Expand Down