pact_broker-client 1.34.0 → 1.38.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/release_gem.yml +1 -0
  3. data/.github/workflows/test.yml +23 -0
  4. data/CHANGELOG.md +39 -0
  5. data/README.md +15 -3
  6. data/Rakefile +2 -0
  7. data/doc/pacts/markdown/Pact Broker Client - Pact Broker.md +331 -8
  8. data/lib/pact_broker/client.rb +1 -1
  9. data/lib/pact_broker/client/backports.rb +13 -0
  10. data/lib/pact_broker/client/cli/broker.rb +112 -34
  11. data/lib/pact_broker/client/cli/can_i_deploy_long_desc.txt +18 -9
  12. data/lib/pact_broker/client/cli/create_or_update_webhook_long_desc.txt +3 -1
  13. data/lib/pact_broker/client/cli/create_webhook_long_desc.txt +2 -0
  14. data/lib/pact_broker/client/cli/custom_thor.rb +11 -17
  15. data/lib/pact_broker/client/git.rb +43 -22
  16. data/lib/pact_broker/client/hal/entity.rb +44 -3
  17. data/lib/pact_broker/client/hal/http_client.rb +5 -1
  18. data/lib/pact_broker/client/hal/links.rb +39 -0
  19. data/lib/pact_broker/client/hal_client_methods.rb +11 -0
  20. data/lib/pact_broker/client/hash_refinements.rb +19 -0
  21. data/lib/pact_broker/client/matrix.rb +2 -1
  22. data/lib/pact_broker/client/matrix/text_formatter.rb +46 -11
  23. data/lib/pact_broker/client/publish_pacts.rb +85 -14
  24. data/lib/pact_broker/client/tasks/publication_task.rb +37 -6
  25. data/lib/pact_broker/client/version.rb +1 -1
  26. data/lib/pact_broker/client/versions/record_deployment.rb +109 -0
  27. data/lib/pact_broker/client/versions/record_undeployment.rb +125 -0
  28. data/pact-broker-client.gemspec +2 -1
  29. data/script/publish-pact.sh +7 -1
  30. data/script/record-deployment.sh +4 -0
  31. data/script/trigger-release.sh +1 -1
  32. data/spec/lib/pact_broker/client/cli/broker_can_i_deploy_spec.rb +51 -6
  33. data/spec/lib/pact_broker/client/cli/broker_publish_spec.rb +108 -12
  34. data/spec/lib/pact_broker/client/cli/custom_thor_spec.rb +1 -7
  35. data/spec/lib/pact_broker/client/git_spec.rb +39 -2
  36. data/spec/lib/pact_broker/client/hal/entity_spec.rb +4 -3
  37. data/spec/lib/pact_broker/client/matrix/text_formatter_spec.rb +29 -4
  38. data/spec/lib/pact_broker/client/publish_pacts_spec.rb +99 -6
  39. data/spec/lib/pact_broker/client/tasks/publication_task_spec.rb +88 -10
  40. data/spec/lib/pact_broker/client/versions/describe_spec.rb +0 -1
  41. data/spec/lib/pact_broker/client/versions/record_deployment_spec.rb +82 -0
  42. data/spec/pacts/pact_broker_client-pact_broker.json +335 -8
  43. data/spec/service_providers/pact_broker_client_create_version_spec.rb +89 -0
  44. data/spec/service_providers/pact_broker_client_matrix_spec.rb +4 -0
  45. data/spec/service_providers/pact_broker_client_versions_spec.rb +1 -2
  46. data/spec/service_providers/record_deployment_spec.rb +219 -0
  47. data/spec/spec_helper.rb +2 -0
  48. data/spec/support/matrix.json +6 -1
  49. data/spec/support/matrix.txt +3 -3
  50. data/spec/support/matrix_error.txt +3 -3
  51. data/spec/support/matrix_with_results.txt +10 -0
  52. data/tasks/pact.rake +2 -0
  53. metadata +36 -8
  54. data/.travis.yml +0 -11
@@ -1,2 +1,2 @@
1
1
  require 'pact_broker/client/version'
2
- require 'pact_broker/client/pact_broker_client'
2
+ require 'pact_broker/client/pact_broker_client'
@@ -0,0 +1,13 @@
1
+ class Hash
2
+ def compact
3
+ h = {}
4
+ each do |key, value|
5
+ h[key] = value unless value == nil
6
+ end
7
+ h
8
+ end unless method_defined? :compact
9
+
10
+ def compact!
11
+ reject! {|_key, value| value == nil}
12
+ end unless method_defined? :compact!
13
+ end
@@ -1,4 +1,5 @@
1
1
  require 'pact_broker/client/cli/custom_thor'
