gitbay/MRs/MRModels.swift

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

307 lines · 12606 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    let createdAt: Date
 87    /// Set once the merge request is resolved. The actor is absent when
 88    /// the account is gone, or the row was imported.
 89    let mergedAt: Date?
 90    let mergedBy: String?
 91    let closedAt: Date?
 92    let closedBy: String?
 93    let checks: [Check]?
 94    let checksCombined: String?
 95    let unresolvedThreads: Int?
 96    let commits: [MRCommit]?
 97    let comments: [MRComment]?
 98    let reviews: [Review]?
 99    /// Open but not asking: does not merge, does not appear in a review queue.
100    let draft: Bool
101    let reviewRequests: [String]
102    let stackedOn: StackRef?
103    let stacked: [StackRef]
104    /// The merge gates, computed by the server the way `mr merge` will
105    /// judge them. Absent from a server older than v1.16.0.
106    let gates: Gates?
107
108    enum CodingKeys: String, CodingKey {
109        case number, title, state, author, source, body, milestone, checks, commits, comments, reviews, draft, stacked, gates
110        case bodyFormat = "body_format"
111        case targetRef = "target_ref"
112        case headSHA = "head_sha"
113        case createdAt = "created_at"
114        case mergedAt = "merged_at"
115        case mergedBy = "merged_by"
116        case closedAt = "closed_at"
117        case closedBy = "closed_by"
118        case checksCombined = "checks_combined"
119        case unresolvedThreads = "unresolved_threads"
120        case reviewRequests = "review_requests"
121        case stackedOn = "stacked_on"
122    }
123
124    init(from decoder: Decoder) throws {
125        let container = try decoder.container(keyedBy: CodingKeys.self)
126        number = try container.decode(Int64.self, forKey: .number)
127        title = try container.decode(String.self, forKey: .title)
128        state = try container.decode(String.self, forKey: .state)
129        author = try container.decode(String.self, forKey: .author)
130        source = try container.decode(String.self, forKey: .source)
131        targetRef = try container.decode(String.self, forKey: .targetRef)
132        headSHA = try container.decode(String.self, forKey: .headSHA)
133        body = try container.decodeIfPresent(String.self, forKey: .body)
134        bodyFormat = try container.decodeIfPresent(String.self, forKey: .bodyFormat)
135        milestone = try container.decodeIfPresent(String.self, forKey: .milestone)
136        createdAt = try container.decode(Date.self, forKey: .createdAt)
137        mergedAt = try container.decodeIfPresent(Date.self, forKey: .mergedAt)
138        mergedBy = try container.decodeIfPresent(String.self, forKey: .mergedBy)
139        closedAt = try container.decodeIfPresent(Date.self, forKey: .closedAt)
140        closedBy = try container.decodeIfPresent(String.self, forKey: .closedBy)
141        checks = try container.decodeIfPresent([Check].self, forKey: .checks)
142        checksCombined = try container.decodeIfPresent(String.self, forKey: .checksCombined)
143        unresolvedThreads = try container.decodeIfPresent(Int.self, forKey: .unresolvedThreads)
144        commits = try container.decodeIfPresent([MRCommit].self, forKey: .commits)
145        comments = try container.decodeIfPresent([MRComment].self, forKey: .comments)
146        reviews = try container.decodeIfPresent([Review].self, forKey: .reviews)
147        draft = try container.decodeIfPresent(Bool.self, forKey: .draft) ?? false
148        reviewRequests = try container.decodeIfPresent([String].self, forKey: .reviewRequests) ?? []
149        stackedOn = try container.decodeIfPresent(StackRef.self, forKey: .stackedOn)
150        stacked = try container.decodeIfPresent([StackRef].self, forKey: .stacked) ?? []
151        gates = try container.decodeIfPresent(Gates.self, forKey: .gates)
152    }
153
154    var isOpen: Bool { state == "open" }
155
156    /// What stands between the merge request and a merge, all at once.
157    nonisolated struct Gates: Decodable, Sendable, Hashable {
158        let draft: Bool
159        let approvalsRequired: Int
160        /// Fresh approvals from reviewers who count.
161        let approvals: [String]
162        let changesRequested: [String]
163        let codeownersRequired: Bool
164        let ownersOutstanding: [Owners]
165        let resolvedRequired: Bool
166        let openThreads: Int
167        let checksRequired: Bool
168        /// Combined check status; nil when none reported.
169        let checks: String?
170        let fastForward: Bool
171        let unmet: [String]
172
173        var isMet: Bool { unmet.isEmpty }
174
175        enum CodingKeys: String, CodingKey {
176            case draft, approvals, checks, unmet
177            case approvalsRequired = "approvals_required"
178            case changesRequested = "changes_requested"
179            case codeownersRequired = "codeowners_required"
180            case ownersOutstanding = "owners_outstanding"
181            case resolvedRequired = "resolved_required"
182            case openThreads = "open_threads"
183            case checksRequired = "checks_required"
184            case fastForward = "fast_forward"
185        }
186
187        init(from decoder: Decoder) throws {
188            let c = try decoder.container(keyedBy: CodingKeys.self)
189            draft = try c.decodeIfPresent(Bool.self, forKey: .draft) ?? false
190            approvalsRequired = try c.decodeIfPresent(Int.self, forKey: .approvalsRequired) ?? 0
191            approvals = try c.decodeIfPresent([String].self, forKey: .approvals) ?? []
192            changesRequested = try c.decodeIfPresent([String].self, forKey: .changesRequested) ?? []
193            codeownersRequired = try c.decodeIfPresent(Bool.self, forKey: .codeownersRequired) ?? false
194            ownersOutstanding = try c.decodeIfPresent([Owners].self, forKey: .ownersOutstanding) ?? []
195            resolvedRequired = try c.decodeIfPresent(Bool.self, forKey: .resolvedRequired) ?? false
196            openThreads = try c.decodeIfPresent(Int.self, forKey: .openThreads) ?? 0
197            checksRequired = try c.decodeIfPresent(Bool.self, forKey: .checksRequired) ?? false
198            checks = try c.decodeIfPresent(String.self, forKey: .checks)
199            fastForward = try c.decodeIfPresent(Bool.self, forKey: .fastForward) ?? false
200            unmet = try c.decodeIfPresent([String].self, forKey: .unmet) ?? []
201        }
202
203        /// Files whose owners have not approved, and who those owners are.
204        nonisolated struct Owners: Decodable, Sendable, Hashable, Identifiable {
205            let files: [String]
206            let owners: [String]
207            var id: String { files.joined(separator: "\u{1f}") }
208        }
209    }
210
211    nonisolated struct Check: Decodable, Sendable, Hashable {
212        let context: String
213        let state: String
214        let url: String?
215        /// When the check last changed state.
216        let updatedAt: Date?
217        /// How long the build behind the check ran, as the server
218        /// formats it ("1m12s"). Only CI checks have a build to time.
219        let duration: String?
220
221        enum CodingKeys: String, CodingKey {
222            case context, state, url, duration
223            case updatedAt = "updated_at"
224        }
225    }
226
227    nonisolated struct MRCommit: Decodable, Sendable, Hashable, Identifiable {
228        let sha: String
229        let subject: String
230        var id: String { sha }
231        var shortSHA: String { String(sha.prefix(10)) }
232    }
233
234    nonisolated struct MRComment: Decodable, Sendable, Hashable, Identifiable {
235        let author: String
236        let body: String
237        let bodyFormat: String?
238        let createdAt: Date
239
240        enum CodingKeys: String, CodingKey {
241            case author, body
242            case bodyFormat = "body_format"
243            case createdAt = "created_at"
244        }
245
246        var id: String { author + createdAt.timeIntervalSince1970.description + body }
247    }
248
249    nonisolated struct Review: Decodable, Sendable, Hashable, Identifiable {
250        let reviewer: String
251        let verdict: String
252        /// The review predates the current head — it approved older code.
253        let stale: Bool
254        /// Whether the merge gates count this verdict: a reader may review
255        /// a public merge request, but only a writer decides it. Absent
256        /// before v1.16.0, when only writers could review at all.
257        let counts: Bool
258        let createdAt: Date?
259
260        enum CodingKeys: String, CodingKey {
261            case reviewer, verdict, stale, counts
262            case createdAt = "created_at"
263        }
264
265        init(from decoder: Decoder) throws {
266            let c = try decoder.container(keyedBy: CodingKeys.self)
267            reviewer = try c.decode(String.self, forKey: .reviewer)
268            verdict = try c.decode(String.self, forKey: .verdict)
269            stale = try c.decodeIfPresent(Bool.self, forKey: .stale) ?? false
270            counts = try c.decodeIfPresent(Bool.self, forKey: .counts) ?? true
271            createdAt = try c.decodeIfPresent(Date.self, forKey: .createdAt)
272        }
273
274        var id: String { reviewer }
275    }
276}
277
278/// One review thread of `mr threads`.
279nonisolated struct ReviewThread: Decodable, Sendable, Hashable, Identifiable {
280    let id: Int64
281    let path: String
282    let side: String // new | old
283    let line: Int64
284    /// Anchored to an older head; the diff has moved under it.
285    let stale: Bool
286    let resolvedBy: String?
287    let comments: [Comment]
288
289    enum CodingKeys: String, CodingKey {
290        case id, path, side, line, stale, comments
291        case resolvedBy = "resolved_by"
292    }
293
294    var isResolved: Bool { resolvedBy != nil }
295
296    nonisolated struct Comment: Decodable, Sendable, Hashable, Identifiable {
297        let id: Int64
298        let author: String
299        let body: String
300        let createdAt: Date
301
302        enum CodingKeys: String, CodingKey {
303            case id, author, body
304            case createdAt = "created_at"
305        }
306    }
307}