pact_broker-client 1.32.0 → 1.37.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) 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 +48 -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 +73 -28
  11. data/lib/pact_broker/client/cli/can_i_deploy_long_desc.txt +18 -9
  12. data/lib/pact_broker/client/cli/custom_thor.rb +9 -12
  13. data/lib/pact_broker/client/git.rb +43 -22
  14. data/lib/pact_broker/client/hal/entity.rb +27 -3
  15. data/lib/pact_broker/client/hal/http_client.rb +4 -0
  16. data/lib/pact_broker/client/hal/links.rb +39 -0
  17. data/lib/pact_broker/client/hal_client_methods.rb +11 -0
  18. data/lib/pact_broker/client/hash_refinements.rb +19 -0
  19. data/lib/pact_broker/client/matrix.rb +2 -1
  20. data/lib/pact_broker/client/matrix/text_formatter.rb +46 -11
  21. data/lib/pact_broker/client/publish_pacts.rb +93 -14
  22. data/lib/pact_broker/client/tasks/publication_task.rb +37 -6
  23. data/lib/pact_broker/client/version.rb +1 -1
  24. data/lib/pact_broker/client/versions/record_deployment.rb +109 -0
  25. data/pact-broker-client.gemspec +2 -0
  26. data/script/publish-pact.sh +7 -1
  27. data/script/trigger-release.sh +1 -1
  28. data/spec/lib/pact_broker/client/cli/broker_can_i_deploy_spec.rb +13 -2
  29. data/spec/lib/pact_broker/client/cli/broker_publish_spec.rb +108 -12
  30. data/spec/lib/pact_broker/client/git_spec.rb +39 -2
  31. data/spec/lib/pact_broker/client/hal/entity_spec.rb +4 -3
  32. data/spec/lib/pact_broker/client/matrix/text_formatter_spec.rb +29 -4
  33. data/spec/lib/pact_broker/client/publish_pacts_spec.rb +119 -7
  34. data/spec/lib/pact_broker/client/tasks/publication_task_spec.rb +88 -10
  35. data/spec/lib/pact_broker/client/versions/describe_spec.rb +0 -1
  36. data/spec/lib/pact_broker/client/versions/record_deployment_spec.rb +82 -0
  37. data/spec/pacts/pact_broker_client-pact_broker.json +335 -8
  38. data/spec/service_providers/pact_broker_client_create_version_spec.rb +89 -0
  39. data/spec/service_providers/pact_broker_client_matrix_spec.rb +4 -0
  40. data/spec/service_providers/pact_broker_client_versions_spec.rb +1 -2
  41. data/spec/service_providers/record_deployment_spec.rb +219 -0
  42. data/spec/support/matrix.json +6 -1
  43. data/spec/support/matrix.txt +3 -3
  44. data/spec/support/matrix_error.txt +3 -3
  45. data/spec/support/matrix_with_results.txt +10 -0
  46. data/tasks/pact.rake +2 -0
  47. metadata +44 -4
  48. 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,20 +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
42
  exit(1) 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"
53
- method_option :verbose, aliases: "-v", type: :boolean, default: false, required: false, desc: "Verbose output. Default: false"
51
+ method_option :build_url, desc: "The build URL that created the pact"
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."
53
+ shared_authentication_options
54
54
 
55
55
  def publish(*pact_files)
56
56
  require 'pact_broker/client/error'
@@ -68,11 +68,7 @@ module PactBroker
68
68
  method_option :tag, aliases: "-t", type: :array, banner: "TAG", desc: "Tag name for pacticipant version. Can be specified multiple times."
69
69
  method_option :auto_create_version, type: :boolean, default: false, desc: "Automatically create the pacticipant version if it does not exist. Default: false"
70
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"
71
- method_option :broker_base_url, required: true, aliases: "-b", desc: "The base URL of the Pact Broker"
72
- method_option :broker_username, aliases: "-u", desc: "Pact Broker basic auth username"
73
- method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password"
74
- method_option :broker_token, aliases: "-k", desc: "Pact Broker bearer token"
75
- method_option :verbose, aliases: "-v", type: :boolean, default: false, required: false, desc: "Verbose output. Default: false"
71
+ shared_authentication_options
76
72
 
77
73
  def create_version_tag
78
74
  require 'pact_broker/client/create_tag'
@@ -92,12 +88,8 @@ module PactBroker
92
88
  method_option :pacticipant, required: true, aliases: "-a", desc: "The name of the pacticipant that the version belongs to."