2
+ require 'pact_broker/client/hash_refinements'
2
3
  require 'thor/error'
3
4
 
4
5
  module PactBroker
@@ -11,6 +12,8 @@ module PactBroker
11
12
  class VersionCreationError < ::Thor::Error; end
12
13
 
13
14
  class Broker < CustomThor
15
+ using PactBroker::Client::HashRefinements
16
+
14
17
  desc 'can-i-deploy', ''
15
18
  long_desc File.read(File.join(File.dirname(__FILE__), 'can_i_deploy_long_desc.txt'))
16
19
 
@@ -18,16 +21,13 @@ module PactBroker
18
21
  method_option :version, required: false, aliases: "-e", desc: "The pacticipant version. Must be entered after the --pacticipant that it relates to."
19
22
  method_option :latest, required: false, aliases: "-l", banner: '[TAG]', desc: "Use the latest pacticipant version. Optionally specify a TAG to use the latest version with the specified tag."
20
23
  method_option :to, required: false, banner: 'TAG', desc: "This is too hard to explain in a short sentence. Look at the examples.", default: nil
21
- method_option :broker_base_url, required: true, aliases: "-b", desc: "The base URL of the Pact Broker"
22
- method_option :broker_username, aliases: "-u", desc: "Pact Broker basic auth username"
23
- method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password"
24
- method_option :broker_token, aliases: "-k", desc: "Pact Broker bearer token"
24
+ method_option :to_environment, required: false, banner: 'ENVIRONMENT', desc: "The environment into which the pacticipant(s) are to be deployed", default: nil, hide: true
25
25
  method_option :output, aliases: "-o", desc: "json or table", default: 'table'
26
- method_option :verbose, aliases: "-v", type: :boolean, default: false, required: false, desc: "Verbose output. Default: false"
27
26
  method_option :retry_while_unknown, banner: 'TIMES', type: :numeric, default: 0, required: false, desc: "The number of times to retry while there is an unknown verification result (ie. the provider verification is likely still running)"
28
27
  method_option :retry_interval, banner: 'SECONDS', type: :numeric, default: 10, required: false, desc: "The time between retries in seconds. Use in conjuction with --retry-while-unknown"
29
28
  # Allow limit to be set manually until https://github.com/pact-foundation/pact_broker-client/issues/53 is fixed
30
29
  method_option :limit, hide: true
30
+ shared_authentication_options
31
31
 
32
32
  def can_i_deploy(*ignored_but_necessary)
33
33
  require 'pact_broker/client/cli/version_selector_options_parser'
@@ -37,21 +37,20 @@ module PactBroker
37
37
  selectors = VersionSelectorOptionsParser.call(ARGV)
38
38
  validate_can_i_deploy_selectors(selectors)
39
39
  can_i_deploy_options = { output: options.output, retry_while_unknown: options.retry_while_unknown, retry_interval: options.retry_interval }
40
- result = CanIDeploy.call(options.broker_base_url, selectors, {to_tag: options.to, limit: options.limit}, can_i_deploy_options, pact_broker_client_options)
40
+ result = CanIDeploy.call(options.broker_base_url, selectors, { to_tag: options.to, to_environment: options.to_environment, limit: options.limit }, can_i_deploy_options, pact_broker_client_options)
41
41
  $stdout.puts result.message
42
- exit(1) unless result.success
42
+ exit(can_i_deploy_exit_status) unless result.success
43
43
  end
44
44
 
45
45
  desc 'publish PACT_DIRS_OR_FILES ...', "Publish pacts to a Pact Broker."
46
46
  method_option :consumer_app_version, required: true, aliases: "-a", desc: "The consumer application version"
47
- method_option :broker_base_url, required: true, aliases: "-b", desc: "The base URL of the Pact Broker"
48
- method_option :broker_username, aliases: "-u", desc: "Pact Broker basic auth username"
49
- method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password"
50
- method_option :broker_token, aliases: "-k", desc: "Pact Broker bearer token"
47
+ method_option :branch, aliases: "-h", desc: "Repository branch of the consumer version"
48
+ method_option :auto_detect_version_properties, hidden: true, type: :boolean, default: false, desc: "Automatically detect the repository branch from known CI environment variables or git CLI."
51
49
  method_option :tag, aliases: "-t", type: :array, banner: "TAG", desc: "Tag name for consumer version. Can be specified multiple times."
