//! Pull request flow controls: draft state and auto-merge. //! //! - `POST /{owner}/{repo}/pulls/{number}/ready` marks a draft ready for review. //! - `POST /{owner}/{repo}/pulls/{number}/draft` converts an open pull request back to a draft. //! - `POST /{owner}/{repo}/pulls/{number}/auto-merge` enables auto-merge with a style. //! - `POST /{owner}/{repo}/pulls/{number}/auto-merge/disable` switches it off. //! //! Draft changes are for the pull request author and for Write holders. Auto-merge needs Write, //! a signed-in viewer with a valid CSRF token, and a pull request that is blocked only by checks //! or approvals that can still arrive. All of them answer with a redirect back to the pull //! request (the soft-nav script follows it without a reload). use forge_core::{Action, IssueState, MergeStyle, PullRequest, authorize}; use serde::Deserialize; use topcoat::Result; use topcoat::context::{Cx, app_context}; use topcoat::router::content::Form; use topcoat::router::error::{bad_request, forbidden, not_found, see_other}; use topcoat::router::response::Response; use topcoat::router::{path_param, route}; use crate::auth::require_csrf; use crate::notify::{self, Activity}; use crate::pages::issue_parts::parse_number; use crate::pages::pulls_merge::{ERR_AUTO_MERGE_BLOCKED, merge_gate}; use crate::pages::repo_ctx::{RepoCtx, 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, } fn pull_href(ctx: &RepoCtx, number: i64) -> String { repo_href( &ctx.repo.owner_name, &ctx.repo.name, "pulls", &number.to_string(), ) } fn can_write(ctx: &RepoCtx, user: &forge_core::User) -> bool { authorize(Some(user), &ctx.repo, ctx.role, ctx.org_role, Action::Write).is_ok() } async fn load(cx: &Cx, ctx: &RepoCtx, number: i64) -> Result { let state = app_context::(cx); match state.store.pull_by_number(&ctx.repo.id, number).await? { Some(pr) => Ok(pr), None => Err(not_found().into()), } } #[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(path_param::(cx))?; let ctx = load_repo(cx, path_param::(cx), path_param::(cx)).await?; let (user, _) = require_csrf(cx, csrf).await?; let state = app_context::(cx); let pr = load(cx, &ctx, number).await?; if ctx.repo.archived || !(can_write(&ctx, &user) || 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 { // Reviewers hear about the pull request now, not when it was opened as a 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(path_param::(cx))?; let ctx = load_repo(cx, path_param::(cx), path_param::(cx)).await?; let (user, _) = require_csrf(cx, &form.csrf).await?; if ctx.repo.archived || !can_write(&ctx, &user) { return Err(forbidden().into()); } let state = app_context::(cx); let pr = load(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?; // Nothing may be missing by now: merge right away instead of waiting for an event. 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(path_param::(cx))?; let ctx = load_repo(cx, path_param::(cx), path_param::(cx)).await?; let (user, _) = require_csrf(cx, &form.csrf).await?; if ctx.repo.archived || !can_write(&ctx, &user) { return Err(forbidden().into()); } let state = app_context::(cx); let pr = load(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()) }