oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1import Foundation
2
3struct ChangeDiff: Codable, Sendable, Equatable {
4 var taskId: String
5 var stepId: String
6 var attempt: Int64
7 var base: String
8 var candidateSha: String
9 var status: String
10 var oldPath: String? = nil
11 var path: String
12 var patch: String? = nil
13 var reason: String? = nil
14 var truncated: Bool
15 var hiddenCharacters: Bool? = nil
16
17 var hasHiddenCharacters: Bool { hiddenCharacters == true || VisibleCodeText.containsHidden(patch ?? "") }
18 var hiddenCharactersNotice: String? { hasHiddenCharacters ? EquipoStrings.text("native.diff.hidden_characters") : nil }
19 var copyText: String { VisibleCodeText.display(patch ?? "") }
20
21 var isValid: Bool {
22 guard !taskId.isEmpty, !stepId.isEmpty, attempt > 0,
23 (base.isEmpty || ThinChangeFiles.commit(base)), ThinChangeFiles.commit(candidateSha),
24 ThinChangeFiles.path(path), ["added", "modified", "deleted", "renamed"].contains(status),
25 (patch ?? "").utf8.count <= 64 * 1024, !(patch ?? "").contains("\0"),
26 (patch ?? "").unicodeScalars.filter({ $0 == "\n" }).count <= 2000 else { return false }
27 if status == "renamed" {
28 guard let oldPath, ThinChangeFiles.path(oldPath), oldPath != path else { return false }
29 } else if oldPath != nil { return false }
30 if let reason {
31 return ["binary", "too_large", "withheld", "unavailable", "expired"].contains(reason)
32 && (patch ?? "").isEmpty && !truncated
33 }
34 return (patch ?? "").isEmpty || patch?.unicodeScalars.last == "\n"
35 }
36 struct Line: Identifiable, Equatable {
37 enum Kind { case added, removed, context }
38 let id: Int
39 let text: String
40 func displaySegments() -> [VisibleCodeText.Segment] { VisibleCodeText.segments(text) }
41 var kind: Kind {
42 if text.hasPrefix("+") && !text.hasPrefix("+++") { return .added }
43 if text.hasPrefix("-") && !text.hasPrefix("---") { return .removed }
44 return .context
45 }
46 }
47 var lines: [Line] {
48 var lines = (patch ?? "").components(separatedBy: "\n")
49 if lines.last == "" { lines.removeLast() }
50 return lines.enumerated().map { Line(id: $0.offset, text: $0.element) }
51 }
52}
53extension EquipoClient {
54 func changeDiff(task: String, candidate: String, path: String) async throws -> ChangeDiff {
55 guard ThinChangeFiles.commit(candidate), ThinChangeFiles.path(path) else { throw EquipoAPIError.undecodable }
56 var request = APIRequest("GET", "/tasks/\(escape(task))/changes/diff",
57 query: [URLQueryItem(name: "candidate_sha", value: candidate), URLQueryItem(name: "path", value: path)], bearer: token)
58 request.maximumResponseBytes = 128 * 1024
59 let response = try await transport.send(request)
60 guard (200..<300).contains(response.status) else { throw Self.error(response) }
61 guard response.data.count <= request.maximumResponseBytes else { throw EquipoAPIError.tooLarge }
62 guard let diff = try? EquipoJSON.decoder.decode(ChangeDiff.self, from: response.data), diff.isValid,
63 diff.taskId == task, diff.candidateSha == candidate, diff.path == path else { throw EquipoAPIError.undecodable }
64 return diff
65 }
66}
67