Commit 8af268e4f7
Verified · cmc
Layout: unified · split
gitbay/Repos/RepoSettingsViewModel.swift +9
| @@ -177,6 +177,7 @@ final class RepoSettingsViewModel { | ||
| 177 | 177 | defer { working = false } |
| 178 | 178 | do { |
| 179 | 179 | let renamed = try await client.run(["repo", "rename", repoPath, name], as: Renamed.self) |
| 180 | NotificationCenter.default.post(name: .gitbayRepositoryChanged, object: repoPath) | |
| 180 | 181 | return renamed?.repo |
| 181 | 182 | } catch let error as GitbayError { |
| 182 | 183 | actionError = error.userFacingMessage |
| @@ -201,6 +202,7 @@ final class RepoSettingsViewModel { | ||
| 201 | 202 | defer { working = false } |
| 202 | 203 | do { |
| 203 | 204 | let moved = try await client.run(["repo", "transfer", repoPath, owner], as: Transferred.self) |
| 205 | NotificationCenter.default.post(name: .gitbayRepositoryChanged, object: repoPath) | |
| 204 | 206 | return moved?.repo |
| 205 | 207 | } catch let error as GitbayError { |
| 206 | 208 | // The owner is typed: a refusal of it says what to fix. |
| @@ -219,6 +221,13 @@ final class RepoSettingsViewModel { | ||
| 219 | 221 | defer { working = false } |
| 220 | 222 | do { |
| 221 | 223 | try await client.run(["repo", "delete", repoPath, "--yes"]) |
| 224 | NotificationCenter.default.post(name: .gitbayRepositoryChanged, object: repoPath) | |
| 225 | return true | |
| 226 | } catch GitbayError.notFound { | |
| 227 | // The client retries a failure once. A delete that removed the | |
| 228 | // row and then failed cleaning up answers the retry with not | |
| 229 | // found: the repository is gone either way. | |
| 230 | NotificationCenter.default.post(name: .gitbayRepositoryChanged, object: repoPath) | |
| 222 | 231 | return true |
| 223 | 232 | } catch let error as GitbayError { |
| 224 | 233 | actionError = error.userFacingMessage |
gitbay/Views/Dashboard/DashboardView.swift +4
| @@ -95,6 +95,10 @@ struct DashboardView: View { | ||
| 95 | 95 | .onReceive(NotificationCenter.default.publisher(for: .gitbaySavedQueriesChanged)) { _ in |
| 96 | 96 | Task { await model.load() } |
| 97 | 97 | } |
| 98 | // A pinned repository deleted or moved elsewhere. | |
| 99 | .onReceive(NotificationCenter.default.publisher(for: .gitbayRepositoryChanged)) { _ in | |
| 100 | Task { await model.load() } | |
| 101 | } | |
| 98 | 102 | .onChange(of: model.state.value?.unread) { _, count in |
| 99 | 103 | if let count { unread = count } |
| 100 | 104 | } |
gitbay/Views/Repos/RepoSettingsView.swift +18 −1
| @@ -24,6 +24,9 @@ struct RepoSettingsView: View { | ||
| 24 | 24 | @State private var deleteConfirm = "" |
| 25 | 25 | /// Set once the repository is deleted: nothing on this screen applies. |
| 26 | 26 | @State private var deleted = false |
| 27 | /// A refused rename, transfer or delete, shown where the user is: the | |
| 28 | /// screen's banner sits at the top of a long list, out of sight. | |
| 29 | @State private var refusal: String? | |
| 27 | 30 | @State private var loadedOnce = false |
| 28 | 31 | @State private var removingTopic: String? |
| 29 | 32 | @State private var newGrantUser = "" |
| @@ -84,6 +87,9 @@ struct RepoSettingsView: View { | ||
| 84 | 87 | .navigationTitle("Settings") |
| 85 | 88 | .navigationBarTitleDisplayMode(.inline) |
| 86 | 89 | .task { |
| 90 | // A screen returned to after its repository moved or went has | |
| 91 | // nothing to load; the notice is all it shows. | |
| 92 | guard !deleted, renamedTo == nil else { return } | |
| 87 | 93 | await model.load() |
| 88 | 94 | if !loadedOnce, let loaded = model.state.value { |
| 89 | 95 | descriptionText = loaded.detail.description ?? "" |
| @@ -95,6 +101,8 @@ struct RepoSettingsView: View { | ||
| 95 | 101 | .task { await model.loadGrants() } |
| 96 | 102 | .task { await model.loadBranches() } |
| 97 | 103 | .refreshable { |
| 104 | // Nothing to reload once the repository is gone or moved. | |
| 105 | guard !deleted, renamedTo == nil else { return } | |
| 98 | 106 | await model.load() |
| 99 | 107 | await model.loadDeps() |
| 100 | 108 | await model.loadGrants() |
| @@ -126,6 +134,8 @@ struct RepoSettingsView: View { | ||
| 126 | 134 | Task { |
| 127 | 135 | if let path = await model.rename(to: newName) { |
| 128 | 136 | renamedTo = path |
| 137 | } else { | |
| 138 | refusal = model.actionError | |
| 129 | 139 | } |
| 130 | 140 | } |
| 131 | 141 | } |
| @@ -135,6 +145,11 @@ struct RepoSettingsView: View { | ||
| 135 | 145 | } message: { |
| 136 | 146 | Text("Clone URLs change. The old path stops resolving. Type \(model.repoPath) to confirm.") |
| 137 | 147 | } |
| 148 | .alert("Not done", isPresented: Binding(get: { refusal != nil }, set: { if !$0 { refusal = nil } })) { | |
| 149 | Button("OK", role: .cancel) {} | |
| 150 | } message: { | |
| 151 | Text(refusal ?? "") | |
| 152 | } | |
| 138 | 153 | .alert("Transfer \(model.repoPath)", isPresented: $transferring) { |
| 139 | 154 | TextField("New owner", text: $newOwner) |
| 140 | 155 | .autocorrectionDisabled() |
| @@ -146,6 +161,8 @@ struct RepoSettingsView: View { | ||
| 146 | 161 | Task { |
| 147 | 162 | if let path = await model.transfer(to: newOwner) { |
| 148 | 163 | renamedTo = path |
| 164 | } else { | |
| 165 | refusal = model.actionError | |
| 149 | 166 | } |
| 150 | 167 | } |
| 151 | 168 | } |
| @@ -161,7 +178,7 @@ struct RepoSettingsView: View { | ||
| 161 | 178 | .textInputAutocapitalization(.never) |
| 162 | 179 | Button("Delete", role: .destructive) { |
| 163 | 180 | Task { |
| 164 | if await model.delete() { deleted = true } | |
| 181 | if await model.delete() { deleted = true } else { refusal = model.actionError } | |
| 165 | 182 | } |
| 166 | 183 | } |
| 167 | 184 | .disabled(deleteConfirm != model.repoPath) |
gitbay/Views/Repos/RepoView.swift +5
| @@ -98,6 +98,11 @@ struct RepoView: View { | ||
| 98 | 98 | .toolbar { toolbar } |
| 99 | 99 | .task { await model.load() } |
| 100 | 100 | .refreshable { await model.load() } |
| 101 | // Settings, pushed on top, may delete or move this repository; | |
| 102 | // reloading turns this screen into the not-found it now is. | |
| 103 | .onReceive(NotificationCenter.default.publisher(for: .gitbayRepositoryChanged)) { note in | |
| 104 | if note.object as? String == path { Task { await model.load() } } | |
| 105 | } | |
| 101 | 106 | .alert("Fork \(path)", isPresented: $confirmingFork) { |
| 102 | 107 | TextField("New name (optional)", text: $forkName) |
| 103 | 108 | .autocorrectionDisabled() |
gitbayTests/RepoTransferDeleteTests.swift +10
| @@ -59,4 +59,14 @@ struct RepoTransferDeleteTests { | ||
| 59 | 59 | #expect(!(await model.delete())) |
| 60 | 60 | #expect(model.actionError == "only the owner or an admin can delete cmc/app") |
| 61 | 61 | } |
| 62 | ||
| 63 | @Test func aDeleteWhoseRetryFindsNothingIsDone() async throws { | |
| 64 | let (client, stub) = try makeClient() | |
| 65 | stub.enqueue(.init(status: 500, json: #"{"protocol_version":1,"error":"removing the directory: busy","exit_code":1}"#, match: "cmd")) | |
| 66 | stub.enqueue(.init(status: 404, json: #"{"protocol_version":1,"error":"no repository cmc/app","exit_code":3}"#, match: "cmd")) | |
| 67 | let model = RepoSettingsViewModel(client: client, repoPath: "cmc/app") | |
| 68 | ||
| 69 | #expect(await model.delete()) | |
| 70 | #expect(model.actionError == nil) | |
| 71 | } | |
| 62 | 72 | } |