From bb8fae4d1145266de4f5117a3b8c48f2ccacb0be Mon Sep 17 00:00:00 2001 From: Christian Cleberg Date: Wed, 18 Mar 2026 19:28:42 -0500 Subject: lazily load file blobs from repository tree views --- Hutch/Models/Git.swift | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'Hutch/Models') diff --git a/Hutch/Models/Git.swift b/Hutch/Models/Git.swift index 19ec31b..905822e 100644 --- a/Hutch/Models/Git.swift +++ b/Hutch/Models/Git.swift @@ -113,7 +113,7 @@ struct GitTree: Codable, Sendable { struct GitTextBlob: Codable, Sendable { let id: String? let shortId: String? - let text: String + let text: String? let size: Int? } @@ -134,14 +134,16 @@ struct GitTreeEntryPage: Codable, Sendable { extension GitObject: Codable { private enum CodingKeys: String, CodingKey { case type, id, shortId, entries, text, size, content + case typename = "__typename" } init(from decoder: any Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) let type = try container.decodeIfPresent(String.self, forKey: .type) + let typename = try container.decodeIfPresent(String.self, forKey: .typename) - switch type { - case "TREE": + switch type ?? typename { + case "TREE", "Tree": let tree = GitTree( id: try container.decodeIfPresent(String.self, forKey: .id), shortId: try container.decodeIfPresent(String.self, forKey: .shortId), @@ -149,13 +151,12 @@ extension GitObject: Codable { ) self = .tree(tree) - case "BLOB": - // TextBlob has a "text" key; BinaryBlob does not - if container.contains(.text) { + case "BLOB", "TextBlob", "BinaryBlob": + if typename == "TextBlob" || container.contains(.text) { let blob = GitTextBlob( id: try container.decodeIfPresent(String.self, forKey: .id), shortId: try container.decodeIfPresent(String.self, forKey: .shortId), - text: try container.decode(String.self, forKey: .text), + text: try container.decodeIfPresent(String.self, forKey: .text), size: try container.decodeIfPresent(Int.self, forKey: .size) ) self = .textBlob(blob) @@ -184,12 +185,14 @@ extension GitObject: Codable { try container.encodeIfPresent(tree.entries, forKey: .entries) case .textBlob(let blob): try container.encode("BLOB", forKey: .type) + try container.encode("TextBlob", forKey: .typename) try container.encodeIfPresent(blob.id, forKey: .id) try container.encodeIfPresent(blob.shortId, forKey: .shortId) - try container.encode(blob.text, forKey: .text) + try container.encodeIfPresent(blob.text, forKey: .text) try container.encodeIfPresent(blob.size, forKey: .size) case .binaryBlob(let blob): try container.encode("BLOB", forKey: .type) + try container.encode("BinaryBlob", forKey: .typename) try container.encodeIfPresent(blob.id, forKey: .id) try container.encodeIfPresent(blob.shortId, forKey: .shortId) try container.encodeIfPresent(blob.size, forKey: .size) -- cgit v1.2.3 From 430741f0236ab21583acda8e2774d95e6e0e4ffe Mon Sep 17 00:00:00 2001 From: Christian Cleberg Date: Wed, 18 Mar 2026 19:34:59 -0500 Subject: stabilize build task identity and log cache keys --- Hutch/Models/Builds.swift | 27 +++++++++++++++++++++++++-- Hutch/Views/Builds/BuildDetailView.swift | 4 ++-- Hutch/Views/Builds/BuildDetailViewModel.swift | 17 +++++++++++------ HutchTests/BuildTaskTests.swift | 27 +++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 HutchTests/BuildTaskTests.swift (limited to 'Hutch/Models') diff --git a/Hutch/Models/Builds.swift b/Hutch/Models/Builds.swift index 6694c35..b0c0e8d 100644 --- a/Hutch/Models/Builds.swift +++ b/Hutch/Models/Builds.swift @@ -34,10 +34,33 @@ enum TaskStatus: String, Codable, Sendable { /// A single task within a build job. struct BuildTask: Codable, Sendable, Identifiable { - var id: String { name } + private(set) var ordinal: Int? let name: String let status: TaskStatus let log: BuildLog? + + var id: String { + if let ordinal { + return "\(ordinal):\(name)" + } + return [name, log?.fullURL, status.rawValue] + .compactMap { $0 } + .joined(separator: "::") + } + + var logCacheKey: String { + id + } + + func withOrdinal(_ ordinal: Int) -> BuildTask { + var task = self + task.ordinal = ordinal + return task + } + + private enum CodingKeys: String, CodingKey { + case name, status, log + } } // MARK: - Job Summary (for list view) @@ -90,7 +113,7 @@ struct JobDetail: Codable, Sendable { let visibility: Visibility? let image: String? let manifest: String? - let tasks: [BuildTask] + var tasks: [BuildTask] let log: BuildLog? let owner: Entity } diff --git a/Hutch/Views/Builds/BuildDetailView.swift b/Hutch/Views/Builds/BuildDetailView.swift index 977cd87..0479f76 100644 --- a/Hutch/Views/Builds/BuildDetailView.swift +++ b/Hutch/Views/Builds/BuildDetailView.swift @@ -312,13 +312,13 @@ private struct TaskLogSection: View { var body: some View { DisclosureGroup(isExpanded: $isExpanded) { - if viewModel.loadingTaskLogs.contains(task.name) { + if viewModel.loadingTaskLogs.contains(task.logCacheKey) { HStack { Spacer() ProgressView("Loading log…") Spacer() } - } else if let logText = viewModel.taskLogs[task.name] { + } else if let logText = viewModel.taskLogs[task.logCacheKey] { ScrollView(.horizontal, showsIndicators: false) { Text(logText) .font(.caption2.monospaced()) diff --git a/Hutch/Views/Builds/BuildDetailViewModel.swift b/Hutch/Views/Builds/BuildDetailViewModel.swift index c14e47b..1768e47 100644 --- a/Hutch/Views/Builds/BuildDetailViewModel.swift +++ b/Hutch/Views/Builds/BuildDetailViewModel.swift @@ -104,7 +104,11 @@ final class BuildDetailViewModel { variables: ["id": jobId], responseType: JobDetailResponse.self ) - job = result.job + var loadedJob = result.job + loadedJob.tasks = loadedJob.tasks.enumerated().map { index, task in + task.withOrdinal(index) + } + job = loadedJob } catch { self.error = error.localizedDescription } @@ -113,19 +117,20 @@ final class BuildDetailViewModel { } func loadTaskLog(task: BuildTask) async { + let cacheKey = task.logCacheKey guard let log = task.log, let logURL = URL(string: log.fullURL), - !loadingTaskLogs.contains(task.name), - taskLogs[task.name] == nil else { return } - loadingTaskLogs.insert(task.name) + !loadingTaskLogs.contains(cacheKey), + taskLogs[cacheKey] == nil else { return } + loadingTaskLogs.insert(cacheKey) do { - taskLogs[task.name] = try await client.fetchText(url: logURL) + taskLogs[cacheKey] = try await client.fetchText(url: logURL) } catch { self.error = error.localizedDescription } - loadingTaskLogs.remove(task.name) + loadingTaskLogs.remove(cacheKey) } func cancelJob() async { diff --git a/HutchTests/BuildTaskTests.swift b/HutchTests/BuildTaskTests.swift new file mode 100644 index 0000000..2e2a1e0 --- /dev/null +++ b/HutchTests/BuildTaskTests.swift @@ -0,0 +1,27 @@ +import Foundation +import Testing +@testable import Hutch + +struct BuildTaskTests { + + @Test + @MainActor + func duplicateTaskNamesProduceDistinctIDsAndLogCacheKeys() { + let firstTask = BuildTask( + name: "test", + status: .failed, + log: BuildLog(fullURL: "https://builds.sr.ht/job/1/task/1") + ).withOrdinal(0) + + let secondTask = BuildTask( + name: "test", + status: .failed, + log: BuildLog(fullURL: "https://builds.sr.ht/job/1/task/2") + ).withOrdinal(1) + + #expect(firstTask.id == "0:test") + #expect(secondTask.id == "1:test") + #expect(firstTask.id != secondTask.id) + #expect(firstTask.logCacheKey != secondTask.logCacheKey) + } +} -- cgit v1.2.3