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:
Richard ZinkandClaude Sonnet 5.5 committed 2026-11-20 10:55:59 +01:00
1 parent 51a60ee75e
commit 25e4167f56
9 files changed
+318 -4

No files matched your search

+89 -1
View File
@@ -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()
}