| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | use serde::Deserialize; |
| 9 | use topcoat::{ |
| 10 | Result, |
| 11 | context::{Cx, app_context}, |
| 12 | icon::{icon, iconify::iconify_icon}, |
| 13 | router::{StatusCode, content::Form, error::forbidden, page}, |
| 14 | view::{attributes, component, view}, |
| 15 | }; |
| 16 | |
| 17 | use crate::{ |
| 18 | application::{ |
| 19 | AppConfig, Error, ImportStatus, NewImport, RepoView, import_failure_message, import_repo, |
| 20 | run_import, |
| 21 | }, |
| 22 | components::{ |
| 23 | button::{ButtonVariant, button}, |
| 24 | flash::{FlashKind, flash}, |
| 25 | input::input, |
| 26 | label::label, |
| 27 | }, |
| 28 | domain::{DomainError, ImportSource, RepoName, Visibility}, |
| 29 | infrastructure::{ |
| 30 | git::{DiskGitImport, DiskGitStorage}, |
| 31 | repository::SqliteRepoRepo, |
| 32 | }, |
| 33 | }; |
| 34 | |
| 35 | use super::{ |
| 36 | context::{current_actor, location, memberships, orgs, pool, repos, server_error}, |
| 37 | profile::profile_for, |
| 38 | repo::{Tab, clone_block, clone_url_for, new_repo_form, repo_header}, |
| 39 | }; |
| 40 | |
| 41 | #[derive(Debug, Deserialize)] |
| 42 | struct ImportForm { |
| 43 | name: String, |
| 44 | source: String, |
| 45 | } |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | #[page(POST "/{handle}/repos/import")] |
| 58 | async fn start_import(cx: &Cx, Form(submitted): Form<ImportForm>) -> Result { |
| 59 | let profile = profile_for(cx).await?; |
| 60 | |
| 61 | let outcome = import_repo( |
| 62 | ¤t_actor(cx).await?, |
| 63 | &profile.handle, |
| 64 | &NewImport { |
| 65 | name: submitted.name.clone(), |
| 66 | source: submitted.source.clone(), |
| 67 | }, |
| 68 | &orgs(cx), |
| 69 | &memberships(cx), |
| 70 | &repos(cx), |
| 71 | ) |
| 72 | .await; |
| 73 | |
| 74 | let repo = match outcome { |
| 75 | Ok(repo) => repo, |
| 76 | Err(Error::Domain(DomainError::Forbidden)) => return Err(forbidden().into()), |
| 77 | Err(error) => { |
| 78 | let Some(message) = import_failure_message(&error, &submitted.name) else { |
| 79 | return Err(server_error(std::io::Error::other(error.to_string()))); |
| 80 | }; |
| 81 | |
| 82 | |
| 83 | |
| 84 | let state = ImportBlock { |
| 85 | name: submitted.name, |
| 86 | source: submitted.source, |
| 87 | error: message, |
| 88 | }; |
| 89 | |
| 90 | return view! { |
| 91 | new_repo_form( |
| 92 | handle: profile.handle.as_str(), |
| 93 | name: "", |
| 94 | description: "", |
| 95 | visibility: Visibility::Public, |
| 96 | error: "", |
| 97 | import: &state, |
| 98 | ) |
| 99 | }; |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | let data_dir = app_context::<AppConfig>(cx).data_dir.clone(); |
| 104 | let pool = pool(cx).clone(); |
| 105 | let handle = profile.handle.clone(); |
| 106 | let id = repo.id.clone(); |
| 107 | let name = repo.name.clone(); |
| 108 | |
| 109 | tokio::spawn(async move { |
| 110 | if let Err(error) = run_import( |
| 111 | &id, |
| 112 | &handle, |
| 113 | &SqliteRepoRepo::new(pool), |
| 114 | &DiskGitImport::new(data_dir.clone()), |
| 115 | &DiskGitStorage::new(data_dir), |
| 116 | ) |
| 117 | .await |
| 118 | { |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | eprintln!("steid: import of {handle}/{name} could not be recorded: {error}"); |
| 124 | } |
| 125 | }); |
| 126 | |
| 127 | view! { |
| 128 | (StatusCode::SEE_OTHER) |
| 129 | (location(&format!("/{}/repos/{}", profile.handle, repo.name))?) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | #[derive(Debug, Default, Clone)] |
| 143 | pub(super) struct ImportBlock { |
| 144 | pub name: String, |
| 145 | pub source: String, |
| 146 | |
| 147 | pub error: String, |
| 148 | } |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | #[component] |
| 161 | pub(super) async fn import_block(handle: &str, state: &ImportBlock) -> Result { |
| 162 | let ImportBlock { |
| 163 | name, |
| 164 | source, |
| 165 | error, |
| 166 | } = state; |
| 167 | view! { |
| 168 | <section class="mt-10 border-t border-border pt-8"> |
| 169 | <h2 class="text-sm font-medium">"Or import an existing repository"</h2> |
| 170 | <p class="mt-1 text-xs text-muted-foreground"> |
| 171 | "Copies every branch and tag from a public " |
| 172 | <span class="font-mono">"https://"</span> |
| 173 | " repository. It runs in the background." |
| 174 | </p> |
| 175 | |
| 176 | if !error.is_empty() { |
| 177 | <div class="mt-4"> |
| 178 | flash(kind: FlashKind::Error, (error.as_str())) |
| 179 | </div> |
| 180 | } |
| 181 | |
| 182 | <form method="post" action=(format!("/{handle}/repos/import")) class="mt-4 space-y-5"> |
| 183 | <div class="space-y-2"> |
| 184 | label(attrs: attributes! { for="source" }, "URL") |
| 185 | input(attrs: attributes! { |
| 186 | id="source" |
| 187 | name="source" |
| 188 | type="url" |
| 189 | value=(source.as_str()) |
| 190 | placeholder="https://github.com/owner/project.git" |
| 191 | required=(true) |
| 192 | maxlength=(ImportSource::MAX_LEN.to_string()) |
| 193 | }) |
| 194 | <p class="text-xs text-muted-foreground"> |
| 195 | "Public repositories only. Steid will not send credentials." |
| 196 | </p> |
| 197 | </div> |
| 198 | |
| 199 | <div class="space-y-2"> |
| 200 | label(attrs: attributes! { for="import-name" }, "Name") |
| 201 | input(attrs: attributes! { |
| 202 | id="import-name" |
| 203 | name="name" |
| 204 | type="text" |
| 205 | value=(name.as_str()) |
| 206 | placeholder="taken from the URL" |
| 207 | maxlength=(RepoName::MAX_LEN.to_string()) |
| 208 | }) |
| 209 | <p class="text-xs text-muted-foreground"> |
| 210 | "Optional. Blank uses the last part of the URL, without " |
| 211 | <span class="font-mono">".git"</span> "." |
| 212 | </p> |
| 213 | </div> |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | button( |
| 219 | attrs: attributes! { type="submit" }, |
| 220 | variant: ButtonVariant::Outline, |
| 221 | "Import repository" |
| 222 | ) |
| 223 | </form> |
| 224 | </section> |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | #[component] |
| 234 | pub(super) async fn import_state_panel(repo: &RepoView, status: &ImportStatus) -> Result { |
| 235 | let source = status |
| 236 | .source |
| 237 | .as_ref() |
| 238 | .map(ImportSource::as_str) |
| 239 | .unwrap_or("its source"); |
| 240 | |
| 241 | view! { |
| 242 | if status.is_running() { |
| 243 | <div class="rounded-lg border border-border px-4 py-5"> |
| 244 | |
| 245 | |
| 246 | |
| 247 | <p class="flex items-start gap-2 text-sm"> |
| 248 | |
| 249 | |
| 250 | icon(data: iconify_icon!("feather:download-cloud"), attrs: attributes! { |
| 251 | class="mt-0.5 size-4 shrink-0 text-primary" |
| 252 | }) |
| 253 | <span class="min-w-0"> |
| 254 | "Importing from " |
| 255 | |
| 256 | |
| 257 | <span class="font-mono break-all">(format!("{source}…"))</span> |
| 258 | </span> |
| 259 | </p> |
| 260 | <p class="mt-2 text-xs text-muted-foreground"> |
| 261 | "Every branch and tag is being copied. Refresh this page to check — " |
| 262 | "there is nothing to wait on here." |
| 263 | </p> |
| 264 | </div> |
| 265 | } else { |
| 266 | <div class="rounded-lg border border-border px-4 py-5"> |
| 267 | <p class="flex items-start gap-2 text-sm"> |
| 268 | icon(data: iconify_icon!("feather:alert-triangle"), attrs: attributes! { |
| 269 | class="mt-0.5 size-4 shrink-0 text-muted-foreground" |
| 270 | }) |
| 271 | <span class="min-w-0"> |
| 272 | "The import from " |
| 273 | <span class="font-mono break-all">(source)</span> |
| 274 | " did not finish." |
| 275 | </span> |
| 276 | </p> |
| 277 | match &status.error { |
| 278 | |
| 279 | |
| 280 | |
| 281 | Some(reason) => <pre class="mt-3 overflow-x-auto rounded-lg border border-border bg-surface px-4 py-3 font-mono text-xs whitespace-pre-wrap">(reason)</pre>, |
| 282 | None => "", |
| 283 | } |
| 284 | if repo.viewer_is_owner { |
| 285 | <p class="mt-4 text-xs text-muted-foreground"> |
| 286 | "Nothing was kept. Delete this repository from its " |
| 287 | <a |
| 288 | href=(format!("/{}/repos/{}/settings", repo.handle, repo.name)) |
| 289 | class="underline hover:text-foreground" |
| 290 | >"settings"</a> |
| 291 | " to free the name and try again." |
| 292 | </p> |
| 293 | } |
| 294 | </div> |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | /// The About sidebar, for a repository that has no contents to count. |
| 300 | /// |
| 301 | /// The same shape as the ready one — description, a definition list, the clone address |
| 302 | /// — with every number an em dash. Leaving the rows out would make the sidebar change |
| 303 | /// height when the import lands; a zero would be a claim that the repository is empty. |
| 304 | #[component] |
| 305 | pub(super) async fn import_about(repo: &RepoView, clone: &str) -> Result { |
| 306 | view! { |
| 307 | <section class="text-sm"> |
| 308 | <h2 class="text-xs font-medium uppercase tracking-wider text-muted-foreground"> |
| 309 | "About" |
| 310 | </h2> |
| 311 | match &repo.description { |
| 312 | Some(description) => <p class="mt-2 leading-relaxed">(description)</p>, |
| 313 | None => <p class="mt-2 text-muted-foreground">"No description."</p>, |
| 314 | } |
| 315 | </section> |
| 316 | |
| 317 | <dl class="mt-4 space-y-1.5 border-t border-border pt-4 text-xs"> |
| 318 | for term in ["Commits", "Branches", "Tags", "Pushed"] { |
| 319 | <div class="flex items-baseline justify-between gap-3"> |
| 320 | <dt class="text-muted-foreground">(term)</dt> |
| 321 | <dd class="font-mono text-muted-foreground">"—"</dd> |
| 322 | </div> |
| 323 | } |
| 324 | </dl> |
| 325 | |
| 326 | clone_block( |
| 327 | handle: repo.handle.as_str(), |
| 328 | name: repo.name.as_str(), |
| 329 | // No revision, so no archive links: there is nothing to pack yet. Empty is |
| 330 | // exactly what an empty repository passes, and `clone_block` already treats |
| 331 | // it that way. |
| 332 | rev: "", |
| 333 | url: clone, |
| 334 | ) |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | /// The whole repository page while an import is running or after it failed. |
| 339 | /// |
| 340 | /// A component rather than a branch inside `repo_page`, so the landing page keeps one |
| 341 | /// shape: resolve the repository, and if it has not arrived, render this instead of |
| 342 | /// asking git five questions about an empty directory. |
| 343 | #[component] |
| 344 | pub(super) async fn importing_page(cx: &Cx, repo: &RepoView, status: &ImportStatus) -> Result { |
| 345 | let clone = clone_url_for(cx, repo); |
| 346 | |
| 347 | view! { |
| 348 | repo_header(repo: repo, rev: "", active: Tab::Code) |
| 349 | |
| 350 | <div class="flex flex-col lg:flex-row lg:items-start lg:gap-6"> |
| 351 | <div class="min-w-0 lg:flex-1"> |
| 352 | import_state_panel(repo: repo, status: status) |
| 353 | </div> |
| 354 | <aside class="order-first mb-6 w-full shrink-0 border-b border-border pb-6 lg:order-none lg:sticky lg:top-6 lg:mb-0 lg:w-72 lg:border-b-0 lg:pb-0"> |
| 355 | import_about(repo: repo, clone: clone.as_str()) |
| 356 | </aside> |
| 357 | </div> |
| 358 | } |
| 359 | } |