| 1 | import Foundation |
| 2 | |
| 3 | struct CheckSection: Identifiable, Equatable, Sendable { |
| 4 | let id: Int |
| 5 | let name: String |
| 6 | let text: String |
| 7 | let failed: Bool |
| 8 | var isSummary = false |
| 9 | static func split(_ log: StepLog, step: ThinStep) -> [Self] { |
| 10 | let tail = log.boundedTail |
| 11 | let failed = step.checks == "failed" || (step.exitCode.map { $0 != 0 } ?? false) |
| 12 | let hosted = step.gate == "hosted CI" |
| 13 | guard step.gate == ".amadan/ci.d" || hosted else { return [Self(id: 0, name: "", text: tail, failed: failed)] } |
| 14 | let source = tail.components(separatedBy: "\n") |
| 15 | // Older devices receive no hosted field. Only then can the header |
| 16 | // supply the allowlist; a present empty field is authoritative. |
| 17 | let headerNames = source.prefix { !$0.hasPrefix("=== ") }.first { $0.hasPrefix("Failed: ") } |
| 18 | .map { String($0.dropFirst(8)).components(separatedBy: "; ") } ?? [] |
| 19 | let names = Set((step.hosted.map { $0.failed ?? [] } ?? headerNames).map(hostedName)) |
| 20 | var chunks: [(name: String, lines: [String])] = [] |
| 21 | var name = "", lines: [String] = [] |
| 22 | for line in source { |
| 23 | let rawName = String(line.dropFirst(4)) |
| 24 | let candidate = hosted ? hostedName(rawName) : rawName |
| 25 | let basename = candidate.range(of: #"^[A-Za-z0-9_@+-][A-Za-z0-9._@+-]*$"#, options: .regularExpression) != nil |
| 26 | && !["RUN", "PAUSE", "CONT", "NAME"].contains(candidate) |
| 27 | let marker = line.hasPrefix("=== ") && (hosted ? names.contains(candidate) : basename) |
| 28 | if marker { |
| 29 | if !name.isEmpty || !lines.isEmpty { chunks.append((name, lines)) } |
| 30 | name = candidate; lines = [line] |
| 31 | } else { lines.append(line) } |
| 32 | } |
| 33 | if !name.isEmpty || !lines.isEmpty { chunks.append((name, lines)) } |
| 34 | var sections = chunks.enumerated().map { index, chunk in |
| 35 | Self(id: index, name: chunk.name, text: chunk.lines.joined(separator: "\n"), |
| 36 | failed: failed && (hosted ? !chunk.name.isEmpty : index == chunks.count - 1), |
| 37 | isSummary: hosted && chunk.name.isEmpty && chunk.lines.first?.hasPrefix("Hosted CI on ") == true) |
| 38 | } |
| 39 | // Local ci.d stops at the first failure. This is a hint: tool output |
| 40 | // can imitate a basename marker. Grouping never removes any log line. |
| 41 | if failed, !hosted, let last = sections.popLast() { sections.insert(last, at: 0) } |
| 42 | return sections |
| 43 | } |
| 44 | // runner/thin/judge_hosted.go: strings.Fields, then clipName, in UTF-8 bytes. |
| 45 | private static func hostedName(_ name: String) -> String { |
| 46 | let collapsed = name.unicodeScalars.split(whereSeparator: { $0.properties.isWhitespace }).map(String.init).joined(separator: " ") |
| 47 | var result = "" |
| 48 | for scalar in collapsed.unicodeScalars { |
| 49 | guard result.utf8.count + scalar.utf8.count <= 120 else { break } |
| 50 | result.unicodeScalars.append(scalar) |
| 51 | } |
| 52 | return result |
| 53 | } |
| 54 | |
| 55 | } |
| 56 | |