g1t/services/work/src/settings.rs
Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| Agents as a team: lifecycle, merge queue, billing and a new shell | 1 | //! A repository's settings for how its pull requests are handled, and the |
| 2 | //! rule about approvals that merging enforces. | |
| 3 | ||
| 4 | use std::collections::HashMap; | |
| 5 | ||
| 6 | use g1t_contracts::time::rfc3339; | |
| 7 | use g1t_contracts::work::*; | |
| 8 | use g1t_contracts::{FailureCode, Outcome}; | |
| 9 | use g1t_kit::now_ms; | |
| 10 | use serde::Deserialize; | |
| 11 | use worker::Result; | |
| 12 | ||
| 13 | use crate::Work; | |
| 14 | use crate::reviews::AGENT_ID; | |
| 15 | ||
| 16 | const MAX_REQUIRED_APPROVALS: u32 = 6; | |
| 17 | const MAX_REVISIONS: u32 = 5; | |
| 18 | ||
| 19 | #[derive(Deserialize)] | |
| 20 | struct SettingsRow { | |
| 21 | auto_merge: u8, | |
| 22 | require_up_to_date: u8, | |
| 23 | required_approvals: u32, | |
| 24 | count_agent_approvals: u8, | |
| 25 | allow_ignoring_checks: u8, | |
| 26 | agent_review: u8, | |
| 27 | max_revisions: u32, | |
| 28 | #[serde(default)] | |
| 29 | merge_queue: u8, | |
| 30 | updated_by: String, | |
| 31 | updated_at: String, | |
| 32 | } | |
| 33 | ||
| 34 | impl From<SettingsRow> for RepoSettings { | |
| 35 | fn from(row: SettingsRow) -> Self { | |
| 36 | RepoSettings { | |
| 37 | auto_merge: row.auto_merge != 0, | |
| 38 | require_up_to_date: row.require_up_to_date != 0, | |
| 39 | required_approvals: row.required_approvals, | |
| 40 | count_agent_approvals: row.count_agent_approvals != 0, | |
| 41 | allow_ignoring_checks: row.allow_ignoring_checks != 0, | |
| 42 | agent_review: row.agent_review != 0, | |
| 43 | max_revisions: row.max_revisions, | |
| 44 | merge_queue: row.merge_queue != 0, | |
| 45 | updated_by: Some(row.updated_by), | |
| 46 | updated_at: Some(row.updated_at), | |
| 47 | } | |
| 48 | } | |
| 49 | } | |
| 50 | ||
| 51 | /// What is missing before a pull request has the approvals its repository | |
| 52 | /// asks for, or `None` if nothing is. `verdicts` is each reviewer's id and | |
| 53 | /// their most recent verdict; the author's own does not count. | |
| 54 | pub(crate) fn approvals_missing( | |
| 55 | settings: &RepoSettings, | |
| 56 | author_id: &str, | |
| 57 | verdicts: &[(String, Verdict)], | |
| 58 | ) -> Option<String> { | |
| 59 | if settings.required_approvals == 0 { | |
| 60 | return None; | |
| 61 | } | |
| 62 | let others = || { | |
| 63 | verdicts | |
| 64 | .iter() | |
| 65 | .filter(|(reviewer, _)| reviewer != author_id) | |
| 66 | }; | |
| 67 | if others().any(|(_, verdict)| *verdict == Verdict::RequestChanges) { | |
| 68 | return Some("A reviewer has asked for changes.".to_owned()); | |
| 69 | } | |
| 70 | let approvals = others() | |
| 71 | .filter(|(reviewer, _)| settings.count_agent_approvals || reviewer != AGENT_ID) | |
| 72 | .count() as u32; | |
| 73 | if approvals >= settings.required_approvals { | |
| 74 | return None; | |
| 75 | } | |
| 76 | let needed = settings.required_approvals; | |
| 77 | let from = if settings.count_agent_approvals { | |
| 78 | "" | |
| 79 | } else { | |
| 80 | " from people" | |
| 81 | }; | |
| 82 | Some(format!( | |
| 83 | "This repository requires {needed} approving {}{from} before a pull request merges; this one has {approvals}.", | |
| 84 | if needed == 1 { "review" } else { "reviews" }, | |
| 85 | )) | |
| 86 | } | |
| 87 | ||
| 88 | #[derive(Deserialize)] | |
| 89 | struct VerdictRow { | |
| 90 | author_id: String, | |
| 91 | verdict: Verdict, | |
| 92 | } | |
| 93 | ||
| 94 | impl Work { | |
| 95 | /// The settings of a repository, by its id. Defaults if none were set. | |
| 96 | pub(crate) async fn settings(&self, repo_id: &str) -> Result<RepoSettings> { | |
| 97 | Ok(self | |
| 98 | .db | |
| 99 | .prepare("SELECT * FROM repo_settings WHERE repo_id = ?") | |
| 100 | .bind(&[repo_id.into()])? | |
| 101 | .first::<SettingsRow>(None) | |
| 102 | .await? | |
| 103 | .map_or_else(RepoSettings::default, RepoSettings::from)) | |
| 104 | } | |
| 105 | ||
| 106 | /// What is missing before a pull request has the approvals its | |
| 107 | /// repository asks for, or `None` if nothing is. | |
| 108 | pub(crate) async fn approvals_gap( | |
| 109 | &self, | |
| 110 | settings: &RepoSettings, | |
| 111 | pull: &Pull, | |
| 112 | ) -> Result<Option<String>> { | |
| 113 | if settings.required_approvals == 0 { | |
| 114 | return Ok(None); | |
| 115 | } | |
| 116 | let rows = self | |
| 117 | .db | |
| 118 | .prepare( | |
| 119 | "SELECT author_id, verdict FROM comments | |
| 120 | WHERE repo_id = ? AND number = ? AND verdict IS NOT NULL ORDER BY id", | |
| 121 | ) | |
| 122 | .bind(&[pull.repo_id.as_str().into(), pull.number.into()])? | |
| 123 | .all() | |
| 124 | .await? | |
| 125 | .results::<VerdictRow>()?; | |
| 126 | // Each reviewer's latest verdict is the one that stands. | |
| 127 | let mut latest: HashMap<String, Verdict> = HashMap::new(); | |
| 128 | for row in rows { | |
| 129 | latest.insert(row.author_id, row.verdict); | |
| 130 | } | |
| 131 | let verdicts: Vec<(String, Verdict)> = latest.into_iter().collect(); | |
| 132 | Ok(approvals_missing(settings, &pull.author.id, &verdicts)) | |
| 133 | } | |
| 134 | ||
| 135 | pub(crate) async fn get_settings(&self, a: ViewArgs) -> Result<Outcome<RepoSettings>> { | |
| 136 | let repo = match self.repo(&a.repo, &a.viewer).await? { | |
| 137 | Outcome::Ok(repo) => repo, | |
| 138 | Outcome::Fail(failure) => return Ok(Outcome::Fail(failure)), | |
| 139 | }; | |
| 140 | Ok(Outcome::Ok(self.settings(&repo.id).await?)) | |
| 141 | } | |
| 142 | ||
| 143 | pub(crate) async fn update_settings( | |
| 144 | &self, | |
| 145 | a: UpdateSettingsArgs, | |
| 146 | ) -> Result<Outcome<RepoSettings>> { | |
| 147 | let repo = match self.repo(&a.repo, &Some(a.actor.clone())).await? { | |
| 148 | Outcome::Ok(repo) => repo, | |
| 149 | Outcome::Fail(failure) => return Ok(Outcome::Fail(failure)), | |
| 150 | }; | |
| 151 | if !a.actor.verified || !a.actor.is_member(&repo.namespace) { | |
| 152 | return Ok(Outcome::fail( | |
| 153 | FailureCode::Forbidden, | |
| 154 | "Only members of the workspace can change a repository's settings.", | |
| 155 | )); | |
| 156 | } | |
| 157 | let settings = RepoSettings { | |
| 158 | required_approvals: a.settings.required_approvals.min(MAX_REQUIRED_APPROVALS), | |
| 159 | max_revisions: a.settings.max_revisions.min(MAX_REVISIONS), | |
| 160 | updated_by: Some(a.actor.username), | |
| 161 | updated_at: Some(rfc3339(now_ms())), | |
| 162 | ..a.settings | |
| 163 | }; | |
| 164 | self.db | |
| 165 | .prepare( | |
| 166 | "INSERT INTO repo_settings | |
| 167 | (repo_id, auto_merge, require_up_to_date, required_approvals, | |
| 168 | count_agent_approvals, allow_ignoring_checks, agent_review, max_revisions, | |
| 169 | merge_queue, updated_by, updated_at) | |
| 170 | VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) | |
| 171 | ON CONFLICT (repo_id) DO UPDATE SET | |
| 172 | auto_merge = excluded.auto_merge, | |
| 173 | require_up_to_date = excluded.require_up_to_date, | |
| 174 | required_approvals = excluded.required_approvals, | |
| 175 | count_agent_approvals = excluded.count_agent_approvals, | |
| 176 | allow_ignoring_checks = excluded.allow_ignoring_checks, | |
| 177 | agent_review = excluded.agent_review, | |
| 178 | max_revisions = excluded.max_revisions, | |
| 179 | merge_queue = excluded.merge_queue, | |
| 180 | updated_by = excluded.updated_by, | |
| 181 | updated_at = excluded.updated_at", | |
| 182 | ) | |
| 183 | .bind(&[ | |
| 184 | repo.id.as_str().into(), | |
| 185 | u32::from(settings.auto_merge).into(), | |
| 186 | u32::from(settings.require_up_to_date).into(), | |
| 187 | settings.required_approvals.into(), | |
| 188 | u32::from(settings.count_agent_approvals).into(), | |
| 189 | u32::from(settings.allow_ignoring_checks).into(), | |
| 190 | u32::from(settings.agent_review).into(), | |
| 191 | settings.max_revisions.into(), | |
| 192 | u32::from(settings.merge_queue).into(), | |
| 193 | settings.updated_by.as_deref().unwrap_or_default().into(), | |
| 194 | settings.updated_at.as_deref().unwrap_or_default().into(), | |
| 195 | ])? | |
| 196 | .run() | |
| 197 | .await?; | |
| 198 | Ok(Outcome::Ok(settings)) | |
| 199 | } | |
| 200 | } | |
| 201 | ||
| 202 | #[cfg(test)] | |
| 203 | mod tests { | |
| 204 | use super::*; | |
| 205 | ||
| 206 | fn verdicts(list: &[(&str, Verdict)]) -> Vec<(String, Verdict)> { | |
| 207 | list.iter() | |
| 208 | .map(|(reviewer, verdict)| ((*reviewer).to_owned(), *verdict)) | |
| 209 | .collect() | |
| 210 | } | |
| 211 | ||
| 212 | fn requiring(approvals: u32) -> RepoSettings { | |
| 213 | RepoSettings { | |
| 214 | required_approvals: approvals, | |
| 215 | ..RepoSettings::default() | |
| 216 | } | |
| 217 | } | |
| 218 | ||
| 219 | #[test] | |
| 220 | fn nothing_is_required_by_default() { | |
| 221 | assert_eq!( | |
| 222 | approvals_missing(&RepoSettings::default(), "usr_a", &[]), | |
| 223 | None | |
| 224 | ); | |
| 225 | } | |
| 226 | ||
| 227 | #[test] | |
| 228 | fn approvals_are_counted_per_reviewer_and_not_from_the_author() { | |
| 229 | let one = requiring(1); | |
| 230 | assert!(approvals_missing(&one, "usr_a", &[]).is_some()); | |
| 231 | let own = verdicts(&[("usr_a", Verdict::Approve)]); | |
| 232 | assert!(approvals_missing(&one, "usr_a", &own).is_some()); | |
| 233 | let other = verdicts(&[("usr_b", Verdict::Approve)]); | |
| 234 | assert_eq!(approvals_missing(&one, "usr_a", &other), None); | |
| 235 | assert!(approvals_missing(&requiring(2), "usr_a", &other).is_some()); | |
| 236 | } | |
| 237 | ||
| 238 | #[test] | |
| 239 | fn a_request_for_changes_blocks_whatever_else_was_approved() { | |
| 240 | let mixed = verdicts(&[ | |
| 241 | ("usr_b", Verdict::Approve), | |
| 242 | ("usr_c", Verdict::RequestChanges), | |
| 243 | ]); | |
| 244 | assert_eq!( | |
| 245 | approvals_missing(&requiring(1), "usr_a", &mixed).as_deref(), | |
| 246 | Some("A reviewer has asked for changes.") | |
| 247 | ); | |
| 248 | } | |
| 249 | ||
| 250 | #[test] | |
| 251 | fn an_agents_approval_counts_only_where_the_repository_lets_it() { | |
| 252 | let agent = verdicts(&[(AGENT_ID, Verdict::Approve)]); | |
| 253 | assert_eq!(approvals_missing(&requiring(1), "usr_a", &agent), None); | |
| 254 | let people_only = RepoSettings { | |
| 255 | count_agent_approvals: false, | |
| 256 | ..requiring(1) | |
| 257 | }; | |
| 258 | assert!(approvals_missing(&people_only, "usr_a", &agent).is_some()); | |
| 259 | } | |
| 260 | } |