Skip to content

Commit b53eab4

Browse files
authored
Merge pull request #75 from puppetlabs/CDPE-4754/v1-create-git-branch
(CDPE-4754) Migrate create_git_branch to V1 endpoint
2 parents 612b8c5 + 8c6bc9c commit b53eab4

4 files changed

Lines changed: 61 additions & 55 deletions

File tree

REFERENCE.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,8 @@ The cd4pe_deployments::create_git_branch function.
154154

155155
Returns: `Hash` contains the results of the function
156156
See [README.md]() for information on the CD4PEFunctionResult hash format
157-
* result [Hash]:
158-
* success [Boolean] whether or not the operation was successful
159-
* error [Hash] contains error information if any
157+
* result [String] "success" on success
158+
* error [Hash] contains error information if any (message + HTTP status code)
160159

161160
##### Examples
162161

lib/puppet/functions/cd4pe_deployments/create_git_branch.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
# create_git_branch("CONTROL_REPO", "feature_carlscoolfeature", "c090ea692e67405c5572af6b2a9dc5f11c9080c0")
1616
# @return [Hash] contains the results of the function
1717
# See [README.md]() for information on the CD4PEFunctionResult hash format
18-
# * result [Hash]:
19-
# * success [Boolean] whether or not the operation was successful
20-
# * error [Hash] contains error information if any
18+
# * result [String] "success" on success
19+
# * error [Hash] contains error information if any (message + HTTP status code)
2120
#
2221
dispatch :create_git_branch do
2322
required_param 'Enum["CONTROL_REPO", "MODULE"]', :repo_type
@@ -31,14 +30,15 @@ def create_git_branch(repo_type, branch_name, commit_sha, cleanup = true)
3130

3231
response = client.create_git_branch(repo_type, branch_name, commit_sha, cleanup)
3332
case response
34-
when Net::HTTPSuccess
35-
response_body = JSON.parse(response.body, symbolize_names: false)
36-
return PuppetX::Puppetlabs::CD4PEFunctionResult.create_result(response_body)
33+
when Net::HTTPNoContent
34+
return PuppetX::Puppetlabs::CD4PEFunctionResult.create_result('success')
3735
when Net::HTTPClientError
38-
response_body = JSON.parse(response.body, symbolize_names: false)
39-
return PuppetX::Puppetlabs::CD4PEFunctionResult.create_error_result(response_body)
36+
body = JSON.parse(response.body, symbolize_names: false)
37+
return PuppetX::Puppetlabs::CD4PEFunctionResult.create_result(
38+
nil, body['message'], response.code,
39+
)
4040
when Net::HTTPServerError
41-
raise Puppet::Error "Unknown HTTP Error with code: #{response.code} and body #{response.body}"
41+
raise Puppet::Error, "Unknown HTTP Error with code: #{response.code} and body #{response.body}"
4242
end
4343
end
4444
end

lib/puppet_x/puppetlabs/cd4pe_client.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,15 @@ def update_git_branch_ref(repo_type, branch_name, commit_sha)
164164
end
165165

166166
def create_git_branch(repo_type, branch_name, commit_sha, cleanup)
167+
path = "#{@api_v1_path}/deployments/#{@config[:deployment_id]}" \
168+
":create-git-branch?workspaceId=#{@config[:deployment_domain]}"
167169
payload = {
168-
op: 'CreateGitBranch',
169-
content: {
170-
deploymentId: @config[:deployment_id],
171-
repoType: repo_type,
172-
branchName: branch_name,
173-
commitSha: commit_sha,
174-
cleanup: cleanup,
175-
},
170+
repoType: repo_type,
171+
branchName: branch_name,
172+
commitSha: commit_sha,
173+
cleanup: cleanup,
176174
}
177-
make_request(:post, @owner_ajax_path, payload.to_json)
175+
make_request(:post, path, payload.to_json)
178176
end
179177

180178
def get_git_branches(repo_type)

