Class: Fastlane::Actions::AndroidReporterAction

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

Class Method Summary collapse

Class Method Details

.authorsObject



283
284
285
# File 'lib/fastlane/plugin/android_reporter/actions/android_reporter_action.rb', line 283

def self.authors
  ["Yazan Tarifi"]
end

.available_optionsObject



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/fastlane/plugin/android_reporter/actions/android_reporter_action.rb', line 296

def self.available_options
  [
    FastlaneCore::ConfigItem.new(
        key: :avd_name,
        env_name: "ANDROID_REPORTER_AVD_NAME",
        description: "Add Android Emulator Name to Start it before Execute the Commands",
        optional: true,
        type: String,
        sensitive: true
    ),
    FastlaneCore::ConfigItem.new(
        key: :android_sdk_path,
        env_name: "ANDROID_REPORTER_ANDROID_SDK",
        description: "Add Android SDK Full Path",
        optional: true,
        type: String,
        sensitive: true
    ),
    FastlaneCore::ConfigItem.new(
        key: :gradle_task_testing_name,
        env_name: "ANDROID_TESTING_GRADLE_TASK",
        description: "Add Android UI Testing Task Name",
        optional: true,
        type: String,
        sensitive: true
    ),
    FastlaneCore::ConfigItem.new(
        key: :generate_adb_file,
        env_name: "ANDROID_REPORTER_ADB_REPORT FILE",
        description: "This Option will Generate Adb Logcat Report File",
        optional: true,
        type: Boolean
    ),
    FastlaneCore::ConfigItem.new(
        key: :generate_gradle_files,
        env_name: "ANDROID_REPORTER_GRADLE_REPORT FILE",
        description: "This Option will Generate Gradle Report File (Success File, Error File)",
        optional: true,
        type: Boolean
    ),
    FastlaneCore::ConfigItem.new(
        key: :is_slack_upload_reports_enabled,
        env_name: "ANDROID_REPORT_FILES_SLACK_UPLOAD_ENABLED",
        description: "This Option will Check if Slack Upload Files Enabled or Not",
        optional: true,
        type: Boolean
    ),
    FastlaneCore::ConfigItem.new(
         key: :is_delete_prev_files_enabled,
         env_name: "ANDROID_GENERATED_PREV_FILES_ENABLED",
         description: "",
         optional: true,
         type: Boolean
    ),
    FastlaneCore::ConfigItem.new(
        key: :slack_api_key,
        env_name: "SLACK_API_TOKEN",
        description: "Add Slack Api Key",
        optional: true,
        type: String,
        sensitive: true
    ),
    FastlaneCore::ConfigItem.new(
        key: :slack_channel_name,
        env_name: "SLACK_CHANNEL_NAME",
        description: "Add Slack Channel Name for Bot to Send Messages on this Channel",
        optional: true,
        type: String,
        sensitive: true
    ),
    FastlaneCore::ConfigItem.new(
        key: :tests_branch_name,
        env_name: "TESTS_BRANCH_NAME",
        description: "Add Branch TO Run Tests on Target Git Branch",
        optional: true,
        type: String,
        sensitive: true
    )
  ]
end

.descriptionObject



279
280
281
# File 'lib/fastlane/plugin/android_reporter/actions/android_reporter_action.rb', line 279

def self.description
  "Open Android Emulators Then Generate Report File From Tests then Upload the File To Slack"
end

.detailsObject



291
292
293
294
# File 'lib/fastlane/plugin/android_reporter/actions/android_reporter_action.rb', line 291

def self.details
  # Optional:
  "This Plugin Will Run Android Emulators from a List of Avd Names then Generate Gradle, Tests Report Files and Upload Them To Slack Channel"
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


377
378
379
380
381
382
383
# File 'lib/fastlane/plugin/android_reporter/actions/android_reporter_action.rb', line 377

def self.is_supported?(platform)
  # Adjust this if your plugin only works for a particular platform (iOS vs. Android, for example)
  # See: https://docs.fastlane.tools/advanced/#control-configuration-by-lane-and-by-platform
  #
  [:android].include?(platform)
  true
end

.return_valueObject



287
288
289
# File 'lib/fastlane/plugin/android_reporter/actions/android_reporter_action.rb', line 287

def self.return_value
  # If your method provides a return value, you can describe here what it does
end

.run(params) ⇒ Object



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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/fastlane/plugin/android_reporter/actions/android_reporter_action.rb', line 11