52
50
  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. Default: false"
51
+ method_option :build_url, desc: "The build URL that created the pact"
53
52
  method_option :merge, type: :boolean, default: false, require: false, desc: "If a pact already exists for this consumer version and provider, merge the contents. Useful when running Pact tests concurrently on different build nodes."
54
- method_option :verbose, aliases: "-v", type: :boolean, default: false, required: false, desc: "Verbose output. Default: false"
53
+ shared_authentication_options
55
54
 
56
55
  def publish(*pact_files)
57
56
  require 'pact_broker/client/error'
@@ -69,11 +68,7 @@ module PactBroker
69
68
  method_option :tag, aliases: "-t", type: :array, banner: "TAG", desc: "Tag name for pacticipant version. Can be specified multiple times."
70
69
  method_option :auto_create_version, type: :boolean, default: false, desc: "Automatically create the pacticipant version if it does not exist. Default: false"
71
70
  method_option :tag_with_git_branch, aliases: "-g", type: :boolean, default: false, required: false, desc: "Tag pacticipant version with the name of the current git branch. Default: false"
72
- method_option :broker_base_url, required: true, aliases: "-b", desc: "The base URL of the Pact Broker"
73
- method_option :broker_username, aliases: "-u", desc: "Pact Broker basic auth username"
74
- method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password"
75
- method_option :broker_token, aliases: "-k", desc: "Pact Broker bearer token"
76
- method_option :verbose, aliases: "-v", type: :boolean, default: false, required: false, desc: "Verbose output. Default: false"
71
+ shared_authentication_options
77
72
 
78
73
  def create_version_tag
79
74
  require 'pact_broker/client/create_tag'
@@ -93,12 +88,8 @@ module PactBroker
93
88
  method_option :pacticipant, required: true, aliases: "-a", desc: "The name of the pacticipant that the version belongs to."
94
89
  method_option :version, required: false, aliases: "-e", desc: "The pacticipant version number."
95
90
  method_option :latest, required: false, aliases: "-l", banner: '[TAG]', desc: "Describe the latest pacticipant version. Optionally specify a TAG to describe the latest version with the specified tag."
96
- method_option :broker_base_url, required: true, aliases: "-b", desc: "The base URL of the Pact Broker"
97
- method_option :broker_username, aliases: "-u", desc: "Pact Broker basic auth username"
98
- method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password"
99
- method_option :broker_token, aliases: "-k", desc: "Pact Broker bearer token"
100
91
  method_option :output, aliases: "-o", desc: "json or table or id", default: 'table'
101
- method_option :verbose, aliases: "-v", type: :boolean, default: false, required: false, desc: "Verbose output. Default: false"
92
+ shared_authentication_options
102
93
 
103
94
  desc 'describe-version', 'Describes a pacticipant version. If no version or tag is specified, the latest version is described.'
104
95
  def describe_version
@@ -139,7 +130,7 @@ module PactBroker
139
130
 
140
131
  desc 'test-webhook', 'Test the execution of a webhook'
141
132
  method_option :uuid, type: :string, required: true, desc: "Specify the uuid for the webhook"
142
- shared_authentication_options_for_pact_broker
133
+ shared_authentication_options
143
134
  def test_webhook
144
135
  require 'pact_broker/client/webhooks/test'
145
136
  result = PactBroker::Client::Webhooks::Test.call(options, pact_broker_client_options)
@@ -157,7 +148,7 @@ module PactBroker
157
148
  desc 'create-or-update-pacticipant', 'Create or update pacticipant by name'
158
149
  method_option :name, type: :string, required: true, desc: "Pacticipant name"
159
150
  method_option :repository_url, type: :string, required: false, desc: "The repository URL of the pacticipant"
160
- shared_authentication_options_for_pact_broker
151
+ shared_authentication_options
161
152
  verbose_option
162
153
  def create_or_update_pacticipant(*required_but_ignored)
163
154
  raise ::Thor::RequiredArgumentMissingError, "Pacticipant name cannot be blank" if options.name.strip.size == 0
@@ -168,7 +159,7 @@ module PactBroker
168
159
  end
169
160
 
170
161
  desc 'list-latest-pact-versions', 'List the latest pact for each integration'
171
- shared_authentication_options_for_pact_broker
162
+ shared_authentication_options
172
163
  method_option :output, aliases: "-o", desc: "json or table", default: 'table'