93
89
  method_option :version, required: false, aliases: "-e", desc: "The pacticipant version number."
94
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."
95
- method_option :broker_base_url, required: true, aliases: "-b", desc: "The base URL of the Pact Broker"
96
- method_option :broker_username, aliases: "-u", desc: "Pact Broker basic auth username"
97
- method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password"
98
- method_option :broker_token, aliases: "-k", desc: "Pact Broker bearer token"
99
91
  method_option :output, aliases: "-o", desc: "json or table or id", default: 'table'
100
- method_option :verbose, aliases: "-v", type: :boolean, default: false, required: false, desc: "Verbose output. Default: false"
92
+ shared_authentication_options
101
93
 
102
94
  desc 'describe-version', 'Describes a pacticipant version. If no version or tag is specified, the latest version is described.'
103
95
  def describe_version
@@ -138,7 +130,7 @@ module PactBroker
138
130
 
139
131
  desc 'test-webhook', 'Test the execution of a webhook'
140
132
  method_option :uuid, type: :string, required: true, desc: "Specify the uuid for the webhook"
141
- shared_authentication_options_for_pact_broker
133
+ shared_authentication_options
142
134
  def test_webhook
143
135
  require 'pact_broker/client/webhooks/test'
144
136
  result = PactBroker::Client::Webhooks::Test.call(options, pact_broker_client_options)
@@ -156,7 +148,7 @@ module PactBroker
156
148
  desc 'create-or-update-pacticipant', 'Create or update pacticipant by name'
157
149
  method_option :name, type: :string, required: true, desc: "Pacticipant name"
158
150
  method_option :repository_url, type: :string, required: false, desc: "The repository URL of the pacticipant"
159
- shared_authentication_options_for_pact_broker
151
+ shared_authentication_options
160
152
  verbose_option
161
153
  def create_or_update_pacticipant(*required_but_ignored)
162
154
  raise ::Thor::RequiredArgumentMissingError, "Pacticipant name cannot be blank" if options.name.strip.size == 0
@@ -167,7 +159,7 @@ module PactBroker
167
159
  end
168
160
 
169
161
  desc 'list-latest-pact-versions', 'List the latest pact for each integration'
170
- shared_authentication_options_for_pact_broker
162
+ shared_authentication_options
171
163
  method_option :output, aliases: "-o", desc: "json or table", default: 'table'
172
164
  def list_latest_pact_versions(*required_but_ignored)
173
165
  require 'pact_broker/client/pacts/list_latest_versions'
@@ -176,6 +168,37 @@ module PactBroker
176
168
  exit(1) unless result.success
177
169
  end
178
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
+ end
201
+
179
202
  ignored_and_hidden_potential_options_from_environment_variables
180
203
  desc 'version', "Show the pact_broker-client gem version"
181
204
  def version
@@ -210,12 +233,20 @@ module PactBroker
210
233
  def publish_pacts pact_files
211
234
  require 'pact_broker/client/publish_pacts'
212
235
 
236
+ write_options = options[:merge] ? { write: :merge } : {}
237
+ consumer_version_params = {
238
+ number: options.consumer_app_version,
239
+ branch: branch,
240
+ tags: tags,
241
+ build_url: options.build_url,
242
+ version_required: (!!options.branch || !!options.build_url || explict_auto_detect_version_properties)
243
+ }.compact
244
+
213
245
  PactBroker::Client::PublishPacts.call(
214
246
  options.broker_base_url,
215
247
  file_list(pact_files),
216
- options.consumer_app_version,
217
- tags,
218
- pact_broker_client_options
248
+ consumer_version_params,
249
+ pact_broker_client_options.merge(write_options)
219
250
  )
220
251
  end
221
252
 
@@ -236,10 +267,24 @@ module PactBroker
236
267
  require 'pact_broker/client/git'
237
268
 
238
269
  t = [*options.tag]
239
- t << PactBroker::Client::Git.branch if options.tag_with_git_branch
270
+ t << PactBroker::Client::Git.branch(raise_error: true) if options.tag_with_git_branch
240
271
  t.compact.uniq
241
272
  end
242
273
 
274
+ def branch
275
+ require 'pact_broker/client/git'
276
+
277
+ if options.branch.nil? && options.auto_detect_version_properties
278
+ PactBroker::Client::Git.branch(raise_error: explict_auto_detect_version_properties)
279
+ else
280
+ options.branch
281
+ end
282
+ end
283
+
284
+ def explict_auto_detect_version_properties
285
+ @explict_auto_detect_version_properties ||= ARGV.include?("--auto-detect-version-properties")
286
+ end
287
+
243
288
  def pact_broker_client_options
