forked from pact-foundation/pact_broker-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(create-or-update-version): support creating or updating a pactic…
…ipant version with a branch and/or tags
- Loading branch information
Showing
8 changed files
with
384 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
module PactBroker | ||
module Client | ||
module CLI | ||
module VersionCommands | ||
def self.included(thor) | ||
thor.class_eval do | ||
desc "create-or-update-version", "Create or update pacticipant version by version number" | ||
method_option :pacticipant, required: true, aliases: "-a", desc: "The pacticipant name" | ||
method_option :version, required: true, aliases: "-e", desc: "The pacticipant version number" | ||
method_option :branch, required: false, desc: "The repository branch name" | ||
method_option :tag, aliases: "-t", type: :array, banner: "TAG", desc: "Tag name for pacticipant version. Can be specified multiple times." | ||
shared_authentication_options | ||
output_option_json_or_text | ||
verbose_option | ||
|
||
def create_or_update_version(*required_but_ignored) | ||
validate_create_version_params | ||
|
||
params = { | ||
pacticipant_name: options.pacticipant.strip, | ||
version_number: options.version.strip, | ||
branch_name: options.branch&.strip, | ||
tags: options.tag&.collect(&:strip) | ||
} | ||
|
||
execute_version_command(params, "Create") | ||
end | ||
|
||
|
||
no_commands do | ||
def execute_version_command(params, command_class_name) | ||
require "pact_broker/client/versions" | ||
command_options = { verbose: options.verbose, output: options.output } | ||
result = PactBroker::Client::Versions.const_get(command_class_name).call(params, command_options, pact_broker_client_options) | ||
$stdout.puts result.message | ||
exit(1) unless result.success | ||
end | ||
|
||
def validate_create_version_params | ||
raise ::Thor::RequiredArgumentMissingError, "Pacticipant name cannot be blank" if options.pacticipant.strip.size == 0 | ||
raise ::Thor::RequiredArgumentMissingError, "Pacticipant version cannot be blank" if options.version.strip.size == 0 | ||
raise ::Thor::RequiredArgumentMissingError, "Version branch cannot be blank" if options.branch && options.branch.strip.size == 0 | ||
raise ::Thor::RequiredArgumentMissingError, "Version tag cannot be blank" if options.tag && options.tag.any?{ | tag | tag.strip.size == 0 } | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
require "pact_broker/client/base_command" | ||
|
||
module PactBroker | ||
module Client | ||
class Versions | ||
class Create < PactBroker::Client::BaseCommand | ||
|
||
def do_call | ||
if branch_name | ||
if branch_versions_supported? | ||
create_branch_version | ||
else | ||
raise PactBroker::Client::Error.new("This version of #{pact_broker_name} does not support branch versions,or you do not have the required permissions to create one. Please ensure you have upgraded to version 2.82.0 or later for branch support.") | ||
end | ||
end | ||
|
||
if tags | ||
create_version_tags | ||
end | ||
|
||
if !branch_name && !tags.any? | ||
create_version | ||
end | ||
|
||
PactBroker::Client::CommandResult.new(true, result_message) | ||
end | ||
|
||
private | ||
|
||
def pacticipant_name | ||
params.fetch(:pacticipant_name) | ||
end | ||
|
||
def version_number | ||
params.fetch(:version_number) | ||
end | ||
|
||
def branch_name | ||
params[:branch_name] | ||
end | ||
|
||
def tags | ||
params[:tags] || [] | ||
end | ||
|
||
def branch_versions_supported? | ||
index_resource._link("pb:pacticipant-branch-version") | ||
end | ||
|
||
def create_branch_version | ||
branch_params = { | ||
"pacticipant" => pacticipant_name, | ||
"version" => version_number, | ||
"branch" => branch_name | ||
} | ||
branch_version_entity = index_resource | ||
._link("pb:pacticipant-branch-version") | ||
.expand(branch_params) | ||
.put! | ||
end | ||
|
||
def create_version_tags | ||
tags.each do | tag | | ||
tag_params = { | ||
"pacticipant" => pacticipant_name, | ||
"version" => version_number, | ||
"tag" => tag | ||
} | ||
index_resource | ||
._link("pb:pacticipant-version-tag") | ||
.expand(tag_params) | ||
.put! | ||
end | ||
end | ||
|
||
def create_version | ||
@version_resource ||= expanded_version_relation.put! | ||
end | ||
|
||
def expanded_version_relation | ||
version_params = { | ||
"pacticipant" => pacticipant_name, | ||
"version" => version_number | ||
} | ||
index_resource | ||
._link("pb:pacticipant-version") | ||
.expand(version_params) | ||
end | ||
|
||
def result_message | ||
if json_output? | ||
(@version_resource || expanded_version_relation.get).response.raw_body | ||
else | ||
message = "Created/updated pacticipant version #{version_number}" | ||
if branch_name && tags.any? | ||
message = message + " with branch #{branch_name} and tag(s) #{tags.join(", ")}" | ||
elsif branch_name | ||
message = message + " with branch #{branch_name}" | ||
elsif tags.any? | ||
message = message + " with tag(s) #{tags.join(", ")}" | ||
end | ||
|
||
message = message + " in #{pact_broker_name}" | ||
green(message) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.