173
164
  def list_latest_pact_versions(*required_but_ignored)
174
165
  require 'pact_broker/client/pacts/list_latest_versions'
@@ -177,6 +168,62 @@ module PactBroker
177
168
  exit(1) unless result.success
178
169
  end
179
170
 
171
+ if ENV.fetch("PACT_BROKER_FEATURES", "").include?("deployments")
172
+
173
+ ignored_and_hidden_potential_options_from_environment_variables
174
+ desc "record-deployment", "Record deployment of a pacticipant version to an environment"
175
+ method_option :pacticipant, required: true, aliases: "-a", desc: "The name of the pacticipant that was deployed."
176
+ method_option :version, required: true, aliases: "-e", desc: "The pacticipant version number that was deployed."
177
+ method_option :environment, required: true, desc: "The name of the environment that the pacticipant version was deployed to."
178
+ method_option :replaced_previous_deployed_version, type: :boolean, default: true, required: false, desc: "Whether or not this deployment replaced the previous deployed version. If it did, the previous deployed version of this pacticipant will be marked as undeployed in the Pact Broker."
179
+ method_option :output, aliases: "-o", desc: "json or text", default: 'text'
180
+ shared_authentication_options
181
+
182
+ def record_deployment
183
+ require 'pact_broker/client/versions/record_deployment'
184
+ params = {
185
+ pacticipant_name: options.pacticipant,
186
+ version_number: options.version,
187
+ environment_name: options.environment,
188
+ replaced_previous_deployed_version: options.replaced_previous_deployed_version,
189
+ output: options.output
190
+ }
191
+ result = PactBroker::Client::Versions::RecordDeployment.call(
192
+ params,
193
+ options.broker_base_url,
194
+ pact_broker_client_options
195
+ )
196
+ $stdout.puts result.message
197
+ exit(1) unless result.success
198
+ end
199
+
200
+ ignored_and_hidden_potential_options_from_environment_variables
201
+ desc "record-undeployment", "Record undeployment of (or the end of support for) a pacticipant version from an environment"
202
+ method_option :pacticipant, required: true, aliases: "-a", desc: "The name of the pacticipant that was deployed."
203
+ method_option :version, required: true, aliases: "-e", desc: "The pacticipant version number that was deployed."
204
+ method_option :environment, required: true, desc: "The name of the environment that the pacticipant version was deployed to."
205
+ method_option :output, aliases: "-o", desc: "json or text", default: 'text'
206
+ shared_authentication_options
207
+
208
+ def record_undeployment
209
+ require 'pact_broker/client/versions/record_undeployment'
210
+ params = {
211
+ pacticipant_name: options.pacticipant,
212
+ version_number: options.version,
213
+ environment_name: options.environment,
214
+ output: options.output
215
+ }
216
+ result = PactBroker::Client::Versions::RecordUndeployment.call(
217
+ params,
218
+ options.broker_base_url,
219
+ pact_broker_client_options
220
+ )
221
+ $stdout.puts result.message
222
+ exit(1) unless result.success
223
+ end
224
+
225
+ end
226
+
180
227
  ignored_and_hidden_potential_options_from_environment_variables
181
228
  desc 'version', "Show the pact_broker-client gem version"
182
229
  def version
@@ -191,6 +238,16 @@ module PactBroker
191
238
  true
192
239
  end
193
240
 
241
+ def can_i_deploy_exit_status
242
+ exit_code_string = ENV.fetch('PACT_BROKER_CAN_I_DEPLOY_EXIT_STATUS_BETA', '')
243
+ if exit_code_string =~ /^\d+$/
244
+ $stderr.puts "Exiting can-i-deploy with configured exit code #{exit_code_string}"
245
+ exit_code_string.to_i
246
+ else
247
+ 1
248
+ end
249
+ end
250
+
194
251
  def validate_credentials
195
252
  if options.broker_username && options.broker_token
196
253
  raise AuthError, "You cannot provide both a username/password and a bearer token. If your Pact Broker uses a bearer token, please remove the username and password configuration."
@@ -210,13 +267,20 @@ module PactBroker
210
267
 
211
268
  def publish_pacts pact_files
212
269
  require 'pact_broker/client/publish_pacts'
270
+
213
271
  write_options = options[:merge] ? { write: :merge } : {}
272
+ consumer_version_params = {
273
+ number: options.consumer_app_version,
274
+ branch: branch,
275
+ tags: tags,
276
+ build_url: options.build_url,
277
+ version_required: (!!options.branch || !!options.build_url || explict_auto_detect_version_properties)
278
+ }.compact
214
279
 
