diff --git a/CHANGELOG.md b/CHANGELOG.md index f34e77d..7b73ea0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,13 @@ All notable changes to CodeCraft. Versions follow [Semantic Versioning](https://semver.org/); until 1.0, minor versions may contain breaking changes. +## [Unreleased] + +### Added + +- **Factory: backlog column.** All open issues of the configured repositories that are not in the factory yet appear in a new column on the far left. Each card has a *Queue* button (and the column *Queue all*, with a confirmation): the issue gets the factory label on the forge and the factory takes it up straight away. Issues whose author may not file factory issues are shown but cannot be queued. The list is read from the forge (cached for a minute) and follows the project dropdown. +- **Factory: cards are tinted by status** – a faint background and border colour per state (review, question, approved, build, acceptance, failed, done), taken from the theme's own colours. + ## [v202611-00068] – 2026-11-20 ### Added diff --git a/README.md b/README.md index b7415fe..3c4bab5 100644 --- a/README.md +++ b/README.md @@ -332,6 +332,12 @@ phase the model, agent (CodeCraft or Claude Code), permission mode, timeout, tri technical failures, the skills that are switched on for it, plus the number of rework rounds (default 2). Failed or rejected issues can be restarted from the board, finished ones are closed with a comment. +**Backlog.** The first column of the board lists every open issue of the configured +repositories that is not in the factory yet. *Queue* gives an issue the factory label on the +forge (so it enters the review at once); *Queue all* does it for the whole list. Issues of authors +who are not allowed to file factory issues are shown but cannot be queued. Cards are tinted by +status. + **Files in the repository** (all optional, kept in `.factory/`): | File | Purpose | diff --git a/server/src/api.rs b/server/src/api.rs index b18ece9..8a90a3b 100644 --- a/server/src/api.rs +++ b/server/src/api.rs @@ -168,6 +168,8 @@ pub fn router(app: AppState) -> Router { .route("/account/factory", get(get_factory).put(put_factory)) .route("/account/factory/test", post(test_factory)) .route("/factory/overview", get(factory_overview)) + .route("/factory/backlog", get(factory_backlog)) + .route("/factory/backlog/enqueue", post(factory_enqueue)) .route("/factory/poll", post(factory_poll)) .route("/factory/issues/restart", post(factory_restart)) .route("/factory/issue-chats", post(factory_new_issue_chat)) @@ -2754,6 +2756,92 @@ async fn factory_overview(Extension(user): Extension, State(app): State)>>> = std::sync::OnceLock::new(); + +#[derive(Deserialize)] +struct BacklogQuery { + #[serde(default)] + refresh: bool, +} + +/// The backlog: open issues of the configured repositories that are not in the factory yet. +async fn factory_backlog(Extension(user): Extension, State(app): State, Query(q): Query) -> ApiResult { + let s: crate::factory::FactorySettings = app.store.user_setting(&user.id, "factory")?; + let cache = BACKLOG_CACHE.get_or_init(Default::default); + let tracked_all = app.store.factory_issues(&user.id)?; + let (mut issues, mut errors) = (Vec::new(), Vec::new()); + for cfg in s.repos.iter().filter(|r| r.enabled).map(|r| s.resolve(r)).filter(|r| !r.token.is_empty()) { + let key = format!("{}|{}", user.id, cfg.url); + let cached = cache.lock().unwrap().get(&key).filter(|(t, _)| !q.refresh && t.elapsed().as_secs() < 60).map(|(_, v)| v.clone()); + let result = async { + let g = crate::factory::Forge::new(&cfg)?; + let open = match cached { + Some(v) => v, + None => { + let v = g.open_issues().await?; + cache.lock().unwrap().insert(key.clone(), (std::time::Instant::now(), v.clone())); + v + } + }; + let allowed = crate::factory::allowed_authors(&g, &cfg).await?; + let tracked: Vec = tracked_all.iter().filter(|r| r["repo"].as_str() == Some(cfg.url.as_str())).map(|r| r["number"].as_i64().unwrap_or(0)).collect(); + anyhow::Ok(crate::factory::backlog_of(&cfg, &open, &tracked, &allowed)) + } + .await; + match result { + Ok(v) => issues.extend(v), + Err(e) => errors.push(format!("{}: {e:#}", cfg.url)), + } + } + Ok(Json(json!({"issues": issues, "errors": errors}))) +} + +#[derive(Deserialize)] +struct Enqueue { + repo: String, + numbers: Vec, +} + +/// Put backlog issues into the process: give them the factory label, the next poll picks them up. +async fn factory_enqueue(Extension(user): Extension, State(app): State, headers: axum::http::HeaderMap, Json(b): Json) -> ApiResult { + let cfg = factory_repo_cfg(&app, &user, &b.repo)?; + if b.numbers.is_empty() || b.numbers.len() > 100 { + return Err(ApiError(StatusCode::BAD_REQUEST, "1 to 100 issues".into())); + } + let bad = |e: anyhow::Error| ApiError(StatusCode::BAD_GATEWAY, format!("{e:#}")); + let g = crate::factory::Forge::new(&cfg).map_err(bad)?; + let allowed = crate::factory::allowed_authors(&g, &cfg).await.map_err(bad)?; + let open = g.open_issues().await.map_err(bad)?; + g.ensure_labels().await.map_err(bad)?; + let (mut queued, mut errors) = (Vec::new(), Vec::new()); + for n in b.numbers { + let Some(issue) = open.iter().find(|i| crate::factory::issue_no(i) == n) else { + errors.push(json!({"number": n, "error": "not an open issue of this repository"})); + continue; + }; + let author = issue["user"]["login"].as_str().unwrap_or(""); + if !allowed.iter().any(|a| a.eq_ignore_ascii_case(author)) { + errors.push(json!({"number": n, "error": format!("the author {author} is not allowed to file factory issues (see the repository settings)")})); + continue; + } + match g.add_label(n as u64, &cfg.label).await { + Ok(()) => queued.push(n), + Err(e) => errors.push(json!({"number": n, "error": format!("{e:#}")})), + } + } + BACKLOG_CACHE.get_or_init(Default::default).lock().unwrap().remove(&format!("{}|{}", user.id, cfg.url)); + app.audit(Some(&user), "factory.backlog.enqueue", &format!("{} {:?}", b.repo, queued), &crate::auth::client_ip(&headers)); + // Take the issues up right away instead of waiting for the next poll. + if !queued.is_empty() { + let (app2, user2) = (app.clone(), user.clone()); + tokio::spawn(async move { + let _ = crate::factory::poll_repo(&app2, &user2, &cfg).await; + }); + } + Ok(Json(json!({"queued": queued, "errors": errors}))) +} + /// Look for new issues right now instead of waiting for the next poll. async fn factory_poll(Extension(user): Extension, State(app): State) -> ApiResult { let s: crate::factory::FactorySettings = app.store.user_setting(&user.id, "factory")?; diff --git a/server/src/factory.rs b/server/src/factory.rs index 03efcc4..3004914 100644 --- a/server/src/factory.rs +++ b/server/src/factory.rs @@ -556,6 +556,49 @@ impl Forge { Ok(out) } + /// All open issues (pull requests excluded), up to 200, in the shape of `issues()`. + pub async fn open_issues(&self) -> Result> { + let mut out = Vec::new(); + for page in 1..=4 { + let url = match self.kind { + Kind::Gitea => self.rp(&format!("/issues?state=open&type=issues&limit=50&page={page}")), + Kind::Github => self.rp(&format!("/issues?state=open&per_page=50&page={page}")), + Kind::Gitlab => self.rp(&format!("/issues?state=opened&per_page=50&page={page}")), + }; + let v = self.call(reqwest::Method::GET, url, None).await?; + let a = v.as_array().cloned().unwrap_or_default(); + let n = a.len(); + for i in a { + if !i.get("pull_request").map_or(true, |p| p.is_null()) { + continue; + } + out.push(if self.kind == Kind::Gitlab { norm_gitlab_issue(&i) } else { i }); + } + if n < 50 { + break; + } + } + Ok(out) + } + + /// Add one label to an issue without touching its other labels. + pub async fn add_label(&self, issue: u64, name: &str) -> Result<()> { + match self.kind { + Kind::Gitea => { + let all = self.label_names().await?; + let id = all.iter().find(|l| l["name"].as_str() == Some(name)).map(|l| l["id"].clone()).with_context(|| format!("label {name} does not exist"))?; + self.call(reqwest::Method::POST, self.rp(&format!("/issues/{issue}/labels")), Some(json!({"labels": [id]}))).await?; + } + Kind::Github => { + self.call(reqwest::Method::POST, self.rp(&format!("/issues/{issue}/labels")), Some(json!({"labels": [name]}))).await?; + } + Kind::Gitlab => { + self.call(reqwest::Method::PUT, self.rp(&format!("/issues/{issue}")), Some(json!({"add_labels": name}))).await?; + } + } + Ok(()) + } + /// Is the issue still open on the forge? pub async fn issue_is_open(&self, number: u64) -> Result { let v = self.call(reqwest::Method::GET, self.rp(&format!("/issues/{number}")), None).await?; @@ -738,7 +781,12 @@ impl Forge { /// GitLab issue → {number, title, body, user.login, html_url}. fn norm_gitlab_issue(i: &Value) -> Value { - json!({"number": i["iid"], "title": i["title"], "body": i["description"], "user": {"login": i["author"]["username"]}, "html_url": i["web_url"]}) + json!({"number": i["iid"], "title": i["title"], "body": i["description"], "user": {"login": i["author"]["username"]}, "html_url": i["web_url"], "labels": i["labels"]}) +} + +/// Label names of an issue (Gitea/GitHub: objects with a name, GitLab: plain strings). +pub fn issue_labels(i: &Value) -> Vec { + i["labels"].as_array().map(|a| a.iter().filter_map(|l| l["name"].as_str().or_else(|| l.as_str()).map(String::from)).collect()).unwrap_or_default() } fn urlenc(s: &str) -> String { @@ -1396,6 +1444,33 @@ pub(crate) async fn alert(app: &App, repo: &str, n: i64, what: &str, detail: &st } /// One pass over a repository: take up new issues, restart reviews after answers. +/// Who may file issues for the factory: the configured users, by default the token's owner. +pub async fn allowed_authors(g: &Forge, cfg: &RepoCfg) -> Result> { + let mut allowed = cfg.allowed_users.clone(); + if allowed.is_empty() { + allowed.push(g.login().await?); + } + Ok(allowed) +} + +/// Open issues of the forge that are not in the factory (yet): the backlog. +/// `open` is the forge's list of open issues, `tracked` the issue numbers the factory already knows. +pub fn backlog_of(cfg: &RepoCfg, open: &[Value], tracked: &[i64], allowed: &[String]) -> Vec { + open.iter() + .filter_map(|i| { + let n = issue_no(i); + let author = i["user"]["login"].as_str().unwrap_or(""); + let ok = allowed.iter().any(|a| a.eq_ignore_ascii_case(author)); + let labelled = issue_labels(i).iter().any(|l| l == &cfg.label); + // Labelled issues of allowed authors are taken up by the next poll anyway. + if n == 0 || tracked.contains(&n) || (labelled && ok) { + return None; + } + Some(json!({"repo": cfg.url, "number": n, "title": i["title"], "author": author, "url": i["html_url"], "allowed": ok, "labels": issue_labels(i)})) + }) + .collect() +} + pub async fn poll_repo(app: &Arc, user: &User, cfg: &RepoCfg) -> Result<()> { let g = Forge::new(cfg)?; let mut allowed = cfg.allowed_users.clone(); @@ -1832,6 +1907,19 @@ pub async fn create_project(app: &Arc, user: &User, settings: &mut FactoryS mod tests { use super::*; + #[test] + fn backlog_skips_tracked_and_queued_issues() { + let cfg = repo("https://git.example.org/o/r.git"); + let issue = |n: i64, author: &str, labels: &[&str]| json!({"number": n, "title": format!("t{n}"), "user": {"login": author}, "html_url": "u", "labels": labels.iter().map(|l| json!({"name": l})).collect::>()}); + let open = vec![issue(1, "me", &[]), issue(2, "me", &[&cfg.label]), issue(3, "me", &["bug"]), issue(4, "stranger", &[&cfg.label]), issue(5, "me", &[])]; + let b = backlog_of(&cfg, &open, &[5], &["ME".to_string()]); + let nums: Vec = b.iter().map(|i| i["number"].as_i64().unwrap()).collect(); + // 2 is labelled by an allowed author (the poll takes it), 5 is tracked; 4 stays visible but not allowed. + assert_eq!(nums, vec![1, 3, 4]); + assert_eq!(b[2]["allowed"], false); + assert_eq!(issue_labels(&json!({"labels": ["a", {"name": "b"}]})), vec!["a", "b"]); + } + fn repo(url: &str) -> RepoCfg { serde_json::from_value(json!({"url": url, "token": "tok"})).unwrap() } diff --git a/web/src/components/Factory.tsx b/web/src/components/Factory.tsx index d514289..1b3205a 100644 --- a/web/src/components/Factory.tsx +++ b/web/src/components/Factory.tsx @@ -79,6 +79,15 @@ interface Issue { created_at: number; updated_at: number; } +interface BacklogIssue { + repo: string; + number: number; + title: string; + author: string; + url: string; + allowed: boolean; + labels: string[]; +} interface Run { id: string; repo: string; @@ -188,10 +197,21 @@ function FactoryOverview({ onSettings }: { onSettings: () => void }) { const [busy, setBusy] = useState(false); const [recreate, setRecreate] = useState(false); - const reload = () => - get("/factory/overview") + const [backlog, setBacklog] = useState([]); + const [backlogErrors, setBacklogErrors] = useState([]); + const loadBacklog = (refresh = false) => + get<{ issues: BacklogIssue[]; errors: string[] }>(`/factory/backlog${refresh ? "?refresh=1" : ""}`) + .then((r) => { + setBacklog(r.issues); + setBacklogErrors(r.errors); + }) + .catch(() => {}); + const reload = () => { + void loadBacklog(); + return get("/factory/overview") .then(setData) .catch((e) => toast(e.message)); + }; useEffect(() => { void reload(); const h = setInterval(() => { @@ -263,6 +283,24 @@ function FactoryOverview({ onSettings }: { onSettings: () => void }) { }; const issues = useMemo(() => (data?.issues ?? []).filter((i) => !repo || i.repo === repo), [data, repo]); + const backlogShown = useMemo(() => backlog.filter((i) => !repo || i.repo === repo), [backlog, repo]); + /** Put backlog issues into the process (they get the factory label on the forge). */ + const enqueue = async (items: BacklogIssue[]) => { + if (items.length === 0) return; + if (items.length > 1 && !(await confirmDialog({ message: t("factory.backlog.allConfirm", { n: items.length }), confirmLabel: t("factory.backlog.queueAll") }))) return; + setBusy(true); + try { + for (const r of [...new Set(items.map((i) => i.repo))]) { + const res = await post<{ queued: number[]; errors: { number: number; error: string }[] }>("/factory/backlog/enqueue", { repo: r, numbers: items.filter((i) => i.repo === r).map((i) => i.number) }); + for (const e of res.errors) toast(`#${e.number}: ${e.error}`); + } + } catch (e: any) { + toast(e.message); + } + setBusy(false); + await loadBacklog(true); + await reload(); + }; const runs = useMemo(() => (data?.runs ?? []).filter((r) => !repo || r.repo === repo), [data, repo]); const selected = issues.find((i) => `${i.repo}#${i.number}` === sel); const [pr, setPr] = useState(null); @@ -462,6 +500,31 @@ function FactoryOverview({ onSettings }: { onSettings: () => void }) {
+
+

+ {t("factory.col.backlog")} {backlogShown.length} +

+ {backlogShown.length > 1 && ( + + )} + {backlogShown.map((i) => ( +
+ + {repoName(i.repo)} #{i.number} · {i.author} + + + {i.title || `#${i.number}`} + + +
+ ))} + {backlogShown.length === 0 && {t("factory.backlog.empty")}} + {backlogErrors.length > 0 && {t("factory.backlog.error")}} +
{COLUMNS.map((c) => { const cards = issues.filter((i) => c.states.includes(i.state)); return ( @@ -475,7 +538,7 @@ function FactoryOverview({ onSettings }: { onSettings: () => void }) { const latest = active ?? rs[0]; const tokens = rs.reduce((a, r) => a + r.input_tokens + r.output_tokens, 0); return ( -