Class: Fastlane::Actions::DiawirebornAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/diawireborn/actions/diawireborn_action.rb

Constant Summary collapse

UPLOAD_URL =
"https://upload.diawi.com/"
STATUS_CHECK_URL =
"https://upload.diawi.com/status"
"https://i.diawi.com"

Documentation collapse

Class Method Summary collapse

Class Method Details

.authorsObject



206
207
208
# File 'lib/fastlane/plugin/diawireborn/actions/diawireborn_action.rb', line 206

def self.authors
    ["imokhles"]
end

.available_optionsObject



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
# File 'lib/fastlane/plugin/diawireborn/actions/diawireborn_action.rb', line 136

def self.available_options
    [
        FastlaneCore::ConfigItem.new(key: :token,
                                env_name: "DIAWI_TOKEN",
                             description: "API access token",
                                optional: false),
        FastlaneCore::ConfigItem.new(key: :file,
                                env_name: "DIAWI_FILE",
                             description: "Path to .ipa or .apk file. Default - `IPA_OUTPUT_PATH` or `GRADLE_APK_OUTPUT_PATH` based on platform",
                                optional: true,
                           default_value: self.default_file_path),
        FastlaneCore::ConfigItem.new(key: :find_by_udid,
                                env_name: "DIAWI_FIND_BY_UDID",
                             description: "Allow your testers to find the app on diawi's mobile web app using their UDID (iOS only)",
                               is_string: false,
                                optional: true),
        FastlaneCore::ConfigItem.new(key: :wall_of_apps,
                                env_name: "DIAWI_WALL_OF_APPS",
                             description: "Allow diawi to display the app's icon on the wall of apps",
                               is_string: false,
                                optional: true),
        FastlaneCore::ConfigItem.new(key: :password,
                                env_name: "DIAWI_PASSWORD",
                             description: "Protect your app with a password: it will be required to access the installation page",
                                optional: true),
        FastlaneCore::ConfigItem.new(key: :comment,
                                env_name: "DIAWI_COMMENT",
                             description: "Additional information to your users on this build: the comment will be displayed on the installation page",
                                optional: true),
        FastlaneCore::ConfigItem.new(key: :callback_url,
                                env_name: "DIAWI_CALLBACK_URL",
                             description: "The URL diawi should call with the result",
                                optional: true,
                            verify_block: proc do |value|
                              UI.user_error!("The `callback_url` not valid.") if value =~ URI::regexp
                            end),
        FastlaneCore::ConfigItem.new(key: :callback_emails,
                                env_name: "DIAWI_CALLBACK_EMAILS",
                             description: "The email addresses diawi will send the result to (up to 5 separated by commas for starter/premium/enterprise accounts, 1 for free accounts). Emails should be a string. Ex: \"[email protected],[email protected]\"",
                                optional: true),
        FastlaneCore::ConfigItem.new(key: :installation_notifications,
                                env_name: "DIAWI_INSTALLATION_NOTIFICATIONS",
                             description: "Receive notifications each time someone installs the app (only starter/premium/enterprise accounts)",
                               is_string: false,
                                optional: true),
        FastlaneCore::ConfigItem.new(key: :timeout,
                                env_name: "DIAWI_TIMEOUT",
                             description: "Timeout for checking upload status in seconds. Default: 60, range: (5, 1800)",
                               is_string: false,
                                optional: true,
                           default_value: 60),
        FastlaneCore::ConfigItem.new(key: :check_status_delay,
                                env_name: "DIAWI_CHECK_STATUS_DELAY",
                             description: "Number of seconds to wait between repeated attempts at checking upload status. Default: 3, range: (1, 30)",
                               is_string: false,
                                optional: true,
                           default_value: 3)
    ]
end

.check_status(token, file, job, timeout, check_status_delay) ⇒ Object



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
# File 'lib/fastlane/plugin/diawireborn/actions/diawireborn_action.rb', line 77