244
289
  client_options = { verbose: options.verbose }
245
290
  client_options[:token] = options.broker_token if options.broker_token
@@ -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:
@@ -86,6 +86,14 @@ module PactBroker
86
86
  method_option :broker_token, hide: true
87
87
  end
88
88
 
89
+ def self.shared_authentication_options
90
+ method_option :broker_base_url, required: true, aliases: "-b", desc: "The base URL of the Pact Broker"
91
+ method_option :broker_username, aliases: "-u", desc: "Pact Broker basic auth username"
92
+ method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password"
93
+ method_option :broker_token, aliases: "-k", desc: "Pact Broker bearer token"
94
+ method_option :verbose, aliases: "-v", type: :boolean, default: false, required: false, desc: "Verbose output. Default: false"
95
+ end
96
+
89
97
  def self.shared_options_for_webhook_commands
90
98
  method_option :request, banner: "METHOD", aliases: "-X", desc: "Webhook HTTP method", required: true
91
99
  method_option :header, aliases: "-H", type: :array, desc: "Webhook Header"
@@ -93,24 +101,13 @@ module PactBroker
93
101
  method_option :user, aliases: "-u", desc: "Webhook basic auth username and password eg. username:password"
94
102
  method_option :consumer, desc: "Consumer name"
95
103
  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
104
  method_option :description, desc: "Wwebhook description"
101
105
  method_option :contract_content_changed, type: :boolean, desc: "Trigger this webhook when the pact content changes"
102
106
  method_option :contract_published, type: :boolean, desc: "Trigger this webhook when a pact is published"
103
107
  method_option :provider_verification_published, type: :boolean, desc: "Trigger this webhook when a provider verification result is published"
104
108
  method_option :provider_verification_failed, type: :boolean, desc: "Trigger this webhook when a failed provider verification result is published"
105
109
  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"
110
+ shared_authentication_options
114
111
  end
115
112
 
116
113
  def self.verbose_option
@@ -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
@@ -1,6 +1,7 @@
1
1
  require 'erb'
2
2
  require 'delegate'
3
3
  require 'pact_broker/client/hal/link'
4
+ require 'pact_broker/client/hal/links'
4
5
 
5
6
  module PactBroker
6
7
  module Client
@@ -51,10 +52,27 @@ module PactBroker
51
52
  end
52
53
  end
53
54
 
55
+ def _links(key)
56
+ if @links[key] && @links[key].is_a?(Array)
57
+ link_collection = @links[key].collect do | hash |
58
+ Link.new(hash, @client)
59
+ end
60
+ Links.new(@href, key, link_collection)
61
+ elsif @links[key].is_a?(Hash)
62
+ Links.new(@href, key, [Link.new(@links[key], @client)])
63
+ else
64
+ nil
65
+ end
66
+ end
67
+
54
68
  def _link!(key)
55
69
  _link(key) or raise RelationNotFoundError.new("Could not find relation '#{key}' in resource at #{@href}")
56
70
  end
57
71
 
72
+ def _links!(key)
73
+ _links(key) or raise RelationNotFoundError.new("Could not find relation '#{key}' in resource at #{@href}")
74
+ end
75
+
58
76
  def success?
59
77
  true
60
78
  end
@@ -85,7 +103,7 @@ module PactBroker
85
103
  @data.key?(method_name) || @links.key?(method_name)
86
104
  end
87
105
 
88
- def assert_success!
106
+ def assert_success!(_ignored = nil)
89
107
  self
90
108
  end
91
109
  end
@@ -107,8 +125,14 @@ module PactBroker
107
125
  false
108
126
  end
109
127
 
110
- def assert_success!
111
- raise ErrorResponseReturned.new("Error retrieving #{@href} status=#{response ? response.code: nil} #{response ? response.raw_body : ''}")
128
+ def assert_success!(messages = {})
129
+ default_message = "Error retrieving #{@href} status=#{response ? response.status: nil} #{response ? response.raw_body : ''}".strip
130
+ message = if response && messages[response.status]
131
+ (messages[response.status] || "") + " (#{default_message})"
132
+ else
133
+ default_message
134
+ end
135
+ raise ErrorResponseReturned.new(message)
112
136
  end
113
137
  end
114
138
  end