Class: DPL::Provider::Releases
- Inherits:
-
DPL::Provider
- Object
- DPL::Provider
- DPL::Provider::Releases
- Defined in:
- lib/dpl/provider/releases.rb
Constant Summary collapse
- BOOLEAN_PARAMS =
%w( draft prerelease )
Class Method Summary collapse
Instance Method Summary collapse
- #api ⇒ Object
- #booleanize!(opts) ⇒ Object
- #check_app ⇒ Object
- #check_auth ⇒ Object
- #files ⇒ Object
- #get_tag ⇒ Object
- #needs_key? ⇒ Boolean
- #push_app ⇒ Object
- #releases ⇒ Object
- #same_repo? ⇒ Boolean
- #setup_auth ⇒ Object
- #slug ⇒ Object
- #travis_tag ⇒ Object
- #upload_file(file, filename, release_url) ⇒ Object
- #user ⇒ Object
Class Method Details
.new(context, options) ⇒ Object
14 15 16 |
# File 'lib/dpl/provider/releases.rb', line 14 def self.new(context, ) super(context, .merge!({needs_git_http_user_agent: false})) end |
Instance Method Details
#api ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/dpl/provider/releases.rb', line 35 def api = { :request => { :timeout => 180, :open_timeout => 180 } } if ([:username] || [:user]) and [:password] @api ||= Octokit::Client.new(:login => [:username] || [:user], :password => [:password], :auto_paginate => true, :connection_options => ) else @api ||= Octokit::Client.new(:access_token => option(:api_key, :token), :auto_paginate => true, :connection_options => ) end end |
#booleanize!(opts) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/dpl/provider/releases.rb', line 170 def booleanize!(opts) opts.map do |k,v| opts[k] = if BOOLEAN_PARAMS.include?(k.to_s.squeeze.downcase) case v.to_s.downcase when 'true' true when 'false' false else v end else v end end end |
#check_app ⇒ Object
75 76 77 78 79 80 |
# File 'lib/dpl/provider/releases.rb', line 75 def check_app log "Deploying to repo: #{slug}" context.shell 'git fetch --tags' if travis_tag.nil? log "Current tag is: #{get_tag}" end |
#check_auth ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/dpl/provider/releases.rb', line 86 def check_auth setup_auth unless api.scopes.include? 'public_repo' or api.scopes.include? 'repo' raise Error, "Dpl does not have permission to upload assets. Make sure your token contains the repo or public_repo scope." end log "Logged in as #{user.name}" end |
#files ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/dpl/provider/releases.rb', line 61 def files if [:file_glob] Array([:file]).map do |glob| Dir.glob(glob) end.flatten else Array([:file]) end end |
#get_tag ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/dpl/provider/releases.rb', line 27 def get_tag if travis_tag.nil? @tag ||= `git describe --tags --exact-match 2>/dev/null`.chomp else @tag ||= travis_tag end end |
#needs_key? ⇒ Boolean
71 72 73 |
# File 'lib/dpl/provider/releases.rb', line 71 def needs_key? false end |
#push_app ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/dpl/provider/releases.rb', line 96 def push_app tag_matched = false release_url = nil booleanize!() if [:release_number] tag_matched = true release_url = "https://api.github.com/repos/" + slug + "/releases/" + [:release_number] else releases.each do |release| if release.tag_name == get_tag release_url = release.rels[:self].href tag_matched = true end end end #If for some reason GitHub hasn't already created a release for the tag, create one if tag_matched == false release_url = api.create_release(slug, get_tag, .merge({:draft => true})).rels[:self].href end files.each do |file| unless File.exist?(file) log "#{file} does not exist." next end unless File.file?(file) log "#{file} is not a regular file. Skipping." next end existing_url = nil filename = Pathname.new(file).basename.to_s api.release_assets(release_url).each do |existing_file| if existing_file.name == filename existing_url = existing_file.url end end if !existing_url upload_file(file, filename, release_url) elsif existing_url && [:overwrite] log "#{filename} already exists, overwriting." api.delete_release_asset(existing_url) upload_file(file, filename, release_url) else log "#{filename} already exists, skipping." end end if ! .key?(:tag_name) && [:draft] [:tag_name] = get_tag.tap {|tag| log "Setting tag_name to #{tag}"} end if same_repo? && !.key?(:target_commitish) [:target_commitish] = sha.tap {|commitish| log "Setting target_commitish to #{commitish}"} end api.update_release(release_url, {:draft => false}.merge()) end |
#releases ⇒ Object
53 54 55 |
# File 'lib/dpl/provider/releases.rb', line 53 def releases @releases ||= api.releases(slug) end |
#same_repo? ⇒ Boolean
166 167 168 |
# File 'lib/dpl/provider/releases.rb', line 166 def same_repo? slug == context.env['TRAVIS_REPO_SLUG'] end |
#setup_auth ⇒ Object
82 83 84 |
# File 'lib/dpl/provider/releases.rb', line 82 def setup_auth user.login end |
#slug ⇒ Object
49 50 51 |
# File 'lib/dpl/provider/releases.rb', line 49 def slug .fetch(:repo) { context.env['TRAVIS_REPO_SLUG'] } end |
#travis_tag ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/dpl/provider/releases.rb', line 18 def travis_tag # Check if $TRAVIS_TAG is unset or set but empty if context.env.fetch('TRAVIS_TAG','') == '' nil else context.env['TRAVIS_TAG'] end end |
#upload_file(file, filename, release_url) ⇒ Object
157 158 159 160 161 162 163 164 |
# File 'lib/dpl/provider/releases.rb', line 157 def upload_file(file, filename, release_url) content_type = MIME::Types.type_for(file).first.to_s if content_type.empty? # Specify the default content type, as it is required by GitHub content_type = "application/octet-stream" end api.upload_asset(release_url, file, {:name => filename, :content_type => content_type}) end |
#user ⇒ Object
57 58 59 |
# File 'lib/dpl/provider/releases.rb', line 57 def user @user ||= api.user end |