krz/hutch
an ios client for sourcehut
clone: git clone https://gitbay.org/krz/hutch.git
v3.1.3: Hutch/Models/RepositorySummary.swift · raw
1import Foundation
2
3/// Lightweight repository model matching the fields returned by the
4/// repositories list query. Avoids optionalizing all fields on the full
5/// `Repository` model.
6struct RepositorySummary: Codable, Sendable, Identifiable, Hashable {
7 let id: Int
8 /// GraphQL resource identifier used by `repository(rid:)` queries.
9 let rid: String
10 let service: SRHTService
11 let name: String
12 let description: String?
13 let visibility: Visibility
14 let updated: Date
15 let owner: Entity
16 let head: Reference?
17
18 enum CodingKeys: String, CodingKey {
19 case id, rid, service, name, description, visibility, updated, owner
20 case head = "HEAD"
21 }
22
23 init(
24 id: Int,
25 rid: String,
26 service: SRHTService,
27 name: String,
28 description: String?,
29 visibility: Visibility,
30 updated: Date,
31 owner: Entity,
32 head: Reference?
33 ) {
34 self.id = id
35 self.rid = rid
36 self.service = service
37 self.name = name
38 self.description = description
39 self.visibility = visibility
40 self.updated = updated
41 self.owner = owner
42 self.head = head
43 }
44
45 init(from decoder: any Decoder) throws {
46 let container = try decoder.container(keyedBy: CodingKeys.self)
47 self.id = try container.decode(Int.self, forKey: .id)
48 self.rid = try container.decode(String.self, forKey: .rid)
49 self.service = try container.decodeIfPresent(SRHTService.self, forKey: .service) ?? .git
50 self.name = try container.decode(String.self, forKey: .name)
51 self.description = try container.decodeIfPresent(String.self, forKey: .description)
52 self.visibility = try container.decode(Visibility.self, forKey: .visibility)
53 self.updated = try container.decode(Date.self, forKey: .updated)
54 self.owner = try container.decode(Entity.self, forKey: .owner)
55 self.head = try container.decodeIfPresent(Reference.self, forKey: .head)
56 }
57}
58
59extension RepositorySummary {
60 var defaultBranchName: String? {
61 head?.name.replacingOccurrences(of: "refs/heads/", with: "")
62 }
63
64 func updating(
65 name: String? = nil,
66 description: String? = nil,
67 visibility: Visibility? = nil,
68 updated: Date? = nil,
69 head: Reference? = nil
70 ) -> RepositorySummary {
71 RepositorySummary(
72 id: id,
73 rid: rid,
74 service: service,
75 name: name ?? self.name,
76 description: description ?? self.description,
77 visibility: visibility ?? self.visibility,
78 updated: updated ?? self.updated,
79 owner: owner,
80 head: head ?? self.head
81 )
82 }
83
84 static func displayBranchName(for reference: Reference) -> String {
85 displayBranchName(for: reference.name)
86 }
87
88 static func displayBranchName(for referenceName: String) -> String {
89 referenceName.replacingOccurrences(of: "refs/heads/", with: "")
90 }
91}