mina-circle 1.2.2 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 81468167f380d30f567dfbee43a809965f9df4ab
4
- data.tar.gz: c9ffa18957e5af1a040a5a805c8f1b1b5348682f
3
+ metadata.gz: 1ebe0fc91eac815704540d8c562c30548babe091
4
+ data.tar.gz: c9ba552f69fbc3b28322df0cd5a3dcfd319063c6
5
5
  SHA512:
6
- metadata.gz: d9bf57d33748fd7a71d43b5d17796ba30db72b962d4df5ab1c97263d42ff7836f9c984161e23b6ffcc8a67b0920c50a9e927e50e2264824a2dd637b948cf54b5
7
- data.tar.gz: 9b2572355f87c327925636b2ab98be4385b0aab92382e3adacf65e7076a5a38cd2f1b60079e177eaebaaf6688a5ffc526fad912e0a18500deeeee2f5da62ce0f
6
+ metadata.gz: d9afb3342f0c9b3d937e1922bcadf66af5b8d10830cb036f2bf2c12738c1bd697f305a56303d80fcc8b6f8c11239b3e1e3dc976a61657ac4ace4e9a3b83c4cdb
7
+ data.tar.gz: 799e3c53775dd0ccfce79eaf54b3e8676cfbd013e13c3c2693b582ded62e94e6076ea2ebead624a0ab8a4962bba33d37ecb039496fe0f8b3a8bf8d6c8e4772e5
data/lib/mina-circle.rb CHANGED
@@ -1,7 +1,7 @@
1
- require 'json'
2
- require 'open-uri'
3
-
4
1
  require 'mina-circle/version'
5
2
  require 'mina-circle/helpers'
6
3
  require 'mina-circle/tasks'
7
- require 'mina-circle/circle_ci'
4
+ require 'mina-circle/circle-ci/circle_ci'
5
+ require 'mina-circle/circle-ci/client'
6
+ require 'mina-circle/circle-ci/artifact'
7
+ require 'mina-circle/circle-ci/project'
@@ -0,0 +1,20 @@
1
+ require 'uri'
2
+
3
+ class CircleCI::Artifact
4
+ attr_reader :response_object, :build
5
+ def initialize(hash)
6
+ @response_object = hash
7
+ end
8
+
9
+ def filename
10
+ File.basename response_object['path']
11
+ end
12
+
13
+ def url
14
+ URI(response_object.fetch('url', ''))
15
+ end
16
+
17
+ def contains_valid_url?
18
+ url.kind_of?(URI::HTTP) or url.kind_of?(URI::HTTPS)
19
+ end
20
+ end
@@ -0,0 +1,17 @@
1
+ class CircleCI
2
+ def self.artifact_url(settings = {})
3
+ project = CircleCI::Project.new(
4
+ organization: settings[:circleci_user],
5
+ name: settings[:circleci_project],
6
+ branch: settings[:branch]
7
+ )
8
+
9
+ artifact = project.artifacts.detect { |a|
10
+ a.filename == settings[:circleci_artifact]
11
+ }
12
+ raise 'Missing or invalid URL from CircleCI' unless artifact.contains_valid_url?
13
+ base_url = artifact.url
14
+ base_url.query = URI.encode_www_form({ 'circle-token' => CircleCI::Client.instance.api_token })
15
+ base_url.to_s
16
+ end
17
+ end
@@ -0,0 +1,39 @@
1
+ require 'singleton'
2
+ require 'uri'
3
+ require 'net/http'
4
+ require 'base64'
5
+ require 'yaml'
6
+ require 'json'
7
+
8
+ class CircleCI::Client
9
+ include Singleton
10
+ BASE_URI = 'https://circleci.com/api/v1.1'
11
+ CONFIG_FILE_PATH = File.join(Dir.home, '.mina-circle.yml')
12
+ attr_writer :api_token
13
+
14
+ def get(path, params = {})
15
+ uri = URI("#{BASE_URI}/#{path}")
16
+ uri.query = URI.encode_www_form(params) unless params.empty?
17
+ request = Net::HTTP::Get.new uri
18
+ request['Accept'] = 'application/json'
19
+ request['Authorization'] = "Basic #{Base64.encode64(api_token).chomp}"
20
+ response = Net::HTTP.start uri.host, uri.port, use_ssl: true do |http|
21
+ http.request request
22
+ end
23
+ JSON.parse response.body
24
+ end
25
+
26
+ def api_token
27
+ [
28
+ @api_token,
29
+ ENV['MINA_CIRCLE_TOKEN'],
30
+ system_token
31
+ ].compact.first
32
+ end
33
+
34
+ private
35
+
36
+ def system_token
37
+ File.exist?(CONFIG_FILE_PATH) ? YAML.load_file(CONFIG_FILE_PATH)['token'] : nil
38
+ end
39
+ end
@@ -0,0 +1,31 @@
1
+ class CircleCI::Project
2
+ attr_writer :artifacts
3
+ attr_reader :vcs_type, :organization, :name, :branch
4
+ def initialize(organization:, name:, branch:, vcs_type: 'github')
5
+ @organization = organization
6
+ @name = name
7
+ @vcs_type = vcs_type
8
+ end
9
+
10
+ def artifacts
11
+ @artifacts ||= fetch_artifacts
12
+ end
13
+
14
+ def api_path
15
+ [
16
+ 'project',
17
+ vcs_type,
18
+ organization,
19
+ name
20
+ ].join('/')
21
+ end
22
+
23
+ private
24
+
25
+ def fetch_artifacts
26
+ artifact_hashes = CircleCI::Client.instance.get "#{api_path}/latest/artifacts", filter: 'successful', branch: branch
27
+ artifact_hashes.collect { |artifact_hash|
28
+ CircleCI::Artifact.new artifact_hash
29
+ }
30
+ end
31
+ end
@@ -1,21 +1,21 @@
1
+ require 'uri'
1
2
  module MinaCircle
