-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
deploygate.rb
209 lines (187 loc) · 9.34 KB
/
deploygate.rb
1
2
3
4
5
6
7
8
9
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
95
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
156
157
158
159
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
module Fastlane
module Actions
module SharedValues
DEPLOYGATE_URL = :DEPLOYGATE_URL
DEPLOYGATE_REVISION = :DEPLOYGATE_REVISION # auto increment revision number
DEPLOYGATE_APP_INFO = :DEPLOYGATE_APP_INFO # contains app revision, bundle identifier, etc.
end
class DeploygateAction < Action
DEPLOYGATE_URL_BASE = 'https://deploygate.com'
def self.is_supported?(platform)
[:ios, :android].include?(platform)
end
def self.upload_build(api_token, user_name, binary, options)
require 'faraday'
require 'faraday_middleware'
connection = Faraday.new(url: DEPLOYGATE_URL_BASE, request: { timeout: 120 }) do |builder|
builder.request(:multipart)
builder.request(:json)
builder.response(:json, content_type: /\bjson$/)
builder.use(FaradayMiddleware::FollowRedirects)
builder.adapter(:net_http)
end
options.update({
token: api_token,
file: Faraday::UploadIO.new(binary, 'application/octet-stream'),
message: options[:message] || ''
})
options[:disable_notify] = 'yes' if options[:disable_notify]
connection.post("/api/users/#{user_name}/apps", options)
rescue Faraday::TimeoutError
UI.crash!("Timed out while uploading build. Check https://deploygate.com/ to see if the upload was completed.")
end
def self.run(options)
# Available options: https://deploygate.com/docs/api
UI.success('Starting with app upload to DeployGate... this could take some time ⏳')
api_token = options[:api_token]
user_name = options[:user]
binary = options[:ipa] || options[:apk]
upload_options = options.values.select do |key, _|
[:message, :distribution_key, :release_note, :disable_notify, :distribution_name].include?(key)
end
UI.user_error!('missing `ipa` and `apk`. deploygate action needs least one.') unless binary
return binary if Helper.test?
response = self.upload_build(api_token, user_name, binary, upload_options)
if parse_response(response)
UI.message("DeployGate URL: #{Actions.lane_context[SharedValues::DEPLOYGATE_URL]}")
UI.success("Build successfully uploaded to DeployGate as revision \##{Actions.lane_context[SharedValues::DEPLOYGATE_REVISION]}!")
else
UI.user_error!("Error when trying to upload app to DeployGate")
end
end
def self.parse_response(response)
if response.body && response.body.key?('error')
if response.body['error']
UI.error("Error uploading to DeployGate: #{response.body['message']}")
help_message(response)
return
else
res = response.body['results']
url = DEPLOYGATE_URL_BASE + res['path']
Actions.lane_context[SharedValues::DEPLOYGATE_URL] = url
Actions.lane_context[SharedValues::DEPLOYGATE_REVISION] = res['revision']
Actions.lane_context[SharedValues::DEPLOYGATE_APP_INFO] = res
end
else
UI.error("Error uploading to DeployGate: #{response.body}")
return
end
true
end
private_class_method :parse_response
def self.help_message(response)
message =
case response.body['message']
when 'you are not authenticated'
'Invalid API Token specified.'
when 'application create error: permit'
'Access denied: May be trying to upload to wrong user or updating app you join as a tester?'
when 'application create error: limit'
'Plan limit: You have reached to the limit of current plan or your plan was expired.'
end
UI.error(message) if message
end
private_class_method :help_message
def self.description
"Upload a new build to [DeployGate](https://deploygate.com/)"
end
def self.details
[
"You can retrieve your username and API token on [your settings page](https://deploygate.com/settings).",
"More information about the available options can be found in the [DeployGate Push API document](https://deploygate.com/docs/api)."
].join("\n")
end
def self.available_options
[
FastlaneCore::ConfigItem.new(key: :api_token,
env_name: "DEPLOYGATE_API_TOKEN",
description: "Deploygate API Token",
sensitive: true,
verify_block: proc do |value|
UI.user_error!("No API Token for DeployGate given, pass using `api_token: 'token'`") unless value.to_s.length > 0
end),
FastlaneCore::ConfigItem.new(key: :user,
env_name: "DEPLOYGATE_USER",
description: "Target username or organization name",
verify_block: proc do |value|
UI.user_error!("No User for DeployGate given, pass using `user: 'user'`") unless value.to_s.length > 0
end),
FastlaneCore::ConfigItem.new(key: :ipa,
env_name: "DEPLOYGATE_IPA_PATH",
description: "Path to your IPA file. Optional if you use the _gym_ or _xcodebuild_ action",
default_value: Actions.lane_context[SharedValues::IPA_OUTPUT_PATH],
default_value_dynamic: true,
optional: true,
verify_block: proc do |value|
UI.user_error!("Couldn't find ipa file at path '#{value}'") unless File.exist?(value)
end),
FastlaneCore::ConfigItem.new(key: :apk,
env_name: "DEPLOYGATE_APK_PATH",
description: "Path to your APK file",
default_value: Actions.lane_context[SharedValues::GRADLE_APK_OUTPUT_PATH],
default_value_dynamic: true,
optional: true,
verify_block: proc do |value|
UI.user_error!("Couldn't find apk file at path '#{value}'") unless File.exist?(value)
end),
FastlaneCore::ConfigItem.new(key: :message,
env_name: "DEPLOYGATE_MESSAGE",
description: "Release Notes",
default_value: "No changelog provided"),
FastlaneCore::ConfigItem.new(key: :distribution_key,
optional: true,
env_name: "DEPLOYGATE_DISTRIBUTION_KEY",
sensitive: true,
description: "Target Distribution Key"),
FastlaneCore::ConfigItem.new(key: :release_note,
optional: true,
env_name: "DEPLOYGATE_RELEASE_NOTE",
description: "Release note for distribution page"),
FastlaneCore::ConfigItem.new(key: :disable_notify,
optional: true,
type: Boolean,
default_value: false,
env_name: "DEPLOYGATE_DISABLE_NOTIFY",
description: "Disables Push notification emails"),
FastlaneCore::ConfigItem.new(key: :distribution_name,
optional: true,
env_name: "DEPLOYGATE_DISTRIBUTION_NAME",
description: "Target Distribution Name")
]
end
def self.output
[
['DEPLOYGATE_URL', 'URL of the newly uploaded build'],
['DEPLOYGATE_REVISION', 'auto incremented revision number'],
['DEPLOYGATE_APP_INFO', 'Contains app revision, bundle identifier, etc.']
]
end
def self.example_code
[
'deploygate(
api_token: "...",
user: "target username or organization name",
ipa: "./ipa_file.ipa",
message: "Build #{lane_context[SharedValues::BUILD_NUMBER]}",
distribution_key: "(Optional) Target Distribution Key",
distribution_name: "(Optional) Target Distribution Name"
)',
'deploygate(
api_token: "...",
user: "target username or organization name",
apk: "./apk_file.apk",
message: "Build #{lane_context[SharedValues::BUILD_NUMBER]}",
distribution_key: "(Optional) Target Distribution Key",
distribution_name: "(Optional) Target Distribution Name"
)'
]
end
def self.category
:beta
end
def self.authors
["tnj", "tomorrowkey"]
end
end
end
end