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