Class: Dpl::Providers::Releases

Inherits:
Dpl::Provider show all
Includes:
Github
Defined in:
lib/dpl/providers/releases.rb

Constant Summary collapse

URL =
'https://api.github.com/repos/%s/releases/%s'
OCTOKIT_OPTS =
%i[
  repo
  name
  body
  prerelease
  release_number
  tag_name
  target_commitish
].freeze
TIMEOUTS =
{
  timeout: 180,
  open_timeout: 180
}.freeze

Constants included from Transliterate

Transliterate::APPROXIMATIONS

Constants inherited from Dpl::Provider

Dpl::Provider::FOLDS, Dpl::Provider::STAGES

Instance Attribute Summary

Attributes inherited from Dpl::Provider

#key_name, #repo_name

Instance Method Summary collapse

Methods included from Github

#normalize_filename

Methods included from Transliterate

#transliterate

Methods inherited from Dpl::Provider

#before_finish, #before_init, #before_install, #before_prepare, #before_setup, #chmod, #cleanup, #cmd, #compact, #err, #error, #escape, examples, #expand, #finish?, #fold, #fold?, #info, #initialize, install_deps, install_deps?, #mkdir_p, move_files, #msg, #mv, #only, #open, #opt_for, #opt_key, #opts_for, #print, #quote, #read, #remove_dpl_dir, #rm_rf, #run, #run_cmd, #run_cmds, #run_stage, #run_stage?, #script, #setup_dpl_dir, #setup_git_config, #setup_git_http_user_agent, #setup_git_ssh, #setup_ssh_key, #shell, #sq, #ssh_keygen, #symbolize, #try_ssh_access, #uncleanup, unmove_files, validate_runtimes, #wait_for_ssh_access, #warn

Methods included from Dpl::Provider::Dsl

#apt, #apt?, #cmds, #description, #env, #errs, #full_name, #gem, #gem?, #keep, #move, #msgs, #needs, #needs?, #node_js, #npm, #npm?, #opt, #path, #pip, #pip?, #python, #ruby_pre?, #ruby_version, #runtimes, #status, #strs, #summary, #user_agent, #vars

Methods included from Squiggle

#sq

Methods included from Env

included, #opts

Methods included from ConfigFile

included, #opts

Methods included from Interpolate

#interpolate, #obfuscate, #vars

Methods included from Memoize

included

Constructor Details

This class inherits a constructor from Dpl::Provider

Instance Method Details

#apiObject



205
206
207
# File 'lib/dpl/providers/releases.rb', line 205

def api
  @api ||= Octokit::Client.new(**creds, auto_paginate: true, connection_options: { request: TIMEOUTS })
end

#asset(name) ⇒ Object



185
186
187
# File 'lib/dpl/providers/releases.rb', line 185

def asset(name)
  api.release_assets(url).detect { |asset| asset.name == name }
end

#content_type(file) ⇒ Object



137
138
139
140
141
# File 'lib/dpl/providers/releases.rb', line 137

def content_type(file)
  type = MIME::Types.type_for(file).first
  type ||= 'application/octet-stream'
  type.to_s
end

#create_releaseObject



158
159
160
161
162
# File 'lib/dpl/providers/releases.rb', line 158

def create_release
  api.create_release(slug, local_tag, octokit_opts.merge(draft: true))
rescue Octokit::NotFound
  error :insufficient_perm
end

#credsObject



209
210
211
# File 'lib/dpl/providers/releases.rb', line 209

def creds
  username && password ? { login: username, password: } : { access_token: token }
end

#delete(asset, file) ⇒ Object



110
111
112
113
# File 'lib/dpl/providers/releases.rb', line 110

def delete(asset, file)
  info :overwrite_existing, file
  api.delete_release_asset(asset.url)
end

#deployObject



91
92
93
94
# File 'lib/dpl/providers/releases.rb', line 91

def deploy
  upload_files
  api.update_release(url, octokit_opts)
end

#env_tagObject



168
169
170
171
# File 'lib/dpl/providers/releases.rb', line 168

def env_tag
  tag = ENV['TRAVIS_TAG']
  tag unless tag.to_s.empty?
end

#exists?(file) ⇒ Boolean

Returns:

  • (Boolean)


