Class: Fastlane::Actions::SetGithubReleaseAction

Inherits:
Fastlane::Action show all
Defined in:
fastlane/lib/fastlane/actions/set_github_release.rb

Constant Summary

Constants inherited from Fastlane::Action

Fastlane::Action::AVAILABLE_CATEGORIES, Fastlane::Action::RETURN_TYPES

Documentation collapse

Class Method Summary collapse

Methods inherited from Fastlane::Action

action_name, author, deprecated_notes, lane_context, method_missing, other_action, sample_return_value, shell_out_should_use_bundle_exec?, step_text

Class Method Details

.authorsObject



260
261
262
# File 'fastlane/lib/fastlane/actions/set_github_release.rb', line 260

def self.authors
  ["czechboy0", "tommeier"]
end

.available_optionsObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'fastlane/lib/fastlane/actions/set_github_release.rb', line 160

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :repository_name,
                                 env_name: "FL_SET_GITHUB_RELEASE_REPOSITORY_NAME",
                                 description: "The path to your repo, e.g. 'fastlane/fastlane'",
                                 verify_block: proc do |value|
                                   UI.user_error!("Please only pass the path, e.g. 'fastlane/fastlane'") if value.include?("github.com")
                                   UI.user_error!("Please only pass the path, e.g. 'fastlane/fastlane'") if value.split('/').count != 2
                                 end),
    FastlaneCore::ConfigItem.new(key: :server_url,
                                 env_name: "FL_GITHUB_RELEASE_SERVER_URL",
                                 description: "The server url. e.g. 'https://your.internal.github.host/api/v3' (Default: 'https://api.github.com')",
                                 default_value: "https://api.github.com",
                                 optional: true,
                                 verify_block: proc do |value|
                                   UI.user_error!("Please include the protocol in the server url, e.g. https://your.github.server/api/v3") unless value.include?("//")
                                 end),
    FastlaneCore::ConfigItem.new(key: :api_token,
                                 env_name: "FL_GITHUB_RELEASE_API_TOKEN",
                                 description: "Personal API Token for GitHub - generate one at https://github.com/settings/tokens",
                                 conflicting_options: [:api_bearer],
                                 sensitive: true,
                                 code_gen_sensitive: true,
                                 is_string: true,
                                 default_value: ENV["GITHUB_API_TOKEN"],
                                 default_value_dynamic: true,
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :api_bearer,
                                 env_name: "FL_GITHUB_RELEASE_API_BEARER",
                                 sensitive: true,
                                 code_gen_sensitive: true,
                                 description: "Use a Bearer authorization token. Usually generated by Github Apps, e.g. GitHub Actions GITHUB_TOKEN environment variable",
                                 conflicting_options: [:api_token],
                                 optional: true,
                                 default_value: nil),
    FastlaneCore::ConfigItem.new(key: :tag_name,
                                 env_name: "FL_SET_GITHUB_RELEASE_TAG_NAME",
                                 description: "Pass in the tag name",
                                 is_string: true,
                                 optional: false),
    FastlaneCore::ConfigItem.new(key: :name,
                                 env_name: "FL_SET_GITHUB_RELEASE_NAME",
                                 description: "Name of this release",
                                 is_string: true,
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :commitish,
                                 env_name: "FL_SET_GITHUB_RELEASE_COMMITISH",
                                 description: "Specifies the commitish value that determines where the Git tag is created from. Can be any branch or commit SHA. Unused if the Git tag already exists. Default: the repository's default branch (usually master)",
                                 is_string: true,
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :description,
                                 env_name: "FL_SET_GITHUB_RELEASE_DESCRIPTION",
                                 description: "Description of this release",
                                 is_string: true,
                                 optional: true,
                                 default_value: Actions.lane_context[SharedValues::FL_CHANGELOG],
                                 default_value_dynamic: true),
    FastlaneCore::ConfigItem.new(key: :is_draft,
                                 env_name: "FL_SET_GITHUB_RELEASE_IS_DRAFT",
                                 description: "Whether the release should be marked as draft",
                                 optional: true,
                                 default_value: false,
                                 is_string: false),
    FastlaneCore::ConfigItem.new(key: :is_prerelease,
                                 env_name: "FL_SET_GITHUB_RELEASE_IS_PRERELEASE",
                                 description: "Whether the release should be marked as prerelease",
                                 optional: true,
                                 default_value: false,
                                 is_string: false),
    FastlaneCore::ConfigItem.new(key: :upload_assets,
                                 env_name: "FL_SET_GITHUB_RELEASE_UPLOAD_ASSETS",
                                 description: "Path to assets to be uploaded with the release",
                                 optional: true,
                                 is_string: false,
                                 type: Array,
                                 verify_block: proc do |value|
                                   UI.user_error!("upload_assets must be an Array of paths to assets") unless value.kind_of?(Array)
                                 end)
  ]
