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:
Richard ZinkandClaude Sonnet 5.5 committed 2026-11-16 12:44:35 +01:00
1 parent 06c1b8df1b
commit cb1e31b6e9
13 files changed
+1082 -19

No files matched your search

+30 -1
View File
@@ -167,6 +167,8 @@ pub fn router(app: AppState) -> Router {
.route("/factory/issue-chats", post(factory_new_issue_chat))
.route("/factory/issue-chats/{id}", get(factory_issue_chat))
.route("/factory/issue-chats/{id}/create", post(factory_create_issues))
.route("/factory/release/preview", get(factory_release_preview))
.route("/factory/release", post(factory_release))
.route("/factory/issues/pr", get(factory_pr))
.route("/factory/issues/pr/merge", post(factory_pr_merge))
.route("/account/factory/connection/test", post(test_factory_connection))
@@ -2637,12 +2639,13 @@ async fn test_factory(Extension(user): Extension<User>, State(app): State<AppSta
/// Everything the factory page shows: repositories, issues with state, runs with tokens/cost.
async fn factory_overview(Extension(user): Extension<User>, State(app): State<AppState>) -> ApiResult {
let s: crate::factory::FactorySettings = app.store.user_setting(&user.id, "factory")?;
let repos: Vec<Value> = s.repos.iter().map(|r| json!({"url": r.url, "enabled": r.enabled, "label": r.label, "max_parallel": r.max_parallel, "kind": s.resolve(r).kind, "has_token": !s.resolve(r).token.is_empty(), "models": {"review": r.review.model, "build": r.build.model, "qa": r.qa.model}, "running": app.store.factory_running(&r.url).unwrap_or(0)})).collect();
let repos: Vec<Value> = s.repos.iter().map(|r| json!({"url": r.url, "enabled": r.enabled, "label": r.label, "max_parallel": r.max_parallel, "kind": s.resolve(r).kind, "has_token": !s.resolve(r).token.is_empty(), "models": {"review": r.review.model, "build": r.build.model, "qa": r.qa.model}, "running": app.store.factory_running(&r.url).unwrap_or(0), "version": app.store.factory_version(&r.url).ok().flatten()})).collect();
Ok(Json(json!({
"area": app.factory_dir().is_some(),
"repos": repos,
"issues": app.store.factory_issues(&user.id)?,
"runs": app.store.factory_runs(&user.id, None, None, 300)?,
"releases": app.store.factory_releases(&user.id, 10)?,
})))
}
@@ -2658,6 +2661,32 @@ async fn factory_poll(Extension(user): Extension<User>, State(app): State<AppSta
Ok(Json(json!({"errors": errors})))
}
#[derive(Deserialize)]
struct ReleaseQuery {
repo: String,
}
/// Current version, suggested next major version and the release recipe (.factory/release.json).
async fn factory_release_preview(Extension(user): Extension<User>, State(app): State<AppState>, Query(q): Query<ReleaseQuery>) -> ApiResult {
let cfg = factory_repo_cfg(&app, &user, &q.repo)?;
let v = crate::factory_version::preview(&app, &user, &cfg).await.map_err(|e| ApiError(StatusCode::BAD_GATEWAY, format!("{e:#}")))?;
Ok(Json(v))
}
#[derive(Deserialize)]
struct StartRelease {
repo: String,
version: String,
}
/// Start a release: tag, Docker image, downloads and the release on the forge (runs in the background).
async fn factory_release(Extension(user): Extension<User>, State(app): State<AppState>, headers: axum::http::HeaderMap, Json(b): Json<StartRelease>) -> ApiResult {
let cfg = factory_repo_cfg(&app, &user, &b.repo)?;
let id = crate::factory_version::start_release(&app, &user, &cfg, b.version.trim()).await.map_err(|e| ApiError(StatusCode::BAD_REQUEST, format!("{e:#}")))?;
app.audit(Some(&user), "factory.release.start", &format!("{} {}", b.repo, b.version.trim()), &crate::auth::client_ip(&headers));
Ok(Json(json!({"id": id})))
}
#[derive(Deserialize)]
struct NewIssueChat {
repo: String,
+120 -13
View File
@@ -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(())
}
+609
View File
@@ -0,0 +1,609 @@
//! Versions, tags and releases of factory repositories.
//!
//! Every merged factory issue gets a version (`MAJOR.MINOR.PATCH`): the patch number goes up with each
//! issue, the minor number when the issue completes a milestone (`M1.3` was the last open `M1.x`
//! issue); the patch restarts at 0. A version is a commit with a `VERSION` file on the default branch
//! plus the tag `vX.Y.Z` on it. Major versions are only made by a manual release, which also builds
//! the Docker image and the downloads described in `.factory/release.json`.
use crate::app::App;
use crate::factory::{Forge, RepoCfg, bot_comment, checkout, git, issue_no, notify};
use crate::store::User;
use anyhow::{Context, Result, bail};
use serde::Deserialize;
use serde_json::{Value, json};
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, OnceLock};
use std::time::Duration;
pub type Ver = (u64, u64, u64);
pub fn parse_version(s: &str) -> Option<Ver> {
let mut it = s.trim().trim_start_matches('v').split('.');
let v = (it.next()?.parse().ok()?, it.next()?.parse().ok()?, it.next()?.parse().ok()?);
it.next().is_none().then_some(v)
}
pub fn fmt(v: Ver) -> String {
format!("{}.{}.{}", v.0, v.1, v.2)
}
/// Milestone of an issue title: `M1.3 – x` → (1, Some(3)), `M1 – x` → (1, None).
pub fn milestone_of(title: &str) -> Option<(u64, Option<u64>)> {
let t = title.trim();
let rest = t.strip_prefix('M').or_else(|| t.strip_prefix('m'))?;
let digits: String = rest.chars().take_while(|c| c.is_ascii_digit()).collect();
let k: u64 = digits.parse().ok()?;
let after = &rest[digits.len()..];
if let Some(a) = after.strip_prefix('.') {
let d: String = a.chars().take_while(|c| c.is_ascii_digit()).collect();
if !d.is_empty() {
return Some((k, Some(d.parse().ok()?)));
}
}
match after.chars().next() {
None | Some(' ') | Some('–') | Some('-') | Some(':') => Some((k, None)),
_ => None,
}
}
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum Bump {
Patch,
Minor,
}
/// How an issue raises the version, given the titles of the *other* open issues of the repository.
pub fn bump_for(title: &str, other_open: &[String]) -> Bump {
match milestone_of(title) {
Some((_, None)) => Bump::Minor,
Some((k, Some(_))) => {
if other_open.iter().any(|t| matches!(milestone_of(t), Some((k2, Some(_))) if k2 == k)) {
Bump::Patch
} else {
Bump::Minor
}
}
None => Bump::Patch,
}
}
pub fn next_version(cur: Ver, b: Bump) -> Ver {
match b {
Bump::Patch => (cur.0, cur.1, cur.2 + 1),
Bump::Minor => (cur.0, cur.1 + 1, 0),
}
}
// --- git helpers ---------------------------------------------------------------------------
/// Highest `vX.Y.Z` tag of the remote (0.0.0 when there is none).
async fn tags_version(cfg: &RepoCfg, dir: &Path) -> Ver {
let _ = git(cfg, dir, &["fetch", "--tags", "--force", "origin"]).await;
git(cfg, dir, &["tag", "-l", "v[0-9]*"]).await.unwrap_or_default().lines().filter_map(parse_version).max().unwrap_or((0, 0, 0))
}
/// `git pull --ff-only` of the default branch in the main checkout, so everything that was merged
/// (also by others) is on disk for the reviews. Does nothing harmful: with local changes or a
/// diverged branch it reports an error and leaves the checkout alone.
pub async fn sync_default(cfg: &RepoCfg, dir: &Path) -> Result<()> {
let base = base_branch(cfg, dir).await;
let current = git(cfg, dir, &["rev-parse", "--abbrev-ref", "HEAD"]).await.unwrap_or_default();
if current != base {
if !git(cfg, dir, &["status", "--porcelain"]).await.unwrap_or_default().is_empty() {
bail!("the checkout has local changes and is on '{current}', not on {base}");
}
git(cfg, dir, &["checkout", "-q", &base]).await?;
}
git(cfg, dir, &["merge", "--ff-only", "-q", &format!("origin/{base}")]).await?;
Ok(())
}
async fn base_branch(cfg: &RepoCfg, repo: &Path) -> String {
git(cfg, repo, &["symbolic-ref", "--short", "refs/remotes/origin/HEAD"]).await.ok().and_then(|s| s.strip_prefix("origin/").map(String::from)).unwrap_or_else(|| "main".to_string())
}
/// A throw-away detached worktree of the default branch below `<owner>/<sub>/<name>`.
async fn fresh_worktree(cfg: &RepoCfg, repo: &Path, sub: &str, name: &str, base: &str) -> Result<PathBuf> {
let work = repo.parent().context("no parent folder")?.join(sub).join(name);
std::fs::create_dir_all(work.parent().unwrap())?;
if work.exists() {
let _ = git(cfg, repo, &["worktree", "remove", "--force", &work.display().to_string()]).await;
let _ = std::fs::remove_dir_all(&work);
}
let _ = git(cfg, repo, &["worktree", "prune"]).await;
git(cfg, repo, &["worktree", "add", "--detach", &work.display().to_string(), &format!("origin/{base}")]).await?;
Ok(work)
}
async fn drop_worktree(cfg: &RepoCfg, repo: &Path, work: &Path) {
let _ = git(cfg, repo, &["worktree", "remove", "--force", &work.display().to_string()]).await;
let _ = std::fs::remove_dir_all(work);
}
const IDENT: [&str; 4] = ["-c", "user.name=CodeCraft Factory", "-c", "user.email=factory@codecraft.invalid"];
/// Write `VERSION` and commit it locally (nothing is pushed yet).
async fn commit_version(cfg: &RepoCfg, work: &Path, ver: &str, msg: &str) -> Result<()> {
std::fs::write(work.join("VERSION"), format!("{ver}\n"))?;
git(cfg, work, &["add", "VERSION"]).await?;
if !git(cfg, work, &["diff", "--cached", "--name-only"]).await?.is_empty() {
let mut args = IDENT.to_vec();
args.extend(["commit", "-m", msg]);
git(cfg, work, &args).await?;
}
Ok(())
}
/// Tag the commit and push default branch and tag. Returns a warning when the branch could not be
/// pushed (e.g. protected): then the tag goes onto the commit already on the branch.
async fn publish_version(cfg: &RepoCfg, work: &Path, base: &str, ver: &str, msg: &str) -> Result<String> {
let mut warn = String::new();
let target = format!("HEAD:refs/heads/{base}");
let mut pushed = git(cfg, work, &["push", "origin", &target]).await;
if pushed.is_err() {
// The branch moved meanwhile: put the version commit on top and try once more.
let _ = git(cfg, work, &["fetch", "origin"]).await;
let upstream = format!("origin/{base}");
let mut args = IDENT.to_vec();
args.extend(["rebase", &upstream]);
if git(cfg, work, &args).await.is_ok() {
pushed = git(cfg, work, &["push", "origin", &target]).await;
} else {
let _ = git(cfg, work, &["rebase", "--abort"]).await;
}
}
if let Err(e) = pushed {
warn = format!("the VERSION commit could not be pushed to {base} ({e:#}); the tag points to the branch head instead");
git(cfg, work, &["reset", "--hard", &format!("origin/{base}")]).await?;
}
let tag = format!("v{ver}");
let mut args = IDENT.to_vec();
args.extend(["tag", "-a", "-f", &tag, "-m", msg]);
git(cfg, work, &args).await?;
git(cfg, work, &["push", "origin", &format!("refs/tags/{tag}")]).await?;
Ok(warn)
}
// --- version of merged issues -----------------------------------------------------------------
static BUSY: OnceLock<Mutex<HashSet<String>>> = OnceLock::new();
/// Keeps two passes (poller and manual poll) from versioning the same repository at once.
struct Busy(String);
fn busy(repo: &str) -> Option<Busy> {
let set = BUSY.get_or_init(Default::default);
set.lock().unwrap().insert(repo.to_string()).then(|| Busy(repo.to_string()))
}
impl Drop for Busy {
fn drop(&mut self) {
if let Some(set) = BUSY.get() {
set.lock().unwrap().remove(&self.0);
}
}
}
fn pr_number(url: &str) -> u64 {
url.trim_end_matches('/').rsplit('/').next().and_then(|n| n.parse().ok()).unwrap_or(0)
}
/// Give every merged, accepted issue its version. The first pass over a repository only
/// sets the baseline: issues that were done before are not versioned.
pub async fn version_pass(app: &Arc<App>, user: &User, cfg: &RepoCfg) -> Result<()> {
let Some(_guard) = busy(&cfg.url) else { return Ok(()) };
let rows: Vec<Value> = app.store.factory_issues(&user.id)?.into_iter().filter(|r| r["repo"].as_str() == Some(cfg.url.as_str())).collect();
let pending = |r: &&Value| r["state"].as_str() == Some("done") && r["version"].as_str() == Some("");
if app.store.factory_version(&cfg.url)?.is_none() {
let dir = checkout(app, user, cfg).await?;
let cur = tags_version(cfg, &dir).await;
for r in rows.iter().filter(pending) {
app.store.factory_issue_set(&cfg.url, issue_no(r), &[("version", json!("-"))])?;
}
app.store.factory_version_set(&cfg.url, &fmt(cur))?;
return Ok(());
}
let mut todo: Vec<&Value> = rows.iter().filter(pending).collect();
if todo.is_empty() {
return Ok(());
}
todo.sort_by_key(|r| issue_no(r));
let g = Forge::new(cfg)?;
for r in todo {
let n = issue_no(r);
let pr_n = pr_number(r["pr_url"].as_str().unwrap_or(""));
if pr_n == 0 {
continue;
}
let pr = match g.pr(pr_n).await {
Ok(p) => p,
Err(e) => {
tracing::warn!("factory {}#{n}: pull request: {e:#}", cfg.url);
continue;
}
};
if pr["state"].as_str() != Some("merged") {
continue;
}
let title = r["title"].as_str().unwrap_or("").to_string();
let others: Vec<String> = g.open_issue_titles().await?.into_iter().filter(|(num, _)| *num != n).map(|(_, t)| t).collect();
let dir = checkout(app, user, cfg).await?;
let cur = tags_version(cfg, &dir).await;
let bump = bump_for(&title, &others);
let new = fmt(next_version(cur, bump));
let base = base_branch(cfg, &dir).await;
let (_, _, name) = cfg.parts()?;
let work = fresh_worktree(cfg, &dir, "_release", &format!("{name}-version"), &base).await?;
let msg = format!("Release v{new}: {title} (#{n})");
let published = async {
commit_version(cfg, &work, &new, &msg).await?;
publish_version(cfg, &work, &base, &new, &msg).await
}
.await;
drop_worktree(cfg, &dir, &work).await;
let warn = match published {
Ok(w) => w,
Err(e) => {
tracing::warn!("factory {}#{n}: version {new}: {e:#}", cfg.url);
continue;
}
};
let _ = checkout(app, user, cfg).await; // pulls the merge and the VERSION commit
app.store.factory_issue_set(&cfg.url, n, &[("version", json!(new.clone()))])?;
app.store.factory_version_set(&cfg.url, &new)?;
let _ = g.close_issue(n as u64).await;
let why = if bump == Bump::Minor && milestone_of(&title).is_some() { " – the milestone is complete" } else { "" };
let extra = if warn.is_empty() { String::new() } else { format!("\n\nNote: {warn}.") };
bot_comment(&g, n as u64, &format!("**Version v{new}**{why}. The change is merged; tag `v{new}` and `VERSION` are on the default branch.{extra}")).await;
notify(app, user, &format!("Factory #{n}"), &format!("v{new}{why}"), n).await;
}
Ok(())
}
// --- manual release ----------------------------------------------------------------------------
/// `.factory/release.json`: how to build the downloads of a release.
#[derive(Debug, Default, Deserialize)]
pub struct ReleaseSpec {
/// Docker image name without tag, e.g. registry.example.org/team/app (empty = no image).
#[serde(default)]
pub image: String,
#[serde(default = "default_platforms")]
pub platforms: String,
#[serde(default = "default_dockerfile")]
pub dockerfile: String,
#[serde(default = "default_context")]
pub context: String,
#[serde(default)]
pub build_args: HashMap<String, String>,
/// Shell commands run in the release checkout before the image is built (e.g. to make zips).
#[serde(default)]
pub commands: Vec<String>,
/// Files (globs relative to the checkout) attached to the release; the source zip is always attached.
#[serde(default)]
pub files: Vec<String>,
/// Name used for the source zip (default: the repository name).
#[serde(default)]
pub name: String,
}
fn default_platforms() -> String {
"linux/amd64,linux/arm64".into()
}
fn default_dockerfile() -> String {
"Dockerfile".into()
}
fn default_context() -> String {
".".into()
}
/// Run a command with a timeout; returns the last lines of its output.
async fn run(cmd: &mut tokio::process::Command, timeout: Duration, stdin: Option<&str>) -> Result<String> {
use tokio::io::AsyncWriteExt;
cmd.stdout(std::process::Stdio::piped()).stderr(std::process::Stdio::piped()).stdin(if stdin.is_some() { std::process::Stdio::piped() } else { std::process::Stdio::null() }).kill_on_drop(true);
let mut child = cmd.spawn().context("could not start the command")?;
if let (Some(text), Some(mut si)) = (stdin, child.stdin.take()) {
si.write_all(text.as_bytes()).await?;
}
let out = tokio::time::timeout(timeout, child.wait_with_output()).await.map_err(|_| anyhow::anyhow!("timed out after {} min", timeout.as_secs() / 60))??;
let text = format!("{}{}", String::from_utf8_lossy(&out.stdout), String::from_utf8_lossy(&out.stderr));
let tail: Vec<&str> = text.lines().rev().take(25).collect::<Vec<_>>().into_iter().rev().collect();
let tail = tail.join("\n");
if !out.status.success() {
bail!("{}", tail);
}
Ok(tail)
}
fn clean_env(cmd: &mut tokio::process::Command, extra: &[(&str, &str)]) {
cmd.env_clear().env("PATH", std::env::var("PATH").unwrap_or_default()).env("HOME", "/tmp");
for (k, v) in extra {
cmd.env(k, v);
}
}
/// Current version, suggested next major version and the release recipe of the default branch.
pub async fn preview(app: &Arc<App>, user: &User, cfg: &RepoCfg) -> Result<Value> {
let dir = checkout(app, user, cfg).await?;
let cur = tags_version(cfg, &dir).await;
let base = base_branch(cfg, &dir).await;
let spec = git(cfg, &dir, &["show", &format!("origin/{base}:.factory/release.json")]).await.ok();
Ok(json!({"current": fmt(cur), "suggested": fmt((cur.0 + 1, 0, 0)), "spec": spec, "running": app.store.factory_release_running(&cfg.url)?}))
}
/// Validate and start a release in the background; returns the release id.
pub async fn start_release(app: &Arc<App>, user: &User, cfg: &RepoCfg, version: &str) -> Result<String> {
let Some(new) = parse_version(version) else { bail!("the version must look like 1.0.0") };
if app.store.factory_release_running(&cfg.url)? {
bail!("a release of this repository is already running");
}
let dir = checkout(app, user, cfg).await?;
let cur = tags_version(cfg, &dir).await;
if new <= cur {
bail!("v{} is not newer than the current version v{}", fmt(new), fmt(cur));
}
let id = crate::types::new_id();
app.store.factory_release_add(&id, &user.id, &cfg.url, &fmt(new))?;
let (app, user, cfg, rid) = (app.clone(), user.clone(), cfg.clone(), id.clone());
tokio::spawn(async move {
let log = |m: &str| {
let _ = app.store.factory_release_log(&rid, m);
};
match run_release(&app, &user, &cfg, new, &rid, &log).await {
Ok(url) => {
let _ = app.store.factory_release_end(&rid, "done", &url);
notify(&app, &user, "Factory release", &format!("v{} released", fmt(new)), 0).await;
}
Err(e) => {
log(&format!("FAILED: {e:#}"));
let _ = app.store.factory_release_end(&rid, "failed", "");
notify(&app, &user, "Factory release", &format!("v{} failed", fmt(new)), 0).await;
}
}
});
Ok(id)
}
async fn run_release(app: &Arc<App>, user: &User, cfg: &RepoCfg, new: Ver, rid: &str, log: &(dyn Fn(&str) + Sync)) -> Result<String> {
let ver = fmt(new);
let tag = format!("v{ver}");
let dir = checkout(app, user, cfg).await?;
let base = base_branch(cfg, &dir).await;
let (_, _, name) = cfg.parts()?;
let g = Forge::new(cfg)?;
log(&format!("release {tag} from {base}"));
let work = fresh_worktree(cfg, &dir, "_release", &format!("{name}-release"), &base).await?;
let tmp = std::env::temp_dir().join(format!("factory-release-{rid}"));
std::fs::create_dir_all(&tmp)?;
let result = release_steps(cfg, &g, &work, &tmp, &base, &name, &ver, &tag, rid, log).await;
drop_worktree(cfg, &dir, &work).await;
let _ = std::fs::remove_dir_all(&tmp);
result
}
#[allow(clippy::too_many_arguments)]
async fn release_steps(cfg: &RepoCfg, g: &Forge, work: &Path, tmp: &Path, base: &str, name: &str, ver: &str, tag: &str, rid: &str, log: &(dyn Fn(&str) + Sync)) -> Result<String> {
let msg = format!("Release {tag}");
commit_version(cfg, work, ver, &msg).await?;
let spec: ReleaseSpec = match std::fs::read_to_string(work.join(".factory/release.json")) {
Ok(t) => serde_json::from_str(&t).context(".factory/release.json is not valid")?,
Err(_) => {
log("no .factory/release.json – only the tag, the release and the source zip are made");
ReleaseSpec::default()
}
};
let hour = Duration::from_secs(3600);
for cmd in &spec.commands {
log(&format!("$ {cmd}"));
let mut c = tokio::process::Command::new("bash");
c.arg("-c").arg(cmd).current_dir(work);
clean_env(&mut c, &[("VERSION", ver), ("CI", "1")]);
log(&run(&mut c, hour, None).await?);
}
if !spec.image.trim().is_empty() {
build_image(cfg, g, &spec, work, tmp, ver, rid, log).await?;
}
// Publish: branch + tag, then the release with its files.
let warn = publish_version(cfg, work, base, ver, &msg).await?;
if !warn.is_empty() {
log(&format!("note: {warn}"));
}
log(&format!("tag {tag} pushed"));
let prev = git(cfg, work, &["tag", "-l", "v[0-9]*"]).await.unwrap_or_default().lines().filter_map(|t| parse_version(t).map(|v| (v, t.to_string()))).filter(|(v, _)| fmt(*v) != ver && *v < parse_version(ver).unwrap_or((0, 0, 0))).max().map(|(_, t)| t);
let range = prev.as_ref().map(|p| format!("{p}..{tag}")).unwrap_or_else(|| tag.to_string());
let changes = git(cfg, work, &["log", "--pretty=format:- %s", "--max-count=200", &range]).await.unwrap_or_default();
let mut notes = format!("Release {tag}\n\n");
if !spec.image.trim().is_empty() {
notes.push_str(&format!("Docker image: `{}:{ver}` ({})\n\n", spec.image.trim(), spec.platforms));
}
notes.push_str(&format!("## Changes\n\n{changes}\n"));
let rel = g.create_release(tag, &format!("{name} {tag}"), &notes).await?;
let url = rel["html_url"].as_str().unwrap_or("").to_string();
log(&format!("release created {url}"));
// Downloads: the source zip and the configured files.
let zip_name = format!("{}-{ver}-source.zip", Some(spec.name.trim()).filter(|n| !n.is_empty()).unwrap_or(name));
let zip = tmp.join(&zip_name);
git(cfg, work, &["archive", "--format=zip", &format!("--prefix={name}-{ver}/"), "-o", &zip.display().to_string(), "HEAD"]).await?;
let mut files: Vec<(String, PathBuf)> = vec![(zip_name, zip)];
if !spec.files.is_empty() {
let mut builder = globset::GlobSetBuilder::new();
for f in &spec.files {
builder.add(globset::Glob::new(f).with_context(|| format!("bad file pattern {f}"))?);
}
let set = builder.build()?;
let mut stack = vec![work.to_path_buf()];
while let Some(d) = stack.pop() {
for e in std::fs::read_dir(&d)?.flatten() {
let p = e.path();
let rel_path = p.strip_prefix(work).unwrap_or(&p).to_path_buf();
if rel_path.starts_with(".git") || rel_path.starts_with("node_modules") || rel_path.starts_with("target") && !set.is_match(&rel_path) {
continue;
}
if p.is_dir() {
stack.push(p);
} else if set.is_match(&rel_path) && files.len() < 60 {
files.push((e.file_name().to_string_lossy().to_string(), p));
}
}
}
}
for (fname, path) in &files {
match g.upload_asset(&rel["id"], fname, path).await {
Ok(()) => log(&format!("attached {fname}")),
Err(e) => log(&format!("not attached: {e:#}")),
}
}
Ok(url)
}
/// Build and push the multi-arch Docker image with buildx (own builder, own login, both removed afterwards).
#[allow(clippy::too_many_arguments)]
async fn build_image(cfg: &RepoCfg, g: &Forge, spec: &ReleaseSpec, work: &Path, tmp: &Path, ver: &str, rid: &str, log: &(dyn Fn(&str) + Sync)) -> Result<()> {
let image = spec.image.trim();
let first = image.split('/').next().unwrap_or("");
let host = if image.contains('/') && (first.contains('.') || first.contains(':')) { first } else { "docker.io" };
let conf = tmp.join("docker");
std::fs::create_dir_all(&conf)?;
let conf_s = conf.display().to_string();
let builder = format!("factory-{}", &rid[..8.min(rid.len())]);
let minute = Duration::from_secs(120);
let env = [("DOCKER_CONFIG", conf_s.as_str())];
let mut c = tokio::process::Command::new("docker");
c.args(["buildx", "version"]);
clean_env(&mut c, &env);
run(&mut c, minute, None).await.context("docker buildx is not available in this installation")?;
let login = g.login().await?;
log(&format!("docker login {host}"));
let mut c = tokio::process::Command::new("docker");
c.args(["login", host, "-u", &login, "--password-stdin"]);
clean_env(&mut c, &env);
run(&mut c, minute, Some(&cfg.token)).await.context("docker login failed")?;
let mut c = tokio::process::Command::new("docker");
c.args(["buildx", "create", "--name", &builder, "--driver", "docker-container"]);
clean_env(&mut c, &env);
run(&mut c, minute, None).await.context("could not create the buildx builder")?;
let mut args: Vec<String> = ["buildx", "build", "--builder", &builder, "--platform", &spec.platforms, "-f", &spec.dockerfile, "-t", &format!("{image}:{ver}"), "-t", &format!("{image}:latest"), "--build-arg", &format!("VERSION={ver}")].iter().map(|s| s.to_string()).collect();
for (k, v) in &spec.build_args {
args.extend(["--build-arg".to_string(), format!("{k}={v}")]);
}
args.extend(["--push".to_string(), spec.context.clone()]);
log(&format!("docker buildx build {} → {image}:{ver}", spec.platforms));
let mut c = tokio::process::Command::new("docker");
c.args(&args).current_dir(work);
clean_env(&mut c, &env);
let built = run(&mut c, Duration::from_secs(3 * 3600), None).await;
let mut c = tokio::process::Command::new("docker");
c.args(["buildx", "rm", "--force", &builder]);
clean_env(&mut c, &env);
let _ = run(&mut c, minute, None).await;
log(&built.context("the image build failed")?);
log("image pushed");
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_and_formats_versions() {
assert_eq!(parse_version("v1.2.3"), Some((1, 2, 3)));
assert_eq!(parse_version("0.0.0"), Some((0, 0, 0)));
assert_eq!(parse_version("1.2"), None);
assert_eq!(parse_version("1.2.3.4"), None);
assert_eq!(parse_version("vx.1.2"), None);
assert_eq!(fmt((0, 1, 10)), "0.1.10");
assert!(parse_version("0.10.0") > parse_version("0.9.9"));
}
#[test]
fn reads_milestones_from_titles() {
assert_eq!(milestone_of("M1.3 – Import"), Some((1, Some(3))));
assert_eq!(milestone_of("M12.10 - x"), Some((12, Some(10))));
assert_eq!(milestone_of("M1 – Foundation"), Some((1, None)));
assert_eq!(milestone_of("M0"), Some((0, None)));
assert_eq!(milestone_of("Make it faster"), None);
assert_eq!(milestone_of("M1abc"), None);
assert_eq!(milestone_of("Fix login"), None);
}
#[test]
fn issues_raise_patch_and_the_last_one_of_a_milestone_the_minor() {
let open = |v: &[&str]| v.iter().map(|s| s.to_string()).collect::<Vec<_>>();
// other M1 issues still open: patch
assert_eq!(bump_for("M1.3 – Import", &open(&["M1.4 – Debayering", "M2.1 – Mail"])), Bump::Patch);
// the last open M1 issue: minor (other milestones do not matter)
assert_eq!(bump_for("M1.11 – Log", &open(&["M2.1 – Mail", "Fix typo"])), Bump::Minor);
assert_eq!(bump_for("Fix login bug", &open(&["M1.1 – x"])), Bump::Patch);
assert_eq!(bump_for("M3 – Mosaics", &open(&[])), Bump::Minor);
assert_eq!(next_version((0, 0, 7), Bump::Patch), (0, 0, 8));
assert_eq!(next_version((0, 0, 10), Bump::Minor), (0, 1, 0));
assert_eq!(next_version((0, 1, 4), Bump::Minor), (0, 2, 0));
}
#[test]
fn release_spec_has_defaults() {
let s: ReleaseSpec = serde_json::from_str(r#"{"image": "r.example/app"}"#).unwrap();
assert_eq!((s.platforms.as_str(), s.dockerfile.as_str(), s.context.as_str()), ("linux/amd64,linux/arm64", "Dockerfile", "."));
assert!(s.commands.is_empty() && s.files.is_empty());
}
#[tokio::test]
async fn sync_default_pulls_what_was_merged() {
let run = |dir: &Path, args: &[&str]| {
let o = std::process::Command::new("git").args(["-c", "user.name=t", "-c", "user.email=t@t", "-c", "init.defaultBranch=main"]).args(args).current_dir(dir).output().unwrap();
assert!(o.status.success(), "git {args:?}: {}", String::from_utf8_lossy(&o.stderr));
};
let root = std::env::temp_dir().join(format!("factory-sync-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&root);
std::fs::create_dir_all(&root).unwrap();
run(&root, &["init", "-q", "--bare", "origin.git"]);
run(&root, &["clone", "-q", "origin.git", "a"]);
std::fs::write(root.join("a/one.txt"), "1").unwrap();
run(&root.join("a"), &["add", "."]);
run(&root.join("a"), &["commit", "-qm", "one"]);
run(&root.join("a"), &["push", "-q", "origin", "HEAD:main"]);
run(&root, &["--git-dir=origin.git", "symbolic-ref", "HEAD", "refs/heads/main"]);
run(&root, &["clone", "-q", "origin.git", "checkout"]);
// somebody merges a second change on the remote
std::fs::write(root.join("a/two.txt"), "2").unwrap();
run(&root.join("a"), &["add", "."]);
run(&root.join("a"), &["commit", "-qm", "two"]);
run(&root.join("a"), &["push", "-q", "origin", "HEAD:main"]);
let cfg: RepoCfg = serde_json::from_value(json!({"url": "https://g.example/o/r.git", "token": "t"})).unwrap();
let dir = root.join("checkout");
git(&cfg, &dir, &["fetch", "origin"]).await.unwrap();
assert!(!dir.join("two.txt").exists(), "a fetch alone does not update the files");
sync_default(&cfg, &dir).await.unwrap();
assert!(dir.join("two.txt").exists());
// local changes block switching, nothing is damaged
std::fs::write(dir.join("one.txt"), "edited").unwrap();
sync_default(&cfg, &dir).await.unwrap();
assert_eq!(std::fs::read_to_string(dir.join("one.txt")).unwrap(), "edited");
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn pr_numbers_come_from_urls() {
assert_eq!(pr_number("https://g.example/o/r/pulls/72"), 72);
assert_eq!(pr_number("https://g.example/o/r/-/merge_requests/5/"), 5);
assert_eq!(pr_number(""), 0);
}
#[test]
fn busy_guard_blocks_a_second_pass() {
let a = busy("https://g.example/o/busy-test");
assert!(a.is_some());
assert!(busy("https://g.example/o/busy-test").is_none());
drop(a);
assert!(busy("https://g.example/o/busy-test").is_some());
}
}
+1
View File
@@ -14,6 +14,7 @@ mod digest;
mod docs;
mod export;
mod factory;
mod factory_version;
mod files;
mod git;
mod health;
+69 -3
View File
@@ -204,6 +204,22 @@ impl Store {
result TEXT NOT NULL DEFAULT ''
);
CREATE INDEX IF NOT EXISTS factory_runs_issue ON factory_runs(repo_url, issue);
CREATE TABLE IF NOT EXISTS factory_versions (
repo_url TEXT PRIMARY KEY,
current TEXT NOT NULL DEFAULT '0.0.0',
since INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS factory_releases (
id TEXT PRIMARY KEY,
owner_id TEXT NOT NULL,
repo_url TEXT NOT NULL,
version TEXT NOT NULL,
state TEXT NOT NULL DEFAULT 'running',
log TEXT NOT NULL DEFAULT '',
url TEXT NOT NULL DEFAULT '',
started_at INTEGER NOT NULL,
ended_at INTEGER
);
CREATE TABLE IF NOT EXISTS project_defaults (
cwd TEXT PRIMARY KEY,
data TEXT NOT NULL
@@ -238,6 +254,7 @@ impl Store {
("jobs", "settings", "ALTER TABLE jobs ADD COLUMN settings TEXT"),
("images", "blob", "ALTER TABLE images ADD COLUMN blob TEXT"),
("usage", "allowance", "ALTER TABLE usage ADD COLUMN allowance REAL"),
("factory_issues", "version", "ALTER TABLE factory_issues ADD COLUMN version TEXT NOT NULL DEFAULT ''"),
] {
let exists: bool = conn
.prepare(&format!("SELECT 1 FROM pragma_table_info('{table}') WHERE name = ?1"))?
@@ -1465,8 +1482,8 @@ impl Store {
pub fn factory_issue(&self, repo: &str, number: i64) -> Result<Option<Value>> {
let conn = self.conn.lock().unwrap();
Ok(conn
.query_row("SELECT title, author, state, attempts, rounds, last_comment, scenarios, branch, pr_url, note, created_at, updated_at, owner_id FROM factory_issues WHERE repo_url = ?1 AND number = ?2", params![repo, number], |r| {
Ok(json!({"repo": repo, "number": number, "title": r.get::<_, String>(0)?, "author": r.get::<_, String>(1)?, "state": r.get::<_, String>(2)?, "attempts": r.get::<_, i64>(3)?, "rounds": r.get::<_, i64>(4)?, "last_comment": r.get::<_, i64>(5)?, "scenarios": r.get::<_, String>(6)?, "branch": r.get::<_, String>(7)?, "pr_url": r.get::<_, String>(8)?, "note": r.get::<_, String>(9)?, "created_at": r.get::<_, i64>(10)?, "updated_at": r.get::<_, i64>(11)?, "owner_id": r.get::<_, String>(12)?}))
.query_row("SELECT title, author, state, attempts, rounds, last_comment, scenarios, branch, pr_url, note, created_at, updated_at, owner_id, version FROM factory_issues WHERE repo_url = ?1 AND number = ?2", params![repo, number], |r| {
Ok(json!({"repo": repo, "number": number, "title": r.get::<_, String>(0)?, "author": r.get::<_, String>(1)?, "state": r.get::<_, String>(2)?, "attempts": r.get::<_, i64>(3)?, "rounds": r.get::<_, i64>(4)?, "last_comment": r.get::<_, i64>(5)?, "scenarios": r.get::<_, String>(6)?, "branch": r.get::<_, String>(7)?, "pr_url": r.get::<_, String>(8)?, "note": r.get::<_, String>(9)?, "created_at": r.get::<_, i64>(10)?, "updated_at": r.get::<_, i64>(11)?, "owner_id": r.get::<_, String>(12)?, "version": r.get::<_, String>(13)?}))
})
.optional()?)
}
@@ -1482,7 +1499,7 @@ impl Store {
/// Update the given columns (whitelisted) of an issue.
pub fn factory_issue_set(&self, repo: &str, number: i64, fields: &[(&str, Value)]) -> Result<()> {
const COLS: [&str; 10] = ["title", "state", "attempts", "rounds", "last_comment", "scenarios", "branch", "pr_url", "note", "author"];
const COLS: [&str; 11] = ["title", "state", "attempts", "rounds", "last_comment", "scenarios", "branch", "pr_url", "note", "author", "version"];
let conn = self.conn.lock().unwrap();
for (k, v) in fields {
if !COLS.contains(k) {
@@ -1498,6 +1515,54 @@ impl Store {
Ok(())
}
/// Version of a repository, None while versioning has not been started for it.
pub fn factory_version(&self, repo: &str) -> Result<Option<String>> {
Ok(self.conn.lock().unwrap().query_row("SELECT current FROM factory_versions WHERE repo_url = ?1", [repo], |r| r.get(0)).optional()?)
}
pub fn factory_version_set(&self, repo: &str, current: &str) -> Result<()> {
self.conn.lock().unwrap().execute(
"INSERT INTO factory_versions (repo_url, current, since) VALUES (?1, ?2, ?3) ON CONFLICT(repo_url) DO UPDATE SET current = ?2",
params![repo, current, crate::types::now_ms()],
)?;
Ok(())
}
pub fn factory_release_add(&self, id: &str, owner: &str, repo: &str, version: &str) -> Result<()> {
self.conn.lock().unwrap().execute(
"INSERT INTO factory_releases (id, owner_id, repo_url, version, started_at) VALUES (?1,?2,?3,?4,?5)",
params![id, owner, repo, version, crate::types::now_ms()],
)?;
Ok(())
}
/// Append a line to the log of a release (the log is capped).
pub fn factory_release_log(&self, id: &str, line: &str) -> Result<()> {
self.conn.lock().unwrap().execute(
"UPDATE factory_releases SET log = substr(log || ?2 || char(10), -30000) WHERE id = ?1",
params![id, line],
)?;
Ok(())
}
pub fn factory_release_end(&self, id: &str, state: &str, url: &str) -> Result<()> {
self.conn.lock().unwrap().execute("UPDATE factory_releases SET state = ?2, url = ?3, ended_at = ?4 WHERE id = ?1", params![id, state, url, crate::types::now_ms()])?;
Ok(())
}
pub fn factory_release_running(&self, repo: &str) -> Result<bool> {
Ok(self.conn.lock().unwrap().query_row("SELECT COUNT(*) FROM factory_releases WHERE repo_url = ?1 AND state = 'running'", [repo], |r| r.get::<_, i64>(0))? > 0)
}
pub fn factory_releases(&self, owner: &str, limit: i64) -> Result<Vec<Value>> {
let conn = self.conn.lock().unwrap();
let mut st = conn.prepare("SELECT id, repo_url, version, state, log, url, started_at, ended_at FROM factory_releases WHERE owner_id = ?1 ORDER BY started_at DESC LIMIT ?2")?;
let rows = st.query_map(params![owner, limit], |r| {
Ok(json!({"id": r.get::<_, String>(0)?, "repo": r.get::<_, String>(1)?, "version": r.get::<_, String>(2)?, "state": r.get::<_, String>(3)?, "log": r.get::<_, String>(4)?, "url": r.get::<_, String>(5)?, "started_at": r.get::<_, i64>(6)?, "ended_at": r.get::<_, Option<i64>>(7)?}))
})?;
Ok(rows.filter_map(|r| r.ok()).collect())
}
pub fn factory_issues(&self, owner: &str) -> Result<Vec<Value>> {
let nums: Vec<(String, i64)> = {
let conn = self.conn.lock().unwrap();
@@ -1548,6 +1613,7 @@ impl Store {
conn.execute("UPDATE factory_issues SET state = 'new' WHERE state = 'review'", [])?;
conn.execute("UPDATE factory_issues SET state = CASE WHEN rounds > 0 THEN 'rework' ELSE 'approved' END WHERE state = 'building'", [])?;
conn.execute("UPDATE factory_issues SET state = 'qa' WHERE state = 'testing'", [])?;
conn.execute("UPDATE factory_releases SET state = 'failed', ended_at = ?1, log = log || char(10) || 'interrupted by a restart' WHERE state = 'running'", [crate::types::now_ms()])?;
// All factory chats live in one sidebar folder (older ones are moved there once).
conn.execute(
"UPDATE sessions SET data = json_set(data, '$.folder', 'Factory') WHERE (json_extract(data, '$.folder') IS NULL OR json_extract(data, '$.folder') = '') AND (id IN (SELECT session_id FROM factory_runs) OR json_extract(data, '$.cwd') LIKE '%/_drafts/%')",