An approval counts only while the PR head is the commit it was given on, so a push after review needs a new approval
Browse files at this commit2e6d8a0ee86879c112f87c1fb61727c79e35586cParent: 9a27aab- harden
- an approval counts only while the PR head is the commit it was given on, so a push after review needs a new approval
- the merge queue checks reviews again right before it lands an entry
- a job's artifacts together are capped at 2 GiB (ci.max_job_artifacts_mb, on the limits page)
8 changed files +63 -12
- crates/forge-core/src/config.rs +3 -0
- crates/forge-core/src/server_settings.rs +10 -0
- crates/forge-core/src/store/reviews.rs +11 -9
- crates/forge-web/src/ci/api.rs +23 -1
- crates/forge-web/src/i18n/server_settings.rs +1 -0
- crates/forge-web/src/merge_queue.rs +6 -0
- crates/forge-web/src/pages/pulls_merge.rs +8 -2
- docs/ci.md +1 -0
crates/forge-core/src/config.rsmodified+3 -0
| @@ -364,6 +364,8 @@ pub struct CiConfig { | ||
| 364 | 364 | pub max_artifact_mb: u64, |
| 365 | 365 | /// Artifacts a single job may upload. |
| 366 | 366 | pub max_artifacts_per_job: u32, |
| 367 | + /// Largest total size of one job's artifacts, in MiB. | |
| 368 | + pub max_job_artifacts_mb: u64, | |
| 367 | 369 | /// Days a job log is kept. |
| 368 | 370 | pub log_retention_days: u64, |
| 369 | 371 | /// Days an artifact is kept. |
| @@ -387,6 +389,7 @@ impl Default for CiConfig { | ||
| 387 | 389 | max_log_mb: 16, |
| 388 | 390 | max_artifact_mb: 512, |
| 389 | 391 | max_artifacts_per_job: 50, |
| 392 | + max_job_artifacts_mb: 2048, | |
| 390 | 393 | log_retention_days: 90, |
| 391 | 394 | artifact_retention_days: 30, |
| 392 | 395 | artifact_keep_runs: 10, |
crates/forge-core/src/server_settings.rsmodified+10 -0
| @@ -286,6 +286,14 @@ pub static FIELDS: &[Field] = &[ | ||
| 286 | 286 | "Artifacts per job", |
| 287 | 287 | "", |
| 288 | 288 | ), |
| 289 | + f( | |
| 290 | + "ci.max_job_artifacts_mb", | |
| 291 | + Limits, | |
| 292 | + L, | |
| 293 | + MB, | |
| 294 | + "Largest total of one job's artifacts (MiB)", | |
| 295 | + "", | |
| 296 | + ), | |
| 289 | 297 | f( |
| 290 | 298 | "ci.log_retention_days", |
| 291 | 299 | Limits, |
| @@ -882,6 +890,7 @@ pub fn get(cfg: &Config, key: &str) -> String { | ||
| 882 | 890 | "ci.max_log_mb" => cfg.ci.max_log_mb.to_string(), |
| 883 | 891 | "ci.max_artifact_mb" => cfg.ci.max_artifact_mb.to_string(), |
| 884 | 892 | "ci.max_artifacts_per_job" => cfg.ci.max_artifacts_per_job.to_string(), |
| 893 | + "ci.max_job_artifacts_mb" => cfg.ci.max_job_artifacts_mb.to_string(), | |
| 885 | 894 | "ci.log_retention_days" => cfg.ci.log_retention_days.to_string(), |
| 886 | 895 | "ci.artifact_retention_days" => cfg.ci.artifact_retention_days.to_string(), |
| 887 | 896 | "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<()> { | ||
| 996 | 1005 | "ci.max_log_mb" => cfg.ci.max_log_mb = n(v), |
| 997 | 1006 | "ci.max_artifact_mb" => cfg.ci.max_artifact_mb = n(v), |
| 998 | 1007 | "ci.max_artifacts_per_job" => cfg.ci.max_artifacts_per_job = n(v) as u32, |
| 1008 | + "ci.max_job_artifacts_mb" => cfg.ci.max_job_artifacts_mb = n(v), | |
| 999 | 1009 | "ci.log_retention_days" => cfg.ci.log_retention_days = n(v), |
| 1000 | 1010 | "ci.artifact_retention_days" => cfg.ci.artifact_retention_days = n(v), |
| 1001 | 1011 | "ci.artifact_keep_runs" => cfg.ci.artifact_keep_runs = n(v) as u32, |
crates/forge-core/src/store/reviews.rsmodified+11 -9
| @@ -804,7 +804,7 @@ impl Store { | ||
| 804 | 804 | /// lets the reviewer's earlier, undismissed state count again. |
| 805 | 805 | pub async fn review_summary(&self, issue_id: &str) -> Result<ReviewSummary> { |
| 806 | 806 | let mut summary = ReviewSummary::default(); |
| 807 | - for (_, name, state) in self.latest_review_states(issue_id).await? { | |
| 807 | + for (_, name, state, _) in self.latest_review_states(issue_id).await? { | |
| 808 | 808 | match state { |
| 809 | 809 | ReviewState::Approved => summary.approved_by.push(name), |
| 810 | 810 | _ => summary.changes_requested_by.push(name), |
| @@ -816,15 +816,16 @@ impl Store { | ||
| 816 | 816 | } |
| 817 | 817 | |
| 818 | 818 | /// Each reviewer's latest non-commented, non-dismissed submitted state, as |
| 819 | - /// `(author_id, username, state)`, ordered by author id. | |
| 819 | + /// `(author_id, username, state, commit_oid)`, ordered by author id. `commit_oid` is the head | |
| 820 | + /// the review was submitted on. | |
| 820 | 821 | pub async fn latest_review_states( |
| 821 | 822 | &self, |
| 822 | 823 | issue_id: &str, |
| 823 | - ) -> Result<Vec<(String, String, ReviewState)>> { | |
| 824 | + ) -> Result<Vec<(String, String, ReviewState, String)>> { | |
| 824 | 825 | let conn = self.read_conn().await; |
| 825 | 826 | let rows = fetch_all( |
| 826 | 827 | &conn, |
| 827 | - "SELECT r.author_id, u.username, r.state FROM pull_reviews r \ | |
| 828 | + "SELECT r.author_id, u.username, r.state, r.commit_oid FROM pull_reviews r \ | |
| 828 | 829 | JOIN users u ON u.id = r.author_id WHERE r.issue_id = ?1 \ |
| 829 | 830 | AND r.state IN ('approved', 'changes_requested') ORDER BY r.submitted_at, r.id", |
| 830 | 831 | (issue_id.to_string(),), |
| @@ -833,18 +834,19 @@ impl Store { | ||
| 833 | 834 | col_text(row, 0)?, |
| 834 | 835 | col_text(row, 1)?, |
| 835 | 836 | parse_db::<ReviewState>(&col_text(row, 2)?)?, |
| 837 | + col_text(row, 3)?, | |
| 836 | 838 | )) |
| 837 | 839 | }, |
| 838 | 840 | ) |
| 839 | 841 | .await?; |
| 840 | 842 | // Rows are in submission order, so the last one per reviewer is the latest. |
| 841 | - let mut latest: HashMap<String, (String, ReviewState)> = HashMap::new(); | |
| 842 | - for (author_id, name, state) in rows { | |
| 843 | - latest.insert(author_id, (name, state)); | |
| 843 | + let mut latest: HashMap<String, (String, ReviewState, String)> = HashMap::new(); | |
| 844 | + for (author_id, name, state, commit) in rows { | |
| 845 | + latest.insert(author_id, (name, state, commit)); | |
| 844 | 846 | } |
| 845 | - let mut out: Vec<(String, String, ReviewState)> = latest | |
| 847 | + let mut out: Vec<(String, String, ReviewState, String)> = latest | |
| 846 | 848 | .into_iter() |
| 847 | - .map(|(id, (name, state))| (id, name, state)) | |
| 849 | + .map(|(id, (name, state, commit))| (id, name, state, commit)) | |
| 848 | 850 | .collect(); |
| 849 | 851 | out.sort_unstable_by(|a, b| a.0.cmp(&b.0)); |
| 850 | 852 | Ok(out) |
crates/forge-web/src/ci/api.rsmodified+23 -1
| @@ -886,7 +886,29 @@ async fn upload(state: &AppState, job: &CiJob, name: &str, body: Body) -> Result | ||
| 886 | 886 | ))); |
| 887 | 887 | } |
| 888 | 888 | |
| 889 | - let max_bytes = state.live().ci.max_artifact_mb.saturating_mul(MIB); | |
| 889 | + // One artifact is held to its own limit and to what is left of the job's total. | |
| 890 | + let used: u64 = existing | |
| 891 | + .iter() | |
| 892 | + .map(|artifact| u64::try_from(artifact.size).unwrap_or(0)) | |
| 893 | + .sum(); | |
| 894 | + let left = state | |
| 895 | + .live() | |
| 896 | + .ci | |
| 897 | + .max_job_artifacts_mb | |
| 898 | + .saturating_mul(MIB) | |
| 899 | + .saturating_sub(used); | |
| 900 | + if left == 0 { | |
| 901 | + return Err(Box::new(api::error( | |
| 902 | + StatusCode::CONFLICT, | |
| 903 | + "the job's artifacts have reached their total size limit", | |
| 904 | + ))); | |
| 905 | + } | |
| 906 | + let max_bytes = state | |
| 907 | + .live() | |
| 908 | + .ci | |
| 909 | + .max_artifact_mb | |
| 910 | + .saturating_mul(MIB) | |
| 911 | + .min(left); | |
| 890 | 912 | let temp_key = format!("ci/tmp/{}", ulid::Ulid::generate()); |
| 891 | 913 | let mut writer = state |
| 892 | 914 | .blobs |
crates/forge-web/src/i18n/server_settings.rsmodified+1 -0
| @@ -63,6 +63,7 @@ pub fn field_tr(key: &str) -> Option<(&'static str, &'static str)> { | ||
| 63 | 63 | ), |
| 64 | 64 | "ci.max_artifact_mb" => ("En büyük yapıt (MiB)", ""), |
| 65 | 65 | "ci.max_artifacts_per_job" => ("İş başına yapıt", ""), |
| 66 | + "ci.max_job_artifacts_mb" => ("Bir işin yapıtlarının toplamı en çok (MiB)", ""), | |
| 66 | 67 | "ci.log_retention_days" => ("Günlük saklama süresi (gün)", ""), |
| 67 | 68 | "ci.artifact_retention_days" => ("Yapıt saklama süresi (gün)", ""), |
| 68 | 69 | "ci.artifact_keep_runs" => ( |
crates/forge-web/src/merge_queue.rsmodified+6 -0
| @@ -347,6 +347,12 @@ async fn land( | ||
| 347 | 347 | finish_removed(state, repo, entry, "permission").await?; |
| 348 | 348 | return Ok(true); |
| 349 | 349 | } |
| 350 | + // Reviews may have changed while the candidate was tested (a change request, a dismissed | |
| 351 | + // approval): the same readiness test as before testing, now right before the target moves. | |
| 352 | + if let Some(reason) = unready(state, repo, pr).await? { | |
| 353 | + finish_removed(state, repo, entry, reason).await?; | |
| 354 | + return Ok(true); | |
| 355 | + } | |
| 350 | 356 | let refname = format!("refs/heads/{}", entry.branch); |
| 351 | 357 | let base_path = repo_path(&state.git.data_dir, &repo.id)?; |
| 352 | 358 | match update_ref_cas(&state.git, &base_path, &refname, &tested, &base).await { |
crates/forge-web/src/pages/pulls_merge.rsmodified+8 -2
| @@ -310,7 +310,8 @@ impl MergeGate { | ||
| 310 | 310 | } |
| 311 | 311 | |
| 312 | 312 | /// Evaluates the effective rule for the pull request's base branch. Approvals and change requests |
| 313 | -/// count only from `counts_for_merge_rules` reviewers, using each reviewer's latest review. | |
| 313 | +/// count only from `counts_for_merge_rules` reviewers, using each reviewer's latest review; an | |
| 314 | +/// approval counts only while the head is the commit it was given on. | |
| 314 | 315 | /// Repo admins are not exempt. A base branch that does not exist counts as up to date; the merge |
| 315 | 316 | /// itself reports that. |
| 316 | 317 | pub async fn merge_gate( |
| @@ -322,7 +323,12 @@ pub async fn merge_gate( | ||
| 322 | 323 | let rule = effective(&rules, &repo.default_branch, &pr.base_branch); |
| 323 | 324 | let mut approvals = 0u32; |
| 324 | 325 | let mut changes_requested = false; |
| 325 | - for (author_id, _, review) in state.store.latest_review_states(&pr.issue.id).await? { | |
| 326 | + for (author_id, _, review, commit) in state.store.latest_review_states(&pr.issue.id).await? { | |
| 327 | + // An approval is of the commits the reviewer saw: a push after it leaves it uncounted | |
| 328 | + // until they approve again. A change request stands until it is dismissed or replaced. | |
| 329 | + if review == ReviewState::Approved && commit != pr.head_oid { | |
| 330 | + continue; | |
| 331 | + } | |
| 326 | 332 | let Some(reviewer) = state.store.user_by_id(&author_id).await? else { |
| 327 | 333 | continue; |
| 328 | 334 | }; |
docs/ci.mdmodified+1 -0
| @@ -762,6 +762,7 @@ enabled = true | ||
| 762 | 762 | max_log_mb = 16 # a log past this is cut with "[log truncated]" |
| 763 | 763 | max_artifact_mb = 512 # per artifact |
| 764 | 764 | max_artifacts_per_job = 50 |
| 765 | +max_job_artifacts_mb = 2048 # all of one job's artifacts together | |
| 765 | 766 | log_retention_days = 90 |
| 766 | 767 | artifact_retention_days = 30 |
| 767 | 768 | artifact_keep_runs = 10 # artifacts of the newest N runs per repo (that have any) are kept; 0 keeps all |