gitbay/MRs/MRDetailViewModel.swift

v1.13.1
gitbay-ios/gitbay/MRs/MRDetailViewModel.swift history · blame · raw

250 lines · 9920 bytes

  1import Foundation
  2import Observation
  3
  4/// One merge request: `mr show`, the parsed diff, the review threads, and
  5/// every write the review cycle needs. Writes reload — the server's state
  6/// is the state.
  7@Observable
  8@MainActor
  9final class MRDetailViewModel {
 10
 11    private(set) var state: LoadState<MRDetail> = .loading
 12    private(set) var diff: UnifiedDiff?
 13    private(set) var threads: [ReviewThread] = []
 14    private(set) var revisions: [MRRevision] = []
 15    /// A write failed; the server's sentence, shown until the next action.
 16    private(set) var actionError: String?
 17    private(set) var working = false
 18
 19    private let client: GitbayClient
 20    let repoPath: String
 21    let number: Int64
 22
 23    init(client: GitbayClient, repoPath: String, number: Int64) {
 24        self.client = client
 25        self.repoPath = repoPath
 26        self.number = number
 27    }
 28
 29    private var ref: [String] { [repoPath, String(number)] }
 30
 31    func load() async {
 32        do {
 33            let detail = try await client.read(["mr", "show"] + ref, as: MRDetail.self)
 34            state = .loaded(detail)
 35        } catch {
 36            state = .from(error)
 37            return
 38        }
 39        // The diff and threads are secondary: their failure leaves the
 40        // header usable rather than sinking the screen.
 41        async let diffText = try? client.readText(["mr", "diff"] + ref)
 42        async let threadList = try? client.readList(["mr", "threads"] + ref, of: ReviewThread.self)
 43        async let revisionList = try? client.readList(["mr", "revisions"] + ref, of: MRRevision.self)
 44        diff = (await diffText).map(UnifiedDiff.parse)
 45        threads = await threadList ?? []
 46        revisions = await revisionList ?? []
 47    }
 48
 49    var unresolvedCount: Int {
 50        threads.count { !$0.isResolved }
 51    }
 52
 53    // MARK: - Writes
 54
 55    enum Verdict: String, Sendable {
 56        case approve = "--approve"
 57        case requestChanges = "--request-changes"
 58    }
 59
 60    func review(_ verdict: Verdict) async {
 61        await perform(["mr", "review"] + ref + [verdict.rawValue])
 62    }
 63
 64    /// Both fields are always sent; an empty body clears it. `format` is
 65    /// appended only when given; omitting it lets the server default.
 66    func edit(title: String, body: String, format: String? = nil) async {
 67        var argv = ["mr", "edit"] + ref + ["--title", title, "--file", "-"]
 68        if let format { argv.append(contentsOf: ["--format", format]) }
 69        await perform(argv, stdin: body)
 70    }
 71
 72    /// Open milestones for the picker; fetched on first use.
 73    private(set) var availableMilestones: [Milestone]?
 74
 75    func loadMilestones() async {
 76        guard availableMilestones == nil else { return }
 77        availableMilestones = (try? await client.readList(
 78            ["milestone", "list", repoPath, "--state", "open"], of: Milestone.self
 79        )) ?? []
 80    }
 81
 82    /// nil clears it; the command spells that "none".
 83    func setMilestone(_ title: String?) async {
 84        await perform(["mr", "milestone"] + ref + [title ?? "none"])
 85    }
 86
 87    /// The repository's labels, for chip colours. Read on first use from
 88    /// the Labels section, not in `load()`: colours are secondary, and the
 89    /// detail screen's tests count `load()`'s four reads.
 90    private(set) var colors = LabelColors()
 91    private var labelsLoaded = false
 92
 93    func loadLabels() async {
 94        guard !labelsLoaded else { return }
 95        labelsLoaded = true
 96        colors = LabelColors(
 97            (try? await client.readList(["label", "list", repoPath], of: IssueLabel.self)) ?? [])
 98    }
 99
100    /// A label that does not exist yet is created on the repository.
101    func addLabel(_ label: String) async {
102        await perform(["mr", "label"] + ref + ["--add", label])
103    }
104
105    func removeLabel(_ label: String) async {
106        await perform(["mr", "label"] + ref + ["--remove", label])
107    }
108
109    func comment(_ text: String, format: String? = nil) async {
110        // Long text travels in stdin, but the server only reads it when
111        // argv says so: --file - is required, not implied.
112        var argv = ["mr", "comment"] + ref + ["--file", "-"]
113        if let format { argv.append(contentsOf: ["--format", format]) }
114        await perform(argv, stdin: text)
115    }
116
117    /// React to the merge request, or to one of its comments. A reaction the
118    /// account already gave is taken back.
119    func react(_ name: String, comment: Int64?) async {
120        let given = comment == nil
121            ? state.value?.reactions
122            : state.value?.comments?.first { $0.commentID == comment }?.reactions
123        await perform(Reaction.argv(noun: "mr", repo: repoPath, number: number, comment: comment,
124                                    name: name, remove: Reaction.remove(name, given: given)))
125    }
126
127    /// `whenReady` queues the merge until the gates pass; the server
128    /// merges at once if they already do.
129    func merge(strategy: String? = nil, whenReady: Bool = false) async {
130        var argv = ["mr", "merge"] + ref
131        if let strategy { argv.append(contentsOf: ["--strategy", strategy]) }
132        if whenReady { argv.append("--when-ready") }
133        await perform(argv)
134    }
135
136    func cancelQueuedMerge() async {
137        await perform(["mr", "merge"] + ref + ["--cancel"])
138    }
139
140    /// `--by` records which merge request replaces this one; the server
141    /// refuses a closed, merged or self reference.
142    func close(inFavourOf successor: Int64? = nil) async {
143        var argv = ["mr", "close"] + ref
144        if let successor { argv.append(contentsOf: ["--by", String(successor)]) }
145        await perform(argv)
146    }
147
148    /// The repository's other open merge requests, for the close-in-
149    /// favour picker. Fetched on demand and cached like `loadBranches()`;
150    /// this merge request is dropped since it cannot supersede itself.
151    private(set) var openMergeRequests: [MergeRequest]?
152    private(set) var openMergeRequestsError: String?
153
154    func loadOpenMergeRequests() async {
155        guard openMergeRequests == nil else { return }
156        openMergeRequestsError = nil
157        do {
158            let rows = try await client.readList(
159                ["mr", "list", repoPath, "--state", "open"], of: MergeRequest.self)
160            openMergeRequests = rows.filter { $0.number != number }
161        } catch let error as GitbayError {
162            openMergeRequestsError = error.userFacingMessage
163        } catch {
164            openMergeRequestsError = GitbayError.transport(error).userFacingMessage
165        }
166    }
167
168    func setResolved(_ thread: ReviewThread, _ resolved: Bool) async {
169        await perform(["mr", resolved ? "resolve" : "unresolve"] + ref + [String(thread.id)])
170    }
171
172    func reply(to thread: ReviewThread, _ text: String) async {
173        await perform(
174            ["mr", "diff-comment"] + ref + ["--reply", String(thread.id), "--file", "-"],
175            stdin: text
176        )
177    }
178
179    /// Start a review thread on lines of the diff. The body goes over
180    /// stdin; a ```suggestion block in it is the server's to read. Returns
181    /// whether it was accepted, so the sheet can close.
182    func diffComment(_ draft: DiffCommentDraft, startLine: Int, body: String) async -> Bool {
183        await perform(draft.argv(repo: repoPath, number: number, startLine: startLine), stdin: body)
184        return actionError == nil
185    }
186
187    /// Commit a thread's suggestion to the source branch on the server.
188    func applySuggestion(_ thread: ReviewThread) async {
189        await perform(["mr", "apply-suggestion"] + ref + [String(thread.id)])
190    }
191
192    func setDraft(_ draft: Bool) async {
193        await perform(["mr", draft ? "draft" : "ready"] + ref)
194    }
195
196    func retarget(to branch: String) async {
197        await perform(["mr", "retarget"] + ref + [branch])
198    }
199
200    func requestReview(from user: String) async {
201        await perform(["mr", "review", "request"] + ref + ["--add", user])
202    }
203
204    func removeReviewRequest(_ user: String) async {
205        await perform(["mr", "review", "request"] + ref + ["--remove", user])
206    }
207
208    /// Branches for the retarget picker, fetched on demand from `repo refs`.
209    /// `nil` means "not yet loaded" so the picker can tell that apart from
210    /// a repository that genuinely has no other branches; a failed fetch
211    /// leaves `branches` nil and sets `branchesError` instead of silently
212    /// showing an empty list.
213    private(set) var branches: [String]?
214    private(set) var branchesError: String?
215
216    /// Cached for the life of this view model, same as `loadMilestones()`:
217    /// branches can go stale after a push, but re-opening the retarget
218    /// picker should not re-fetch every time. A pull-to-refresh of the
219    /// whole screen doesn't clear this cache either — retarget is a
220    /// secondary action, not the primary content `load()` refreshes.
221    func loadBranches() async {
222        guard branches == nil else { return }
223        branchesError = nil
224        do {
225            let refs = try await client.read(["repo", "refs", repoPath], as: RepoRefs.self)
226            branches = refs.branches.map(\.name)
227        } catch let error as GitbayError {
228            branchesError = error.userFacingMessage
229        } catch {
230            branchesError = GitbayError.transport(error).userFacingMessage
231        }
232    }
233
234    private func perform(_ argv: [String], stdin: String? = nil) async {
235        working = true
236        actionError = nil
237        defer { working = false }
238        do {
239            try await client.run(argv, stdin: stdin)
240            await load()
241        } catch let error as GitbayError {
242            // A refusal explains the rule (approvals missing, threads
243            // unresolved, checks red). Surface it verbatim; never route
244            // around it.
245            actionError = error.userFacingMessage
246        } catch {
247            actionError = GitbayError.transport(error).userFacingMessage
248        }
249    }
250}