gitbay/MRs/MRModels.swift

v1.6.1
gitbay-ios/gitbay/MRs/MRModels.swift history · blame · raw

315 lines · 13046 bytes

  1import Foundation
  2
  3/// The MR below or above a stack member: `mr show` sends `{number, title}`.
  4nonisolated struct StackRef: Decodable, Sendable, Hashable, Identifiable {
  5    let number: Int64
  6    let title: String
  7
  8    var id: Int64 { number }
  9}
 10
 11/// One row of `mr list`, and the header half of `mr show`.
 12nonisolated struct MergeRequest: Decodable, Sendable, Hashable, Identifiable {
 13    let number: Int64
 14    let title: String
 15    let state: String
 16    let author: String
 17    /// "branch" for a same-repo MR, "owner/name:branch" cross-repo,
 18    /// "" when the source is gone.
 19    let source: String
 20    let targetRef: String
 21    let headSHA: String
 22    let body: String?
 23    let createdAt: Date
 24    /// Open but not asking: does not merge, does not appear in a review queue.
 25    let draft: Bool
 26    let stackedOn: StackRef?
 27
 28    enum CodingKeys: String, CodingKey {
 29        case number, title, state, author, source, body, draft
 30        case targetRef = "target_ref"
 31        case headSHA = "head_sha"
 32        case createdAt = "created_at"
 33        case stackedOn = "stacked_on"
 34    }
 35
 36    init(from decoder: Decoder) throws {
 37        let container = try decoder.container(keyedBy: CodingKeys.self)
 38        number = try container.decode(Int64.self, forKey: .number)
 39        title = try container.decode(String.self, forKey: .title)
 40        state = try container.decode(String.self, forKey: .state)
 41        author = try container.decode(String.self, forKey: .author)
 42        source = try container.decode(String.self, forKey: .source)
 43        targetRef = try container.decode(String.self, forKey: .targetRef)
 44        headSHA = try container.decode(String.self, forKey: .headSHA)
 45        body = try container.decodeIfPresent(String.self, forKey: .body)
 46        createdAt = try container.decode(Date.self, forKey: .createdAt)
 47        draft = try container.decodeIfPresent(Bool.self, forKey: .draft) ?? false
 48        stackedOn = try container.decodeIfPresent(StackRef.self, forKey: .stackedOn)
 49    }
 50
 51    var id: Int64 { number }
 52    var isOpen: Bool { state == "open" }
 53}
 54
 55/// One row of `mr revisions`: a head the merge request has had. `n`
 56/// counts from the first push; `current` marks the head it has now.
 57nonisolated struct MRRevision: Decodable, Sendable, Hashable, Identifiable {
 58    let n: Int
 59    let sha: String
 60    let baseSHA: String?
 61    let createdAt: String
 62    let current: Bool?
 63
 64    enum CodingKeys: String, CodingKey {
 65        case n, sha, current
 66        case baseSHA = "base_sha"
 67        case createdAt = "created_at"
 68    }
 69
 70    var id: Int { n }
 71    var isCurrent: Bool { current ?? false }
 72}
 73
 74/// The whole of `mr show`.
 75nonisolated struct MRDetail: Decodable, Sendable, Hashable {
 76    let number: Int64
 77    let title: String
 78    let state: String
 79    let author: String
 80    let source: String
 81    let targetRef: String
 82    let headSHA: String
 83    let body: String?
 84    let bodyFormat: String?
 85    let milestone: String?
 86    /// Omitted by the server when there are none.
 87    let labels: [String]
 88    let createdAt: Date
 89    /// Set once the merge request is resolved. The actor is absent when
 90    /// the account is gone, or the row was imported.
 91    let mergedAt: Date?
 92    let mergedBy: String?
 93    let closedAt: Date?
 94    let closedBy: String?
 95    /// The merge request this one was closed in favour of. Omitted by
 96    /// the server when there is none.
 97    let supersededBy: Int64?
 98    let checks: [Check]?
 99    let checksCombined: String?
100    let unresolvedThreads: Int?
101    let commits: [MRCommit]?
102    let comments: [MRComment]?
103    let reviews: [Review]?
104    /// Open but not asking: does not merge, does not appear in a review queue.
105    let draft: Bool
106    let reviewRequests: [String]
107    let stackedOn: StackRef?
108    let stacked: [StackRef]
109    /// The merge gates, computed by the server the way `mr merge` will
110    /// judge them. Absent from a server older than v1.16.0.
111    let gates: Gates?
112
113    enum CodingKeys: String, CodingKey {
114        case number, title, state, author, source, body, milestone, labels, checks, commits, comments, reviews, draft, stacked, gates
115        case bodyFormat = "body_format"
116        case targetRef = "target_ref"
117        case headSHA = "head_sha"
118        case createdAt = "created_at"
119        case mergedAt = "merged_at"
120        case mergedBy = "merged_by"
121        case closedAt = "closed_at"
122        case closedBy = "closed_by"
123        case supersededBy = "superseded_by"
124        case checksCombined = "checks_combined"
125        case unresolvedThreads = "unresolved_threads"
126        case reviewRequests = "review_requests"
127        case stackedOn = "stacked_on"
128    }
129
130    init(from decoder: Decoder) throws {
131        let container = try decoder.container(keyedBy: CodingKeys.self)
132        number = try container.decode(Int64.self, forKey: .number)
133        title = try container.decode(String.self, forKey: .title)
134        state = try container.decode(String.self, forKey: .state)
135        author = try container.decode(String.self, forKey: .author)
136        source = try container.decode(String.self, forKey: .source)
137        targetRef = try container.decode(String.self, forKey: .targetRef)
138        headSHA = try container.decode(String.self, forKey: .headSHA)
139        body = try container.decodeIfPresent(String.self, forKey: .body)
140        bodyFormat = try container.decodeIfPresent(String.self, forKey: .bodyFormat)
141        milestone = try container.decodeIfPresent(String.self, forKey: .milestone)
142        labels = try container.decodeIfPresent([String].self, forKey: .labels) ?? []
143        createdAt = try container.decode(Date.self, forKey: .createdAt)
144        mergedAt = try container.decodeIfPresent(Date.self, forKey: .mergedAt)
145        mergedBy = try container.decodeIfPresent(String.self, forKey: .mergedBy)
146        closedAt = try container.decodeIfPresent(Date.self, forKey: .closedAt)
147        closedBy = try container.decodeIfPresent(String.self, forKey: .closedBy)
148        supersededBy = try container.decodeIfPresent(Int64.self, forKey: .supersededBy)
149        checks = try container.decodeIfPresent([Check].self, forKey: .checks)
150        checksCombined = try container.decodeIfPresent(String.self, forKey: .checksCombined)
151        unresolvedThreads = try container.decodeIfPresent(Int.self, forKey: .unresolvedThreads)
152        commits = try container.decodeIfPresent([MRCommit].self, forKey: .commits)
153        comments = try container.decodeIfPresent([MRComment].self, forKey: .comments)
154        reviews = try container.decodeIfPresent([Review].self, forKey: .reviews)
155        draft = try container.decodeIfPresent(Bool.self, forKey: .draft) ?? false
156        reviewRequests = try container.decodeIfPresent([String].self, forKey: .reviewRequests) ?? []
157        stackedOn = try container.decodeIfPresent(StackRef.self, forKey: .stackedOn)
158        stacked = try container.decodeIfPresent([StackRef].self, forKey: .stacked) ?? []
159        gates = try container.decodeIfPresent(Gates.self, forKey: .gates)
160    }
161
162    var isOpen: Bool { state == "open" }
163
164    /// What stands between the merge request and a merge, all at once.
165    nonisolated struct Gates: Decodable, Sendable, Hashable {
166        let draft: Bool
167        let approvalsRequired: Int
168        /// Fresh approvals from reviewers who count.
169        let approvals: [String]
170        let changesRequested: [String]
171        let codeownersRequired: Bool
172        let ownersOutstanding: [Owners]
173        let resolvedRequired: Bool
174        let openThreads: Int
175        let checksRequired: Bool
176        /// Combined check status; nil when none reported.
177        let checks: String?
178        let fastForward: Bool
179        let unmet: [String]
180
181        var isMet: Bool { unmet.isEmpty }
182
183        enum CodingKeys: String, CodingKey {
184            case draft, approvals, checks, unmet
185            case approvalsRequired = "approvals_required"
186            case changesRequested = "changes_requested"
187            case codeownersRequired = "codeowners_required"
188            case ownersOutstanding = "owners_outstanding"
189            case resolvedRequired = "resolved_required"
190            case openThreads = "open_threads"
191            case checksRequired = "checks_required"
192            case fastForward = "fast_forward"
193        }
194
195        init(from decoder: Decoder) throws {
196            let c = try decoder.container(keyedBy: CodingKeys.self)
197            draft = try c.decodeIfPresent(Bool.self, forKey: .draft) ?? false
198            approvalsRequired = try c.decodeIfPresent(Int.self, forKey: .approvalsRequired) ?? 0
199            approvals = try c.decodeIfPresent([String].self, forKey: .approvals) ?? []
200            changesRequested = try c.decodeIfPresent([String].self, forKey: .changesRequested) ?? []
201            codeownersRequired = try c.decodeIfPresent(Bool.self, forKey: .codeownersRequired) ?? false
202            ownersOutstanding = try c.decodeIfPresent([Owners].self, forKey: .ownersOutstanding) ?? []
203            resolvedRequired = try c.decodeIfPresent(Bool.self, forKey: .resolvedRequired) ?? false
204            openThreads = try c.decodeIfPresent(Int.self, forKey: .openThreads) ?? 0
205            checksRequired = try c.decodeIfPresent(Bool.self, forKey: .checksRequired) ?? false
206            checks = try c.decodeIfPresent(String.self, forKey: .checks)
207            fastForward = try c.decodeIfPresent(Bool.self, forKey: .fastForward) ?? false
208            unmet = try c.decodeIfPresent([String].self, forKey: .unmet) ?? []
209        }
210
211        /// Files whose owners have not approved, and who those owners are.
212        nonisolated struct Owners: Decodable, Sendable, Hashable, Identifiable {
213            let files: [String]
214            let owners: [String]
215            var id: String { files.joined(separator: "\u{1f}") }
216        }
217    }
218
219    nonisolated struct Check: Decodable, Sendable, Hashable {
220        let context: String
221        let state: String
222        let url: String?
223        /// When the check last changed state.
224        let updatedAt: Date?
225        /// How long the build behind the check ran, as the server
226        /// formats it ("1m12s"). Only CI checks have a build to time.
227        let duration: String?
228
229        enum CodingKeys: String, CodingKey {
230            case context, state, url, duration
231            case updatedAt = "updated_at"
232        }
233    }
234
235    nonisolated struct MRCommit: Decodable, Sendable, Hashable, Identifiable {
236        let sha: String
237        let subject: String
238        var id: String { sha }
239        var shortSHA: String { String(sha.prefix(10)) }
240    }
241
242    nonisolated struct MRComment: Decodable, Sendable, Hashable, Identifiable {
243        let author: String
244        let body: String
245        let bodyFormat: String?
246        let createdAt: Date
247
248        enum CodingKeys: String, CodingKey {
249            case author, body
250            case bodyFormat = "body_format"
251            case createdAt = "created_at"
252        }
253
254        var id: String { author + createdAt.timeIntervalSince1970.description + body }
255    }
256
257    nonisolated struct Review: Decodable, Sendable, Hashable, Identifiable {
258        let reviewer: String
259        let verdict: String
260        /// The review predates the current head — it approved older code.
261        let stale: Bool
262        /// Whether the merge gates count this verdict: a reader may review
263        /// a public merge request, but only a writer decides it. Absent
264        /// before v1.16.0, when only writers could review at all.
265        let counts: Bool
266        let createdAt: Date?
267
268        enum CodingKeys: String, CodingKey {
269            case reviewer, verdict, stale, counts
270            case createdAt = "created_at"
271        }
272
273        init(from decoder: Decoder) throws {
274            let c = try decoder.container(keyedBy: CodingKeys.self)
275            reviewer = try c.decode(String.self, forKey: .reviewer)
276            verdict = try c.decode(String.self, forKey: .verdict)
277            stale = try c.decodeIfPresent(Bool.self, forKey: .stale) ?? false
278            counts = try c.decodeIfPresent(Bool.self, forKey: .counts) ?? true
279            createdAt = try c.decodeIfPresent(Date.self, forKey: .createdAt)
280        }
281
282        var id: String { reviewer }
283    }
284}
285
286/// One review thread of `mr threads`.
287nonisolated struct ReviewThread: Decodable, Sendable, Hashable, Identifiable {
288    let id: Int64
289    let path: String
290    let side: String // new | old
291    let line: Int64
292    /// Anchored to an older head; the diff has moved under it.
293    let stale: Bool
294    let resolvedBy: String?
295    let comments: [Comment]
296
297    enum CodingKeys: String, CodingKey {
298        case id, path, side, line, stale, comments
299        case resolvedBy = "resolved_by"
300    }
301
302    var isResolved: Bool { resolvedBy != nil }
303
304    nonisolated struct Comment: Decodable, Sendable, Hashable, Identifiable {
305        let id: Int64
306        let author: String
307        let body: String
308        let createdAt: Date
309
310        enum CodingKeys: String, CodingKey {
311            case id, author, body
312            case createdAt = "created_at"
313        }
314    }
315}