def self.run(params)
  UI.message("The Android Reporter Plugin Working !!")

  # Create Variables
  avd_name = params[:avd_name]
  android_sdk_path = params[:android_sdk_path]
  generate_adb_file = params[:generate_adb_file]
  generate_gradle_files = params[:generate_gradle_files]
  gradle_task_testing_name = params[:gradle_task_testing_name]
  is_slack_enabled = params[:is_slack_upload_reports_enabled]
  slack_api_key = params[:slack_api_key]
  time = Time.now.to_s
  time = DateTime.parse(time).strftime("%d/%m/%Y %H:%M")
  isLogcatFileUploaded = false
  isGradleDebugFileUploaded = false
  isGradleErrorFileUploaded = false

  # 1. Delete Previus Generated Files
  if params[:is_delete_prev_files_enabled]
      begin
        File.open('logcat.txt', 'r') do |f|
          File.delete(f)
        end
      rescue Errno::ENOENT
          UI.message("Something Wrong When Delete Previus File [logcat.txt]")
      end

      begin
        File.open('android-logs.txt', 'r') do |f|
            File.delete(f)
        end
      rescue Errno::ENOENT
          UI.message("Something Wrong When Delete Previus File [android-logs.txt]")
      end

      begin
        File.open('android-error-logs.txt', 'r') do |f|
          File.delete(f)
        end
      rescue Errno::ENOENT
          UI.message("Something Wrong When Delete Previus File [android-error-logs.txt]")
      end
  end

  # 0. Checkout git to Current Branch to Run Tests on the Current Branch
  if params[:tests_branch_name].to_s.length > 0
      begin
          sh("git fetch")
          sh("git checkout ", params[:tests_branch_name].string)
      rescue => error
          UI.error("Something Error with Git Commands")
      end
  end

  # 1. Send Slack Message from Android Reporter Bot That Testing Task Has been Started ....
  if is_slack_enabled
      UI.message("Slack Messages Bot Started .... [Configuration Step]")
      Slack.configure do |config|
         config.token = slack_api_key
      end
         client = Slack::Web::Client.new
         begin
            client.chat_postMessage(
                  channel: "#" + params[:slack_channel_name],
                  text:   " ================================================================================== \n" +
                          " New UI Testing Task Started For Current Release : " + time + " \n " +
                          " Current Branch Build : " + sh("git rev-parse --abbrev-ref HEAD") +
                          " ================================================================================== \n",
                  as_user: true
            )

            UI.message("Slack Build Task Message Result : [Send First Task Message] ")
         rescue => error
            UI.error("Something Error with Slack Configuration : [Failed Send First Task Message]")
            puts error.backtrace
         end
  end

  # 2. List All Available AVD's in The Device
  # 3. Run the Target AVD Name From Params
  if params[:avd_name].to_s.length > 0
      UI.message("Android Reporting Started to Run AVD : (" + avd_name + ")")
      sh(android_sdk_path + "/tools/bin/avdmanager", "list", "avd")
      sh("cd", android_sdk_path)
      sh(android_sdk_path + "/emulator/emulator", "-avd", avd_name)
  end

  # 4. Clear the Logcat Server Before Start
  if params[:generate_adb_file]
      sh("adb logcat -c")
  end

  # 5. Start Clean Task with Target UI Task
  # 6. If Gradle Task Success then Will Generate ADB Log File then Kill Server Connection inside ADB
  begin
    UI.message([
         "Android Reporting Started to Run Gradle Testing Task ...",
         "Gradle Tasks : [Clean , Build , " + gradle_task_testing_name + "]"
    ])
    if params[:gradle_task_testing_name].to_s.length > 0
        sh("./gradlew clean")
        gradle_task_result = sh("./gradlew " + gradle_task_testing_name)
        if gradle_task_result
            UI.message("Gradle Task Finished Successfully ...")
            if params[:generate_adb_file]
                Process.fork do
                    UI.message([
                      "Android Reporter Started to Save Logcat Logs From ADB Connection Server",
                      "ADB Reporting File : [logcat.txt]"
                    ])

                    begin
                      sh("adb logcat -d > logcat.txt kill-server")
                      sh("adb kill-server")
                    rescue => error
                      if error
                         UI.error("Something Error with Logcat Debug Report File")
                      end
                         puts error.message
                         puts error.backtrace
                    end
                end
            end
        end
  end
  rescue
    UI.error("Gradle Task Finished With Error ...")
    if params[:generate_adb_file]
         begin
            sh("adb logcat -e > logcat.txt kill-server")
         rescue => error
            if error
               UI.error("Something Error with Logcat Error Report File")
            end
               puts error.message
               puts error.backtrace
         end
    end
  end

  # 7. Generate Gradle Tasks (Failed and Success Files)
  if params[:generate_gradle_files]
       UI.message([
            "Android Reporting Started to Generate Gradle Reports Files",
            "Gradle Success Debug File : android-logs.txt",
            "Gradle Failed Debug File : android-error-logs.txt"
       ])

       begin
          sh("./gradlew build > android-logs.txt 2> android-error-logs.txt -Dhttp.socketTimeout=60000 -Dhttp.connectionTimeout=60000 -Dorg.gradle.daemon.idletimeout=60000")
          sh("./gradlew --stop")
       rescue => error
          if error
             UI.error("Something Error with Generate Gradle Files")
          end
             puts error.message
             puts error.backtrace
       end
  end

  # 8. Check Slack Option if Enabled or Not To Print The Message
  if is_slack_enabled
      UI.message("Slack Configuration Status : Started ...")
  else
      UI.message("Slack Configuration Status : Disabled ...")
  end

  sleep(60000)
  # 9. Upload Files to Slack
  if is_slack_enabled
      begin
        UI.message("Slack Messages Bot Upload Files Tasks Started ....")
        UI.message(__dir__)
        if(File.exist?("logcat.txt"))
           UI.message("logcat.txt File exists ...")
           client.files_upload(
              channels: "#" + params[:slack_channel_name],
              as_user: true,
              file: Faraday::UploadIO.new('logcat.txt', 'text/plain'),
              title: "Android UI Testing (ADB Logcat Result)",
              filename: "logcat.txt",
              initial_comment: "Current UI Testing Build Result File Report [logcat.txt]"
           )

           isLogcatFileUploaded = true
           UI.message("Slack Step Upload [logcat.txt]")
        else
           isLogcatFileUploaded = false
           UI.error("File does not exist [logcat.txt]")
        end
      rescue => error
        if error
           UI.error("Something Error with Slack Upload File : [Step Upload Logcat.txt File to Slack]")
        end
           puts error.message
           puts error.backtrace
      end

      begin
        if(File.exist?("android-logs.txt"))
           UI.message("android-logs.txt File exists ...")
           client.files_upload(
                channels: "#" + params[:slack_channel_name],
                as_user: true,
                file: Faraday::UploadIO.new('android-logs.txt', 'text/plain'),
                title: "Android UI Testing (Gradle Debug File Result)",
                filename: "android-logs.txt",
                initial_comment: "Current UI Testing Build Result File Report [Gradle Debug Result]"
           )

           isGradleDebugFileUploaded = true
           UI.message("Slack Step Upload [android-logs.txt]")
        else
           isGradleDebugFileUploaded = false
           UI.error("File does not exist [android-logs.txt]")
        end
      rescue => error
           if error
               UI.error("Something Error with Slack Upload File : [Step Upload Logcat.txt File to Slack]")
           end
               puts error.message
               puts error.backtrace
      end

      begin
        if(File.exist?("android-error-logs.txt"))
           UI.message("android-error-logs.txt File exists ...")
           client.files_upload(
                 channels: "#" + params[:slack_channel_name],
                 as_user: true,
                 file: Faraday::UploadIO.new('android-error-logs.txt', 'text/plain'),
                 title: "Android UI Testing (Gradle Error File Result)",
                 filename: "android-error-logs.txt",
                 initial_comment: "Current UI Testing Build Result File Report [Gradle Error Result]"
           )

           isGradleErrorFileUploaded = true
           UI.message("Slack Step Upload [android-error-logs.txt]")
        else
           isGradleErrorFileUploaded = false
           UI.error("File does not exist [android-error-logs.txt]")
        end
      rescue => error
        if error
          UI.error("Something Error with Slack Upload File : [Step Upload Logcat.txt File to Slack]")
        end
        puts error.message
        puts error.backtrace
      end

      slackFinalMessageContent = "Files Status : With Time : " + time + " : Info = \n"
      if isGradleDebugFileUploaded
          slackFinalMessageContent += "Gradle Debug File Uploaded \n"
      end

      if isGradleErrorFileUploaded
          slackFinalMessageContent += "Gradle Error File Uploaded \n"
      end

      if isLogcatFileUploaded
          slackFinalMessageContent += "Logcat ADB Logging File Uploaded \n"
      end

      client.chat_postMessage(channel: "#" + params[:slack_channel_name], text: slackFinalMessageContent, as_user: true)
  end

end