215
280
  PactBroker::Client::PublishPacts.call(
216
281
  options.broker_base_url,
217
282
  file_list(pact_files),
218
- options.consumer_app_version,
219
- tags,
283
+ consumer_version_params,
220
284
  pact_broker_client_options.merge(write_options)
221
285
  )
222
286
  end
@@ -238,21 +302,35 @@ module PactBroker
238
302
  require 'pact_broker/client/git'
239
303
 
240
304
  t = [*options.tag]
241
- t << PactBroker::Client::Git.branch if options.tag_with_git_branch
305
+ t << PactBroker::Client::Git.branch(raise_error: true) if options.tag_with_git_branch
242
306
  t.compact.uniq
243
307
  end
244
308
 
309
+ def branch
310
+ require 'pact_broker/client/git'
311
+
312
+ if options.branch.nil? && options.auto_detect_version_properties
313
+ PactBroker::Client::Git.branch(raise_error: explict_auto_detect_version_properties)
314
+ else
315
+ options.branch
316
+ end
317
+ end
318
+
319
+ def explict_auto_detect_version_properties
320
+ @explict_auto_detect_version_properties ||= ARGV.include?("--auto-detect-version-properties")
321
+ end
322
+
245
323
  def pact_broker_client_options
246
324
  client_options = { verbose: options.verbose }
247
- client_options[:token] = options.broker_token if options.broker_token
248
- if options.broker_username
325
+ client_options[:token] = options.broker_token || ENV['PACT_BROKER_TOKEN']
326
+ if options.broker_username || ENV['PACT_BROKER_USERNAME']
249
327
  client_options[:basic_auth] = {
250
- username: options.broker_username,
251
- password: options.broker_password
252
- }
328
+ username: options.broker_username || ENV['PACT_BROKER_USERNAME'],
329
+ password: options.broker_password || ENV['PACT_BROKER_PASSWORD']
330
+ }.compact
253
331
  end
254
332
 
255
- client_options
333
+ client_options.compact
256
334
  end
257
335
 
258
336
  def parse_webhook_events
@@ -19,17 +19,27 @@ Using a specific version is the easiest way to ensure you get an accurate respon
19
19
 
20
20
  #### Recommended usage - allowing the Pact Broker to automatically determine the dependencies
21
21
 
22
- Prerequisite: if you would like the Pact Broker to calculate the dependencies for you when you want to deploy an application into a given environment, you will need to let the Broker know which version of each application is in that environment. To do this, the relevant application version resource in the Broker will need to be "tagged" with the name of the environment during the deployment process:
22
+ Prerequisite: if you would like the Pact Broker to calculate the dependencies for you when you want to deploy an application into a given environment, you will need to let the Broker know which version of each application is in that environment.
23
+
24
+ How you do this depends on the version of the Pact Broker you are running.
25
+
26
+ If you are using a Broker version where deployment versions are supported, then you would notify the Broker of the deployment of this application version like so:
27
+
28
+ $ pact-broker record-deployment --pacticipant Foo --version 173153ae0 --environment test
29
+
30
+ This assumes that you have already set up an environment named "test" in the Broker.
31
+
32
+ If you are using a Broker version that does not support deployment environments, then you will need to use tags to notify the broker of the deployment of this application version, like so:
23
33
 
24
34
  $ pact-broker create-version-tag --pacticipant Foo --version 173153ae0 --tag test
25
35
 
26
- This allows you to use the following simple command to find out if you are safe to deploy:
36
+ Once you have configured your build to notify the Pact Broker of the successful deployment using either method describe above, you can use the following simple command to find out if you are safe to deploy (use either `--to` or `--to-environment` as supported):
27
37
 
28
- $ pact-broker can-i-deploy --pacticipant PACTICIPANT --version VERSION \
29
- --to ENVIRONMENT \
38
+ $ pact-broker can-i-deploy --pacticipant PACTICIPANT --version VERSION
39
+ [--to-environment ENVIRONMENT | --to ENVIRONMENT_TAG ]
30
40
  --broker-base-url BROKER_BASE_URL
31
41
 
32
- If the `--to` tag is omitted, then the query will return the compatiblity with the overall latest version of each of the other applications.
42
+ If the `--to` or `--to-environment` options are omitted, then the query will return the compatiblity with the overall latest version of each of the other applications.
33
43
 
