|
| 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 << "| Run | Status | Target | Platform | OS track | Device | Suite |" |
| 29 | + lines << "|---|---|---|---|---|---|---|" |
| 30 | + @results.each do |result| |
| 31 | + lines << "| `#{result.fetch("id")}` | #{status_icon(result)} #{result.fetch("status")} | #{result.fetch("target")} | #{result.fetch("platform")} | #{result.fetch("os_track")} | #{result.fetch("device_selector")} → #{result.fetch("resolved_device")} | `#{result.fetch("execute")}` |" |
| 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.concat(failure_lines) |
| 38 | + end |
| 39 | + lines.join("\n") |
| 40 | + end |
| 41 | + |
| 42 | + def commit_status_payloads |
| 43 | + @results.map do |result| |
| 44 | + { |
| 45 | + state: result.fetch("passed") ? "success" : "failure", |
| 46 | + context: result.fetch("status_context"), |
| 47 | + description: status_description(result), |
| 48 | + target_url: browserstack_build_url(result) |
| 49 | + } |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + def failure_comment_body |
| 54 | + return nil if failed_results.empty? |
| 55 | + |
| 56 | + [COMMENT_MARKER, markdown_summary].join("\n") |
| 57 | + end |
| 58 | + |
| 59 | + private |
| 60 | + |
| 61 | + def check_run_payload |
| 62 | + conclusion = failed_results.empty? ? "success" : "failure" |
| 63 | + { |
| 64 | + name: "Checkout Kit E2E", |
| 65 | + head_sha: @sha, |
| 66 | + status: "completed", |
| 67 | + conclusion: conclusion, |
| 68 | + output: { |
| 69 | + title: "Checkout Kit E2E #{conclusion}", |
| 70 | + summary: markdown_summary |
| 71 | + } |
| 72 | + } |
| 73 | + end |
| 74 | + |
| 75 | + def sync_failure_comment |
| 76 | + body = failure_comment_body |
| 77 | + existing = existing_failure_comment |
| 78 | + if body |
| 79 | + if existing |
| 80 | + patch_json("/repos/#{@repository}/issues/comments/#{existing.fetch("id")}", {body: body}) |
| 81 | + else |
| 82 | + post_json("/repos/#{@repository}/issues/#{@pr_number}/comments", {body: body}) |
| 83 | + end |
| 84 | + elsif existing |
| 85 | + patch_json("/repos/#{@repository}/issues/comments/#{existing.fetch("id")}", {body: "#{COMMENT_MARKER}\n✅ Checkout Kit E2E failures resolved."}) |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + def existing_failure_comment |
| 90 | + get_json("/repos/#{@repository}/issues/#{@pr_number}/comments").find do |comment| |
| 91 | + comment.fetch("body", "").include?(COMMENT_MARKER) |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + def failed_results |
| 96 | + @results.reject { |result| result.fetch("passed") } |
| 97 | + end |
| 98 | + |
| 99 | + def failure_details(result) |
| 100 | + lines = [] |
| 101 | + lines << "" |
| 102 | + lines << "### `#{result.fetch("id")}`" |
| 103 | + lines << "" |
| 104 | + lines << "- Device selector: `#{result.fetch("device_selector")}`" |
| 105 | + lines << "- Resolved device: `#{result.fetch("resolved_device")}`" |
| 106 | + lines << "- Suite: `#{result.fetch("execute")}`" |
| 107 | + lines << "- BrowserStack build: #{browserstack_build_url(result)}" |
| 108 | + result.fetch("failed_tests", []).each do |testcase| |
| 109 | + lines << "- Failed test: `#{testcase.fetch("name", "unknown")}` (`#{testcase.fetch("status", "unknown")}`)" |
| 110 | + link_fields.each do |label, key| |
| 111 | + value = testcase[key] |
| 112 | + lines << " - #{label}: #{value}" if value && !value.empty? |
| 113 | + end |
| 114 | + end |
| 115 | + lines |
| 116 | + end |
| 117 | + |
| 118 | + def link_fields |
| 119 | + { |
| 120 | + "Video" => "video", |
| 121 | + "Screenshot" => "screenshots", |
| 122 | + "Maestro commands" => "maestro_commands", |
| 123 | + "Maestro log" => "maestro_log", |
| 124 | + "Device log" => "device_log", |
| 125 | + "Network log" => "network_log" |
| 126 | + } |
| 127 | + end |
| 128 | + |
| 129 | + def status_icon(result) |
| 130 | + result.fetch("passed") ? "✅" : "❌" |
| 131 | + end |
| 132 | + |
| 133 | + def status_description(result) |
| 134 | + description = result.fetch("passed") ? "passed" : "#{result.fetch("status")} on #{result.fetch("resolved_device")}" |
| 135 | + description[0, 140] |
| 136 | + end |
| 137 | + |
| 138 | + def browserstack_build_url(result) |
| 139 | + "https://app-automate.browserstack.com/dashboard/v2/builds/#{result.fetch("build_id")}" |
| 140 | + end |
| 141 | + |
| 142 | + def get_json(path) |
| 143 | + execute_request(Net::HTTP::Get.new(path)) |
| 144 | + end |
| 145 | + |
| 146 | + def post_json(path, body) |
| 147 | + request = Net::HTTP::Post.new(path) |
| 148 | + request.body = JSON.generate(body) |
| 149 | + execute_json_request(request) |
| 150 | + end |
| 151 | + |
| 152 | + def patch_json(path, body) |
| 153 | + request = Net::HTTP::Patch.new(path) |
| 154 | + request.body = JSON.generate(body) |
| 155 | + execute_json_request(request) |
| 156 | + end |
| 157 | + |
| 158 | + def execute_json_request(request) |
| 159 | + request["Content-Type"] = "application/json" |
| 160 | + execute_request(request) |
| 161 | + end |
| 162 | + |
| 163 | + def execute_request(request) |
| 164 | + raise "GitHub token is required" unless @token |
| 165 | + |
| 166 | + request["Accept"] = "application/vnd.github+json" |
| 167 | + request["Authorization"] = "Bearer #{@token}" |
| 168 | + response = Net::HTTP.start("api.github.com", 443, use_ssl: true) { |http| http.request(request) } |
| 169 | + body = response.body.to_s.empty? ? {} : JSON.parse(response.body) |
| 170 | + return body if response.is_a?(Net::HTTPSuccess) |
| 171 | + |
| 172 | + raise "GitHub request failed #{response.code}: #{body}" |
| 173 | + end |
| 174 | +end |
0 commit comments