use forge_core::{IssueState, MergeStyle}; use serde::Deserialize; use topcoat::Result; use topcoat::context::{Cx, context}; use topcoat::router::content::Form; use topcoat::router::error::{bad_request, forbidden, see_other}; use topcoat::router::response::Response; use topcoat::router::{path_param, route}; use crate::auth::require_csrf; use crate::merge::{ERR_AUTO_MERGE_BLOCKED, merge_gate}; use crate::notify::{self, Activity}; use crate::pages::issue_parts::parse_number; use crate::pages::repo_ctx::{RepoCtx, load_pull, load_repo}; use crate::state::AppState; use crate::views::repo_href; use crate::webhooks::emit::{Sender, emit_item_action}; path_param!(owner); path_param!(repo); path_param!(number); #[derive(Deserialize)] struct FlowForm { #[serde(default)] csrf: String, #[serde(default)] style: String, } pub(crate) fn pull_href(ctx: &RepoCtx, number: i64) -> String { repo_href( &ctx.repo.owner_name, &ctx.repo.name, "pulls", &number.to_string(), ) } #[route(POST "/{owner}/{repo}/pulls/{number}/ready")] async fn ready_post(cx: &Cx, Form(form): Form) -> Result { set_draft(cx, &form.csrf, false).await } #[route(POST "/{owner}/{repo}/pulls/{number}/draft")] async fn draft_post(cx: &Cx, Form(form): Form) -> Result { set_draft(cx, &form.csrf, true).await } async fn set_draft(cx: &Cx, csrf: &str, draft: bool) -> Result { let number = parse_number(number(cx))?; let ctx = load_repo(cx, owner(cx), repo(cx)).await?; let (user, _) = require_csrf(cx, csrf).await?; let state = context::(cx); let pr = load_pull(cx, &ctx, number).await?; if ctx.repo.archived || !(ctx.can_write() || user.id == pr.issue.author_id) { return Err(forbidden().into()); } let href = pull_href(&ctx, number); if pr.merged_at.is_some() || pr.issue.state != IssueState::Open { return Err(see_other(href).into()); } let changed = match state .store .set_pull_draft(&pr.issue.id, Some(&user.id), draft) .await { Ok(changed) => changed, Err(forge_core::Error::Conflict(_)) => false, Err(e) => return Err(e.into()), }; if changed { let action = if draft { "converted_to_draft" } else { "ready_for_review" }; emit_item_action(state, &ctx.repo, &Sender::of(&user), true, number, action).await; if !draft { notify::dispatch( state, &ctx.repo, &pr.issue, &user.id, Activity::Opened, Some(&pr.issue.body), ) .await; } } Err(see_other(href).into()) } #[route(POST "/{owner}/{repo}/pulls/{number}/auto-merge")] async fn auto_merge_enable(cx: &Cx, Form(form): Form) -> Result { let number = parse_number(number(cx))?; let ctx = load_repo(cx, owner(cx), repo(cx)).await?; let (user, _) = require_csrf(cx, &form.csrf).await?; if !ctx.can_write() { return Err(forbidden().into()); } let state = context::(cx); let pr = load_pull(cx, &ctx, number).await?; let style = form .style .parse::() .map_err(|e| bad_request(e.to_string()))?; let href = pull_href(&ctx, number); if pr.merged_at.is_some() || pr.issue.state != IssueState::Open || pr.head_repo_id.is_none() { return Err(see_other(href).into()); } let gate = merge_gate(state, &ctx.repo, &pr).await?; if !gate.waiting_only() { return Err(see_other(format!("{href}?merge_error={ERR_AUTO_MERGE_BLOCKED}")).into()); } state .store .enable_auto_merge(&pr.issue.id, &user.id, style) .await?; crate::automerge::on_pull(state, &ctx.repo, number).await; Err(see_other(href).into()) } #[route(POST "/{owner}/{repo}/pulls/{number}/auto-merge/disable")] async fn auto_merge_disable(cx: &Cx, Form(form): Form) -> Result { let number = parse_number(number(cx))?; let ctx = load_repo(cx, owner(cx), repo(cx)).await?; let (user, _) = require_csrf(cx, &form.csrf).await?; if !ctx.can_write() { return Err(forbidden().into()); } let state = context::(cx); let pr = load_pull(cx, &ctx, number).await?; state .store .disable_auto_merge(&pr.issue.id, Some(&user.id), "manual") .await?; Err(see_other(pull_href(&ctx, number)).into()) }