Factory: show and merge pull requests, issue chats (plan features, cut milestones)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a0a4082383
commit
56c20d92f8
10 files changed
+673
-3
No files matched your search
@@ -164,6 +164,11 @@ pub fn router(app: AppState) -> Router {
|
||||
.route("/factory/overview", get(factory_overview))
|
||||
.route("/factory/poll", post(factory_poll))
|
||||
.route("/factory/issues/restart", post(factory_restart))
|
||||
.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/issues/pr", get(factory_pr))
|
||||
.route("/factory/issues/pr/merge", post(factory_pr_merge))
|
||||
.route("/account/factory/connection/test", post(test_factory_connection))
|
||||
.route("/factory/drafts", post(factory_new_draft))
|
||||
.route("/factory/drafts/{id}", get(factory_draft))
|
||||
@@ -2653,6 +2658,128 @@ async fn factory_poll(Extension(user): Extension<User>, State(app): State<AppSta
|
||||
Ok(Json(json!({"errors": errors})))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct NewIssueChat {
|
||||
repo: String,
|
||||
/// "feature" or "slice"
|
||||
mode: String,
|
||||
#[serde(default)]
|
||||
topic: String,
|
||||
#[serde(default)]
|
||||
model: Option<String>,
|
||||
}
|
||||
|
||||
fn factory_repo_cfg(app: &AppState, user: &User, repo: &str) -> Result<crate::factory::RepoCfg, ApiError> {
|
||||
let s: crate::factory::FactorySettings = app.store.user_setting(&user.id, "factory")?;
|
||||
s.repos.iter().find(|r| r.url == repo).map(|r| s.resolve(r)).ok_or_else(|| ApiError(StatusCode::NOT_FOUND, "the repository is not configured".into()))
|
||||
}
|
||||
|
||||
/// Start a chat that works out a feature or cuts a milestone into issues.
|
||||
async fn factory_new_issue_chat(Extension(user): Extension<User>, State(app): State<AppState>, Json(b): Json<NewIssueChat>) -> ApiResult {
|
||||
if !["feature", "slice"].contains(&b.mode.as_str()) {
|
||||
return Err(ApiError(StatusCode::BAD_REQUEST, "unknown mode".into()));
|
||||
}
|
||||
let cfg = factory_repo_cfg(&app, &user, &b.repo)?;
|
||||
let s: crate::factory::FactorySettings = app.store.user_setting(&user.id, "factory")?;
|
||||
let model = b.model.filter(|m| !m.is_empty()).unwrap_or(s.defaults.spec_model.clone());
|
||||
let session = crate::factory::start_issue_chat(&app, &user, &cfg, &model, &b.mode, &b.topic).await.map_err(|e| ApiError(StatusCode::BAD_REQUEST, format!("{e:#}")))?;
|
||||
Ok(Json(json!(session)))
|
||||
}
|
||||
|
||||
fn issue_chat_session(app: &AppState, user: &User, id: &str) -> Result<(String, std::path::PathBuf), ApiError> {
|
||||
let session = app.session(id).map_err(|_| ApiError(StatusCode::NOT_FOUND, "unknown chat".into()))?;
|
||||
let dir = crate::factory::issue_chat_of(app, user, &session).ok_or_else(|| ApiError(StatusCode::NOT_FOUND, "not an issue chat".into()))?;
|
||||
let repo = std::fs::read_to_string(dir.join("repo.txt")).unwrap_or_default().trim().to_string();
|
||||
Ok((repo, dir))
|
||||
}
|
||||
|
||||
/// The issue list the model proposed in its last message.
|
||||
async fn factory_issue_chat(Extension(user): Extension<User>, State(app): State<AppState>, Path(id): Path<String>) -> ApiResult {
|
||||
let (repo, _) = issue_chat_session(&app, &user, &id)?;
|
||||
Ok(Json(json!({"repo": repo, "proposal": crate::factory::issue_proposal(&app, &id)})))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct CreateIssues {
|
||||
spec: String,
|
||||
issues: Vec<Value>,
|
||||
/// Give the issues the factory label so the workflow starts right away.
|
||||
#[serde(default)]
|
||||
start: bool,
|
||||
}
|
||||
|
||||
/// Create the (possibly edited) issues of a proposal on the forge.
|
||||
async fn factory_create_issues(Extension(user): Extension<User>, State(app): State<AppState>, headers: axum::http::HeaderMap, Path(id): Path<String>, Json(b): Json<CreateIssues>) -> ApiResult {
|
||||
let (repo, _) = issue_chat_session(&app, &user, &id)?;
|
||||
let cfg = factory_repo_cfg(&app, &user, &repo)?;
|
||||
if b.issues.is_empty() || b.issues.len() > 100 {
|
||||
return Err(ApiError(StatusCode::BAD_REQUEST, "choose between 1 and 100 issues".into()));
|
||||
}
|
||||
let created = crate::factory::create_issues(&cfg, &b.spec, &b.issues, b.start).await.map_err(|e| ApiError(StatusCode::BAD_GATEWAY, format!("{e:#}")))?;
|
||||
app.audit(Some(&user), "factory.issues.create", &format!("{repo} ({} issues)", created.len()), &crate::auth::client_ip(&headers));
|
||||
if b.start {
|
||||
let _ = crate::factory::poll_repo(&app, &user, &cfg).await;
|
||||
}
|
||||
Ok(Json(json!({"created": created})))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct PrQuery {
|
||||
repo: String,
|
||||
number: i64,
|
||||
}
|
||||
|
||||
/// The issue's forge client and row; the issue must belong to the user.
|
||||
fn factory_issue_forge(app: &AppState, user: &User, repo: &str, number: i64) -> Result<(crate::factory::Forge, Value), ApiError> {
|
||||
let row = app.store.factory_issue(repo, number)?.filter(|r| r["owner_id"].as_str() == Some(user.id.as_str())).ok_or_else(|| ApiError(StatusCode::NOT_FOUND, "unknown issue".into()))?;
|
||||
let s: crate::factory::FactorySettings = app.store.user_setting(&user.id, "factory")?;
|
||||
let cfg = s.repos.iter().find(|r| r.url == repo).map(|r| s.resolve(r)).ok_or_else(|| ApiError(StatusCode::NOT_FOUND, "the repository is not configured".into()))?;
|
||||
let g = crate::factory::Forge::new(&cfg).map_err(|e| ApiError(StatusCode::BAD_REQUEST, format!("{e:#}")))?;
|
||||
Ok((g, row))
|
||||
}
|
||||
|
||||
/// The pull request of an issue: the open one from its branch, else the one on record.
|
||||
async fn factory_pr(Extension(user): Extension<User>, State(app): State<AppState>, Query(q): Query<PrQuery>) -> ApiResult {
|
||||
let (g, row) = factory_issue_forge(&app, &user, &q.repo, q.number)?;
|
||||
let bad = |e: anyhow::Error| ApiError(StatusCode::BAD_GATEWAY, format!("{e:#}"));
|
||||
let branch = row["branch"].as_str().unwrap_or("");
|
||||
let mut pr = if branch.is_empty() { None } else { g.find_pr(branch).await.map_err(bad)? };
|
||||
if pr.is_none() {
|
||||
let n: u64 = row["pr_url"].as_str().unwrap_or("").trim_end_matches('/').rsplit('/').next().and_then(|n| n.parse().ok()).unwrap_or(0);
|
||||
if n > 0 {
|
||||
pr = g.pr(n).await.ok();
|
||||
}
|
||||
}
|
||||
Ok(Json(json!({"pr": pr})))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct MergePr {
|
||||
repo: String,
|
||||
number: i64,
|
||||
pr: u64,
|
||||
#[serde(default)]
|
||||
method: String,
|
||||
}
|
||||
|
||||
/// Merge the pull request of an issue – only on the user's own click in the factory page.
|
||||
async fn factory_pr_merge(Extension(user): Extension<User>, State(app): State<AppState>, headers: axum::http::HeaderMap, Json(b): Json<MergePr>) -> ApiResult {
|
||||
let (g, row) = factory_issue_forge(&app, &user, &b.repo, b.number)?;
|
||||
let method = if b.method.is_empty() { "merge" } else { b.method.as_str() };
|
||||
if !["merge", "squash", "rebase"].contains(&method) {
|
||||
return Err(ApiError(StatusCode::BAD_REQUEST, "unknown merge method".into()));
|
||||
}
|
||||
let pr = g.pr(b.pr).await.map_err(|e| ApiError(StatusCode::BAD_GATEWAY, format!("{e:#}")))?;
|
||||
// Only the pull request of this issue's own branch may be merged from here.
|
||||
if pr["head"].as_str() != row["branch"].as_str() || pr["state"].as_str() != Some("open") {
|
||||
return Err(ApiError(StatusCode::BAD_REQUEST, "this is not the open pull request of the issue".into()));
|
||||
}
|
||||
g.merge_pr(b.pr, method).await.map_err(|e| ApiError(StatusCode::BAD_GATEWAY, format!("{e:#}")))?;
|
||||
app.store.factory_issue_set(&b.repo, b.number, &[("pr_url", json!(pr["html_url"].as_str().unwrap_or("")))])?;
|
||||
app.audit(Some(&user), "factory.pr.merge", &format!("{}#{}", b.repo, b.number), &crate::auth::client_ip(&headers));
|
||||
Ok(Json(json!({"ok": true})))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct RestartIssue {
|
||||
repo: String,
|
||||
|
||||
Reference in new issue
Block a user