spec/functions/cd4pe/create_git_branch_spec.rb

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
require 'webmock/rspec'
44

55
describe 'cd4pe_deployments::create_git_branch' do
6-
let(:ajax_op) { 'CreateGitBranch' }
7-
86
context 'table steaks' do
97
include_context 'deployment'
108

@@ -21,61 +19,72 @@
2119
include_context 'deployment'
2220

2321
let(:repo_type) { 'CONTROL_REPO' }
24-
let(:git_branch) { 'development_b' }
22+
let(:branch_name) { 'development_b' }
2523
let(:commit_sha) { 'c090ea692e67405c5572af6b2a9dc5f11c9080c0' }
26-
let(:response) do
27-
{
28-
'result' => {
29-
'success' => true,
30-
},
31-
'error' => nil,
32-
}
24+
let(:full_path) do
25+
"#{api_v1_path}/deployments/#{deployment_id}:create-git-branch?workspaceId=#{deployment_domain}"
3326
end
3427

35-
it 'succeeds with parameters' do
36-
stub_request(:post, ajax_url)
28+
it 'returns success on 204' do
29+
stub_request(:post, full_path)
3730
.with(
3831
body: {
39-
op: ajax_op,
40-
content: {
41-
repoType: repo_type,
42-
deploymentId: deployment_id,
43-
branchName: git_branch,
44-
commitSha: commit_sha,
45-
cleanup: true,
46-
},
32+
repoType: repo_type,
33+
branchName: branch_name,
34+
commitSha: commit_sha,
35+
cleanup: true,
4736
},
4837
headers: {
4938
'authorization' => ENV['DEPLOYMENT_TOKEN'],
5039
},
5140
)
52-
.to_return(body: JSON.generate(response['result']))
41+
.to_return(status: 204, body: '')
5342
.times(1)
5443

55-
is_expected.to run.with_params(repo_type, git_branch, commit_sha).and_return(response)
44+
is_expected
45+
.to run
46+
.with_params(repo_type, branch_name, commit_sha)
47+
.and_return({'result' => 'success', 'error' => nil})
5648
end
5749

58-
it 'fails with non-200 response code' do
59-
stub_request(:post, ajax_url)
50+
it 'returns error result on 4xx with a V1 error body' do
51+
v1_error_body = {
52+
'message' => 'Some error message',
53+
'traceId' => 'abc',
54+
'uriPath' => full_path,
55+
}
56+
stub_request(:post, full_path)
6057
.with(
6158
body: {
62-
op: ajax_op,
63-
content: {
64-
repoType: repo_type,
65-
deploymentId: deployment_id,
66-
branchName: git_branch,
67-
commitSha: commit_sha,
68-
cleanup: true,
69-
},
59+
repoType: repo_type,
60+
branchName: branch_name,
61+
commitSha: commit_sha,
62+
cleanup: true,
7063
},
7164
headers: {
7265
'authorization' => ENV['DEPLOYMENT_TOKEN'],
7366
},
7467
)
75-
.to_return(body: JSON.generate(error_response), status: 404)
68+
.to_return(status: 400, body: JSON.generate(v1_error_body))
69+
.times(1)
70+
71+
is_expected
72+
.to run
73+
.with_params(repo_type, branch_name, commit_sha)
74+
.and_return(
75+
{'result' => nil, 'error' => {'message' => 'Some error message', 'code' => '400'}},
76+
)
77+
end
78+
79+
it 'raises on 5xx' do
80+
stub_request(:post, full_path)
81+
.to_return(status: 500, body: 'boom')
7682
.times(1)
7783

78-
is_expected.to run.with_params(repo_type, git_branch, commit_sha).and_return(error_response)
84+
is_expected
85+
.to run
86+
.with_params(repo_type, branch_name, commit_sha)
87+
.and_raise_error(Puppet::Error)
7988
end
8089
end
8190
end

0 commit comments

Comments
 (0)