Skip to content

Commit

Permalink
feat: auto detect git commit - add shorthand -r flag
Browse files Browse the repository at this point in the history
  • Loading branch information
YOU54F committed Sep 14, 2023
1 parent 3a342de commit 98fd08f
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 21 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ Authentication using a bearer token can be specified using the environment varia

```
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 and 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.
-t, [--tag=TAG]
# Tag name for consumer version. Can be specified multiple
times.
Expand Down
10 changes: 10 additions & 0 deletions example/scripts/publish-pact-rake.sh
Original file line number Diff line number Diff line change
@@ -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
25 changes: 24 additions & 1 deletion example/scripts/publish-pact.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
# assumes you've set PACT_BROKER_BASE_URL, PACT_BROKER_USERNAME and PACT_BROKER_PASSWORD already

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
17 changes: 14 additions & 3 deletions lib/pact_broker/client/cli/pact_commands.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "pact_broker/client/hash_refinements"
require 'pact_broker/client/string_refinements'

module PactBroker
module Client
Expand All @@ -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 and 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 :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"
Expand Down Expand Up @@ -56,7 +58,7 @@ def publish_pacts pact_files

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,
Expand Down Expand Up @@ -116,6 +118,15 @@ def branch
end
end

def consumer_app_version
require 'pact_broker/client/git'
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

def explict_auto_detect_version_properties
@explict_auto_detect_version_properties ||= ARGV.include?("--auto-detect-version-properties")
end
Expand Down
21 changes: 18 additions & 3 deletions lib/pact_broker/client/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand All @@ -26,12 +25,13 @@ 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)
Expand Down Expand Up @@ -79,6 +79,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."
Expand All @@ -93,6 +97,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")
Expand Down
13 changes: 11 additions & 2 deletions lib/pact_broker/client/tasks/publication_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ module Client
class PublicationTask < ::Rake::TaskLib
using PactBroker::Client::HashRefinements

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, :write_method, :tag_with_git_branch, :pact_broker_basic_auth, :pact_broker_token
attr_reader :auto_detect_version_properties, :branch, :consumer_version, :build_url
alias_method :tags=, :tag=
alias_method :tags, :tag

Expand All @@ -44,6 +44,14 @@ def branch= branch
@branch = branch
end

def consumer_version=(consumer_version)
if consumer_version.nil? && @auto_detect_version_properties
@consumer_version = PactBroker::Client::Git.commit(raise_error: true)
else
@consumer_version = consumer_version
end
end

def build_url= build_url
@version_required = version_required || !!build_url
@build_url = build_url
Expand Down Expand Up @@ -87,6 +95,7 @@ def the_branch
branch
end
end

end
end
end
41 changes: 36 additions & 5 deletions spec/lib/pact_broker/client/cli/broker_publish_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

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')
subject.options = OpenStruct.new(minimum_valid_options)
allow($stdout).to receive(:puts)
end
Expand Down Expand Up @@ -162,7 +163,7 @@ module PactBroker::Client::CLI
context "with --auto-detect-version-properties on by default" 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(false)
end
Expand All @@ -182,13 +183,27 @@ module PactBroker::Client::CLI
)
invoke_broker
end
end
it 'determines the git 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 with version_required: false' do
expect(PactBroker::Client::PublishPacts).to receive(:call).with(
anything,
anything,
hash_including(number: '6.6.6', version_required: false),
anything,
anything
)
invoke_broker
end
end

context "with --auto-detect-version-properties specified explicitly" 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
Expand All @@ -209,6 +224,22 @@ 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 with version_required: true' do
expect(PactBroker::Client::PublishPacts).to receive(:call).with(
anything,
anything,
hash_including(number: '6.6.6', version_required: true),
anything,
anything
)
invoke_broker
end

context "with the branch specified as well" do
before do
subject.options = OpenStruct.new(
Expand Down
1 change: 1 addition & 0 deletions spec/lib/pact_broker/client/tasks/publication_task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ 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($stdout).to receive(:puts)
end

Expand Down
79 changes: 79 additions & 0 deletions tasks/pact.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 98fd08f

Please sign in to comment.