From 2e6d8a0ee86879c112f87c1fb61727c79e35586c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 28 Oct 2026 17:40:40 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=92=20harden:=20an=20approval=20counts?= =?UTF-8?q?=20only=20while=20the=20PR=20head=20is=20the=20commit=20it=20wa?= =?UTF-8?q?s=20given=20on,=20so=20a=20push=20after=20review=20needs=20a=20?= =?UTF-8?q?new=20approval;=20the=20merge=20queue=20checks=20reviews=20agai?= =?UTF-8?q?n=20right=20before=20it=20lands=20an=20entry;=20a=20job's=20art?= =?UTF-8?q?ifacts=20together=20are=20capped=20at=202=20GiB=20(ci.max=5Fjob?= =?UTF-8?q?=5Fartifacts=5Fmb,=20on=20the=20limits=20page)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude --- crates/forge-core/src/config.rs | 3 +++ crates/forge-core/src/server_settings.rs | 10 ++++++++ crates/forge-core/src/store/reviews.rs | 20 ++++++++-------- crates/forge-web/src/ci/api.rs | 24 +++++++++++++++++++- crates/forge-web/src/i18n/server_settings.rs | 1 + crates/forge-web/src/merge_queue.rs | 6 +++++ crates/forge-web/src/pages/pulls_merge.rs | 10 ++++++-- docs/ci.md | 1 + 8 files changed, 63 insertions(+), 12 deletions(-) diff --git a/crates/forge-core/src/config.rs b/crates/forge-core/src/config.rs index dffc211..af70ab1 100644 --- a/crates/forge-core/src/config.rs +++ b/crates/forge-core/src/config.rs @@ -364,6 +364,8 @@ pub struct CiConfig { pub max_artifact_mb: u64, /// Artifacts a single job may upload. pub max_artifacts_per_job: u32, + /// Largest total size of one job's artifacts, in MiB. + pub max_job_artifacts_mb: u64, /// Days a job log is kept. pub log_retention_days: u64, /// Days an artifact is kept. @@ -387,6 +389,7 @@ impl Default for CiConfig { max_log_mb: 16, max_artifact_mb: 512, max_artifacts_per_job: 50, + max_job_artifacts_mb: 2048, log_retention_days: 90, artifact_retention_days: 30, artifact_keep_runs: 10, diff --git a/crates/forge-core/src/server_settings.rs b/crates/forge-core/src/server_settings.rs index 14c5e28..e5f1f15 100644 --- a/crates/forge-core/src/server_settings.rs +++ b/crates/forge-core/src/server_settings.rs @@ -286,6 +286,14 @@ pub static FIELDS: &[Field] = &[ "Artifacts per job", "", ), + f( + "ci.max_job_artifacts_mb", + Limits, + L, + MB, + "Largest total of one job's artifacts (MiB)", + "", + ), f( "ci.log_retention_days", Limits, @@ -882,6 +890,7 @@ pub fn get(cfg: &Config, key: &str) -> String { "ci.max_log_mb" => cfg.ci.max_log_mb.to_string(), "ci.max_artifact_mb" => cfg.ci.max_artifact_mb.to_string(), "ci.max_artifacts_per_job" => cfg.ci.max_artifacts_per_job.to_string(), + "ci.max_job_artifacts_mb" => cfg.ci.max_job_artifacts_mb.to_string(), "ci.log_retention_days" => cfg.ci.log_retention_days.to_string(), "ci.artifact_retention_days" => cfg.ci.artifact_retention_days.to_string(), "ci.artifact_keep_runs" => cfg.ci.artifact_keep_runs.to_string(), @@ -996,6 +1005,7 @@ pub fn set(cfg: &mut Config, key: &str, raw: &str) -> Result<()> { "ci.max_log_mb" => cfg.ci.max_log_mb = n(v), "ci.max_artifact_mb" => cfg.ci.max_artifact_mb = n(v), "ci.max_artifacts_per_job" => cfg.ci.max_artifacts_per_job = n(v) as u32, + "ci.max_job_artifacts_mb" => cfg.ci.max_job_artifacts_mb = n(v), "ci.log_retention_days" => cfg.ci.log_retention_days = n(v), "ci.artifact_retention_days" => cfg.ci.artifact_retention_days = n(v), "ci.artifact_keep_runs" => cfg.ci.artifact_keep_runs = n(v) as u32, diff --git a/crates/forge-core/src/store/reviews.rs b/crates/forge-core/src/store/reviews.rs index aacf5b2..14f5935 100644 --- a/crates/forge-core/src/store/reviews.rs +++ b/crates/forge-core/src/store/reviews.rs @@ -804,7 +804,7 @@ impl Store { /// lets the reviewer's earlier, undismissed state count again. pub async fn review_summary(&self, issue_id: &str) -> Result { let mut summary = ReviewSummary::default(); - for (_, name, state) in self.latest_review_states(issue_id).await? { + for (_, name, state, _) in self.latest_review_states(issue_id).await? { match state { ReviewState::Approved => summary.approved_by.push(name), _ => summary.changes_requested_by.push(name), @@ -816,15 +816,16 @@ impl Store { } /// Each reviewer's latest non-commented, non-dismissed submitted state, as - /// `(author_id, username, state)`, ordered by author id. + /// `(author_id, username, state, commit_oid)`, ordered by author id. `commit_oid` is the head + /// the review was submitted on. pub async fn latest_review_states( &self, issue_id: &str, - ) -> Result> { + ) -> Result> { let conn = self.read_conn().await; let rows = fetch_all( &conn, - "SELECT r.author_id, u.username, r.state FROM pull_reviews r \ + "SELECT r.author_id, u.username, r.state, r.commit_oid FROM pull_reviews r \ JOIN users u ON u.id = r.author_id WHERE r.issue_id = ?1 \ AND r.state IN ('approved', 'changes_requested') ORDER BY r.submitted_at, r.id", (issue_id.to_string(),), @@ -833,18 +834,19 @@ impl Store { col_text(row, 0)?, col_text(row, 1)?, parse_db::(&col_text(row, 2)?)?, + col_text(row, 3)?, )) }, ) .await?; // Rows are in submission order, so the last one per reviewer is the latest. - let mut latest: HashMap = HashMap::new(); - for (author_id, name, state) in rows { - latest.insert(author_id, (name, state)); + let mut latest: HashMap = HashMap::new(); + for (author_id, name, state, commit) in rows { + latest.insert(author_id, (name, state, commit)); } - let mut out: Vec<(String, String, ReviewState)> = latest + let mut out: Vec<(String, String, ReviewState, String)> = latest .into_iter() - .map(|(id, (name, state))| (id, name, state)) + .map(|(id, (name, state, commit))| (id, name, state, commit)) .collect(); out.sort_unstable_by(|a, b| a.0.cmp(&b.0)); Ok(out) diff --git a/crates/forge-web/src/ci/api.rs b/crates/forge-web/src/ci/api.rs index f5e1a4b..2516eb6 100644 --- a/crates/forge-web/src/ci/api.rs +++ b/crates/forge-web/src/ci/api.rs @@ -886,7 +886,29 @@ async fn upload(state: &AppState, job: &CiJob, name: &str, body: Body) -> Result ))); } - let max_bytes = state.live().ci.max_artifact_mb.saturating_mul(MIB); + // One artifact is held to its own limit and to what is left of the job's total. + let used: u64 = existing + .iter() + .map(|artifact| u64::try_from(artifact.size).unwrap_or(0)) + .sum(); + let left = state + .live() + .ci + .max_job_artifacts_mb + .saturating_mul(MIB) + .saturating_sub(used); + if left == 0 { + return Err(Box::new(api::error( + StatusCode::CONFLICT, + "the job's artifacts have reached their total size limit", + ))); + } + let max_bytes = state + .live() + .ci + .max_artifact_mb + .saturating_mul(MIB) + .min(left); let temp_key = format!("ci/tmp/{}", ulid::Ulid::generate()); let mut writer = state .blobs diff --git a/crates/forge-web/src/i18n/server_settings.rs b/crates/forge-web/src/i18n/server_settings.rs index 7d798c1..c5b47b6 100644 --- a/crates/forge-web/src/i18n/server_settings.rs +++ b/crates/forge-web/src/i18n/server_settings.rs @@ -63,6 +63,7 @@ pub fn field_tr(key: &str) -> Option<(&'static str, &'static str)> { ), "ci.max_artifact_mb" => ("En büyük yapıt (MiB)", ""), "ci.max_artifacts_per_job" => ("İş başına yapıt", ""), + "ci.max_job_artifacts_mb" => ("Bir işin yapıtlarının toplamı en çok (MiB)", ""), "ci.log_retention_days" => ("Günlük saklama süresi (gün)", ""), "ci.artifact_retention_days" => ("Yapıt saklama süresi (gün)", ""), "ci.artifact_keep_runs" => ( diff --git a/crates/forge-web/src/merge_queue.rs b/crates/forge-web/src/merge_queue.rs index 0d502d5..57c9f47 100644 --- a/crates/forge-web/src/merge_queue.rs +++ b/crates/forge-web/src/merge_queue.rs @@ -347,6 +347,12 @@ async fn land( finish_removed(state, repo, entry, "permission").await?; return Ok(true); } + // Reviews may have changed while the candidate was tested (a change request, a dismissed + // approval): the same readiness test as before testing, now right before the target moves. + if let Some(reason) = unready(state, repo, pr).await? { + finish_removed(state, repo, entry, reason).await?; + return Ok(true); + } let refname = format!("refs/heads/{}", entry.branch); let base_path = repo_path(&state.git.data_dir, &repo.id)?; match update_ref_cas(&state.git, &base_path, &refname, &tested, &base).await { diff --git a/crates/forge-web/src/pages/pulls_merge.rs b/crates/forge-web/src/pages/pulls_merge.rs index cf2144d..a876110 100644 --- a/crates/forge-web/src/pages/pulls_merge.rs +++ b/crates/forge-web/src/pages/pulls_merge.rs @@ -310,7 +310,8 @@ impl MergeGate { } /// Evaluates the effective rule for the pull request's base branch. Approvals and change requests -/// count only from `counts_for_merge_rules` reviewers, using each reviewer's latest review. +/// count only from `counts_for_merge_rules` reviewers, using each reviewer's latest review; an +/// approval counts only while the head is the commit it was given on. /// Repo admins are not exempt. A base branch that does not exist counts as up to date; the merge /// itself reports that. pub async fn merge_gate( @@ -322,7 +323,12 @@ pub async fn merge_gate( let rule = effective(&rules, &repo.default_branch, &pr.base_branch); let mut approvals = 0u32; let mut changes_requested = false; - for (author_id, _, review) in state.store.latest_review_states(&pr.issue.id).await? { + for (author_id, _, review, commit) in state.store.latest_review_states(&pr.issue.id).await? { + // An approval is of the commits the reviewer saw: a push after it leaves it uncounted + // until they approve again. A change request stands until it is dismissed or replaced. + if review == ReviewState::Approved && commit != pr.head_oid { + continue; + } let Some(reviewer) = state.store.user_by_id(&author_id).await? else { continue; }; diff --git a/docs/ci.md b/docs/ci.md index edd77a2..2080d04 100644 --- a/docs/ci.md +++ b/docs/ci.md @@ -762,6 +762,7 @@ enabled = true max_log_mb = 16 # a log past this is cut with "[log truncated]" max_artifact_mb = 512 # per artifact max_artifacts_per_job = 50 +max_job_artifacts_mb = 2048 # all of one job's artifacts together log_retention_days = 90 artifact_retention_days = 30 artifact_keep_runs = 10 # artifacts of the newest N runs per repo (that have any) are kept; 0 keeps all