34
44
  Examples:
35
45
 
@@ -38,7 +48,7 @@ Can I deploy version 173153ae0 of application Foo to the test environment?
38
48
 
39
49
 
40
50
  $ pact-broker can-i-deploy --pacticipant Foo --version 173153ae0 \
41
- --to test \
51
+ --to-environment test \
42
52
  --broker-base-url https://my-pact-broker
43
53
 
44
54
 
@@ -56,21 +66,20 @@ Can I deploy the latest version of the application Foo that has the tag "test" t
56
66
  --broker-base-url https://my-pact-broker
57
67
 
58
68
 
59
-
60
69
  #### Alternate usage - specifying dependencies explicitly
61
70
 
62
71
  If you are unable to use tags, or there is some other limitation that stops you from using the recommended approach, you can specify one or more of the dependencies explictly. You must also do this if you want to use the `--all TAG` option for any of the pacticipants.
63
72
 
64
73
  You can specify as many application versions as you like, and you can even specify multiple versions of the same application (repeat the `--pacticipant` name and supply a different version.)
65
74
 
66
- You can use explictly declared dependencies with or without the `--to ENVIRONMENT`. For example, if you declare two (or more) application versions with no `--to ENVIRONMENT`, then only the applications you specify will be taken into account when determining if it is safe to deploy. If you declare two (or more) application versions _as well as_ a `--to ENVIRONMENT`, then the Pact Broker will work out what integrations your declared applications will have in that environment when determining if it safe to deploy. When using this script for a production release, and you are using tags, it is always the most future-proof option to use the `--to` if possible, as it will catch any newly added consumers or providers.
75
+ You can use explictly declared dependencies with or without the `--to ENVIRONMENT_TAG`. For example, if you declare two (or more) application versions with no `--to ENVIRONMENT_TAG`, then only the applications you specify will be taken into account when determining if it is safe to deploy. If you declare two (or more) application versions _as well as_ a `--to ENVIRONMENT`, then the Pact Broker will work out what integrations your declared applications will have in that environment when determining if it safe to deploy. When using this script for a production release, and you are using tags, it is always the most future-proof option to use the `--to` if possible, as it will catch any newly added consumers or providers.
67
76
 
68
77
  If you are finding that your dependencies are not being automatically included when you supply multiple pacticipant versions, please upgrade to the latest version of the Pact Broker, as this is a more recently added feature.
69
78
 
70
79
 
71
80
  $ pact-broker can-i-deploy --pacticipant PACTICIPANT_1 [--version VERSION_1 | --latest [TAG_1] | --all TAG_1] \
72
81
  --pacticipant PACTICIPANT_2 [--version VERSION_2 | --latest [TAG_2] | --all TAG_2] \
73
- [--to ENVIRONMENT] \
82
+ [--to-environment ENVIRONMENT | --to ENVIRONMENT_TAG] \
74
83
  --broker-base-url BROKER_BASE_URL
75
84
 
76
85
  Examples:
@@ -1 +1,3 @@
1
- Create a curl command that executes the request that you want your webhook to execute, then replace "curl" with "pact-broker create-or-update-webhook" and add the consumer, provider, event types and broker details. Note that the URL must be the first parameter when executing create-or-update-webhook and a uuid must also be provided. You can generate a valid UUID by using the `generate-uuid` command.
1
+ Create a curl command that executes the request that you want your webhook to execute, then replace "curl" with "pact-broker create-or-update-webhook" and add the consumer, provider, event types and broker details. Note that the URL must be the first parameter when executing create-or-update-webhook and a uuid must also be provided. You can generate a valid UUID by using the `generate-uuid` command.
2
+
3
+ Note that the -u option from the curl command clashes with the -u option from the pact-broker CLI. When used in this command, the -u will be used as a curl option. Please use the --broker-username or environment variable for the Pact Broker username.
@@ -1 +1,3 @@
1
1
  Create a curl command that executes the request that you want your webhook to execute, then replace "curl" with "pact-broker create-webhook" and add the consumer, provider, event types and broker details. Note that the URL must be the first parameter when executing create-webhook.
2
+
3
+ Note that the -u option from the curl command clashes with the -u option from the pact-broker CLI. When used in this command, the -u will be used as a curl option. Please use the --broker-username or environment variable for the Pact Broker username.
@@ -22,10 +22,7 @@ module PactBroker
22
22
  def self.add_broker_config_from_environment_variables argv
23
23
  return argv if argv[0] == 'help' || argv.empty?
