Factory: backlog column with queue buttons, status-tinted issue cards
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
51a60ee75e
commit
25e4167f56
9 files changed
+318
-4
No files matched your search
@@ -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<User>, State(app): State<Ap
|
||||
})))
|
||||
}
|
||||
|
||||
/// Open issues per repository as the forge lists them; cached for a minute so that the board can ask often.
|
||||
static BACKLOG_CACHE: std::sync::OnceLock<std::sync::Mutex<std::collections::HashMap<String, (std::time::Instant, Vec<Value>)>>> = 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<User>, State(app): State<AppState>, Query(q): Query<BacklogQuery>) -> 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<i64> = 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<i64>,
|
||||
}
|
||||
|
||||
/// Put backlog issues into the process: give them the factory label, the next poll picks them up.
|
||||
async fn factory_enqueue(Extension(user): Extension<User>, State(app): State<AppState>, headers: axum::http::HeaderMap, Json(b): Json<Enqueue>) -> 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<User>, State(app): State<AppState>) -> ApiResult {
|
||||
let s: crate::factory::FactorySettings = app.store.user_setting(&user.id, "factory")?;
|
||||
|
||||
Reference in new issue
Block a user