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



235
236
237
# File 'lib/fastlane/plugin/android_reporter/actions/android_reporter_action.rb', line 235

def self.authors
  ["Yazan Tarifi"]
end

.available_optionsObject



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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
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
# File 'lib/fastlane/plugin/android_reporter/actions/android_reporter_action.rb', line 248

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: :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



231
232
233
# File 'lib/fastlane/plugin/android_reporter/actions/android_reporter_action.rb', line 231

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

.detailsObject



243
244
245
246
# File 'lib/fastlane/plugin/android_reporter/actions/android_reporter_action.rb', line 243

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)


322
323
324
325
326
327
328
# File 'lib/fastlane/plugin/android_reporter/actions/android_reporter_action.rb', line 322

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



239
240
241
# File 'lib/fastlane/plugin/android_reporter/actions/android_reporter_action.rb', line 239

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
# 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

  # 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
      Thread.new do
         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
  end

  # 4. Clear the Logcat Server Before Start
  if params[:generate_adb_file]
      Process.fork do
         sh("adb logcat -c")
      end
  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]"
                    ])
                    sh("adb logcat -d > logcat.txt kill-server")
                end
            end
        end
  end
  rescue
    UI.error("Gradle Task Finished With Error ...")
    if params[:generate_adb_file]
         Process.fork do
             sh("adb logcat -e > logcat.txt kill-server")
         end
    end
  end

  # 7. Generate Gradle Tasks (Failed and Success Files)
  if params[:generate_gradle_files]
       Process.fork do
          sh("./gradlew build > android-logs.txt 2> android-error-logs.txt")
          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"
          ])
       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

  # 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