24
24
 
25
- new_argv = add_option_from_environment_variable(argv, 'broker-base-url', 'b', 'PACT_BROKER_BASE_URL')
26
- new_argv = add_option_from_environment_variable(new_argv, 'broker-username', 'u', 'PACT_BROKER_USERNAME')
27
- new_argv = add_option_from_environment_variable(new_argv, 'broker-token', 'k', 'PACT_BROKER_TOKEN')
28
- add_option_from_environment_variable(new_argv, 'broker-password', 'p', 'PACT_BROKER_PASSWORD')
25
+ add_option_from_environment_variable(argv, 'broker-base-url', 'b', 'PACT_BROKER_BASE_URL')
29
26
  end
30
27
 
31
28
  def self.add_option_from_environment_variable argv, long_name, short_name, environment_variable_name
@@ -86,6 +83,14 @@ module PactBroker
86
83
  method_option :broker_token, hide: true
87
84
  end
88
85
 
86
+ def self.shared_authentication_options
87
+ method_option :broker_base_url, required: true, aliases: "-b", desc: "The base URL of the Pact Broker"
88
+ method_option :broker_username, aliases: "-u", desc: "Pact Broker basic auth username"
89
+ method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password"
90
+ method_option :broker_token, aliases: "-k", desc: "Pact Broker bearer token"
91
+ method_option :verbose, aliases: "-v", type: :boolean, default: false, required: false, desc: "Verbose output. Default: false"
92
+ end
93
+
89
94
  def self.shared_options_for_webhook_commands
90
95
  method_option :request, banner: "METHOD", aliases: "-X", desc: "Webhook HTTP method", required: true
91
96
  method_option :header, aliases: "-H", type: :array, desc: "Webhook Header"
@@ -93,24 +98,13 @@ module PactBroker
93
98
  method_option :user, aliases: "-u", desc: "Webhook basic auth username and password eg. username:password"
94
99
  method_option :consumer, desc: "Consumer name"
95
100
  method_option :provider, desc: "Provider name"
96
- method_option :broker_base_url, required: true, aliases: "-b", desc: "The base URL of the Pact Broker"
97
- method_option :broker_username, desc: "Pact Broker basic auth username"
98
- method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password"
99
- method_option :broker_token, aliases: "-k", desc: "Pact Broker bearer token"
100
101
  method_option :description, desc: "Wwebhook description"
101
102
  method_option :contract_content_changed, type: :boolean, desc: "Trigger this webhook when the pact content changes"
102
103
  method_option :contract_published, type: :boolean, desc: "Trigger this webhook when a pact is published"
103
104
  method_option :provider_verification_published, type: :boolean, desc: "Trigger this webhook when a provider verification result is published"
104
105
  method_option :provider_verification_failed, type: :boolean, desc: "Trigger this webhook when a failed provider verification result is published"
105
106
  method_option :provider_verification_succeeded, type: :boolean, desc: "Trigger this webhook when a successful provider verification result is published"
106
- method_option :verbose, aliases: "-v", type: :boolean, default: false, required: false, desc: "Verbose output. Default: false"
107
- end
108
-
109
- def self.shared_authentication_options_for_pact_broker
110
- method_option :broker_base_url, required: true, aliases: "-b", desc: "The base URL of the Pact Broker"
111
- method_option :broker_username, aliases: "-u", desc: "Pact Broker basic auth username"
112
- method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password"
113
- method_option :broker_token, aliases: "-k", desc: "Pact Broker bearer token"
107
+ shared_authentication_options
114
108
  end
115
109
 
116
110
  def self.verbose_option
@@ -120,4 +114,4 @@ module PactBroker
120
114
  end
121
115
  end
122
116
  end
123
- end
117
+ end
@@ -1,4 +1,5 @@
1
1
  require 'pact_broker/client/error'
2
+ require 'pact_broker/client/hash_refinements'
2
3
 
3
4
  =begin
4
5
 
@@ -16,19 +17,25 @@ BITBUCKET_BRANCH BITBUCKET_COMMIT https://confluence.atlassian.com/bitbucket/var
16
17
  =end
17
18
 
18
19
  # Keep in sync with pact-provider-verifier/lib/pact/provider_verifier/git.rb
20
+
21
+ # `git name-rev --name-only HEAD` provides "tags/v1.35.0^0"
19
22
  module PactBroker
20
23
  module Client
21
24
  module Git
