Module: Fastlane::Helper

Defined in:
lib/fastlane/plugin/firebase_test_lab_android/helper/Helper.rb

Class Method Summary collapse

Class Method Details

.authenticate(gcloud_key_file) ⇒ Object



30
31
32
33
# File 'lib/fastlane/plugin/firebase_test_lab_android/helper/Helper.rb', line 30

def self.authenticate(gcloud_key_file)
  UI.message "Authenticate with GCP"
  Action.sh("gcloud auth activate-service-account --key-file #{gcloud_key_file}")
end

.config(project_id) ⇒ Object



25
26
27
28
# File 'lib/fastlane/plugin/firebase_test_lab_android/helper/Helper.rb', line 25

def self.config(project_id)
  UI.message "Set Google Cloud target project"
  Action.sh("gcloud config set project #{project_id}")
end

.copy_from_gcs(bucket_and_path, copy_to) ⇒ Object



40
41
42
43
# File 'lib/fastlane/plugin/firebase_test_lab_android/helper/Helper.rb', line 40

def self.copy_from_gcs(bucket_and_path, copy_to)
  UI.message("Copy from gs://#{bucket_and_path}")
  Action.sh("gsutil -m cp -r gs://#{bucket_and_path} #{copy_to}")
end

.emoji_status(outcome) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fastlane/plugin/firebase_test_lab_android/helper/Helper.rb', line 69

def self.emoji_status(outcome)
  # Github emoji list
  # https://github.com/ikatyang/emoji-cheat-sheet
  return case outcome
         when PASSED
           ":tada:"
         when FAILED
           ":fire:"
         when INCONCLUSIVE
           ":warning:"
         when SKIPPED
           ":expressionless:"
         else
           ":question:"
         end
end

.firebase_test_lab_histories_url(project_id) ⇒ Object



21
22
23
# File 'lib/fastlane/plugin/firebase_test_lab_android/helper/Helper.rb', line 21

def self.firebase_test_lab_histories_url(project_id)
  "https://console.firebase.google.com/u/0/project/#{project_id}/testlab/histories/"
end

.gcs_object_url(bucket, path) ⇒ Object



17
18
19
# File 'lib/fastlane/plugin/firebase_test_lab_android/helper/Helper.rb', line 17

def self.gcs_object_url(bucket, path)
  "https://storage.googleapis.com/#{bucket}/#{CGI.escape(path)}"
end

.gcs_result_bucket_url(bucket, dir) ⇒ Object



13
14
15
# File 'lib/fastlane/plugin/firebase_test_lab_android/helper/Helper.rb', line 13

def self.gcs_result_bucket_url(bucket, dir)
  "https://console.developers.google.com/storage/browser/#{bucket}/#{CGI.escape(dir)}"
end

.if_need_dir(path) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/fastlane/plugin/firebase_test_lab_android/helper/Helper.rb', line 54

def self.if_need_dir(path)
  dirname = File.dirname(path)
  unless File.directory?(dirname)
    UI.message("Crate directory: #{dirname}")
    FileUtils.mkdir_p(dirname)
  end
  path
end

.is_failure(outcome) ⇒ Object



50
51
52
# File 'lib/fastlane/plugin/firebase_test_lab_android/helper/Helper.rb', line 50

def self.is_failure(outcome)
  outcome == FAILED || outcome == INCONCLUSIVE
end

.make_github_text(json, project_id, bucket, dir, test_type) ⇒ Object



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
# File 'lib/fastlane/plugin/firebase_test_lab_android/helper/Helper.rb', line 108

def self.make_github_text(json, project_id, bucket, dir, test_type)
  prefix = "<img src=\"https://github.com/cats-oss/fastlane-plugin-firebase_test_lab_android/blob/master/art/firebase_test_lab_logo.png?raw=true\" width=\"65%\" loading=\"lazy\" />"
  cells = json.map { |data|
    axis = data["axis_value"]
    device = split_device_name(axis)
    outcome = data["outcome"]
    status = "#{emoji_status(outcome)} #{outcome}"
    message = data["test_details"]
    logcat = "<a href=\"#{gcs_object_url(bucket, "#{dir}/#{axis}/logcat")}\" target=\"_blank\" >#{random_emoji_cat}</a>"
    if test_type == "robo"
      sitemp = "<img src=\"#{gcs_object_url(bucket, "#{dir}/#{axis}/artifacts/sitemap.png")}\" height=\"64px\" loading=\"lazy\" target=\"_blank\" />"
    else
      sitemp = "--"
    end

    "| **#{device}** | #{status} | #{message} | #{logcat} | #{sitemp} |\n"
  }.inject(&:+)
  comment = <<~EOS
    #{prefix}

    ### Results
    Firebase console: [#{project_id}](#{Helper.firebase_test_lab_histories_url(project_id)}) 
    Test results: [#{dir}](#{Helper.gcs_result_bucket_url(bucket, dir)})

    | :iphone: Device | :thermometer: Status | :memo: Message | :eyes: Logcat | :japan: Sitemap | 
    | --- | :---: | --- | :---: | :---: |
    #{cells}
  EOS
  return prefix, comment
end

.make_slack_text(json) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/fastlane/plugin/firebase_test_lab_android/helper/Helper.rb', line 92

def self.make_slack_text(json)
  success = true
  json.each do |status|
    success = !is_failure(status["outcome"])
    break unless success
  end

  body = json.map { |status|
    outcome = status["outcome"]
    emoji = emoji_status(outcome)
    device = split_device_name(status["axis_value"])
    "#{device}: #{emoji} #{outcome}\n"
  }.inject(&:+)
  return success, body
end

.random_emoji_catObject



86
87
88
89
90
# File 'lib/fastlane/plugin/firebase_test_lab_android/helper/Helper.rb', line 86

def self.random_emoji_cat
  # Github emoji list
  # https://github.com/ikatyang/emoji-cheat-sheet#cat-face
  %w(:smiley_cat: :smile_cat: :joy_cat: :heart_eyes_cat: :smirk_cat: :kissing_cat:).sample
end

.run_tests(gcloud_components_channel, arguments) ⇒ Object



35
36
37
38
# File 'lib/fastlane/plugin/firebase_test_lab_android/helper/Helper.rb', line 35

def self.run_tests(gcloud_components_channel, arguments)
  UI.message("Test running...")
  Action.sh("set +e; gcloud #{gcloud_components_channel unless gcloud_components_channel == "stable"} firebase test android run #{arguments}; set -e")
end

.set_public(bucket_and_path) ⇒ Object



45
46
47
48
# File 'lib/fastlane/plugin/firebase_test_lab_android/helper/Helper.rb', line 45

def self.set_public(bucket_and_path)
  UI.message("Set public for reading gs://#{bucket_and_path}")
  Action.sh("gsutil -m acl -r set public-read gs://#{bucket_and_path}")
end

.split_device_name(axis_value) ⇒ Object



63
64
65
66
67
# File 'lib/fastlane/plugin/firebase_test_lab_android/helper/Helper.rb', line 63

def self.split_device_name(axis_value)
  # Sample Nexus6P-23-ja_JP-portrait
  array = axis_value.split("-")
  "#{array[0]} (API #{array[1]})"
end