end

.categoryObject



282
283
284
# File 'fastlane/lib/fastlane/actions/set_github_release.rb', line 282

def self.category
  :source_control
end

.descriptionObject



148
149
150
# File 'fastlane/lib/fastlane/actions/set_github_release.rb', line 148

def self.description
  "This will create a new release on GitHub and upload assets for it"
end

.detailsObject



152
153
154
155
156
157
158
# File 'fastlane/lib/fastlane/actions/set_github_release.rb', line 152

def self.details
  [
    "Creates a new release on GitHub. You must provide your GitHub Personal token (get one from [https://github.com/settings/tokens/new](https://github.com/settings/tokens/new)), the repository name and tag name. By default, that's `master`.",
    "If the tag doesn't exist, one will be created on the commit or branch passed in as commitish.",
    "Out parameters provide the release's id, which can be used for later editing and the release HTML link to GitHub. You can also specify a list of assets to be uploaded to the release with the `:upload_assets` parameter."
  ].join("\n")
end

.example_codeObject



268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'fastlane/lib/fastlane/actions/set_github_release.rb', line 268

def self.example_code
  [
    'github_release = set_github_release(
      repository_name: "fastlane/fastlane",
      api_token: ENV["GITHUB_TOKEN"],
      name: "Super New actions",
      tag_name: "v1.22.0",
      description: (File.read("changelog") rescue "No changelog provided"),
      commitish: "master",
      upload_assets: ["example_integration.ipa", "./pkg/built.gem"]
    )'
  ]
end

.is_supported?(platform) ⇒ Boolean

Returns:



264
265
266
# File 'fastlane/lib/fastlane/actions/set_github_release.rb', line 264

def self.is_supported?(platform)
  true
end

.outputObject



241
242
243
244
245
246
247
# File 'fastlane/lib/fastlane/actions/set_github_release.rb', line 241

def self.output
  [
    ['SET_GITHUB_RELEASE_HTML_LINK', 'Link to your created release'],
    ['SET_GITHUB_RELEASE_RELEASE_ID', 'Release id (useful for subsequent editing)'],
    ['SET_GITHUB_RELEASE_JSON', 'The whole release JSON object']
  ]
end

.return_typeObject



256
257
258
# File 'fastlane/lib/fastlane/actions/set_github_release.rb', line 256

def self.return_type
  :hash
end

.return_valueObject



249
250
251
252
253
254
# File 'fastlane/lib/fastlane/actions/set_github_release.rb', line 249

def self.return_value
  [
    "A hash containing all relevant information of this release",
    "Access things like 'html_url', 'tag_name', 'name', 'body'"
  ].join("\n")
end

.run(params) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'fastlane/lib/fastlane/actions/set_github_release.rb', line 10

