| 1 | //! Rows as they come out of D1, and their conversion to contract types. |
| 2 | |
| 3 | use g1t_contracts::User; |
| 4 | use g1t_contracts::repos::RepoPath; |
| 5 | use g1t_contracts::work::{ |
| 6 | AgentRef, AgentVerdict, CheckStatus, Comment, CommentKind, Issue, IssueReason, MilestoneRef, Pull, PullStatus, Runtime, SessionEntry, |
| 7 | SessionEntryKind, State, Verdict, |
| 8 | }; |
| 9 | use serde::Deserialize; |
| 10 | |
| 11 | /// An author or actor as stored: g1t itself when the id is its own (a |
| 12 | /// security update it opened, a merge its settings made), g1t's agent by |
| 13 | /// its id (what it made or filed), a person otherwise. |
| 14 | pub(crate) fn user(id: String, username: String) -> User { |
| 15 | let kind = if g1t_contracts::system::is_system_id(&id) { |
| 16 | g1t_contracts::PrincipalKind::System |
| 17 | } else if id == g1t_contracts::identity::AGENT_ID { |
| 18 | g1t_contracts::PrincipalKind::Agent |
| 19 | } else { |
| 20 | g1t_contracts::PrincipalKind::User |
| 21 | }; |
| 22 | User { |
| 23 | id, |
| 24 | username, |
| 25 | kind, |
| 26 | ..User::default() |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | /// Who asked g1t for the work, as stored beside its author: both columns, |
| 31 | /// or nobody. |
| 32 | pub(crate) fn requester(id: Option<String>, username: Option<String>) -> Option<User> { |
| 33 | Some(user(id?, username?)) |
| 34 | } |
| 35 | |
| 36 | #[derive(Deserialize)] |
| 37 | pub struct IssueRow { |
| 38 | pub id: String, |
| 39 | pub repo_id: String, |
| 40 | pub number: u32, |
| 41 | pub title: String, |
| 42 | pub body: String, |
| 43 | /// JSON array of label names. |
| 44 | pub labels: String, |
| 45 | pub state: State, |
| 46 | pub reason: Option<IssueReason>, |
| 47 | pub resolved_by: Option<u32>, |
| 48 | pub author_id: String, |
| 49 | pub author_name: String, |
| 50 | #[serde(default)] |
| 51 | pub requested_by_id: Option<String>, |
| 52 | #[serde(default)] |
| 53 | pub requested_by_name: Option<String>, |
| 54 | pub created_at: String, |
| 55 | pub updated_at: String, |
| 56 | pub closed_at: Option<String>, |
| 57 | pub pull_count: u32, |
| 58 | pub comment_count: u32, |
| 59 | /// JSON array of usernames. |
| 60 | pub assignees: String, |
| 61 | /// JSON array of issue numbers. |
| 62 | pub blocked_by: String, |
| 63 | pub agent: Option<String>, |
| 64 | /// The number of its milestone, and that milestone's title. |
| 65 | #[serde(default)] |
| 66 | pub milestone: Option<u32>, |
| 67 | #[serde(default)] |
| 68 | pub milestone_title: Option<String>, |
| 69 | } |
| 70 | |
| 71 | /// A milestone as an issue or pull request names it, from its number and |
| 72 | /// title as read beside it. |
| 73 | pub(crate) fn milestone_ref(number: Option<u32>, title: Option<String>) -> Option<MilestoneRef> { |
| 74 | Some(MilestoneRef { number: number?, title: title? }) |
| 75 | } |
| 76 | |
| 77 | impl From<IssueRow> for Issue { |
| 78 | fn from(row: IssueRow) -> Self { |
| 79 | Issue { |
| 80 | id: row.id, |
| 81 | repo_id: row.repo_id, |
| 82 | number: row.number, |
| 83 | title: row.title, |
| 84 | body: row.body, |
| 85 | labels: serde_json::from_str(&row.labels).unwrap_or_default(), |
| 86 | state: row.state, |
| 87 | reason: row.reason, |
| 88 | resolved_by: row.resolved_by, |
| 89 | author: user(row.author_id, row.author_name), |
| 90 | requested_by: requester(row.requested_by_id, row.requested_by_name), |
| 91 | created_at: row.created_at, |
| 92 | updated_at: row.updated_at, |
| 93 | closed_at: row.closed_at, |
| 94 | pull_count: row.pull_count, |
| 95 | comment_count: row.comment_count, |
| 96 | assignees: serde_json::from_str(&row.assignees).unwrap_or_default(), |
| 97 | blocked_by: serde_json::from_str(&row.blocked_by).unwrap_or_default(), |
| 98 | agent: row.agent, |
| 99 | milestone: milestone_ref(row.milestone, row.milestone_title), |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /// The remembered assessment of a pull request, read beside its row. |
| 105 | #[derive(Deserialize)] |
| 106 | pub struct Snapshot { |
| 107 | pub stage: Option<g1t_contracts::work::Stage>, |
| 108 | pub stage_detail: Option<String>, |
| 109 | #[serde(default)] |
| 110 | pub revisions: u32, |
| 111 | /// Whether g1t is seeing it through at all. |
| 112 | #[serde(default)] |
| 113 | pub managed: u32, |
| 114 | } |
| 115 | |
| 116 | /// A pull request's columns, with its milestone's title beside them: what |
| 117 | /// every read of a [`PullRow`] selects. |
| 118 | pub const PULL_COLUMNS: &str = "pulls.*, |
| 119 | (SELECT title FROM milestones |
| 120 | WHERE milestones.repo_id = pulls.repo_id AND milestones.number = pulls.milestone) AS milestone_title"; |
| 121 | |
| 122 | #[derive(Deserialize)] |
| 123 | pub struct PullRow { |
| 124 | pub id: String, |
| 125 | pub repo_id: String, |
| 126 | pub number: u32, |
| 127 | pub issue_number: Option<u32>, |
| 128 | pub title: String, |
| 129 | pub body: Option<String>, |
| 130 | pub agent: String, |
| 131 | pub runtime: Runtime, |
| 132 | pub status: PullStatus, |
| 133 | pub fork_repo_id: Option<String>, |
| 134 | pub fork_namespace: Option<String>, |
| 135 | pub fork_name: Option<String>, |
| 136 | pub source_branch: Option<String>, |
| 137 | /// The fork whose branch it is, for one opened from a fork |
| 138 | /// (migrations/0041_pull_head_repo.sql). |
| 139 | #[serde(default)] |
| 140 | pub head_repo_id: Option<String>, |
| 141 | #[serde(default)] |
| 142 | pub head_namespace: Option<String>, |
| 143 | #[serde(default)] |
| 144 | pub head_name: Option<String>, |
| 145 | pub head_commit: Option<String>, |
| 146 | pub merge_base: Option<String>, |
| 147 | pub merged_by: Option<String>, |
| 148 | pub merged_at: Option<String>, |
| 149 | pub superseded_by: Option<u32>, |
| 150 | pub check_status: Option<CheckStatus>, |
| 151 | /// JSON array of changed files; `None` until first worked out. |
| 152 | pub files: Option<String>, |
| 153 | /// JSON arrays of usernames. |
| 154 | pub assignees: String, |
| 155 | pub reviewers: String, |
| 156 | /// JSON array of `workspace/team`. |
| 157 | #[serde(default)] |
| 158 | pub team_reviewers: Option<String>, |
| 159 | pub author_id: String, |
| 160 | pub author_name: String, |
| 161 | #[serde(default)] |
| 162 | pub requested_by_id: Option<String>, |
| 163 | #[serde(default)] |
| 164 | pub requested_by_name: Option<String>, |
| 165 | pub created_at: String, |
| 166 | pub updated_at: String, |
| 167 | /// JSON array of label names. |
| 168 | #[serde(default)] |
| 169 | pub labels: Option<String>, |
| 170 | #[serde(default)] |
| 171 | pub milestone: Option<u32>, |
| 172 | #[serde(default)] |
| 173 | pub milestone_title: Option<String>, |
| 174 | /// The branch it merges into; NULL for the default branch. |
| 175 | #[serde(default)] |
| 176 | pub base_branch: Option<String>, |
| 177 | #[serde(default)] |
| 178 | pub head_pushed_by: Option<String>, |
| 179 | #[serde(default)] |
| 180 | pub head_pushed_at: Option<String>, |
| 181 | /// How it merged and what its merge left the base at; its auto-merge |
| 182 | /// (JSON), the issues it closes (JSON array) and whether its branch was |
| 183 | /// deleted (migrations/0042_merge_settings.sql). |
| 184 | #[serde(default)] |
| 185 | pub merge_method: Option<String>, |
| 186 | #[serde(default)] |
| 187 | pub merge_commit: Option<String>, |
| 188 | #[serde(default)] |
| 189 | pub auto_merge: Option<String>, |
| 190 | #[serde(default)] |
| 191 | pub closes: Option<String>, |
| 192 | #[serde(default)] |
| 193 | pub head_branch_deleted: Option<u8>, |
| 194 | } |
| 195 | |
| 196 | impl From<PullRow> for Pull { |
| 197 | fn from(row: PullRow) -> Self { |
| 198 | Pull { |
| 199 | id: row.id, |
| 200 | repo_id: row.repo_id, |
| 201 | number: row.number, |
| 202 | issue: row.issue_number, |
| 203 | title: row.title, |
| 204 | body: row.body, |
| 205 | agent: row.agent, |
| 206 | runtime: row.runtime, |
| 207 | status: row.status, |
| 208 | fork: row |
| 209 | .fork_namespace |
| 210 | .zip(row.fork_name) |
| 211 | .map(|(namespace, name)| RepoPath { namespace, name }), |
| 212 | fork_repo_id: row.fork_repo_id, |
| 213 | branch: row.source_branch, |
| 214 | head_repo: row |
| 215 | .head_namespace |
| 216 | .zip(row.head_name) |
| 217 | .map(|(namespace, name)| RepoPath { namespace, name }), |
| 218 | head_repo_id: row.head_repo_id, |
| 219 | head_commit: row.head_commit, |
| 220 | merge_base: row.merge_base, |
| 221 | merged_by: row.merged_by, |
| 222 | merged_at: row.merged_at, |
| 223 | superseded_by: row.superseded_by, |
| 224 | check_status: row.check_status, |
| 225 | files: row |
| 226 | .files |
| 227 | .as_deref() |
| 228 | .and_then(|files| serde_json::from_str(files).ok()) |
| 229 | .unwrap_or_default(), |
| 230 | assignees: serde_json::from_str(&row.assignees).unwrap_or_default(), |
| 231 | reviewers: serde_json::from_str(&row.reviewers).unwrap_or_default(), |
| 232 | team_reviewers: row |
| 233 | .team_reviewers |
| 234 | .as_deref() |
| 235 | .and_then(|teams| serde_json::from_str(teams).ok()) |
| 236 | .unwrap_or_default(), |
| 237 | author: user(row.author_id, row.author_name), |
| 238 | requested_by: requester(row.requested_by_id, row.requested_by_name), |
| 239 | created_at: row.created_at, |
| 240 | updated_at: row.updated_at, |
| 241 | labels: row |
| 242 | .labels |
| 243 | .as_deref() |
| 244 | .and_then(|labels| serde_json::from_str(labels).ok()) |
| 245 | .unwrap_or_default(), |
| 246 | milestone: milestone_ref(row.milestone, row.milestone_title), |
| 247 | base: row.base_branch, |
| 248 | head_pushed_by: row.head_pushed_by, |
| 249 | head_pushed_at: row.head_pushed_at, |
| 250 | merge_method: row |
| 251 | .merge_method |
| 252 | .and_then(|name| serde_json::from_value(serde_json::Value::String(name)).ok()), |
| 253 | merge_commit: row.merge_commit, |
| 254 | auto_merge: row.auto_merge.as_deref().and_then(|json| serde_json::from_str(json).ok()), |
| 255 | closes: row.closes.as_deref().and_then(|json| serde_json::from_str(json).ok()).unwrap_or_default(), |
| 256 | head_branch_deleted: row.head_branch_deleted.is_some_and(|deleted| deleted != 0), |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | #[derive(Deserialize)] |
| 262 | pub struct CommentRow { |
| 263 | pub id: String, |
| 264 | pub kind: CommentKind, |
| 265 | pub author_id: String, |
| 266 | pub author_name: String, |
| 267 | pub body: String, |
| 268 | pub path: Option<String>, |
| 269 | pub line: Option<u32>, |
| 270 | pub verdict: Option<Verdict>, |
| 271 | pub created_at: String, |
| 272 | #[serde(default)] |
| 273 | pub edited_at: Option<String>, |
| 274 | /// The issue or pull request it is on, where the query reads it. |
| 275 | #[serde(default)] |
| 276 | pub number: u32, |
| 277 | /// One of the workspace's agents that wrote it, as itself, and whom it |
| 278 | /// acted for (migrations/0033_agent_comments.sql). |
| 279 | #[serde(default)] |
| 280 | pub agent_id: Option<String>, |
| 281 | #[serde(default)] |
| 282 | pub agent_handle: Option<String>, |
| 283 | #[serde(default)] |
| 284 | pub agent_name: Option<String>, |
| 285 | #[serde(default)] |
| 286 | pub agent_avatar_seed: Option<String>, |
| 287 | #[serde(default)] |
| 288 | pub acting_for_id: Option<String>, |
| 289 | #[serde(default)] |
| 290 | pub acting_for_name: Option<String>, |
| 291 | /// An agent review's verdict, kept apart from `verdict`, which every |
| 292 | /// rule about approvals reads: so it is advisory. |
| 293 | #[serde(default)] |
| 294 | pub agent_verdict: Option<String>, |
| 295 | } |
| 296 | |
| 297 | impl CommentRow { |
| 298 | /// Who answers for it: whoever an agent acted for, or its author. |
| 299 | pub fn answerable_id(&self) -> &str { |
| 300 | self.acting_for_id.as_deref().unwrap_or(&self.author_id) |
| 301 | } |
| 302 | |
| 303 | /// Whether it carries a verdict, a person's or an agent's. |
| 304 | pub fn has_verdict(&self) -> bool { |
| 305 | self.verdict.is_some() || self.agent_verdict.is_some() |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | impl From<CommentRow> for Comment { |
| 310 | fn from(row: CommentRow) -> Self { |
| 311 | let agent = match (row.agent_id, row.agent_handle) { |
| 312 | (Some(id), Some(handle)) => Some(AgentRef { |
| 313 | display_name: row.agent_name.unwrap_or_else(|| handle.clone()), |
| 314 | avatar_seed: row.agent_avatar_seed.unwrap_or_else(|| handle.clone()), |
| 315 | id, |
| 316 | handle, |
| 317 | }), |
| 318 | _ => None, |
| 319 | }; |
| 320 | let advisory_verdict = row.agent_verdict.as_deref().and_then(AgentVerdict::parse); |
| 321 | let mut author = user(row.author_id, row.author_name); |
| 322 | if agent.is_some() { |
| 323 | author.kind = g1t_contracts::PrincipalKind::Agent; |
| 324 | } |
| 325 | Comment { |
| 326 | id: row.id, |
| 327 | kind: row.kind, |
| 328 | author, |
| 329 | body: row.body, |
| 330 | path: row.path, |
| 331 | line: row.line, |
| 332 | // A person's verdict, or for showing, an agent's advisory one. |
| 333 | verdict: row.verdict.or(advisory_verdict.and_then(AgentVerdict::verdict)), |
| 334 | created_at: row.created_at, |
| 335 | edited_at: row.edited_at, |
| 336 | acting_for: agent.as_ref().and(requester(row.acting_for_id, row.acting_for_name)), |
| 337 | advisory: agent.is_some() && advisory_verdict.is_some(), |
| 338 | agent, |
| 339 | reactions: Vec::new(), |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | #[derive(Deserialize)] |
| 345 | pub struct SessionRow { |
| 346 | pub seq: u32, |
| 347 | pub kind: SessionEntryKind, |
| 348 | pub text: String, |
| 349 | pub tool: Option<String>, |
| 350 | pub commit: Option<String>, |
| 351 | pub at: String, |
| 352 | } |
| 353 | |
| 354 | impl From<SessionRow> for SessionEntry { |
| 355 | fn from(row: SessionRow) -> Self { |
| 356 | SessionEntry { |
| 357 | seq: row.seq, |
| 358 | kind: row.kind, |
| 359 | text: row.text, |
| 360 | tool: row.tool, |
| 361 | commit: row.commit, |
| 362 | at: row.at, |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | /// A pull request whose head a push moved. |
| 368 | #[derive(Deserialize)] |
| 369 | pub struct MovedRow { |
| 370 | pub id: String, |
| 371 | pub repo_id: String, |
| 372 | pub number: u32, |
| 373 | pub issue_number: Option<u32>, |
| 374 | pub status: PullStatus, |
| 375 | pub author_id: String, |
| 376 | pub author_name: String, |
| 377 | #[serde(default)] |
| 378 | pub requested_by_id: Option<String>, |
| 379 | #[serde(default)] |
| 380 | pub requested_by_name: Option<String>, |
| 381 | } |
| 382 | |
| 383 | /// A single number selected as `n`. |
| 384 | #[derive(Deserialize)] |
| 385 | pub struct NumberRow { |
| 386 | pub n: u32, |
| 387 | } |
| 388 | |
| 389 | /// A single string selected as `value`. |
| 390 | #[derive(Deserialize)] |
| 391 | pub struct ValueRow { |
| 392 | pub value: String, |
| 393 | } |
| 394 | |
| 395 | /// Rows as the database holds them, for tests of the rules that read them. |
| 396 | #[cfg(test)] |
| 397 | pub(crate) mod stored { |
| 398 | use super::*; |
| 399 | |
| 400 | /// A pull request g1t made in a fork, read back as stored: g1t as its |
| 401 | /// author, and `requested_by` the person who asked, if anyone did. |
| 402 | pub(crate) fn pull(author: (&str, &str), requested_by: Option<(&str, &str)>) -> Pull { |
| 403 | let row: PullRow = serde_json::from_value(serde_json::json!({ |
| 404 | "id": "pr_1", "repo_id": "rep_1", "number": 14, "issue_number": 12, "title": "Fix it", |
| 405 | "body": null, "agent": "g1t", "runtime": "hosted", "status": "open", |
| 406 | "fork_repo_id": "rep_f", "fork_namespace": "pulls", "fork_name": "pr_1", |
| 407 | "source_branch": null, "head_commit": null, "merge_base": null, "merged_by": null, |
| 408 | "merged_at": null, "superseded_by": null, "check_status": null, "files": null, |
| 409 | "assignees": "[]", "reviewers": "[]", |
| 410 | "author_id": author.0, "author_name": author.1, |
| 411 | "requested_by_id": requested_by.map(|(id, _)| id), |
| 412 | "requested_by_name": requested_by.map(|(_, name)| name), |
| 413 | "created_at": "", "updated_at": "" |
| 414 | })) |
| 415 | .unwrap(); |
| 416 | row.into() |
| 417 | } |
| 418 | |
| 419 | /// g1t's agent, as an author is stored. |
| 420 | pub(crate) const G1T: (&str, &str) = (g1t_contracts::identity::AGENT_ID, "g1t"); |
| 421 | /// The person who asked g1t for the work. |
| 422 | pub(crate) const ASKER: (&str, &str) = ("usr_1", "syntaqx"); |
| 423 | } |
| 424 | |
| 425 | #[cfg(test)] |
| 426 | mod tests { |
| 427 | use super::stored::{ASKER, G1T, pull}; |
| 428 | use super::*; |
| 429 | use g1t_contracts::PrincipalKind; |
| 430 | |
| 431 | #[test] |
| 432 | fn g1t_s_pull_request_reads_back_as_g1t_s_requested_by_the_person() { |
| 433 | let made = pull(G1T, Some(ASKER)); |
| 434 | assert_eq!((made.author.username.as_str(), made.author.kind), ("g1t", PrincipalKind::Agent)); |
| 435 | let asked = made.requested_by.as_ref().expect("who asked is kept"); |
| 436 | assert_eq!((asked.id.as_str(), asked.username.as_str(), asked.kind), ("usr_1", "syntaqx", PrincipalKind::User)); |
| 437 | assert_eq!(made.owner().id, "usr_1"); |
| 438 | } |
| 439 | |
| 440 | #[test] |
| 441 | fn anyone_else_s_reads_back_as_theirs_alone() { |
| 442 | let own = pull(ASKER, None); |
| 443 | assert_eq!(own.author.kind, PrincipalKind::User); |
| 444 | assert!(own.requested_by.is_none()); |
| 445 | assert_eq!(own.owner().id, "usr_1"); |
| 446 | // Half a requester is nobody. |
| 447 | assert!(requester(Some("usr_1".into()), None).is_none()); |
| 448 | } |
| 449 | |
| 450 | #[test] |
| 451 | fn g1t_s_own_work_reads_back_as_g1t_itself() { |
| 452 | let own = pull(("g1t", "g1t"), None); |
| 453 | assert_eq!(own.author.kind, PrincipalKind::System); |
| 454 | assert!(own.requested_by.is_none()); |
| 455 | } |
| 456 | } |