buildkit 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1c6dff3c82bb149b2cdc144fd406166383677156
4
- data.tar.gz: 546b10638c5c057c039938453c91f950c39762a6
3
+ metadata.gz: 6bfeb459ca2ab1b4f33f68b3af71c8e5000a3c1d
4
+ data.tar.gz: 627be0e606c9781b8a1378630abceb8954ec49ae
5
5
  SHA512:
6
- metadata.gz: fc285edc5ef194f29f87ffbe771fcd624d1f629d651c90f119a077d22a3fbf4b6393a1c2bb8d3e16a5ab0876be5ac3f35490fcb856d8d24e78ad6ccbf18ca8d8
7
- data.tar.gz: 91d612ddcc59751858f38788fd8e882847ff03863149aaa00534cdeebb85b07635ce0f040a87526cf817d7bc9f6db1812cefff92d6ffb5c83eb1c99798a3694a
6
+ metadata.gz: 7ef6f8e563bccf36437c2def7676473a124a5f7a4a3cb9ef6f8cfaea8bf424d4a72c933c52714756928bbd14aed289cd17afc45aaed6fea1f7ff75502b2d445a
7
+ data.tar.gz: 733f57b132f8788603b8e618448769967c5f78daa5524ed8abc5b5e425d2b1199609b5fd62bdd222a313a3874d007da022802edd3026c7548d7402bc8a0399fe
@@ -1,7 +1,7 @@
1
1
  inherit_from: .rubocop_todo.yml
2
2
 
3
3
  AllCops:
4
- TargetRubyVersion: 2.1
4
+ TargetRubyVersion: 2.2
5
5
 
6
6
  Metrics/LineLength:
7
7
  Max: 120
@@ -9,6 +9,9 @@ Metrics/LineLength:
9
9
  Metrics/MethodLength:
10
10
  Max: 25
11
11
 
12
+ Metrics/ClassLength:
13
+ Enabled: false
14
+
12
15
  Metrics/BlockLength:
13
16
  Enabled: false # RSpec ... :facepalm:
14
17
 
@@ -39,7 +42,10 @@ Style/PerlBackrefs:
39
42
  Style/SpaceInsideHashLiteralBraces:
40
43
  EnforcedStyle: no_space
41
44
 
42
- Style/TrailingCommaInLiteral:
45
+ Style/TrailingCommaInArrayLiteral:
46
+ EnforcedStyleForMultiline: comma
47
+
48
+ Style/TrailingCommaInHashLiteral:
43
49
  EnforcedStyleForMultiline: comma
44
50
 
45
51
  Style/TrailingCommaInArguments:
@@ -1,6 +1,6 @@
1
1
  sudo: false
2
2
  before_install: gem install bundler
3
3
  rvm:
4
- - '2.1'
5
4
  - '2.2'
5
+ - '2.3'
6
6
 
@@ -1,6 +1,4 @@
1
- # coding: utf-8
2
-
3
- lib = File.expand_path('../lib', __FILE__)
1
+ lib = File.expand_path('lib', __dir__)
4
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
3
  require 'buildkit/version'
6
4
 
@@ -19,7 +17,7 @@ Gem::Specification.new do |spec|
19
17
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
18
  spec.require_paths = ['lib']
21
19
 
22
- spec.required_ruby_version = '>= 2.0'
20
+ spec.required_ruby_version = '>= 2.2'
23
21
 
24
22
  spec.add_dependency 'sawyer', '~> 0.6'
25
23
  spec.add_development_dependency 'bundler'
@@ -6,6 +6,7 @@ require 'buildkit/client/pipelines'
6
6
  require 'buildkit/client/jobs'
7
7
  require 'buildkit/client/artifacts'
8
8
  require 'buildkit/response/raise_error'
9
+ require 'buildkit/header_link_parser'
9
10
 
10
11
  module Buildkit
11
12
  class Client
@@ -15,6 +16,7 @@ module Buildkit
15
16
  include Pipelines
16
17
  include Jobs
17
18
  include Artifacts
19
+ include HeaderLinkParser
18
20
 
19
21
  DEFAULT_ENDPOINT = 'https://api.buildkite.com/v2/'.freeze
20
22
 
@@ -24,6 +26,8 @@ module Buildkit
24
26
  # In Faraday 0.9, Faraday::Builder was renamed to Faraday::RackBuilder
25
27
  RACK_BUILDER_CLASS = defined?(Faraday::RackBuilder) ? Faraday::RackBuilder : Faraday::Builder
26
28
 
29
+ attr_accessor :auto_paginate
30
+
27
31
  class << self
28
32
  def build_middleware
29
33
  RACK_BUILDER_CLASS.new do |builder|
@@ -36,10 +40,11 @@ module Buildkit
36
40
 
37
41
  def initialize(endpoint: ENV.fetch('BUILDKITE_API_ENDPOINT', DEFAULT_ENDPOINT),
38
42
  token: ENV.fetch('BUILDKITE_API_TOKEN'),
39
- middleware: self.class.build_middleware)
43
+ middleware: self.class.build_middleware, auto_paginate: false)
40
44
  @middleware = middleware
41
45
  @endpoint = endpoint
42
46
  @token = token
47
+ @auto_paginate = auto_paginate
43
48
  end
44
49
 
45
50
  # Make a HTTP GET request