def self.run(params)
  UI.important("Creating release of #{params[:repository_name]} on tag \"#{params[:tag_name]}\" with name \"#{params[:name]}\".")
  UI.important("Will also upload assets #{params[:upload_assets]}.") if params[:upload_assets]

  repo_name = params[:repository_name]
  api_token = params[:api_token]
  api_bearer = params[:api_bearer]
  server_url = params[:server_url]
  tag_name = params[:tag_name]

  payload = {
    'tag_name' => params[:tag_name],
    'name' => params[:name],
    'body' => params[:description],
    'draft' => !!params[:is_draft],
    'prerelease' => !!params[:is_prerelease]
  }
  payload['target_commitish'] = params[:commitish] if params[:commitish]

  GithubApiAction.run(
    server_url: server_url,
    api_token: api_token,
    api_bearer: api_bearer,
    http_method: 'POST',
    path: "repos/#{repo_name}/releases",
    body: payload,
    error_handlers: {
      422 => proc do |result|
        UI.error(result[:body])
        UI.error("Release on tag #{tag_name} already exists!")
        return nil
      end,
      404 => proc do |result|
        UI.error(result[:body])
        UI.user_error!("Repository #{repo_name} cannot be found, please double check its name and that you provided a valid API token (GITHUB_API_TOKEN)")
      end,
      401 => proc do |result|
        UI.error(result[:body])
        UI.user_error!("You are not authorized to access #{repo_name}, please make sure you provided a valid API token (GITHUB_API_TOKEN)")
      end,
      '*' => proc do |result|
        UI.error("GitHub responded with #{result[:status]}:#{result[:body]}")
        return nil
      end
    }
  ) do |result|
    json = result[:json]
    html_url = json['html_url']
    release_id = json['id']

    UI.success("Successfully created release at tag \"#{tag_name}\" on GitHub")
    UI.important("See release at \"#{html_url}\"")

    Actions.lane_context[SharedValues::SET_GITHUB_RELEASE_HTML_LINK] = html_url
    Actions.lane_context[SharedValues::SET_GITHUB_RELEASE_RELEASE_ID] = release_id
    Actions.lane_context[SharedValues::SET_GITHUB_RELEASE_JSON] = json

    assets = params[:upload_assets]
    if assets && assets.count > 0
      # upload assets
      self.upload_assets(assets, json['upload_url'], api_token, api_bearer)

      # fetch the release again, so that it contains the uploaded assets
      GithubApiAction.run(
        server_url: server_url,
        api_token: api_token,
        api_bearer: api_bearer,
        http_method: 'GET',
        path: "repos/#{repo_name}/releases/#{release_id}",
        error_handlers: {
          '*' => proc do |get_result|
            UI.error("GitHub responded with #{get_result[:status]}:#{get_result[:body]}")
            UI.user_error!("Failed to fetch the newly created release, but it *has been created* successfully.")
          end
        }
      ) do |get_result|
        Actions.lane_context[SharedValues::SET_GITHUB_RELEASE_JSON] = get_result[:json]
        UI.success("Successfully uploaded assets #{assets} to release \"#{html_url}\"")
        return get_result[:json]
      end
    else
      return json || result[:body]
    end
  end
end

.upload(asset_path, upload_url_template, api_token, api_bearer) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'fastlane/lib/fastlane/actions/set_github_release.rb', line 102

def self.upload(asset_path, upload_url_template, api_token, api_bearer)
  # if it's a directory, zip it first in a temp directory, because we can only upload binary files
  absolute_path = File.absolute_path(asset_path)

  # check that the asset even exists
  UI.user_error!("Asset #{absolute_path} doesn't exist") unless File.exist?(absolute_path)

  if File.directory?(absolute_path)
    Dir.mktmpdir do |dir|
      tmpzip = File.join(dir, File.basename(absolute_path) + '.zip')
      sh("cd \"#{File.dirname(absolute_path)}\"; zip -r --symlinks \"#{tmpzip}\" \"#{File.basename(absolute_path)}\" 2>&1 >/dev/null")
      self.upload_file(tmpzip, upload_url_template, api_token, api_bearer)
    end
  else
    self.upload_file(absolute_path, upload_url_template, api_token, api_bearer)
  end
end

.upload_assets(assets, upload_url_template, api_token, api_bearer) ⇒ Object



96
97
98
99
100
# File 'fastlane/lib/fastlane/actions/set_github_release.rb', line 96

def self.upload_assets(assets, upload_url_template, api_token, api_bearer)
  assets.each do |asset|
    self.upload(asset, upload_url_template, api_token, api_bearer)
  end
end

.upload_file(file, url_template, api_token, api_bearer) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'fastlane/lib/fastlane/actions/set_github_release.rb', line 120

def self.upload_file(file, url_template, api_token, api_bearer)
  require 'addressable/template'
  file_name = File.basename(file)
  expanded_url = Addressable::Template.new(url_template).expand(name: file_name).to_s
  headers = { 'Content-Type' => 'application/zip' } # works for all binary files
  UI.important("Uploading #{file_name}")
  GithubApiAction.run(
    api_token: api_token,
    api_bearer: api_bearer,
    http_method: 'POST',
    headers: headers,
    url: expanded_url,
    raw_body: File.read(file),
    error_handlers: {
      '*' => proc do |result|
        UI.error("GitHub responded with #{result[:status]}:#{result[:body]}")
        UI.user_error!("Failed to upload asset #{file_name} to GitHub.")
      end
    }
  ) do |result|
    UI.success("Successfully uploaded #{file_name}.")
  end
end