22
- COMMAND = 'git name-rev --name-only HEAD'.freeze
25
+ using PactBroker::Client::HashRefinements
26
+
27
+ COMMAND = 'git rev-parse --abbrev-ref HEAD'.freeze
23
28
  BRANCH_ENV_VAR_NAMES = %w{BUILDKITE_BRANCH CIRCLE_BRANCH TRAVIS_BRANCH GIT_BRANCH GIT_LOCAL_BRANCH APPVEYOR_REPO_BRANCH CI_COMMIT_REF_NAME BITBUCKET_BRANCH}.freeze
24
29
  COMMIT_ENV_VAR_NAMES = %w{BUILDKITE_COMMIT CIRCLE_SHA1 TRAVIS_COMMIT GIT_COMMIT APPVEYOR_REPO_COMMIT CI_COMMIT_ID BITBUCKET_COMMIT}
25
30
 
31
+ BUILD_URL_ENV_VAR_NAMES = %w{BUILDKITE_BUILD_URL CIRCLE_BUILD_URL TRAVIS_BUILD_WEB_URL BUILD_URL }
32
+
26
33
  def self.commit
27
34
  find_commit_from_env_vars
28
35
  end
29
36
 
30
- def self.branch
31
- find_branch_from_env_vars || branch_from_git_command
37
+ def self.branch(options)
38
+ find_branch_from_known_env_vars || find_branch_from_env_var_ending_with_branch || branch_from_git_command(options[:raise_error])
32
39
  end
33
40
 
34
41
  # private
@@ -37,10 +44,21 @@ module PactBroker
37
44
  COMMIT_ENV_VAR_NAMES.collect { |env_var_name| value_from_env_var(env_var_name) }.compact.first
38
45
  end
39
46
 
40
- def self.find_branch_from_env_vars
47
+ def self.find_branch_from_known_env_vars
41
48
  BRANCH_ENV_VAR_NAMES.collect { |env_var_name| value_from_env_var(env_var_name) }.compact.first
42
49
  end
43
50
 
51
+ def self.find_branch_from_env_var_ending_with_branch
52
+ values = ENV.keys
53
+ .select{ |env_var_name| env_var_name.end_with?("_BRANCH") }
54
+ .collect{ |env_var_name| value_from_env_var(env_var_name) }.compact
55
+ if values.size == 1
56
+ values.first
57
+ else
58
+ nil
59
+ end
60
+ end
61
+
44
62
  def self.value_from_env_var(env_var_name)
45
63
  val = ENV[env_var_name]
46
64
  if val && val.strip.size > 0
@@ -50,24 +68,10 @@ module PactBroker
50
68
  end
51
69
  end
52
70
 
53
- def self.branch_from_git_command
54
- branch_names = nil
55
- begin
56
- branch_names = execute_git_command
57
- .split("\n")
58
- .collect(&:strip)
59
- .reject(&:empty?)
60
- .collect(&:split)
61
- .collect(&:first)
62
- .collect{ |line| line.gsub(/^origin\//, '') }
63
- .reject{ |line| line == "HEAD" }
64
-
65
- rescue StandardError => e
66
- raise PactBroker::Client::Error, "Could not determine current git branch using command `#{COMMAND}`. #{e.class} #{e.message}"
67
- end
68
-
69
- validate_branch_names(branch_names)
70
- branch_names[0]
71
+ def self.branch_from_git_command(raise_error)
72
+ branch_names = execute_and_parse_command(raise_error)
73
+ validate_branch_names(branch_names) if raise_error
74
+ branch_names.size == 1 ? branch_names[0] : nil
71
75
  end
72
76
 
73
77
  def self.validate_branch_names(branch_names)
@@ -83,6 +87,23 @@ module PactBroker
83
87
  def self.execute_git_command
84
88
  `#{COMMAND}`
85
89
  end
90
+
91
+ def self.execute_and_parse_command(raise_error)
92
+ execute_git_command
93
+ .split("\n")
94
+ .collect(&:strip)
95
+ .reject(&:empty?)
96
+ .collect(&:split)
97
+ .collect(&:first)
98
+ .collect{ |line| line.gsub(/^origin\//, '') }
99
+ .reject{ |line| line == "HEAD" }
100
+ rescue StandardError => e
101
+ if raise_error
102
+ raise PactBroker::Client::Error, "Could not determine current git branch using command `#{COMMAND}`. #{e.class} #{e.message}"
103
+ else
104
+ return []
105
+ end
106
+ end
86
107
  end
87
108
  end
88
109
  end