Factory: versions and tags, releases, retry settings, pull of the default branch
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
06c1b8df1b
commit
cb1e31b6e9
13 files changed
+1082
-19
No files matched your search
+120
-13
@@ -43,6 +43,9 @@ pub struct PhaseCfg {
|
||||
pub permission_mode: String,
|
||||
#[serde(default = "default_timeout")]
|
||||
pub timeout_minutes: u64,
|
||||
/// Tries of this phase when it fails technically (timeout, no result, no changes) before the issue is `failed`.
|
||||
#[serde(default = "default_attempts")]
|
||||
pub max_attempts: i64,
|
||||
/// Extra instructions appended to the phase prompt.
|
||||
#[serde(default)]
|
||||
pub extra_prompt: String,
|
||||
@@ -57,10 +60,16 @@ fn default_mode() -> String {
|
||||
fn default_timeout() -> u64 {
|
||||
60
|
||||
}
|
||||
fn default_attempts() -> i64 {
|
||||
2
|
||||
}
|
||||
fn default_rework() -> i64 {
|
||||
2
|
||||
}
|
||||
|
||||
impl Default for PhaseCfg {
|
||||
fn default() -> Self {
|
||||
Self { model: String::new(), agent: default_agent(), permission_mode: default_mode(), timeout_minutes: default_timeout(), extra_prompt: String::new() }
|
||||
Self { model: String::new(), agent: default_agent(), permission_mode: default_mode(), timeout_minutes: default_timeout(), max_attempts: default_attempts(), extra_prompt: String::new() }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,6 +110,9 @@ pub struct RepoCfg {
|
||||
pub label: String,
|
||||
#[serde(default = "default_parallel")]
|
||||
pub max_parallel: usize,
|
||||
/// Rounds in which a failed acceptance sends the issue back to the build (0 = no rework).
|
||||
#[serde(default = "default_rework")]
|
||||
pub max_rework: i64,
|
||||
/// Only issues opened by these users start a run (empty = only the token's owner).
|
||||
#[serde(default)]
|
||||
pub allowed_users: Vec<String>,
|
||||
@@ -147,6 +159,7 @@ impl RepoCfg {
|
||||
self.label = default_label();
|
||||
}
|
||||
self.max_parallel = self.max_parallel.clamp(1, 4);
|
||||
self.max_rework = self.max_rework.clamp(0, 10);
|
||||
self.allowed_users = self.allowed_users.iter().map(|u| u.trim().to_string()).filter(|u| !u.is_empty()).collect();
|
||||
for p in [&mut self.review, &mut self.build, &mut self.qa] {
|
||||
p.model = p.model.trim().to_string();
|
||||
@@ -157,6 +170,7 @@ impl RepoCfg {
|
||||
bail!("invalid permission_mode '{}'", p.permission_mode);
|
||||
}
|
||||
p.timeout_minutes = p.timeout_minutes.clamp(1, 24 * 60);
|
||||
p.max_attempts = p.max_attempts.clamp(1, 10);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -504,6 +518,88 @@ impl Forge {
|
||||
}
|
||||
|
||||
/// Open a pull request (GitLab: merge request); returns {number, html_url}.
|
||||
fn authed(&self, req: reqwest::RequestBuilder) -> reqwest::RequestBuilder {
|
||||
let req = req.header("User-Agent", "codecraft-factory");
|
||||
match self.kind {
|
||||
Kind::Gitea => req.header("Authorization", format!("token {}", self.token)),
|
||||
Kind::Github => req.header("Authorization", format!("Bearer {}", self.token)).header("Accept", "application/vnd.github+json").header("X-GitHub-Api-Version", "2022-11-28"),
|
||||
Kind::Gitlab => req.header("PRIVATE-TOKEN", &self.token),
|
||||
}
|
||||
}
|
||||
|
||||
/// (number, title) of the open issues of the repository, newest page first, at most 200.
|
||||
pub async fn open_issue_titles(&self) -> Result<Vec<(i64, String)>> {
|
||||
let mut out = Vec::new();
|
||||
for page in 1..=4 {
|
||||
let (url, key) = match self.kind {
|
||||
Kind::Gitea => (self.rp(&format!("/issues?state=open&type=issues&limit=50&page={page}")), "number"),
|
||||
Kind::Github => (self.rp(&format!("/issues?state=open&per_page=50&page={page}")), "number"),
|
||||
Kind::Gitlab => (self.rp(&format!("/issues?state=opened&per_page=50&page={page}")), "iid"),
|
||||
};
|
||||
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").is_some() {
|
||||
continue;
|
||||
}
|
||||
out.push((i[key].as_i64().unwrap_or(0), i["title"].as_str().unwrap_or("").to_string()));
|
||||
}
|
||||
if n < 50 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
pub async fn close_issue(&self, number: u64) -> Result<()> {
|
||||
match self.kind {
|
||||
Kind::Gitlab => self.call(reqwest::Method::PUT, self.rp(&format!("/issues/{number}")), Some(json!({"state_event": "close"}))).await.map(|_| ()),
|
||||
_ => self.call(reqwest::Method::PATCH, self.rp(&format!("/issues/{number}")), Some(json!({"state": "closed"}))).await.map(|_| ()),
|
||||
}
|
||||
}
|
||||
|
||||
/// A release for an existing tag. Returns {id, html_url}.
|
||||
pub async fn create_release(&self, tag: &str, name: &str, body: &str) -> Result<Value> {
|
||||
match self.kind {
|
||||
Kind::Gitlab => {
|
||||
let v = self.call(reqwest::Method::POST, self.rp("/releases"), Some(json!({"tag_name": tag, "name": name, "description": body}))).await?;
|
||||
Ok(json!({"id": tag, "html_url": v["_links"]["self"]}))
|
||||
}
|
||||
_ => {
|
||||
let v = self.call(reqwest::Method::POST, self.rp("/releases"), Some(json!({"tag_name": tag, "name": name, "body": body, "draft": false, "prerelease": false}))).await?;
|
||||
Ok(json!({"id": v["id"], "html_url": v["html_url"]}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Attach a file to a release (Gitea and GitHub; GitLab has no plain attachment upload).
|
||||
pub async fn upload_asset(&self, release_id: &Value, name: &str, path: &std::path::Path) -> Result<()> {
|
||||
let size = std::fs::metadata(path)?.len();
|
||||
if size > 400 * 1024 * 1024 {
|
||||
bail!("{name} is larger than 400 MB");
|
||||
}
|
||||
let data = tokio::fs::read(path).await?;
|
||||
let id = release_id.as_i64().map(|i| i.to_string()).or_else(|| release_id.as_str().map(String::from)).unwrap_or_default();
|
||||
let resp = match self.kind {
|
||||
Kind::Gitea => {
|
||||
let part = reqwest::multipart::Part::bytes(data).file_name(name.to_string());
|
||||
let form = reqwest::multipart::Form::new().part("attachment", part);
|
||||
self.authed(crate::provider::http_client().post(self.rp(&format!("/releases/{id}/assets?name={}", urlenc(name)))).multipart(form).timeout(Duration::from_secs(900))).send().await?
|
||||
}
|
||||
Kind::Github => {
|
||||
let base = if self.api == "https://api.github.com" { "https://uploads.github.com".to_string() } else { self.api.replace("/api/v3", "/api/uploads") };
|
||||
let url = format!("{base}/repos/{}/{}/releases/{id}/assets?name={}", self.owner, self.repo, urlenc(name));
|
||||
self.authed(crate::provider::http_client().post(url).header("Content-Type", "application/octet-stream").body(data).timeout(Duration::from_secs(900))).send().await?
|
||||
}
|
||||
Kind::Gitlab => bail!("GitLab has no plain release attachment upload – the file {name} was not attached"),
|
||||
};
|
||||
if !resp.status().is_success() {
|
||||
bail!("uploading {name}: {} {}", resp.status(), resp.text().await.unwrap_or_default().chars().take(200).collect::<String>());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// A pull/merge request in one shape: number, html_url, title, state (open|merged|closed),
|
||||
/// mergeable, changed_files, additions, deletions, head.
|
||||
fn norm_pr(&self, v: &Value) -> Value {
|
||||
@@ -655,7 +751,7 @@ pub fn parse_result(text: &str) -> Option<Value> {
|
||||
candidates.into_iter().rev().find_map(|c| serde_json::from_str::<Value>(c.trim()).ok().filter(|v| v["decision"].is_string()))
|
||||
}
|
||||
|
||||
fn issue_no(v: &Value) -> i64 {
|
||||
pub(crate) fn issue_no(v: &Value) -> i64 {
|
||||
v["number"].as_i64().unwrap_or(0)
|
||||
}
|
||||
|
||||
@@ -731,6 +827,10 @@ pub async fn checkout(app: &App, user: &User, cfg: &RepoCfg) -> Result<std::path
|
||||
let (base, owner, name) = cfg.parts()?;
|
||||
if dir.join(".git").exists() {
|
||||
git(cfg, &dir, &["fetch", "--prune", "origin"]).await?;
|
||||
// The reviews read the files in this checkout: bring the default branch up to date (git pull).
|
||||
if let Err(e) = crate::factory_version::sync_default(cfg, &dir).await {
|
||||
tracing::warn!("factory {}: pull of the default branch: {e:#}", cfg.url);
|
||||
}
|
||||
} else {
|
||||
let parent = dir.parent().context("no parent folder")?;
|
||||
std::fs::create_dir_all(parent)?;
|
||||
@@ -801,7 +901,7 @@ async fn wait_run(app: &Arc<App>, session_id: &str, timeout_minutes: u64) -> Opt
|
||||
Some(last_text(app, session_id))
|
||||
}
|
||||
|
||||
async fn bot_comment(g: &Forge, issue: u64, text: &str) {
|
||||
pub(crate) async fn bot_comment(g: &Forge, issue: u64, text: &str) {
|
||||
if let Err(e) = g.comment(issue, &format!("{BOT}\n{text}")).await {
|
||||
tracing::warn!("factory: comment on #{issue}: {e:#}");
|
||||
}
|
||||
@@ -817,6 +917,10 @@ async fn review(app: Arc<App>, user: User, cfg: RepoCfg, issue: Value, comments:
|
||||
};
|
||||
set(&[("state", json!("review"))]);
|
||||
let _ = g.set_labels(n as u64, &status_labels(&cfg, "review")).await;
|
||||
// The review reads the checkout: fetch and pull first so it sees everything merged so far.
|
||||
if let Err(e) = checkout(&app, &user, &cfg).await {
|
||||
tracing::warn!("factory {repo}#{n}: update before review: {e:#}");
|
||||
}
|
||||
let prompt = review_prompt(&cfg, &issue, &comments, &cfg.review.extra_prompt);
|
||||
let title = format!("Factory #{n} – review – {}", issue["title"].as_str().unwrap_or(""));
|
||||
let (run_id, sid) = match start_phase(&app, &user, &cfg, n, "review", 0, prompt, &title, None) {
|
||||
@@ -941,7 +1045,7 @@ async fn prepare_worktree(app: &App, user: &User, cfg: &RepoCfg, n: i64, branch:
|
||||
/// Build failure: back to `approved` (the next poll tries again) or, after the second failure, `failed`.
|
||||
async fn build_fail(app: &Arc<App>, g: &Forge, cfg: &RepoCfg, n: i64, why: &str) {
|
||||
let attempts = app.store.factory_issue(&cfg.url, n).ok().flatten().and_then(|i| i["attempts"].as_i64()).unwrap_or(0) + 1;
|
||||
if attempts >= 2 {
|
||||
if attempts >= cfg.build.max_attempts {
|
||||
let _ = app.store.factory_issue_set(&cfg.url, n, &[("state", json!("failed")), ("attempts", json!(attempts)), ("note", json!(why))]);
|
||||
let _ = g.set_labels(n as u64, &status_labels(cfg, "failed")).await;
|
||||
bot_comment(g, n as u64, &format!("**Build failed:** {why}.")).await;
|
||||
@@ -1022,8 +1126,7 @@ async fn build(app: Arc<App>, user: User, cfg: RepoCfg, issue: Value, comments:
|
||||
|
||||
// --- phase 3: acceptance (qa) -------------------------------------------------------------
|
||||
|
||||
/// Reworks after a failed acceptance, at most.
|
||||
const MAX_REWORK: i64 = 2;
|
||||
|
||||
|
||||
pub fn qa_prompt(cfg: &RepoCfg, issue: &Value, scenarios: &str, built: &str, extra: &str) -> String {
|
||||
format!(
|
||||
@@ -1052,7 +1155,7 @@ How to work:\n\
|
||||
/// Acceptance failure of the process itself: retried once from `qa`, then `failed`.
|
||||
async fn qa_fail(app: &Arc<App>, g: &Forge, cfg: &RepoCfg, n: i64, why: &str) {
|
||||
let attempts = app.store.factory_issue(&cfg.url, n).ok().flatten().and_then(|i| i["attempts"].as_i64()).unwrap_or(0) + 1;
|
||||
if attempts >= 2 {
|
||||
if attempts >= cfg.qa.max_attempts {
|
||||
let _ = app.store.factory_issue_set(&cfg.url, n, &[("state", json!("failed")), ("attempts", json!(attempts)), ("note", json!(why))]);
|
||||
let _ = g.set_labels(n as u64, &status_labels(cfg, "failed")).await;
|
||||
bot_comment(g, n as u64, &format!("**Acceptance failed to run:** {why}.")).await;
|
||||
@@ -1126,16 +1229,16 @@ async fn qa(app: Arc<App>, user: User, cfg: RepoCfg, issue: Value) {
|
||||
} else {
|
||||
res["findings"].as_str().filter(|f| !f.trim().is_empty()).unwrap_or(&summary).trim().to_string()
|
||||
};
|
||||
if round >= MAX_REWORK {
|
||||
if round >= cfg.max_rework {
|
||||
set(&[("state", json!("failed")), ("note", json!(findings.clone()))]);
|
||||
let _ = g.set_labels(n as u64, &status_labels(&cfg, "failed")).await;
|
||||
bot_comment(&g, n as u64, &format!("**Acceptance failed after {MAX_REWORK} rework rounds.** {summary}\n\n{table}\n{findings}")).await;
|
||||
bot_comment(&g, n as u64, &format!("**Acceptance failed after {} rework rounds.** {summary}\n\n{table}\n{findings}", cfg.max_rework)).await;
|
||||
notify(&app, &user, &format!("Factory #{n}"), "acceptance failed, rework limit reached", n).await;
|
||||
} else {
|
||||
set(&[("state", json!("rework")), ("rounds", json!(round + 1)), ("attempts", json!(0)), ("note", json!(findings.clone()))]);
|
||||
let _ = g.set_labels(n as u64, &status_labels(&cfg, "building")).await;
|
||||
bot_comment(&g, n as u64, &format!("**Acceptance: problems found** – rework round {} of {MAX_REWORK} starts. {summary}\n\n{table}\n{findings}", round + 1)).await;
|
||||
notify(&app, &user, &format!("Factory #{n}"), &format!("acceptance found problems, rework {}/{MAX_REWORK}", round + 1), n).await;
|
||||
bot_comment(&g, n as u64, &format!("**Acceptance: problems found** – rework round {} of {} starts. {summary}\n\n{table}\n{findings}", round + 1, cfg.max_rework)).await;
|
||||
notify(&app, &user, &format!("Factory #{n}"), &format!("acceptance found problems, rework {}/{}", round + 1, cfg.max_rework), n).await;
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -1162,7 +1265,7 @@ async fn qa(app: Arc<App>, user: User, cfg: RepoCfg, issue: Value) {
|
||||
/// First failure: back to `new` (retried by the next poll); second: failed.
|
||||
async fn fail_or_retry(app: &Arc<App>, g: &Forge, cfg: &RepoCfg, n: i64, why: &str) {
|
||||
let attempts = app.store.factory_issue(&cfg.url, n).ok().flatten().and_then(|i| i["attempts"].as_i64()).unwrap_or(0) + 1;
|
||||
if attempts >= 2 {
|
||||
if attempts >= cfg.review.max_attempts {
|
||||
let _ = app.store.factory_issue_set(&cfg.url, n, &[("state", json!("failed")), ("attempts", json!(attempts)), ("note", json!(why))]);
|
||||
let _ = g.set_labels(n as u64, &status_labels(cfg, "failed")).await;
|
||||
bot_comment(g, n as u64, &format!("**Failed:** {why}.")).await;
|
||||
@@ -1171,7 +1274,7 @@ async fn fail_or_retry(app: &Arc<App>, g: &Forge, cfg: &RepoCfg, n: i64, why: &s
|
||||
}
|
||||
}
|
||||
|
||||
async fn notify(app: &App, user: &User, title: &str, body: &str, issue: i64) {
|
||||
pub(crate) async fn notify(app: &App, user: &User, title: &str, body: &str, issue: i64) {
|
||||
let payload = json!({"kind": "factory", "title": title, "body": body, "tag": format!("factory-{issue}"), "url": "/#/factory"});
|
||||
let _ = crate::push::send_to_user(app, &user.id, &payload).await;
|
||||
}
|
||||
@@ -1231,6 +1334,10 @@ pub async fn poll_repo(app: &Arc<App>, user: &User, cfg: &RepoCfg) -> Result<()>
|
||||
app.store.factory_issue_set(&cfg.url, n, &[("title", json!(title)), ("state", json!("review"))])?;
|
||||
tokio::spawn(review(app.clone(), user.clone(), cfg.clone(), issue, comments));
|
||||
}
|
||||
// Merged issues get their version and tag.
|
||||
if let Err(e) = crate::factory_version::version_pass(app, user, cfg).await {
|
||||
tracing::warn!("factory {}: versions: {e:#}", cfg.url);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user