gitbay/Repos/RepoSettingsViewModel.swift
219 lines · 7866 bytes
1import Foundation
2import Observation
3
4/// The repo settings screen: `repo settings show` for the admin knobs,
5/// `repo show` for description/topics/visibility, and one write command
6/// per control. Non-admins get the server's refusal verbatim.
7@Observable
8@MainActor
9final class RepoSettingsViewModel {
10
11 nonisolated struct Loaded: Sendable, Hashable {
12 let settings: RepoSettings
13 let detail: RepoDetail
14 }
15
16 private(set) var state: LoadState<Loaded> = .loading
17 private(set) var actionError: String?
18 private(set) var working = false
19
20 /// `repo deps status`, exposed as an optional plus a separate error
21 /// string rather than folded into one state: `nil` with no error means
22 /// "not loaded yet", `nil` with an error means "the read failed". The
23 /// settings screen must never render "checks are off" when it means
24 /// "we could not ask".
25 private(set) var deps: DepsStatus?
26 private(set) var depsError: String?
27
28 /// `repo access list`, with the same nil-plus-error shape as `deps`:
29 /// "not loaded" and "could not ask" must not read as "nobody granted".
30 private(set) var grants: [AccessGrant]?
31 private(set) var grantsError: String?
32
33 /// Branch names from `repo refs`, for the default-branch picker.
34 private(set) var branches: [String] = []
35
36 private let client: GitbayClient
37 let repoPath: String
38
39 init(client: GitbayClient, repoPath: String) {
40 self.client = client
41 self.repoPath = repoPath
42 }
43
44 func load() async {
45 do {
46 async let settings = client.read(
47 ["repo", "settings", "show", repoPath], as: RepoSettings.self)
48 async let detail = client.read(["repo", "show", repoPath], as: RepoDetail.self)
49 state = .loaded(Loaded(settings: try await settings, detail: try await detail))
50 } catch {
51 state = .from(error)
52 }
53 }
54
55 /// `repo deps status` — three words, unlike every other `repo` command
56 /// in this file, which is exactly why it's easy to get wrong.
57 func loadDeps() async {
58 depsError = nil
59 deps = nil
60 do {
61 deps = try await client.read(["repo", "deps", "status", repoPath], as: DepsStatus.self)
62 } catch let error as GitbayError {
63 depsError = error.userFacingMessage
64 } catch {
65 depsError = GitbayError.transport(error).userFacingMessage
66 }
67 }
68
69 /// `enable`/`disable` are two distinct commands, not one command with a
70 /// flag. Reloads the status afterward so the report reflects the
71 /// change; a refused write leaves `deps` untouched.
72 func setDepsEnabled(_ on: Bool) async {
73 await perform(["repo", "deps", on ? "enable" : "disable", repoPath], reload: loadDeps)
74 }
75
76 // MARK: - Writes, one command per knob
77
78 func setDescription(_ text: String) async {
79 await perform(["repo", "settings", "description", repoPath, text])
80 }
81
82 func setWebsite(_ url: String) async {
83 await perform(["repo", "settings", "website", repoPath, url])
84 }
85
86 func setVisibility(_ visibility: String) async {
87 await perform(["repo", "settings", "visibility", repoPath, visibility])
88 }
89
90 func setGitDaemon(_ on: Bool) async {
91 await perform(["repo", "settings", "git-daemon", repoPath, on ? "on" : "off"])
92 }
93
94 func protectBranch(_ branch: String) async {
95 await perform(["repo", "settings", "protect", repoPath, branch])
96 }
97
98 func unprotectBranch(_ branch: String) async {
99 await perform(["repo", "settings", "unprotect", repoPath, branch])
100 }
101
102 func setRequireApprovals(_ count: Int) async {
103 await perform(["repo", "settings", "require-approvals", repoPath, String(count)])
104 }
105
106 func setRequireResolved(_ on: Bool) async {
107 await perform(["repo", "settings", "require-resolved", repoPath, on ? "on" : "off"])
108 }
109
110 func setRequireChecks(_ on: Bool) async {
111 await perform(["repo", "settings", "require-checks", repoPath, on ? "on" : "off"])
112 }
113
114 func setRequireSigned(_ on: Bool) async {
115 await perform(["repo", "settings", "require-signed", repoPath, on ? "on" : "off"])
116 }
117
118 func setRequireCodeowners(_ on: Bool) async {
119 await perform(["repo", "settings", "require-codeowners", repoPath, on ? "on" : "off"])
120 }
121
122 func setRequireMR(_ on: Bool) async {
123 await perform(["repo", "settings", "require-mr", repoPath, on ? "on" : "off"])
124 }
125
126 func protectTag(_ glob: String) async {
127 await perform(["repo", "settings", "protect-tag", repoPath, glob])
128 }
129
130 func unprotectTag(_ glob: String) async {
131 await perform(["repo", "settings", "unprotect-tag", repoPath, glob])
132 }
133
134 /// `repo settings default-branch`: moves HEAD and the record. The
135 /// server refuses a branch that does not exist, so the picker only
136 /// offers what `repo refs` listed.
137 func setDefaultBranch(_ branch: String) async {
138 await perform(["repo", "settings", "default-branch", repoPath, branch])
139 }
140
141 nonisolated private struct Renamed: Decodable, Sendable {
142 let repo: String
143 }
144
145 /// `repo rename <owner/name> <new-name>`: the row and the directory
146 /// move together and clone URLs change. Returns the new path; the
147 /// caller navigates there, since this screen's path is now gone.
148 func rename(to name: String) async -> String? {
149 let name = name.trimmingCharacters(in: .whitespaces)
150 guard !name.isEmpty else { return nil }
151 working = true
152 actionError = nil
153 defer { working = false }
154 do {
155 let renamed = try await client.run(["repo", "rename", repoPath, name], as: Renamed.self)
156 return renamed?.repo
157 } catch let error as GitbayError {
158 actionError = error.userFacingMessage
159 } catch {
160 actionError = GitbayError.transport(error).userFacingMessage
161 }
162 return nil
163 }
164
165 func loadBranches() async {
166 let refs = try? await client.read(["repo", "refs", repoPath], as: RepoRefs.self)
167 branches = refs?.branches.map(\.name) ?? []
168 }
169
170 func loadGrants() async {
171 grantsError = nil
172 grants = nil
173 do {
174 grants = try await client.readList(["repo", "access", "list", repoPath], of: AccessGrant.self)
175 } catch let error as GitbayError {
176 grantsError = error.userFacingMessage
177 } catch {
178 grantsError = GitbayError.transport(error).userFacingMessage
179 }
180 }
181
182 /// `repo access grant <repo> <user> read|write|admin` — granting again
183 /// changes an existing user's role.
184 func grant(user: String, role: String) async {
185 let user = user.trimmingCharacters(in: .whitespaces)
186 guard !user.isEmpty else { return }
187 await perform(["repo", "access", "grant", repoPath, user, role], reload: loadGrants)
188 }
189
190 func revoke(user: String) async {
191 await perform(["repo", "access", "revoke", repoPath, user], reload: loadGrants)
192 }
193
194 func addTopic(_ topic: String) async {
195 await perform(["repo", "topics", "add", repoPath, topic])
196 }
197
198 func removeTopic(_ topic: String) async {
199 await perform(["repo", "topics", "remove", repoPath, topic])
200 }
201
202 private func perform(_ argv: [String]) async {
203 await perform(argv, reload: load)
204 }
205
206 private func perform(_ argv: [String], reload: () async -> Void) async {
207 working = true
208 actionError = nil
209 defer { working = false }
210 do {
211 try await client.run(argv)
212 await reload()
213 } catch let error as GitbayError {
214 actionError = error.userFacingMessage
215 } catch {
216 actionError = GitbayError.transport(error).userFacingMessage
217 }
218 }
219}