diff --git a/README.md b/README.md index 3a2e381..92dd54f 100644 --- a/README.md +++ b/README.md @@ -59,18 +59,18 @@ In the next major version, an error will be raised by default. ``` Usage: - pact-broker publish PACT_DIRS_OR_FILES ... -a, --consumer-app-version=CONSUMER_APP_VERSION -b, --broker-base-url=BROKER_BASE_URL + pact-broker publish PACT_DIRS_OR_FILES ... -b, --broker-base-url=BROKER_BASE_URL Options: - -a, --consumer-app-version=CONSUMER_APP_VERSION + -a, [--consumer-app-version=CONSUMER_APP_VERSION] # The consumer application version -h, [--branch=BRANCH] # Repository branch of the consumer version - [--auto-detect-version-properties], [--no-auto-detect-version-properties] - # Automatically detect the repository branch from known CI - environment variables or git CLI. Supports Buildkite, Circle - CI, Travis CI, GitHub Actions, Jenkins, Hudson, AppVeyor, - GitLab, CodeShip, Bitbucket and Azure DevOps. + -r, [--auto-detect-version-properties], [--no-auto-detect-version-properties] + # Automatically detect the repository commit, branch and build + URL from known CI environment variables or git CLI. Supports + Buildkite, Circle CI, Travis CI, GitHub Actions, Jenkins, + Hudson, AppVeyor, GitLab, CodeShip, Bitbucket and Azure DevOps. -t, [--tag=TAG] # Tag name for consumer version. Can be specified multiple times. diff --git a/example/scripts/publish-pact-rake.sh b/example/scripts/publish-pact-rake.sh new file mode 100755 index 0000000..f4b4a3d --- /dev/null +++ b/example/scripts/publish-pact-rake.sh @@ -0,0 +1,10 @@ +#!/bin/bash +# assumes you've set PACT_BROKER_BASE_URL, PACT_BROKER_TOKEN already + +bundle exec rake pact:publish:pactflow_auto_on_nil_commit_nil_branch +bundle exec rake pact:publish:pactflow_auto_on_user_commit_user_branch +bundle exec rake pact:publish:pactflow_auto_on_user_commit_nil_branch +bundle exec rake pact:publish:pactflow_auto_on_nil_commit_user_branch +bundle exec rake pact:publish:pactflow_auto_off_user_commit_nil_branch +bundle exec rake pact:publish:pactflow_auto_off_nil_commit_nil_branch +bundle exec rake pact:publish:pactflow_auto_off_empty_string_commit_nil_branch diff --git a/example/scripts/publish-pact.sh b/example/scripts/publish-pact.sh index 7811e41..7b20c07 100755 --- a/example/scripts/publish-pact.sh +++ b/example/scripts/publish-pact.sh @@ -1,3 +1,27 @@ # assumes you've set PACT_BROKER_BASE_URL, PACT_BROKER_USERNAME and PACT_BROKER_PASSWORD already +# Must be executed from root directory of project. -bundle exec bin/pact-broker publish $(dirname "$0")/pact.json --consumer-app-version=1.0.0 --tag master --verbose +bundle exec bin/pact-broker publish $(dirname "$0")/pact.json +bundle exec bin/pact-broker publish $(dirname "$0")/pact.json --auto-detect-version-properties +bundle exec bin/pact-broker publish $(dirname "$0")/pact.json --auto-detect-version-properties --tag-with-git-branch +bundle exec bin/pact-broker publish $(dirname "$0")/pact.json --branch branch-user-override +bundle exec bin/pact-broker publish $(dirname "$0")/pact.json --branch branch-user-override --auto-detect-version-properties +bundle exec bin/pact-broker publish $(dirname "$0")/pact.json --branch branch-user-override --auto-detect-version-properties --tag-with-git-branch +bundle exec bin/pact-broker publish $(dirname "$0")/pact.json --consumer-app-version commit-user-override +bundle exec bin/pact-broker publish $(dirname "$0")/pact.json --consumer-app-version commit-user-override --branch branch-user-override +bundle exec bin/pact-broker publish $(dirname "$0")/pact.json --consumer-app-version commit-user-override --auto-detect-version-properties +bundle exec bin/pact-broker publish $(dirname "$0")/pact.json --consumer-app-version commit-user-override --auto-detect-version-properties --tag-with-git-branch +bundle exec bin/pact-broker publish $(dirname "$0")/pact.json --consumer-app-version commit-user-override --branch branch-user-override --auto-detect-version-properties +bundle exec bin/pact-broker publish $(dirname "$0")/pact.json --consumer-app-version commit-user-override --branch branch-user-override --auto-detect-version-properties --tag-with-git-branch + +bundle exec bin/pact-broker publish $(dirname "$0")/pact.json -r +bundle exec bin/pact-broker publish $(dirname "$0")/pact.json -r -g +bundle exec bin/pact-broker publish $(dirname "$0")/pact.json -h branch-user-override +bundle exec bin/pact-broker publish $(dirname "$0")/pact.json -h branch-user-override -r +bundle exec bin/pact-broker publish $(dirname "$0")/pact.json -h branch-user-override -r -g +bundle exec bin/pact-broker publish $(dirname "$0")/pact.json -a commit-user-override +bundle exec bin/pact-broker publish $(dirname "$0")/pact.json -a commit-user-override -r +bundle exec bin/pact-broker publish $(dirname "$0")/pact.json -a commit-user-override -r -g +bundle exec bin/pact-broker publish $(dirname "$0")/pact.json -a commit-user-override -h branch-user-override +bundle exec bin/pact-broker publish $(dirname "$0")/pact.json -a commit-user-override -h branch-user-override -r +bundle exec bin/pact-broker publish $(dirname "$0")/pact.json -a commit-user-override -h branch-user-override -r -g diff --git a/example/scripts/publish-provider-contract.sh b/example/scripts/publish-provider-contract.sh index 9fa4b59..222edd3 100755 --- a/example/scripts/publish-provider-contract.sh +++ b/example/scripts/publish-provider-contract.sh @@ -1,4 +1,5 @@ -# assumes you've set PACT_BROKER_BASE_URL, PACT_BROKER_USERNAME and PACT_BROKER_PASSWORD already +# Assumes you've set PACT_BROKER_BASE_URL, PACT_BROKER_USERNAME and PACT_BROKER_PASSWORD already +# Must be executed from root directory of project. bundle exec bin/pactflow publish-provider-contract $(dirname "$0")/oas.yml \ --provider pactflow-cli-test-provider \ diff --git a/lib/pact_broker/client/cli/pact_commands.rb b/lib/pact_broker/client/cli/pact_commands.rb index b89d22f..18d2a34 100644 --- a/lib/pact_broker/client/cli/pact_commands.rb +++ b/lib/pact_broker/client/cli/pact_commands.rb @@ -1,4 +1,5 @@ require "pact_broker/client/hash_refinements" +require 'pact_broker/client/string_refinements' module PactBroker module Client @@ -8,13 +9,14 @@ class PactPublicationError < ::Thor::Error; end module PactCommands using PactBroker::Client::HashRefinements + using PactBroker::Client::StringRefinements def self.included(thor) thor.class_eval do desc 'publish PACT_DIRS_OR_FILES ...', "Publish pacts to a Pact Broker." - method_option :consumer_app_version, required: true, aliases: "-a", desc: "The consumer application version" + method_option :consumer_app_version, aliases: "-a", desc: "The consumer application version" method_option :branch, aliases: "-h", desc: "Repository branch of the consumer version" - method_option :auto_detect_version_properties, type: :boolean, default: false, desc: "Automatically detect the repository branch from known CI environment variables or git CLI. Supports Buildkite, Circle CI, Travis CI, GitHub Actions, Jenkins, Hudson, AppVeyor, GitLab, CodeShip, Bitbucket and Azure DevOps." + method_option :auto_detect_version_properties, aliases: "-r", type: :boolean, default: false, desc: "Automatically detect the repository commit, branch and build URL from known CI environment variables or git CLI. Supports Buildkite, Circle CI, Travis CI, GitHub Actions, Jenkins, Hudson, AppVeyor, GitLab, CodeShip, Bitbucket and Azure DevOps." method_option :tag, aliases: "-t", type: :array, banner: "TAG", desc: "Tag name for consumer version. Can be specified multiple times." method_option :tag_with_git_branch, aliases: "-g", type: :boolean, default: false, required: false, desc: "Tag consumer version with the name of the current git branch. Supports Buildkite, Circle CI, Travis CI, GitHub Actions, Jenkins, Hudson, AppVeyor, GitLab, CodeShip, Bitbucket and Azure DevOps. Default: false" method_option :build_url, desc: "The build URL that created the pact" @@ -23,8 +25,10 @@ def self.included(thor) shared_authentication_options def publish(*pact_files) - require 'pact_broker/client/error' + require "pact_broker/client/error" + require "pact_broker/client/git" validate_credentials + validate_consumer_version validate_pact_files(pact_files) result = publish_pacts(pact_files) $stdout.puts result.message @@ -50,16 +54,21 @@ def validate_pact_files pact_files end end + def validate_consumer_version + if consumer_app_version.blank? + raise ::Thor::RequiredArgumentMissingError, "No value provided for required option --consumer-app-version" + end + end def publish_pacts pact_files require 'pact_broker/client/publish_pacts' write_options = options[:merge] ? { write: :merge } : {} consumer_version_params = { - number: options.consumer_app_version, + number: consumer_app_version, branch: branch, tags: tags, - build_url: options.build_url + build_url: build_url }.compact PactBroker::Client::PublishPacts.call( @@ -98,25 +107,38 @@ def validate_pact_path_list(paths) end def tags - require 'pact_broker/client/git' - t = [*options.tag] t << PactBroker::Client::Git.branch(raise_error: true) if options.tag_with_git_branch t.compact.uniq end def branch - require 'pact_broker/client/git' - if options.branch.nil? && options.auto_detect_version_properties - PactBroker::Client::Git.branch(raise_error: explict_auto_detect_version_properties) + PactBroker::Client::Git.branch(raise_error: true) else options.branch end end - def explict_auto_detect_version_properties - @explict_auto_detect_version_properties ||= ARGV.include?("--auto-detect-version-properties") + def consumer_app_version + if defined?(@consumer_app_version) + @consumer_app_version + else + @consumer_app_version = if options.consumer_app_version.blank? && options.auto_detect_version_properties + PactBroker::Client::Git.commit(raise_error: true) + else + options.consumer_app_version + end + end + + end + + def build_url + if options.build_url.blank? && options.auto_detect_version_properties + PactBroker::Client::Git.build_url + else + options.build_url + end end end end diff --git a/lib/pact_broker/client/git.rb b/lib/pact_broker/client/git.rb index d12efd8..a92490c 100644 --- a/lib/pact_broker/client/git.rb +++ b/lib/pact_broker/client/git.rb @@ -2,7 +2,6 @@ require 'pact_broker/client/hash_refinements' =begin - BUILDKITE_BRANCH BUILDKITE_COMMIT https://buildkite.com/docs/pipelines/environment-variables CIRCLE_BRANCH CIRCLE_SHA1 https://circleci.com/docs/2.0/env-vars/ TRAVIS_COMMIT TRAVIS_BRANCH - TRAVIS_PULL_REQUEST_BRANCH TRAVIS_PULL_REQUEST_SHA https://docs.travis-ci.com/user/environment-variables/ @@ -26,18 +25,21 @@ module Git using PactBroker::Client::HashRefinements COMMAND = 'git rev-parse --abbrev-ref HEAD'.freeze + COMMIT_COMMAND = 'git rev-parse HEAD'.freeze BRANCH_ENV_VAR_NAMES = %w{GITHUB_HEAD_REF GITHUB_REF BUILDKITE_BRANCH CIRCLE_BRANCH TRAVIS_BRANCH GIT_BRANCH GIT_LOCAL_BRANCH APPVEYOR_REPO_BRANCH CI_COMMIT_REF_NAME BITBUCKET_BRANCH BUILD_SOURCEBRANCHNAME CIRRUS_BRANCH}.freeze COMMIT_ENV_VAR_NAMES = %w{GITHUB_SHA BUILDKITE_COMMIT CIRCLE_SHA1 TRAVIS_COMMIT GIT_COMMIT APPVEYOR_REPO_COMMIT CI_COMMIT_ID BITBUCKET_COMMIT BUILD_SOURCEVERSION CIRRUS_CHANGE_IN_REPO} BUILD_URL_ENV_VAR_NAMES = %w{BUILDKITE_BUILD_URL CIRCLE_BUILD_URL TRAVIS_BUILD_WEB_URL BUILD_URL } - def self.commit - find_commit_from_env_vars + def self.commit(options) + find_commit_from_env_vars || commit_from_git_command(options[:raise_error]) end def self.branch(options) find_branch_from_known_env_vars || find_branch_from_env_var_ending_with_branch || branch_from_git_command(options[:raise_error]) end + # This does not belong in the Git module. + # TODO move it. def self.build_url github_build_url || BUILD_URL_ENV_VAR_NAMES.collect{ | name | value_from_env_var(name) }.compact.first end @@ -79,6 +81,10 @@ def self.branch_from_git_command(raise_error) branch_names.size == 1 ? branch_names[0] : nil end + def self.commit_from_git_command(raise_error) + execute_git_commit_command(raise_error) + end + def self.validate_branch_names(branch_names) if branch_names.size == 0 raise PactBroker::Client::Error, "Command `#{COMMAND}` didn't return anything that could be identified as the current branch." @@ -93,6 +99,17 @@ def self.execute_git_command `#{COMMAND}` end + def self.execute_git_commit_command(raise_error) + `#{COMMIT_COMMAND}` + rescue StandardError => e + if raise_error + raise PactBroker::Client::Error, + "Could not determine current git commit using command `#{COMMIT_COMMAND}`. #{e.class} #{e.message}" + else + return nil + end + end + def self.execute_and_parse_command(raise_error) execute_git_command .split("\n") diff --git a/lib/pact_broker/client/tasks/publication_task.rb b/lib/pact_broker/client/tasks/publication_task.rb index 08af69a..16de166 100644 --- a/lib/pact_broker/client/tasks/publication_task.rb +++ b/lib/pact_broker/client/tasks/publication_task.rb @@ -1,6 +1,8 @@ require 'rake/tasklib' require 'pact_broker/client/git' require 'pact_broker/client/hash_refinements' +require 'pact_broker/client/string_refinements' +require "pact_broker/client/error" =begin require pact_broker/client/tasks @@ -19,9 +21,11 @@ module PactBroker module Client class PublicationTask < ::Rake::TaskLib using PactBroker::Client::HashRefinements + using PactBroker::Client::StringRefinements - attr_accessor :pattern, :pact_broker_base_url, :consumer_version, :tag, :write_method, :tag_with_git_branch, :pact_broker_basic_auth, :pact_broker_token - attr_reader :auto_detect_version_properties, :branch, :build_url + attr_accessor :pattern, :pact_broker_base_url, :tag, :build_url, :write_method, :tag_with_git_branch, :pact_broker_basic_auth, :pact_broker_token + attr_reader :auto_detect_version_properties, :build_url + attr_writer :consumer_version, :branch alias_method :tags=, :tag= alias_method :tags, :tag @@ -36,15 +40,7 @@ def initialize name = nil, &block def auto_detect_version_properties= auto_detect_version_properties @auto_detect_version_properties = auto_detect_version_properties end - - def branch= branch - @branch = branch - end - - def build_url= build_url - @build_url = build_url - end - + private def rake_task &block @@ -52,18 +48,25 @@ def rake_task &block desc "Publish pacts to pact broker" task task_name do block.call(self) + validate! require 'pact_broker/client/publish_pacts' pact_broker_client_options = { write: write_method, token: pact_broker_token } pact_broker_client_options[:basic_auth] = pact_broker_basic_auth if pact_broker_basic_auth && pact_broker_basic_auth.any? pact_broker_client_options.compact! - consumer_version_params = { number: consumer_version, branch: the_branch, build_url: build_url, tags: all_tags }.compact + consumer_version_params = { number: consumer_version, branch: branch, build_url: build_url, tags: all_tags }.compact result = PactBroker::Client::PublishPacts.new(pact_broker_base_url, FileList[pattern], consumer_version_params, {}, pact_broker_client_options).call $stdout.puts result.message - raise "One or more pacts failed to be published" unless result.success + raise PactBroker::Client::Error.new("One or more pacts failed to be published") unless result.success end end end + def validate! + if consumer_version.blank? + raise PactBroker::Client::Error.new("A consumer version must be provided") + end + end + def task_name @name ? "publish:#{@name}" : "publish" end @@ -74,11 +77,31 @@ def all_tags t.compact.uniq end - def the_branch - if branch.nil? && auto_detect_version_properties != false - PactBroker::Client::Git.branch(raise_error: auto_detect_version_properties == true) + # Attempt to detect the branch automatically, but don't raise an error if the branch can't be found + # unless the user has explicitly enabled auto_detect_version_properties. + # This approach is an attempt to include the branch without the user having to explicitly + # set it, because people tend to not update things. + def branch + if @branch.nil? && auto_detect_version_properties != false + @branch = PactBroker::Client::Git.branch(raise_error: auto_detect_version_properties == true) + else + @branch + end + end + + def consumer_version + if @consumer_version.nil? && @auto_detect_version_properties + @consumer_version = PactBroker::Client::Git.commit(raise_error: true) + else + @consumer_version + end + end + + def build_url + if @build_url.nil? && @auto_detect_version_properties + @build_url = PactBroker::Client::Git.build_url else - branch + @build_url end end end diff --git a/spec/lib/pact_broker/client/cli/broker_publish_spec.rb b/spec/lib/pact_broker/client/cli/broker_publish_spec.rb index 19e44a4..bd58eb0 100644 --- a/spec/lib/pact_broker/client/cli/broker_publish_spec.rb +++ b/spec/lib/pact_broker/client/cli/broker_publish_spec.rb @@ -4,10 +4,12 @@ module PactBroker::Client::CLI describe Broker do - describe ".broker" do + describe '.broker' do before do allow(PactBroker::Client::PublishPacts).to receive(:call).and_return(result) - allow(PactBroker::Client::Git).to receive(:branch).and_return("bar") + allow(PactBroker::Client::Git).to receive(:branch).and_return('bar') + allow(PactBroker::Client::Git).to receive(:commit).and_return('6.6.6') + allow(PactBroker::Client::Git).to receive(:build_url).and_return('build_url') subject.options = OpenStruct.new(minimum_valid_options) allow($stdout).to receive(:puts) end @@ -159,12 +161,11 @@ module PactBroker::Client::CLI end end - context "with --auto-detect-version-properties specified explicitly" do + context "with --auto-detect-version-properties specified" do before do subject.options = OpenStruct.new( - minimum_valid_options.merge(auto_detect_version_properties: true) + minimum_valid_options.merge(auto_detect_version_properties: true, consumer_app_version: nil) ) - allow(subject).to receive(:explict_auto_detect_version_properties).and_return(true) end it "determines the git branch name" do @@ -183,6 +184,38 @@ module PactBroker::Client::CLI invoke_broker end + it 'determines the commit sha' do + expect(PactBroker::Client::Git).to receive(:commit).with(raise_error: true) + invoke_broker + end + + it 'passes in the auto detected commit sha option' do + expect(PactBroker::Client::PublishPacts).to receive(:call).with( + anything, + anything, + hash_including(number: '6.6.6'), + anything, + anything + ) + invoke_broker + end + + it 'determines the build URL' do + expect(PactBroker::Client::Git).to receive(:build_url) + invoke_broker + end + + it 'passes in the auto detected build URL' do + expect(PactBroker::Client::PublishPacts).to receive(:call).with( + anything, + anything, + hash_including(build_url: 'build_url'), + anything, + anything + ) + invoke_broker + end + context "with the branch specified as well" do before do subject.options = OpenStruct.new( @@ -222,6 +255,16 @@ module PactBroker::Client::CLI end end + context "with no consumer_app_version" do + before do + subject.options.consumer_app_version = nil + end + + it "raises an error" do + expect { invoke_broker }.to raise_error ::Thor::RequiredArgumentMissingError, /--consumer-app-version/ + end + end + context "with basic auth options specified" do before do subject.options = OpenStruct.new( diff --git a/spec/lib/pact_broker/client/tasks/publication_task_spec.rb b/spec/lib/pact_broker/client/tasks/publication_task_spec.rb index 1e947de..5811ed3 100644 --- a/spec/lib/pact_broker/client/tasks/publication_task_spec.rb +++ b/spec/lib/pact_broker/client/tasks/publication_task_spec.rb @@ -5,7 +5,6 @@ module PactBroker::Client describe PublicationTask do - before do @consumer_version = "1.2.3" end @@ -19,6 +18,8 @@ module PactBroker::Client allow(PactBroker::Client::PublishPacts).to receive(:new).and_return(publish_pacts) allow(FileList).to receive(:[]).with(pattern).and_return(pact_file_list) allow(PactBroker::Client::Git).to receive(:branch).and_return('foo') + allow(PactBroker::Client::Git).to receive(:commit).and_return('6.6.6') + allow(PactBroker::Client::Git).to receive(:build_url).and_return("build_url") allow($stdout).to receive(:puts) end @@ -87,81 +88,92 @@ module PactBroker::Client context "when auto_detect_version_properties is explicitly set to true" do before :all do PactBroker::Client::PublicationTask.new(:git_branch_auto_detect_true) do | task | - task.consumer_version = '1.2.3' task.auto_detect_version_properties = true end end - it "gets the git branch name" do + # Don't usually put 3 expects into the one it block, but if I separate them, + # only the first it block passes for some reason that I can't work out. + it "gets the commit, build_url and branch" do + expect(PactBroker::Client::Git).to receive(:commit).with(raise_error: true) + expect(PactBroker::Client::Git).to receive(:build_url) expect(PactBroker::Client::Git).to receive(:branch).with(raise_error: true) Rake::Task['pact:publish:git_branch_auto_detect_true'].execute end - it "invokes PublishPacts with the branch name" do - expect(PactBroker::Client::PublishPacts).to receive(:new).with(anything, anything, hash_including(branch: "foo"), anything, anything).and_return(publish_pacts) + it "invokes PublishPacts with the branch name and build URL" do + expect(PactBroker::Client::PublishPacts).to receive(:new).with(anything, anything, hash_including(number: "6.6.6", branch: "foo", build_url: "build_url"), anything, anything).and_return(publish_pacts) Rake::Task['pact:publish:git_branch_auto_detect_true'].execute end end - context "when auto_detect_version_properties is explicitly set to true and the branch is specified" do + context "when auto_detect_version_properties is explicitly set to true and the auto detectable attributes are specified" do before :all do - PactBroker::Client::PublicationTask.new(:git_branch_auto_detect_true_with_branch) do | task | - task.consumer_version = '1.2.3' + PactBroker::Client::PublicationTask.new(:auto_detect_true_with_attributes_specified) do | task | task.auto_detect_version_properties = true - task.branch = "main" + task.consumer_version = '1.2.3' + task.branch = "feat/foo" + task.consumer_version = "3" + task.build_url = "some_build" end end - it "does not get the branch name" do + it "does not get the commit, branch or build URL from Git" do + expect(PactBroker::Client::Git).to_not receive(:commit) + expect(PactBroker::Client::Git).to_not receive(:build_url) expect(PactBroker::Client::Git).to_not receive(:branch) - Rake::Task['pact:publish:git_branch_auto_detect_true_with_branch'].execute + Rake::Task['pact:publish:auto_detect_true_with_attributes_specified'].execute end - it "invokes PublishPacts with the specified branch name" do - expect(PactBroker::Client::PublishPacts).to receive(:new).with(anything, anything, hash_including(branch: "main"), anything, anything).and_return(publish_pacts) - Rake::Task['pact:publish:git_branch_auto_detect_true_with_branch'].execute + it "invokes PublishPacts with the specified attributes" do + expect(PactBroker::Client::PublishPacts).to receive(:new).with(anything, anything, hash_including(number: "3", branch: "feat/foo", build_url: "some_build"), anything, anything).and_return(publish_pacts) + Rake::Task['pact:publish:auto_detect_true_with_attributes_specified'].execute end end context "when auto_detect_version_properties is explicitly set to false" do before :all do - PactBroker::Client::PublicationTask.new(:git_branch_auto_detect_false) do | task | - task.consumer_version = '1.2.3' + PactBroker::Client::PublicationTask.new(:auto_detect_false) do | task | task.auto_detect_version_properties = false + task.consumer_version = '1.2.3' + task.branch = "feat/foo" + task.consumer_version = "3" + task.build_url = "some_build" end end - it "does not get the git branch name" do + it "does not get the commit, branch or build URL from Git" do + expect(PactBroker::Client::Git).to_not receive(:commit) + expect(PactBroker::Client::Git).to_not receive(:build_url) expect(PactBroker::Client::Git).to_not receive(:branch) - Rake::Task['pact:publish:git_branch_auto_detect_false'].execute + Rake::Task['pact:publish:auto_detect_false'].execute end - it "invokes PublishPacts without the branch name" do - expect(PactBroker::Client::PublishPacts).to receive(:new).with(anything, anything, hash_not_including(branch: "foo"), anything, anything).and_return(publish_pacts) - Rake::Task['pact:publish:git_branch_auto_detect_false'].execute + it "invokes PublishPacts with the specified attributes" do + expect(PactBroker::Client::PublishPacts).to receive(:new).with(anything, anything, hash_including(number: "3", branch: "feat/foo", build_url: "some_build"), anything, anything).and_return(publish_pacts) + Rake::Task['pact:publish:auto_detect_false'].execute end end - context "when auto_detect_version_properties is left as its default" do + context "when auto_detect_version_properties is left as its default and the branch is not specified" do before :all do - PactBroker::Client::PublicationTask.new(:git_branch_auto_detect_default) do | task | + PactBroker::Client::PublicationTask.new(:auto_detect_default) do | task | task.consumer_version = '1.2.3' end end - it "gets the git branch name" do + it "gets the git branch name but won't raise an error if it can't be determined" do expect(PactBroker::Client::Git).to receive(:branch).with(raise_error: false) - Rake::Task['pact:publish:git_branch_auto_detect_default'].execute + Rake::Task['pact:publish:auto_detect_default'].execute end - it "invokes PublishPacts with the branch name" do + it "invokes PublishPacts with the branch name if found" do expect(PactBroker::Client::PublishPacts).to receive(:new).with(anything, anything, hash_including(branch: "foo"),anything, anything).and_return(publish_pacts) - Rake::Task['pact:publish:git_branch_auto_detect_default'].execute + Rake::Task['pact:publish:auto_detect_default'].execute end end describe "custom task" do - before :all do @pact_broker_base_url = "http://some-host" @pattern = "pact/*.json" @@ -193,6 +205,17 @@ module PactBroker::Client end end + describe "with no consumer version" do + before :all do + PactBroker::Client::PublicationTask.new(:no_consumer_version) do + end + end + + it "raises an error" do + expect { Rake::Task['pact:publish:no_consumer_version'].execute }.to raise_error PactBroker::Client::Error, /consumer version/ + end + end + describe "timing of block execution" do before :all do PactBroker::Client::PublicationTask.new(:exception) do | task | diff --git a/tasks/pact.rake b/tasks/pact.rake index 100bdd3..d2b60e7 100644 --- a/tasks/pact.rake +++ b/tasks/pact.rake @@ -59,3 +59,82 @@ PactBroker::Client::PublicationTask.new(:pactflow_pact_foundation) do | task | task.pact_broker_token = ENV["PACT_BROKER_TOKEN_PACT_FOUNDATION"] task.build_url = PactBroker::Client::Git.build_url end +PactBroker::Client::PublicationTask.new(:pactflow_auto_on_nil_commit_nil_branch) do | task | + require 'pact_broker/client/version' + # publish with auto detected commit and branch + task.auto_detect_version_properties = true + task.branch = nil + task.consumer_version = nil + task.pact_broker_base_url = ENV['PACT_BROKER_BASE_URL'] + task.pact_broker_token = ENV['PACT_BROKER_TOKEN'] + task.build_url = PactBroker::Client::Git.build_url +end +PactBroker::Client::PublicationTask.new(:pactflow_auto_on_user_commit_user_branch) do | task | + require 'pact_broker/client/version' + # always accept user provided commit and branch + # even when auto_detect_version_properties enabled + task.auto_detect_version_properties = true + task.branch = 'user-provided-branch' + task.consumer_version = 'user-provided-version' + task.pact_broker_base_url = ENV['PACT_BROKER_BASE_URL'] + task.pact_broker_token = ENV['PACT_BROKER_TOKEN'] + task.build_url = PactBroker::Client::Git.build_url +end + +PactBroker::Client::PublicationTask.new(:pactflow_auto_on_user_commit_nil_branch) do | task | + require 'pact_broker/client/version' + # auto detect branch, always accept user provided commit + # even where set to auto_detect_version_properties + task.auto_detect_version_properties = true + task.branch = nil + task.consumer_version = 'user-provided-version' + task.pact_broker_base_url = ENV['PACT_BROKER_BASE_URL'] + task.pact_broker_token = ENV['PACT_BROKER_TOKEN'] + task.build_url = PactBroker::Client::Git.build_url +end +PactBroker::Client::PublicationTask.new(:pactflow_auto_on_nil_commit_user_branch) do | task | + require 'pact_broker/client/version' + # auto detect commit, always accept user provided branch + # even where set to auto_detect_version_properties + task.auto_detect_version_properties = true + task.branch = 'user-provided-branch' + task.consumer_version = nil + task.pact_broker_base_url = ENV['PACT_BROKER_BASE_URL'] + task.pact_broker_token = ENV['PACT_BROKER_TOKEN'] + task.build_url = PactBroker::Client::Git.build_url +end + +PactBroker::Client::PublicationTask.new(:pactflow_auto_off_user_commit_nil_branch) do | task | + require 'pact_broker/client/version' + # accept publish without branch, but has user provided commit + # auto_detect_version_properties off + task.auto_detect_version_properties = false + task.branch = nil + task.consumer_version = 'user-provided-version' + task.pact_broker_base_url = ENV['PACT_BROKER_BASE_URL'] + task.pact_broker_token = ENV['PACT_BROKER_TOKEN'] + task.build_url = PactBroker::Client::Git.build_url +end + +PactBroker::Client::PublicationTask.new(:pactflow_auto_off_nil_commit_nil_branch) do | task | + require 'pact_broker/client/version' + # reject publish without user provided commit + # auto_detect_version_properties off + task.auto_detect_version_properties = false + task.branch = nil + task.consumer_version = nil + task.pact_broker_base_url = ENV['PACT_BROKER_BASE_URL'] + task.pact_broker_token = ENV['PACT_BROKER_TOKEN'] + task.build_url = PactBroker::Client::Git.build_url +end +PactBroker::Client::PublicationTask.new(:pactflow_auto_off_empty_string_commit_nil_branch) do | task | + require 'pact_broker/client/version' + # reject publish without user provided commit + # auto_detect_version_properties off + task.auto_detect_version_properties = false + task.branch = nil + task.consumer_version = '' + task.pact_broker_base_url = ENV['PACT_BROKER_BASE_URL'] + task.pact_broker_token = ENV['PACT_BROKER_TOKEN'] + task.build_url = PactBroker::Client::Git.build_url +end