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")?;
|
||||
|
||||
+89
-1
@@ -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<Vec<Value>> {
|
||||
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<bool> {
|
||||
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<String> {
|
||||
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<Vec<String>> {
|
||||
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<Value> {
|
||||
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<App>, 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<App>, 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::<Vec<_>>()});
|
||||
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<i64> = 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()
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user