@@ -48,7 +53,11 @@ module Buildkit
48
53
  # @param options [Hash] Query and header params for request
49
54
  # @return [Sawyer::Resource]
50
55
  def get(url, options = {})
51
- request :get, url, parse_query_and_convenience_headers(options)
56
+ if @auto_paginate
57
+ paginate :get, url, parse_query_and_convenience_headers(options)
58
+ else
59
+ request :get, url, parse_query_and_convenience_headers(options)
60
+ end
52
61
  end
53
62
 
54
63
  # Make a HTTP POST request
@@ -109,19 +118,46 @@ module Buildkit
109
118
 
110
119
  def request(method, path, data, options = {})
111
120
  if data.is_a?(Hash)
112
- options[:query] = data.delete(:query) || {}
113
- options[:headers] = data.delete(:headers) || {}
121
+ options = extract_query_and_headers_from data
114
122
  if accept = data.delete(:accept)
115
123
  options[:headers][:accept] = accept
116
124
  end
117
125
  end
118
126
 
119
- @last_response = response = sawyer_agent.call(method, URI::Parser.new.escape(path.to_s), data, options)
127
+ @last_response = response = sawyer_agent.call(method, URI::DEFAULT_PARSER.escape(path.to_s), data, options)
120
128
  response.data
121
129
  end
122
130
 
131
+ def extract_query_and_headers_from(data)
132
+ {
133
+ query: data.delete(:query) || {},
134
+ headers: data.delete(:headers) || {},
135
+ }
136
+ end
137
+
138
+ def paginate(method, path, data, options = {})
139
+ response = []
140
+ loop do
141
+ request method, path, data, options
142
+ response.concat @last_response.data
143
+
144
+ link_header = parse_link_header(@last_response.headers[:link])
145
+ break if link_header[:next].nil?
146
+ path = next_page(link_header[:next])
147
+ end
148
+ response
149
+ end
150
+
151
+ def next_page(next_page)
152
+ build_path URI(next_page)
153
+ end
154
+
155
+ def build_path(uri)
156
+ "#{uri.path}?#{uri.query}"
157
+ end
158
+
123
159
  def sawyer_agent
124
- @agent ||= Sawyer::Agent.new(@endpoint, sawyer_options) do |http|
160
+ @sawyer_agent ||= Sawyer::Agent.new(@endpoint, sawyer_options) do |http|
125
161
  http.headers[:accept] = 'application/json'
126
162
  http.headers[:content_type] = 'application/json'
127
163
  http.headers[:user_agent] = "Buildkit v#{Buildkit::VERSION}"
@@ -13,6 +13,16 @@ module Buildkit
13
13
  def artifacts(org, pipeline, build, options = {})
14
14
  get("/v2/organizations/#{org}/pipelines/#{pipeline}/builds/#{build}/artifacts", options)
15
15
  end
16
+
17
+ # List all artifacts for a job
18
+ #
19
+ # @return [Array<Sawyer::Resource>] Array of hashes representing Buildkite artifacts.
20
+ # @see https://buildkite.com/docs/rest-api/artifacts#list-artifacts-for-a-job
21
+ # @example
22
+ # Buildkit.job_artifacts('my-great-org', 'great-pipeline', 42, '76365070-34d5-4104-8b91-952780f8029f')
23
+ def job_artifacts(org, pipeline, build, job, options = {})
24
+ get("/v2/organizations/#{org}/pipelines/#{pipeline}/builds/#{build}/jobs/#{job}/artifacts", options)
25
+ end
16
26
  end
17
27
  end
18
28
  end
@@ -0,0 +1,26 @@
1
+ module HeaderLinkParser
2
+ module_function
3
+
4
+ def parse_link_header(link_header)
5
+ {}.tap do |hash_link|
6
+ link_header.split(',').each do |link|
7
+ link_obj = LinkParser.new(link)
8
+ hash_link[link_obj.name] = link_obj.link
9
+ end
10
+ end
11
+ end
12
+
13
+ class LinkParser
14
+ def initialize(value)
15
+ @value = value
16
+ end
17
+
18
+ def name
19
+ @name ||= @value[/rel="(.*)"/, 1].to_sym
20
+ end
21
+
22
+ def link
23
+ @link ||= @value[/<(.+)>/, 1]
24
+ end
25
+ end
26
+ end
@@ -1,3 +1,3 @@
1
1
  module Buildkit
2
- VERSION = '1.3.0'.freeze
2
+ VERSION = '1.4.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: buildkit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean Boussier
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-01-25 00:00:00.000000000 Z
11
+ date: 2018-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sawyer
@@ -66,6 +66,7 @@ files:
66
66
  - lib/buildkit/client/organizations.rb
67
67
  - lib/buildkit/client/pipelines.rb
68
68
  - lib/buildkit/error.rb
69
+ - lib/buildkit/header_link_parser.rb
69
70
  - lib/buildkit/response/raise_error.rb
70
71
  - lib/buildkit/version.rb
71
72
  homepage: https://github.com/shopify/buildkit
@@ -80,7 +81,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
80
81
  requirements:
81
82
  - - ">="
82
83
  - !ruby/object:Gem::Version
83
- version: '2.0'
84
+ version: '2.2'
84
85
  required_rubygems_version: !ruby/object:Gem::Requirement
85
86
  requirements:
86
87
  - - ">="