def self.check_status(token, file, job, timeout, check_status_delay)
    status_ok = 2000
    status_in_progress = 2001
    status_error = 4000

    timeout_time = Time.now + timeout
    current_time = Time.now

    while timeout_time > current_time  do
        response = RestClient.get STATUS_CHECK_URL, {params: {token: token, job: job}}

        begin
            response
        rescue RestClient::ExceptionWithResponse => error
            UI.important("Check file status request error:")
            UI.important(error)
            sleep(check_status_delay)
            current_time = Time.now
            next
        end

        json = JSON.parse(response.body)

        case json['status']
        when status_ok
            link = "#{DIAWI_FILE_LINK}/#{json['hash']}"
            UI.success("File successfully uploaded to diawi. Link: #{link}")
            Actions.lane_context[SharedValues::UPLOADED_FILE_LINK_TO_DIAWI] = link
            return

        when status_in_progress
            UI.message("Processing file...")

        when status_error
            UI.important("Error uploading file to diawi.")
            UI.important(json['message'])
        else
            UI.important("Unknown error uploading file to diawi.")
        end

        sleep(check_status_delay)
        current_time = Time.now
    end

    UI.important("File is not processed.")
    UI.important("Try to upload file by yourself: #{file}")
end

.default_file_pathObject



125
126
127
128
129
130
# File 'lib/fastlane/plugin/diawireborn/actions/diawireborn_action.rb', line 125

def self.default_file_path
    platform = Actions.lane_context[SharedValues::PLATFORM_NAME]
    ios_path = Actions.lane_context[SharedValues::IPA_OUTPUT_PATH]
    android_path = Actions.lane_context[SharedValues::GRADLE_APK_OUTPUT_PATH]
    return platform == :ios ? ios_path : android_path 
end

.descriptionObject



202
203
204
# File 'lib/fastlane/plugin/diawireborn/actions/diawireborn_action.rb', line 202

def self.description
    "Upload .ipa/.apk file to diawi.com"
end

.detailsObject



210
211
212
# File 'lib/fastlane/plugin/diawireborn/actions/diawireborn_action.rb', line 210

def self.details
    "This action upload .ipa/.apk file to https://www.diawi.com and return link to uploaded file."
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


214
215
216
# File 'lib/fastlane/plugin/diawireborn/actions/diawireborn_action.rb', line 214

def self.is_supported?(platform)
    [:ios, :android].include?(platform)
end

.outputObject



196
197
198
199
200
# File 'lib/fastlane/plugin/diawireborn/actions/diawireborn_action.rb', line 196

def self.output
    [
        ['UPLOADED_FILE_LINK_TO_DIAWI', 'URL to uploaded .ipa or .apk file to diawi.']
    ]
end

.run(options) ⇒ Object



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
# File 'lib/fastlane/plugin/diawireborn/actions/diawireborn_action.rb', line 16

def self.run(options)
    Actions.verify_gem!('rest-client')
    require 'rest-client'
    require 'json'

    if options[:file].nil?
        UI.important("File didn't come to diawi_plugin. Uploading is unavailable.")
        return
    end

    if options[:token].nil?
        UI.important("Diawi token is nil - uploading is unavailable.")
        UI.important("Try to upload file by yourself. Path: #{options[:file]}")
        return
    end

    upload_options = options.values.select do |key, value|
        [:password, :comment, :callback_url, :callback_emails].include? key unless value.nil?
    end

    options.values.each do |key, value|
        if [:find_by_udid, :wall_of_apps, :installation_notifications].include? key
            upload_options[key] = value ? 1 : 0 unless value.nil?
        end
    end

    upload_options[:token] = options[:token]
    upload_options[:file] = File.new(options[:file], 'rb')

    UI.success("Start uploading file to diawi. Please, be patient. This could take some time.")

    response = RestClient.post(UPLOAD_URL, upload_options)

    begin
        response
    rescue RestClient::ExceptionWithResponse => error
        UI.important("Faild to upload file to diawi, because of:")
        UI.important(error)
        UI.important("Try to upload file by yourself. Path: #{options[:file]}")
        return
    end

    job = JSON.parse(response.body)['job']
    
    if job
        timeout = options[:timeout].clamp(5, 1800)
        check_status_delay = options[:check_status_delay].clamp(1, 30)

        if check_status_delay > timeout
            UI.important("`check_status_delay` is greater than `timeout`")
        end

        UI.success("Upload completed successfully.")
        UI.success("\n\nProcessing started with params:\n\njob: #{job}\ntimeout: #{timeout},\ncheck_status_delay: #{check_status_delay}\n")
        return self.check_status(options[:token], options[:file], job, options[:timeout], options[:check_status_delay])
    end

    UI.important("Something went wrong and `job` value didn't come from uploading request. Check out your dashboard: https://dashboard.diawi.com/. Maybe your file already has been uploaded successfully.")
    UI.important("If not, try to upload file by yourself. Path: #{options[:file]}")
end