219
220
221
222
223
224
# File 'lib/dpl/providers/releases.rb', line 219

def exists?(file)
  return true if File.exist?(file)

  error :missing_file, file
  false
end

#file?(file) ⇒ Boolean

Returns:

  • (Boolean)


226
227
228
229
230
231
# File 'lib/dpl/providers/releases.rb', line 226

def file?(file)
  return true if File.file?(file)

  warn :not_a_file, file
  false
end

#filesObject



213
214
215
216
217
# File 'lib/dpl/providers/releases.rb', line 213

def files
  files = file_glob? ? Dir.glob("{#{file.join(',')}}").uniq : file
  files = files.select { |file| exists?(file) }
  files.select { |file| file?(file) }
end

#local_tagObject



164
165
166
# File 'lib/dpl/providers/releases.rb', line 164

def local_tag
  env_tag || git_tag
end

#loginObject



85
86
87
88
89
# File 'lib/dpl/providers/releases.rb', line 85

def 
  user.
  info :login, user.
  error :insufficient_scopes unless sufficient_scopes?
end

#octokit_optsObject



115
116
117
118
119
120
# File 'lib/dpl/providers/releases.rb', line 115

def octokit_opts
  opts = with_tag(self.opts.dup)
  opts = with_target_commitish(opts)
  opts = opts.select { |key, _| OCTOKIT_OPTS.include?(key) }
  compact(opts.merge(body: release_notes, draft: draft?))
end

#releaseObject



154
155
156
# File 'lib/dpl/providers/releases.rb', line 154

def release
  releases.detect { |release| release.tag_name == local_tag }
end

#release_notesObject



189
190
191
# File 'lib/dpl/providers/releases.rb', line 189

def release_notes
  super || release_notes_file || nil
end

#release_notes_fileObject



193
194
195
# File 'lib/dpl/providers/releases.rb', line 193

def release_notes_file
  release_notes_file? && exists?(super) && read(super)
end

#releasesObject



201
202
203
# File 'lib/dpl/providers/releases.rb', line 201

def releases
  @releases ||= api.releases(slug)
end

#same_repo?Boolean

Returns:

  • (Boolean)


181
182
183
# File 'lib/dpl/providers/releases.rb', line 181

def same_repo?
  slug == repo_slug
end

#slugObject



177
178
179
# File 'lib/dpl/providers/releases.rb', line 177

def slug
  repo || repo_slug
end

#sufficient_scopes?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/dpl/providers/releases.rb', line 173

def sufficient_scopes?
  api.scopes.include?('public_repo') || api.scopes.include?('repo')
end

#upload_file(path) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/dpl/providers/releases.rb', line 100

def upload_file(path)
  file = normalize_filename(path)
  asset = asset(file)
  return info :skip_existing, file if asset && !overwrite?

  delete(asset, file) if asset
  info :upload_file, file
  api.upload_asset(url, path, name: file, content_type: content_type(path))
end

#upload_filesObject



96
97
98
# File 'lib/dpl/providers/releases.rb', line 96

def upload_files
  files.each { |file| upload_file(file) }
end

#urlObject



143
144
145
146
147
148
149
150
151
# File 'lib/dpl/providers/releases.rb', line 143

def url
  if release_number?
    format(URL, slug, release_number)
  elsif release
    release.rels[:self].href
  else
    create_release.rels[:self].href
  end
end

#userObject



197
198
199
# File 'lib/dpl/providers/releases.rb', line 197

def user
  @user ||= api.user
end

#validateObject



79
80
81
82
83
# File 'lib/dpl/providers/releases.rb', line 79

def validate
  info :deploy
  shell :git_fetch_tags if env_tag.nil?
  info :local_tag
end

#with_tag(opts) ⇒ Object



123
124
125
126
127
128
# File 'lib/dpl/providers/releases.rb', line 123

def with_tag(opts)
  return opts if tag_name? || draft?

  info :set_tag_name, local_tag
  opts.merge(tag_name: local_tag)
end

#with_target_commitish(opts) ⇒ Object



130
131
132
133
134
135
# File 'lib/dpl/providers/releases.rb', line 130

def with_target_commitish(opts)
  return opts if target_commitish? || !same_repo?

  info :set_target_commitish, git_sha
  opts.merge(target_commitish: git_sha)
end