2
3
  module Helpers
3
- def build_url
4
- latest_build = circle_ci.builds.first
5
- artifacts = circle_ci.artifacts(latest_build).select { |artifact|
6
- artifact.filename == circle_artifact
4
+ def artifact_url
5
+ options = settings.select { |k,v|
6
+ k.to_s.start_with? artifact_source.to_s.downcase or
7
+ options_whitelist.include? k
7
8
  }
8
- raise "No artifacts found for build: #{latest_build.build_num} on #{branch}. #{latest_build.status}" if artifacts.empty?
9
- artifacts.first.url
10
- rescue Exception => e
9
+ Module.const_get(artifact_source.to_s).artifact_url(options)
10
+ rescue RuntimeError => e
11
+ puts "Unable to determine url for deployment artifact"
11
12
  puts e.message
12
- puts ""
13
- puts "Review your build at to ensure artifacts are being captured as described in https://circleci.com/docs/build-artifacts/"
14
13
  end
15
14
 
16
- def circle_ci
17
- @circle_ci ||= CircleCI.new circle_user, circle_project, branch
15
+ def options_whitelist
16
+ [
17
+ :branch
18
+ ]
18
19
  end
19
20
  end
20
21
  end
21
-
@@ -6,26 +6,26 @@ extend MinaCircle::Helpers
6
6
  #
7
7
  # ## Settings
8
8
  #
9
- # ### circle_user
9
+ # ### circleci_user
10
10
  # user name
11
11
  #
12
- # ### circle_project
12
+ # ### circleci_project
13
13
  # Name by which CircleCI knows your project
14
14
  #
15
- # ### circle_artifact
15
+ # ### circleci_artifact
16
16
  # Name CircleCI calls your build archive
17
17
  #
18
- # ### circle_explode_command
18
+ # ### circleci_explode_command
19
19
  # Command with options for decompressing the artifact archive
20
20
 
21
- namespace :circleci do
21
+ set :artifact_source, :CircleCI
22
22
 
23
+ namespace :mina_circle do
23
24
  desc 'Downloads and explodes the archive file containing the build'
24
25
  task :deploy do
25
- puts "[mina-circle] Fetching: #{circle_artifact}"
26
- queue "curl -o #{circle_artifact} #{build_url}"
27
- queue "#{circle_explode_command} #{circle_artifact}"
28
- queue "rm #{circle_artifact}"
26
+ puts "[mina-circle] Fetching: #{circleci_artifact}"
27
+ queue echo_cmd("curl -o #{circleci_artifact} #{artifact_url}")
28
+ queue echo_cmd("#{circleci_explode_command} #{circleci_artifact}")
29
+ queue echo_cmd("rm #{circleci_artifact}")
29
30
  end
30
31
  end
31
-
@@ -1,3 +1,3 @@
1
1
  module MinaCircle
2
- VERSION = '1.2.2'
2
+ VERSION = '1.3.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mina-circle
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick Simpson
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-10-05 00:00:00.000000000 Z
12
+ date: 2016-11-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mina
@@ -51,46 +51,6 @@ dependencies:
51
51
  - - ">="
52
52
  - !ruby/object:Gem::Version
53
53
  version: 10.4.0
54
- - !ruby/object:Gem::Dependency
55
- name: vcr
56
- requirement: !ruby/object:Gem::Requirement
57
- requirements:
58
- - - "~>"
59
- - !ruby/object:Gem::Version
60
- version: '3.0'
61
- - - ">="
62
- - !ruby/object:Gem::Version
63
- version: 3.0.0
64
- type: :development
65
- prerelease: false
66
- version_requirements: !ruby/object:Gem::Requirement
67
- requirements:
68
- - - "~>"
69
- - !ruby/object:Gem::Version
70
- version: '3.0'
71
- - - ">="
72
- - !ruby/object:Gem::Version
73
- version: 3.0.0
74
- - !ruby/object:Gem::Dependency
75
- name: webmock
76
- requirement: !ruby/object:Gem::Requirement
77
- requirements:
78
- - - "~>"
79
- - !ruby/object:Gem::Version
80
- version: '1.22'
81
- - - ">="
82
- - !ruby/object:Gem::Version
83
- version: 1.22.0
84
- type: :development
85
- prerelease: false
86
- version_requirements: !ruby/object:Gem::Requirement
87
- requirements:
88
- - - "~>"
89
- - !ruby/object:Gem::Version
90
- version: '1.22'
91
- - - ">="
92
- - !ruby/object:Gem::Version
93
- version: 1.22.0
94
54
  - !ruby/object:Gem::Dependency
95
55
  name: minitest
96
56
  requirement: !ruby/object:Gem::Requirement
@@ -120,7 +80,10 @@ extensions: []
120
80
  extra_rdoc_files: []
121
81
  files:
122
82
  - lib/mina-circle.rb
123
- - lib/mina-circle/circle_ci.rb
83
+ - lib/mina-circle/circle-ci/artifact.rb
84
+ - lib/mina-circle/circle-ci/circle_ci.rb
85
+ - lib/mina-circle/circle-ci/client.rb
86
+ - lib/mina-circle/circle-ci/project.rb
124
87
  - lib/mina-circle/helpers.rb
125
88
  - lib/mina-circle/tasks.rb
126
89
  - lib/mina-circle/version.rb
@@ -1,64 +0,0 @@
1
- require 'open-uri'
2
-
3
- class CircleCI
4
- BASE_URI = 'https://circleci.com/api/v1'
5
- attr_accessor :organization, :project, :branch
6
- def initialize(organization, project, branch = 'master')
7
- @organization = organization
8
- @project = project
9
- @branch = branch
10
- end
11
-
12
- def builds
13
- get("tree/#{branch}").collect { |build_hash| Build.new(build_hash) }
14
- end
15
-
16
- def artifacts(build)
17
- puts "Build: #{build.build_num}"
18
- get("#{build.build_num}/artifacts").collect { |artifact_hash| Artifact.new(artifact_hash) }
19
- rescue
20
- puts "No artifact found for build #{build.build_num} on #{@branch}"
21
- end
22
-
23
- def get(path)
24
- url = build_url path
25
- JSON.parse(open(url).read)
26
- rescue OpenURI::HTTPError
27
- []
28
- end
29
-
30
- def build_url(path)
31
- url = "#{BASE_URI}/project/#{organization}/#{project}/#{path}"
32
- url << "?circle-token=#{CircleCI.token}" if CircleCI.token
33
- url
34
- end
35
-
36
- def self.token
37
- @@token ||= get_token
38
- end
39
-
40
- def self.get_token
41
- return ENV['MINA_CIRCLE_TOKEN'] if ENV['MINA_CIRCLE_TOKEN']
42
- config_file = File.join(Dir.home, '.mina-circle.yml')
43
- File.exist?(config_file) ? YAML.load_file(config_file)['token'] : nil
44
- end
45
-
46
- class Artifact
47
- attr_reader :source_url, :filename
48
- def initialize(hash)
49
- @filename = File.basename hash['path']
50
- @source_url = hash['url']
51
- end
52
-
53
- def url
54
- CircleCI.token.nil? ? source_url : "#{source_url}?circle-token=#{CircleCI.token}"
55
- end
56
- end
57
-
58
- class Build
59
- attr_reader :build_num
60
- def initialize(hash)
61
- @build_num = hash['build_num']
62
- end
63
- end
64
- end