diff --git a/README.md b/README.md index cdd89cf..c1c461a 100644 --- a/README.md +++ b/README.md @@ -31,8 +31,8 @@ Aucune authentification : le service est prévu pour tourner en local. |--------------|----------------------------------------------------| | GET, POST | `/clients` (`?archived=true` pour inclure les archivés) — `{"name", "color"?}` | | PATCH, DELETE| `/clients/{id}` — `{"name"?, "archived"?, "color"?}` | -| GET, POST | `/clients/{id}/projects` — `{"name", "color"?}` | -| PATCH, DELETE| `/projects/{id}` — `"color": null` pour reprendre celle du client | +| GET, POST | `/clients/{id}/projects` — `{"name", "color"?, "repo_url"?}` | +| PATCH, DELETE| `/projects/{id}` — `"color": null` pour reprendre celle du client, `"repo_url": ""` pour l'effacer | | GET | `/timer` — entrée en cours ou `null` | | POST | `/timer/start` `{"project_id", "note"?}` · `/timer/stop` | | GET, POST | `/entries` (`?from&to&client_id&project_id`) | diff --git a/migrations/0003_repo_url.sql b/migrations/0003_repo_url.sql new file mode 100644 index 0000000..3301d15 --- /dev/null +++ b/migrations/0003_repo_url.sql @@ -0,0 +1,2 @@ +-- Optional link to the project's source repository (http(s) URL). +ALTER TABLE projects ADD COLUMN repo_url TEXT; diff --git a/src/models.rs b/src/models.rs index 33ba53f..740b8be 100644 --- a/src/models.rs +++ b/src/models.rs @@ -25,6 +25,8 @@ pub struct Project { pub color: Option, /// `color`, or the client's color. pub effective_color: String, + /// Link to the source repository. + pub repo_url: Option, pub archived: bool, pub created_at: DateTime, } @@ -97,6 +99,9 @@ pub struct NameInput { /// `#rrggbb`. Clients get one from the palette, projects inherit their client's. #[serde(default)] pub color: Option, + /// Projects only: `http(s)://` link to the source repository. + #[serde(default)] + pub repo_url: Option, } #[derive(Debug, Default, Deserialize)] @@ -106,6 +111,9 @@ pub struct UpdateInput { /// Absent: unchanged. `null`: reset to the client's color (projects only). #[serde(default, deserialize_with = "present")] pub color: Option>, + /// Projects only. Absent: unchanged. `null` or `""`: removed. + #[serde(default, deserialize_with = "present")] + pub repo_url: Option>, } #[derive(Debug, Deserialize)] diff --git a/src/repo/mod.rs b/src/repo/mod.rs index dd0e8ae..011a55a 100644 --- a/src/repo/mod.rs +++ b/src/repo/mod.rs @@ -33,6 +33,22 @@ fn clean_color(color: &str) -> AppResult { Ok(color.to_ascii_lowercase()) } +/// Validates an optional repository URL; blank means none. +fn clean_repo_url(url: &str) -> AppResult> { + let url = url.trim(); + if url.is_empty() { + return Ok(None); + } + let valid = (url.starts_with("https://") || url.starts_with("http://")) + && !url.contains(char::is_whitespace); + if !valid { + return Err(AppError::Validation(format!( + "URL du repo invalide « {url} » (doit commencer par http:// ou https://)" + ))); + } + Ok(Some(url.to_owned())) +} + /// Maps "zero rows affected" to `NotFound`. fn affected(result: sqlx::sqlite::SqliteQueryResult) -> AppResult<()> { if result.rows_affected() == 0 { diff --git a/src/repo/projects.rs b/src/repo/projects.rs index 87d6648..a360ccb 100644 --- a/src/repo/projects.rs +++ b/src/repo/projects.rs @@ -1,12 +1,13 @@ use sqlx::SqlitePool; -use super::{affected, clean_color, clean_name}; +use super::{affected, clean_color, clean_name, clean_repo_url}; use crate::clock; use crate::error::{AppError, AppResult}; use crate::models::{Project, UpdateInput}; const SELECT: &str = "SELECT p.id, p.client_id, c.name AS client_name, p.name, - p.color, COALESCE(p.color, c.color) AS effective_color, p.archived, p.created_at + p.color, COALESCE(p.color, c.color) AS effective_color, p.repo_url, p.archived, + p.created_at FROM projects p JOIN clients c ON c.id = p.client_id"; /// Lists projects, optionally for one client. Archived projects (or projects of @@ -40,17 +41,21 @@ pub async fn create( client_id: i64, name: &str, color: Option<&str>, + repo_url: Option<&str>, ) -> AppResult { let name = clean_name(name)?; let color = color.map(clean_color).transpose()?; + let repo_url = repo_url.map(clean_repo_url).transpose()?.flatten(); // Surface a missing client as 404 rather than a foreign key error. super::clients::get(pool, client_id).await?; let id = sqlx::query( - "INSERT INTO projects (client_id, name, color, created_at) VALUES (?1, ?2, ?3, ?4)", + "INSERT INTO projects (client_id, name, color, repo_url, created_at) + VALUES (?1, ?2, ?3, ?4, ?5)", ) .bind(client_id) .bind(name) .bind(color) + .bind(repo_url) .bind(clock::ts(&clock::now())) .execute(pool) .await? @@ -64,10 +69,15 @@ pub async fn update(pool: &SqlitePool, id: i64, input: &UpdateInput) -> AppResul Some(Some(c)) => Some(clean_color(c)?), _ => None, }; + let repo_url = match &input.repo_url { + Some(Some(u)) => clean_repo_url(u)?, + _ => None, + }; affected( sqlx::query( "UPDATE projects SET name = COALESCE(?2, name), archived = COALESCE(?3, archived), - color = CASE WHEN ?4 THEN ?5 ELSE color END + color = CASE WHEN ?4 THEN ?5 ELSE color END, + repo_url = CASE WHEN ?6 THEN ?7 ELSE repo_url END WHERE id = ?1", ) .bind(id) @@ -75,6 +85,8 @@ pub async fn update(pool: &SqlitePool, id: i64, input: &UpdateInput) -> AppResul .bind(input.archived) .bind(input.color.is_some()) .bind(color) + .bind(input.repo_url.is_some()) + .bind(repo_url) .execute(pool) .await?, )?; diff --git a/src/routes/api.rs b/src/routes/api.rs index 6bfc89e..a1f6aa7 100644 --- a/src/routes/api.rs +++ b/src/routes/api.rs @@ -90,7 +90,16 @@ async fn create_project( ) -> AppResult<(StatusCode, Json)> { Ok(( StatusCode::CREATED, - Json(projects::create(&pool, client_id, &input.name, input.color.as_deref()).await?), + Json( + projects::create( + &pool, + client_id, + &input.name, + input.color.as_deref(), + input.repo_url.as_deref(), + ) + .await?, + ), )) } diff --git a/src/routes/pages.rs b/src/routes/pages.rs index 0564fa9..9a36139 100644 --- a/src/routes/pages.rs +++ b/src/routes/pages.rs @@ -417,6 +417,9 @@ struct ItemForm { /// Projects only: go back to the client's color. #[serde(default)] inherit: bool, + /// Projects only; blank removes it. + #[serde(default)] + repo_url: Option, } #[derive(Deserialize)] @@ -476,7 +479,14 @@ async fn create_project( // The color picker is prefilled with the client's color: keeping it means inheriting. let client = clients::get(&pool, client_id).await?; let color = f.color.filter(|c| !c.eq_ignore_ascii_case(&client.color)); - projects::create(&pool, client_id, &f.name, color.as_deref()).await?; + projects::create( + &pool, + client_id, + &f.name, + color.as_deref(), + f.repo_url.as_deref(), + ) + .await?; back_to_clients() } @@ -497,6 +507,7 @@ async fn save_project( let input = UpdateInput { name: Some(f.name), color, + repo_url: f.repo_url.map(Some), ..Default::default() }; projects::update(&pool, id, &input).await?; diff --git a/static/app.css b/static/app.css index 0120217..900d2cd 100644 --- a/static/app.css +++ b/static/app.css @@ -87,6 +87,7 @@ label { display: flex; align-items: center; gap: .35rem; color: var(--muted); } border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: .45rem .9rem; cursor: pointer; white-space: nowrap; } +a.btn { color: var(--text); text-decoration: none; } .btn:hover { border-color: var(--muted); } .btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; } .btn.danger { color: var(--danger); } @@ -153,6 +154,7 @@ input[type="color"] { padding: .4rem 0; border-bottom: 1px solid var(--border); } .projects li.archived { opacity: .6; } +.row.grow input.repo-url { flex: 1.3; } .projects li > form::before { content: "-"; color: var(--muted); flex: none; } /* Periods & reports */ diff --git a/templates/_icons.html b/templates/_icons.html index 21c2337..948adfe 100644 --- a/templates/_icons.html +++ b/templates/_icons.html @@ -6,6 +6,7 @@ {%- else if name == "check" -%} {%- else if name == "plus" -%} {%- else if name == "reset" -%} +{%- else if name == "link" -%} {%- else if name == "archive" -%} {%- else if name == "unarchive" -%} {%- endif -%} diff --git a/templates/clients.html b/templates/clients.html index d103885..4ef65e5 100644 --- a/templates/clients.html +++ b/templates/clients.html @@ -47,6 +47,7 @@ +
{% if p.color.is_some() %} @@ -61,6 +62,9 @@
+ {% if let Some(u) = p.repo_url %} + {% call icons::icon("link") %}{% endcall %} + {% endif %} {% if p.archived %} {% else %} @@ -76,6 +80,7 @@
+
diff --git a/tests/api.rs b/tests/api.rs index 68e504a..8f58bb0 100644 --- a/tests/api.rs +++ b/tests/api.rs @@ -707,3 +707,55 @@ async fn move_timer_start() { let (status, _) = send_form(&app, "/ui/timer/debut", &format!("started_at={future}")).await; assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY); } + +#[tokio::test] +async fn project_repo_url() { + let app = app().await; + let (_, c) = send( + &app, + "POST", + "/api/clients", + Some(json!({ "name": "Acme" })), + ) + .await; + let uri = format!("/api/clients/{}/projects", c["id"]); + + let (status, p) = send( + &app, + "POST", + &uri, + Some(json!({ "name": "Site", "repo_url": " https://git.example.com/acme/site " })), + ) + .await; + assert_eq!(status, StatusCode::CREATED); + assert_eq!(p["repo_url"], "https://git.example.com/acme/site"); + + let (status, _) = send( + &app, + "POST", + &uri, + Some(json!({ "name": "Bad", "repo_url": "git@example.com:acme/site.git" })), + ) + .await; + assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY); + + // Absent: unchanged; "" clears it. + let project = format!("/api/projects/{}", p["id"]); + let (_, p) = send(&app, "PATCH", &project, Some(json!({ "name": "Site web" }))).await; + assert_eq!(p["repo_url"], "https://git.example.com/acme/site"); + let (_, html) = get_html(&app, "/clients").await; + assert!(html.contains("href=\"https://git.example.com/acme/site\"")); + let (_, p) = send(&app, "PATCH", &project, Some(json!({ "repo_url": "" }))).await; + assert!(p["repo_url"].is_null()); + + // HTML form: the URL field is saved with the project. + let (status, _) = send_form( + &app, + &project.replace("/api/", "/ui/"), + "name=Site+web&repo_url=https%3A%2F%2Fgit.example.com%2Fx", + ) + .await; + assert_eq!(status, StatusCode::SEE_OTHER); + let (_, p) = send(&app, "GET", &format!("{uri}?archived=true"), None).await; + assert_eq!(p[0]["repo_url"], "https://git.example.com/x"); +}