|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "json" |
| 4 | +require "net/http" |
| 5 | +require "uri" |
| 6 | + |
| 7 | +class E2EGitHubReporter |
| 8 | + COMMENT_MARKER = "<!-- checkout-kit-e2e-report -->" |
| 9 | + |
| 10 | + def initialize(results, repository:, sha:, pr_number:, token: nil) |
| 11 | + @results = results |
| 12 | + @repository = repository |
| 13 | + @sha = sha |
| 14 | + @pr_number = pr_number |
| 15 | + @token = token |
| 16 | + end |
| 17 | + |
| 18 | + def publish! |
| 19 | + commit_status_payloads.each { |payload| post_json("/repos/#{@repository}/statuses/#{@sha}", payload) } |
| 20 | + post_json("/repos/#{@repository}/check-runs", check_run_payload) |
| 21 | + sync_failure_comment |
| 22 | + end |
| 23 | + |
| 24 | + def markdown_summary |
| 25 | + lines = [] |
| 26 | + lines << "## Checkout Kit E2E results" |
| 27 | + lines << "" |
| 28 | + lines << "| Status | Suite | Target | Platform | OS track | Device |" |
| 29 | + lines << "|---|---|---|---|---|---|" |
| 30 | + @results.each do |result| |
| 31 | + lines << "| #{status_icon(result)} | `#{result.fetch("execute")}` | #{result.fetch("target")} | #{result.fetch("platform")} | #{result.fetch("os_track")} | #{device_cell(result)} |" |
| 32 | + end |
| 33 | + failure_lines = failed_results.flat_map { |result| failure_details(result) } |
| 34 | + unless failure_lines.empty? |
| 35 | + lines << "" |
| 36 | + lines << "## Failures" |
| 37 | + lines << "" |
| 38 | + lines << "> BrowserStack artifacts require BrowserStack access. Sign in to [BrowserStack App Automate](https://app-automate.browserstack.com/dashboard/v2/builds) before opening artifact links." |
| 39 | + lines.concat(failure_lines) |
| 40 | + end |
| 41 | + lines.join("\n") |
| 42 | + end |
| 43 | + |
| 44 | + def commit_status_payloads |
| 45 | + @results.map do |result| |
| 46 | + { |
| 47 | + state: result.fetch("passed") ? "success" : "failure", |
| 48 | + context: result.fetch("status_context"), |
| 49 | + description: status_description(result), |
| 50 | + target_url: browserstack_build_url(result) |
| 51 | + } |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + def failure_comment_body |
| 56 | + return nil if failed_results.empty? |
| 57 | + |
| 58 | + [COMMENT_MARKER, markdown_summary].join("\n") |
| 59 | + end |
| 60 | + |
| 61 | + private |
| 62 | + |
| 63 | + def check_run_payload |
| 64 | + conclusion = failed_results.empty? ? "success" : "failure" |
| 65 | + { |
| 66 | + name: "Checkout Kit E2E", |
| 67 | + head_sha: @sha, |
| 68 | + status: "completed", |
| 69 | + conclusion: conclusion, |
| 70 | + output: { |
| 71 | + title: "Checkout Kit E2E #{conclusion}", |
| 72 | + summary: markdown_summary |
| 73 | + } |
| 74 | + } |
| 75 | + end |
| 76 | + |
| 77 | + def sync_failure_comment |
| 78 | + body = failure_comment_body |
| 79 | + existing = existing_failure_comment |
| 80 | + if body |
| 81 | + if existing |
| 82 | + patch_json("/repos/#{@repository}/issues/comments/#{existing.fetch("id")}", {body: body}) |
| 83 | + else |
| 84 | + post_json("/repos/#{@repository}/issues/#{@pr_number}/comments", {body: body}) |
| 85 | + end |
| 86 | + elsif existing |
| 87 | + patch_json("/repos/#{@repository}/issues/comments/#{existing.fetch("id")}", {body: "#{COMMENT_MARKER}\n✅ Checkout Kit E2E failures resolved."}) |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + def existing_failure_comment |
| 92 | + get_json("/repos/#{@repository}/issues/#{@pr_number}/comments").find do |comment| |
| 93 | + comment.fetch("body", "").include?(COMMENT_MARKER) |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + def failed_results |
| 98 | + @results.reject { |result| result.fetch("passed") } |
| 99 | + end |
| 100 | + |
| 101 | + def failure_details(result) |
| 102 | + lines = [] |
| 103 | + lines << "" |
| 104 | + lines << "### #{failure_heading(result)}" |
| 105 | + lines << "" |
| 106 | + lines << "| Test | Status | Artifacts |" |
| 107 | + lines << "|---|---|---|" |
| 108 | + tests = result.fetch("failed_tests", []) |
| 109 | + if tests.empty? |
| 110 | + lines << "| — | #{status_icon(result)} | #{artifact_links(nil, result)} |" |
| 111 | + else |
| 112 | + tests.each do |testcase| |
| 113 | + lines << "| `#{testcase.fetch("name", "unknown")}` | ❌ | #{artifact_links(testcase, result)} |" |
| 114 | + end |
| 115 | + end |
| 116 | + lines |
| 117 | + end |
| 118 | + |
| 119 | + def failure_heading(result) |
| 120 | + suite = File.basename(result.fetch("execute"), ".*") |
| 121 | + "#{os_label(result.fetch("platform"))} — #{suite}" |
| 122 | + end |
| 123 | + |
| 124 | + def artifact_links(testcase, result) |
| 125 | + links = ["[BrowserStack](#{browserstack_build_url(result)})"] |
| 126 | + if testcase |
| 127 | + link_fields.each do |label, key| |
| 128 | + value = testcase[key] |
| 129 | + links << "[#{label}](#{value})" if value && !value.empty? |
| 130 | + end |
| 131 | + end |
| 132 | + links.join(" · ") |
| 133 | + end |
| 134 | + |
| 135 | + def link_fields |
| 136 | + { |
| 137 | + "Video" => "video", |
| 138 | + "Screenshot" => "screenshots", |
| 139 | + "Maestro commands" => "maestro_commands", |
| 140 | + "Maestro log" => "maestro_log", |
| 141 | + "Device log" => "device_log", |
| 142 | + "Network log" => "network_log" |
| 143 | + } |
| 144 | + end |
| 145 | + |
| 146 | + def device_cell(result) |
| 147 | + name, version = result.fetch("resolved_device").split("-", 2) |
| 148 | + version ? "#{name}<br>#{os_label(result.fetch("platform"))} #{version}" : name |
| 149 | + end |
| 150 | + |
| 151 | + def os_label(platform) |
| 152 | + platform == "ios" ? "iOS" : "Android" |
| 153 | + end |
| 154 | + |
| 155 | + def status_icon(result) |
| 156 | + result.fetch("passed") ? "✅" : "❌" |
| 157 | + end |
| 158 | + |
| 159 | + def status_description(result) |
| 160 | + description = result.fetch("passed") ? "passed" : "#{result.fetch("status")} on #{result.fetch("resolved_device")}" |
| 161 | + description[0, 140] |
| 162 | + end |
| 163 | + |
| 164 | + def browserstack_build_url(result) |
| 165 | + "https://app-automate.browserstack.com/dashboard/v2/builds/#{result.fetch("build_id")}" |
| 166 | + end |
| 167 | + |
| 168 | + def get_json(path) |
| 169 | + execute_request(Net::HTTP::Get.new(path)) |
| 170 | + end |
| 171 | + |
| 172 | + def post_json(path, body) |
| 173 | + request = Net::HTTP::Post.new(path) |
| 174 | + request.body = JSON.generate(body) |
| 175 | + execute_json_request(request) |
| 176 | + end |
| 177 | + |
| 178 | + def patch_json(path, body) |
| 179 | + request = Net::HTTP::Patch.new(path) |
| 180 | + request.body = JSON.generate(body) |
| 181 | + execute_json_request(request) |
| 182 | + end |
| 183 | + |
| 184 | + def execute_json_request(request) |
| 185 | + request["Content-Type"] = "application/json" |
| 186 | + execute_request(request) |
| 187 | + end |
| 188 | + |
| 189 | + def execute_request(request) |
| 190 | + raise "GitHub token is required" unless @token |
| 191 | + |
| 192 | + request["Accept"] = "application/vnd.github+json" |
| 193 | + request["Authorization"] = "Bearer #{@token}" |
| 194 | + response = Net::HTTP.start("api.github.com", 443, use_ssl: true) { |http| http.request(request) } |
| 195 | + body = response.body.to_s.empty? ? {} : JSON.parse(response.body) |
| 196 | + return body if response.is_a?(Net::HTTPSuccess) |
| 197 | + |
| 198 | + raise "GitHub request failed #{response.code}: #{body}" |
| 199 | + end |
| 200 | +end |
0 commit comments