Class: FirebaseTestLabIntegration::Helper::GithubHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/firebase_test_lab_integration/helper/github_helper.rb

Constant Summary collapse

PASSED =
'Passed'
FAILED =
'Failed'
SKIPPED =
'Skipped'
INCONCLUSIVE =
'Inconclusive'

Instance Method Summary collapse

Constructor Details

#initialize(owner, repository, api_token) ⇒ GithubHelper

Returns a new instance of GithubHelper.



13
14
15
16
17
# File 'lib/fastlane/plugin/firebase_test_lab_integration/helper/github_helper.rb', line 13

def initialize(owner, repository, api_token)
  @owner = owner
  @repository = repository
  @api_token = api_token
end

Instance Method Details

#delete_comment(comment_id) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/fastlane/plugin/firebase_test_lab_integration/helper/github_helper.rb', line 89

def delete_comment(comment_id)
  api_url = "https://api.github.com/repos/#{@owner}/#{@repository}/issues/comments/#{comment_id}"
  FastlaneCore::UI.message("Deleting Github comment #{api_url}")

  uri = URI.parse(api_url)
  req = Net::HTTP::Delete.new(uri)
  req["Content-Type"] = "application/json"
  req["Authorization"] = "token #{@api_token}"

  res = Net::HTTP.start(uri.hostname, uri.port, { use_ssl: uri.scheme = "https" }) { |http| http.request(req) }
  FastlaneCore::UI.message("#{res.code}\n#{res.body}")

  res
end

#delete_comments(github_pr_number, comment_prefix) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/fastlane/plugin/firebase_test_lab_integration/helper/github_helper.rb', line 33

def delete_comments(github_pr_number, comment_prefix)
  FastlaneCore::UI.message("Deleting old Github comments.")
  res = get_comments(github_pr_number)
  JSON.parse(res.body)
      .select { |comment| comment["body"].start_with?(comment_prefix) }
      .each { |comment| delete_comment(comment["id"]) }
end

#fold_comments(github_pr_number, comment_prefix, summary) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/fastlane/plugin/firebase_test_lab_integration/helper/github_helper.rb', line 23

def fold_comments(github_pr_number, comment_prefix, summary)
  res = get_comments(github_pr_number)
  JSON.parse(res.body)
      .select { |comment| comment["body"].start_with?(comment_prefix) }
      .each do |comment|
        body = "<details><summary>#{summary}</summary>\n\n#{comment['body']}\n\n</details>\n"
        patch_comment(comment["id"], body)
      end
end

#generate_comment_content(json, project_id, bucket, dir, platform, test_type) ⇒ Object



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
# File 'lib/fastlane/plugin/firebase_test_lab_integration/helper/github_helper.rb', line 104

def generate_comment_content(json, project_id, bucket, dir, platform, test_type)
  prefix = "<img alt=\"#{platform}\" src=\"https://github.com/crisboarna/fastlane-plugin-firebase_test_lab_integration/blob/main/docs/images/firebase_test_lab_logo.png?raw=true\" width=\"65%\" loading=\"lazy\" />"
  cells = json.map do |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=\"#{::FirebaseTestLabIntegration::Helper::GcloudHelper.gcloud_bucket_object_url(bucket, "#{dir}/#{axis}/logcat")}\" target=\"_blank\" >#{random_emoji_cat}</a>"
    if platform == :android
      if test_type == "robo"
        sitemp = "<img src=\"#{::FirebaseTestLabIntegration::Helper::GcloudHelper.gcloud_bucket_object_url(bucket, "#{dir}/#{axis}/artifacts/sitemap.png")}\" height=\"64px\" loading=\"lazy\" target=\"_blank\" />"
      else
        sitemp = "--"
      end
    end

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

    ### Results
    Firebase console: [#{project_id}](#{::FirebaseTestLabIntegration::Helper::GcloudHelper.firebase_test_lab_history_url(project_id)})#{' '}
    Test results: [#{dir}](#{::FirebaseTestLabIntegration::Helper::GcloudHelper.gcloud_result_bucket_url(bucket, dir)})

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

#get_comments(github_pr_number) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fastlane/plugin/firebase_test_lab_integration/helper/github_helper.rb', line 41

def get_comments(github_pr_number)
  api_url = "https://api.github.com/repos/#{@owner}/#{@repository}/issues/#{github_pr_number}/comments"
  FastlaneCore::UI.message("get comments #{api_url}")

  uri = URI.parse(api_url)
  req = Net::HTTP::Get.new(uri)
  req["Content-Type"] = "application/json"
  req["Authorization"] = "token #{@api_token}"

  res = Net::HTTP.start(uri.hostname, uri.port, { use_ssl: uri.scheme = "https" }) { |http| http.request(req) }
  FastlaneCore::UI.message("#{res.code}\n#{res.body}")

  res
end

#patch_comment(comment_id, body) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/fastlane/plugin/firebase_test_lab_integration/helper/github_helper.rb', line 73

def patch_comment(comment_id, body)
  api_url = "https://api.github.com/repos/#{@owner}/#{@repository}/issues/comments/#{comment_id}"
  FastlaneCore::UI.message("patch comment #{api_url}")

  uri = URI.parse(api_url)
  req = Net::HTTP::Patch.new(uri)
  req["Content-Type"] = "application/json"
  req["Authorization"] = "token #{@api_token}"
  req.body = { body: body }.to_json

  res = Net::HTTP.start(uri.hostname, uri.port, { use_ssl: uri.scheme = "https" }) { |http| http.request(req) }
  FastlaneCore::UI.message("#{res.code}\n#{res.body}")

  res
end

#put_comment(github_pr_number, body) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/fastlane/plugin/firebase_test_lab_integration/helper/github_helper.rb', line 56

def put_comment(github_pr_number, body)
  FastlaneCore::UI.message("Adding Github comments.")
  api_url = "https://api.github.com/repos/#{@owner}/#{@repository}/issues/#{github_pr_number}/comments"
  FastlaneCore::UI.message("put comment #{api_url}")

  uri = URI.parse(api_url)
  req = Net::HTTP::Post.new(uri)
  req["Content-Type"] = "application/json"
  req["Authorization"] = "token #{@api_token}"
  req.body = { body: body }.to_json

  res = Net::HTTP.start(uri.hostname, uri.port, { use_ssl: uri.scheme = "https" }) { |http| http.request(req) }
  FastlaneCore::UI.message("#{res.code}\n#{res.body}")

  res
end

#valid_params?(pr_number) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/fastlane/plugin/firebase_test_lab_integration/helper/github_helper.rb', line 19

def valid_params?(pr_number)
  !@owner.nil? && !@owner.empty? && !@repository.nil? && !@repository.empty? && !pr_number.nil? && !pr_number.empty? && !@api_token.nil? && !@api_token.empty?
end