flagon-io/g1t

public

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

g1t/services/work/src/lifecycle.rs

1,518 lines55,347 bytesCodeBlame
1//! Seeing a pull request through. Once a g1t agent has made a change, g1t
2//! takes each remaining step itself: the acceptance checks, a review by
3//! another agent, sending the author back to address what either found,
4//! and catching up when the branch it would land on has moved. It stops
5//! when the pull request is ready for a person to merge, or when it has
6//! tried and a person has to decide.
7//!
8//! This service decides what the next step is and claims it. The runner
9//! service asks, on every event that could change the answer, and carries
10//! the step out in a sandbox.
11
12use g1t_contracts::events::PullEvent;
13use g1t_contracts::repos::{GetByIdArgs, Repo, RepoPath};
14use g1t_contracts::time::rfc3339;
15use g1t_contracts::work::*;
16use g1t_contracts::{FailureCode, Membership, Outcome, Role, User, Viewer};
17use g1t_kit::now_ms;
18use std::collections::HashMap;
19
20use serde::Deserialize;
21use worker::Result;
22use worker::wasm_bindgen::JsValue;
23
24use crate::Work;
25use crate::reviews::{AGENT_ID, AGENT_NAME};
26use crate::statuses::{self, WorkflowFacts};
27use crate::rows::ValueRow;
28
29/// How long a claimed step is waited for before it may be taken again.
30const REVIEW_MINUTES: u64 = 20;
31const REVISION_MINUTES: u64 = 60;
32const CATCH_UP_MINUTES: u64 = 30;
33const MERGE_MINUTES: u64 = 2;
34/// Who a merge made by a repository's settings is attributed to. Not an
35/// account: `g1t` cannot be registered.
36pub(crate) const POLICY_ACTOR_ID: &str = "g1t_policy";
37pub(crate) const POLICY_ACTOR_NAME: &str = "g1t";
38/// How much of a failed check's output the author is shown.
39const MAX_CHECK_OUTPUT_CHARS: usize = 4_000;
40const MANAGED_PAGE: u32 = 200;
41
42/// The part of a pull request's row that tracks its lifecycle.
43#[derive(Deserialize)]
44struct Progress {
45 managed: u8,
46 revisions: u32,
47 revised_at: Option<String>,
48 working_on: Option<String>,
49 working_until: Option<String>,
50 stalled: Option<String>,
51}
52
53/// A merge asked for while the pull request was behind, as stored.
54#[derive(serde::Serialize, Deserialize)]
55#[serde(rename_all = "camelCase")]
56struct LandRequest {
57 actor: User,
58 keep_issue_open: bool,
59}
60
61#[derive(Deserialize)]
62struct LandRow {
63 land_requested: Option<String>,
64 land_requested_at: Option<String>,
65 stalled: Option<String>,
66}
67
68#[derive(Deserialize)]
69struct FinishedReview {
70 finished_at: String,
71 verdict: Option<Verdict>,
72}
73
74#[derive(Deserialize)]
75struct ReviewNote {
76 body: String,
77 path: Option<String>,
78 line: Option<u32>,
79}
80
81/// What the author is being sent back to address.
82pub(crate) enum Feedback {
83 FailedChecks,
84 /// Workflows that failed on its head.
85 FailedWorkflows,
86 /// The review that finished at this time.
87 Review(String),
88 /// What a person who asked for changes wrote since the last revision.
89 Person(PersonRequest),
90}
91
92/// A person's request for changes that still stands: their latest verdict,
93/// made after the agent last revised.
94#[derive(Clone, Debug, Deserialize)]
95pub(crate) struct PersonRequest {
96 author_id: String,
97 author_name: String,
98 created_at: String,
99}
100
101/// What should happen next, if it is g1t's turn.
102pub(crate) enum Next {
103 /// A step is under way, or it is a person's turn.
104 Wait,
105 Review,
106 Revise(Feedback),
107 CatchUp,
108 /// Land it, because the repository says ready pull requests land.
109 Merge,
110}
111
112/// Whether g1t made this pull request, and so sees it through.
113pub(crate) fn made_by_g1t(pull: &Pull) -> bool {
114 pull.runtime == Runtime::Hosted && pull.agent == AGENT_NAME && pull.fork.is_some()
115}
116
117fn at(stage: Stage, detail: impl Into<String>, revisions: u32) -> Lifecycle {
118 Lifecycle {
119 stage,
120 detail: detail.into(),
121 revisions,
122 }
123}
124
125fn times(count: u32) -> String {
126 match count {
127 1 => "once".to_owned(),
128 2 => "twice".to_owned(),
129 count => format!("{count} times"),
130 }
131}
132
133/// Everything the next step depends on.
134struct Facts {
135 /// Still being made: not yet marked ready for review.
136 draft: bool,
137 /// Why g1t stopped, if it has.
138 stalled: Option<String>,
139 /// The step under way, if one was claimed and is still being waited for.
140 working_on: Option<String>,
141 check_status: Option<CheckStatus>,
142 /// A review someone asked for is being written.
143 review_pending: bool,
144 /// Whether the issue has acceptance checks at all.
145 has_checks: bool,
146 revisions: u32,
147 /// The latest finished review of the change as it is now.
148 review: Option<FinishedReview>,
149 /// Whether the branch it would land on has moved without it.
150 behind: bool,
151 /// Whether the repository lands a ready pull request by itself.
152 auto_merge: bool,
153 /// Whether the repository refuses to merge one that is behind.
154 require_up_to_date: bool,
155 /// Whether a second agent reviews it without being asked.
156 agent_review: bool,
157 /// How many times the author may be sent back.
158 max_revisions: u32,
159 /// What the repository's approval rule still wants, if anything.
160 approvals_missing: Option<String>,
161 /// A person asked for changes since the agent last revised.
162 person_request: Option<PersonRequest>,
163 /// Its place in the merge queue, and what is ahead of it there.
164 queued: Option<(QueueState, Vec<u32>)>,
165 /// What the workflows run on its head say.
166 workflows: WorkflowFacts,
167}
168
169/// Where a pull request stands, and the step to take if it is g1t's turn.
170///
171/// The order is: nothing while a step is under way; a person asking for
172/// changes is answered first; the checks must pass; then a review must
173/// approve; then it must be up to date. A person's or an agent's request
174/// for changes, or a failed check, sends the author back, a limited number
175/// of times, after which a person is asked.
176fn decide(facts: Facts) -> (Lifecycle, Next) {
177 let revisions = facts.revisions;
178 let wait = |stage, detail: &str| (at(stage, detail, revisions), Next::Wait);
179 let exhausted = revisions >= facts.max_revisions;
180
181 if facts.draft {
182 return wait(Stage::Working, "A g1t agent is making the change.");
183 }
184 if let Some(reason) = &facts.stalled {
185 return wait(Stage::NeedsYou, reason);
186 }
187 if let Some((state, ahead)) = &facts.queued {
188 let named = ahead.iter().map(|n| format!("#{n}")).collect::<Vec<_>>().join(", ");
189 let detail = match (state, ahead.is_empty()) {
190 (QueueState::Testing, true) => "In the merge queue: being tested on the default branch as it is.".to_owned(),
191 (QueueState::Testing, false) => {
192 format!("In the merge queue: being tested together with {named}, ahead of it.")
193 }
194 (QueueState::Passed, true) => "Passed in the merge queue. Landing.".to_owned(),
195 (QueueState::Passed, false) => {
196 format!("Passed in the merge queue together with {named}. It lands once they have.")
197 }
198 _ => "In the merge queue, waiting for its turn to be tested.".to_owned(),
199 };
200 return wait(Stage::Queued, &detail);
201 }
202 match facts.working_on.as_deref() {
203 Some("revision") => {
204 return wait(
205 Stage::Revising,
206 "The agent is addressing what the checks or the review found.",
207 );
208 }
209 Some("catch_up") => {
210 return wait(
211 Stage::CatchingUp,
212 "The agent is merging in the branch this will land on, which has moved.",
213 );
214 }
215 Some("answer") => {
216 return wait(
217 Stage::Answering,
218 "The agent is answering what another agent asked it.",
219 );
220 }
221 Some("merge") => return wait(Stage::Ready, "Merging."),
222 Some(_) => return wait(Stage::Reviewing, "A g1t agent is reviewing the change."),
223 None => {}
224 }
225 if matches!(
226 facts.check_status,
227 Some(CheckStatus::Queued | CheckStatus::Running)
228 ) {
229 return wait(Stage::Checking, "The acceptance checks are running.");
230 }
231 if facts.review_pending {
232 return wait(Stage::Reviewing, "A g1t agent is reviewing the change.");
233 }
234 // A person asked for changes: the agent makes them, as it would for a
235 // review it asked for, before anything else.
236 if let Some(request) = &facts.person_request {
237 if exhausted {
238 return wait(
239 Stage::NeedsYou,
240 &format!(
241 "{} asked for changes, and the agent has already revised {}.",
242 request.author_name,
243 times(revisions)
244 ),
245 );
246 }
247 return (
248 at(
249 Stage::Revising,
250 format!(
251 "{} asked for changes. The agent is being sent back to make them.",
252 request.author_name
253 ),
254 revisions,
255 ),
256 Next::Revise(Feedback::Person(request.clone())),
257 );
258 }
259
260 // An issue's checks, and any failure recorded as one, such as the merge
261 // queue taking the pull request out.
262 if facts.has_checks || matches!(facts.check_status, Some(CheckStatus::Failed | CheckStatus::Errored)) {
263 match facts.check_status {
264 Some(CheckStatus::Passed) => {}
265 Some(CheckStatus::Failed) if exhausted => {
266 return wait(
267 Stage::NeedsYou,
268 &format!(
269 "The acceptance checks still fail after the agent revised {}.",
270 times(revisions)
271 ),
272 );
273 }
274 Some(CheckStatus::Failed) => {
275 return (
276 at(
277 Stage::Revising,
278 "The acceptance checks failed. The agent is being sent back to fix them.",
279 revisions,
280 ),
281 Next::Revise(Feedback::FailedChecks),
282 );
283 }
284 Some(CheckStatus::Errored) => {
285 return wait(Stage::NeedsYou, "The acceptance checks could not be run.");
286 }
287 _ => {
288 return wait(
289 Stage::Checking,
290 "Waiting for the acceptance checks to start.",
291 );
292 }
293 }
294 }
295
296 // Its workflows, like its checks, must pass.
297 if !facts.workflows.failed.is_empty() {
298 let failed = statuses::list(&facts.workflows.failed);
299 if exhausted {
300 return wait(
301 Stage::NeedsYou,
302 &format!("{failed} still fails after the agent revised {}.", times(revisions)),
303 );
304 }
305 return (
306 at(
307 Stage::Revising,
308 format!("{failed} failed. The agent is being sent back to fix it."),
309 revisions,
310 ),
311 Next::Revise(Feedback::FailedWorkflows),
312 );
313 }
314 if !facts.workflows.pending.is_empty() {
315 return wait(
316 Stage::Checking,
317 &format!("Waiting for {} to finish.", statuses::list(&facts.workflows.pending)),
318 );
319 }
320
321 // A second agent reviews it, unless the repository leaves review to people.
322 if facts.agent_review {
323 match facts.review {
324 None => {
325 return (
326 at(
327 Stage::Reviewing,
328 "A g1t agent is about to review the change.",
329 revisions,
330 ),
331 Next::Review,
332 );
333 }
334 Some(FinishedReview { verdict: None, .. }) => {
335 return wait(Stage::NeedsYou, "The review could not be completed.");
336 }
337 Some(FinishedReview {
338 verdict: Some(Verdict::RequestChanges),
339 ..
340 }) if exhausted => {
341 return wait(
342 Stage::NeedsYou,
343 &format!(
344 "The review still asks for changes after the agent revised {}.",
345 times(revisions)
346 ),
347 );
348 }
349 Some(FinishedReview {
350 verdict: Some(Verdict::RequestChanges),
351 finished_at,
352 }) => {
353 return (
354 at(
355 Stage::Revising,
356 "The review asked for changes. The agent is being sent back to make them.",
357 revisions,
358 ),
359 Next::Revise(Feedback::Review(finished_at)),
360 );
361 }
362 Some(FinishedReview {
363 verdict: Some(Verdict::Approve),
364 ..
365 }) => {}
366 }
367 }
368
369 // Only where the repository insists is catching up a step of its own,
370 // followed by the checks again. Elsewhere it happens as part of merging.
371 if facts.behind && facts.require_up_to_date {
372 return (
373 at(
374 Stage::CatchingUp,
375 "The branch it will land on has moved. The agent is catching up.",
376 revisions,
377 ),
378 Next::CatchUp,
379 );
380 }
381 // The repository wants approvals this does not have yet: people's turn,
382 // so it is shown as needing someone, not as g1t still working.
383 if let Some(missing) = &facts.approvals_missing {
384 return wait(Stage::NeedsYou, missing);
385 }
386 if facts.auto_merge {
387 return (
388 at(
389 Stage::Ready,
390 "Everything this repository asks for is met. Merging, as its settings say.",
391 revisions,
392 ),
393 Next::Merge,
394 );
395 }
396 wait(
397 Stage::Ready,
398 if facts.behind {
399 "Ready to merge. Merging brings it up to date with the default branch first."
400 } else {
401 "Everything this repository asks for is met. Ready to merge."
402 },
403 )
404}
405
406impl Work {
407 /// Where a pull request stands and what g1t does next, remembered so
408 /// lists can show it without working it out again. `None` for one g1t
409 /// is not seeing through.
410 pub(crate) async fn assess(
411 &self,
412 pull: &Pull,
413 issue: &Option<Issue>,
414 behind: bool,
415 ) -> Result<Option<(Lifecycle, Next)>> {
416 let assessed = self.assess_now(pull, issue, behind).await?;
417 if let Some((lifecycle, _)) = &assessed {
418 self.remember(&pull.id, lifecycle).await?;
419 }
420 Ok(assessed)
421 }
422
423 /// Saves where a pull request stands, for [`Self::remembered`].
424 pub(crate) async fn remember(&self, pull_id: &str, lifecycle: &Lifecycle) -> Result<()> {
425 let stage = serde_json::to_value(lifecycle.stage)?;
426 self.db
427 .prepare("UPDATE pulls SET stage = ?, stage_detail = ? WHERE id = ?")
428 .bind(&[
429 stage.as_str().unwrap_or_default().into(),
430 lifecycle.detail.as_str().into(),
431 pull_id.into(),
432 ])?
433 .run()
434 .await?;
435 Ok(())
436 }
437
438 async fn assess_now(
439 &self,
440 pull: &Pull,
441 issue: &Option<Issue>,
442 behind: bool,
443 ) -> Result<Option<(Lifecycle, Next)>> {
444 if !pull.status.is_active() {
445 return Ok(None);
446 }
447 let Some(progress) = self
448 .db
449 .prepare(
450 "SELECT managed, revisions, revised_at, working_on, working_until, stalled
451 FROM pulls WHERE id = ?",
452 )
453 .bind(&[pull.id.as_str().into()])?
454 .first::<Progress>(None)
455 .await?
456 .filter(|progress| progress.managed != 0)
457 else {
458 return Ok(None);
459 };
460 let now = rfc3339(now_ms());
461 let working_on = progress
462 .working_until
463 .as_deref()
464 .is_some_and(|until| until > now.as_str())
465 .then(|| progress.working_on.clone().unwrap_or_default());
466 let review = self
467 .db
468 .prepare(
469 "SELECT finished_at, verdict FROM review_runs
470 WHERE pull_id = ? AND finished_at IS NOT NULL ORDER BY id DESC LIMIT 1",
471 )
472 .bind(&[pull.id.as_str().into()])?
473 .first::<FinishedReview>(None)
474 .await?
475 // A review of what the change was before its last revision says
476 // nothing about what it is now.
477 .filter(|review| {
478 progress
479 .revised_at
480 .as_deref()
481 .is_none_or(|revised| review.finished_at.as_str() >= revised)
482 });
483 let settings = self.settings(&pull.repo_id).await?;
484 Ok(Some(decide(Facts {
485 draft: pull.status == PullStatus::Draft,
486 stalled: progress.stalled,
487 working_on,
488 check_status: pull.check_status,
489 review_pending: self.review_pending(&pull.id).await?,
490 has_checks: issue.as_ref().is_some_and(|issue| !issue.checks.is_empty()),
491 revisions: progress.revisions,
492 review,
493 behind,
494 auto_merge: settings.auto_merge,
495 require_up_to_date: settings.require_up_to_date,
496 agent_review: settings.agent_review,
497 max_revisions: settings.max_revisions,
498 approvals_missing: self.approvals_gap(&settings, pull).await?,
499 person_request: self
500 .person_request(pull, progress.revised_at.as_deref())
501 .await?,
502 queued: self.queued_entry(&pull.id).await?,
503 workflows: WorkflowFacts::of(&self.statuses(&pull.repo_id, pull.head_commit.as_deref()).await?),
504 })))
505 }
506
507 /// The latest request for changes by a person other than the author,
508 /// if it is that person's latest verdict and came after the last
509 /// revision.
510 async fn person_request(
511 &self,
512 pull: &Pull,
513 revised_at: Option<&str>,
514 ) -> Result<Option<PersonRequest>> {
515 #[derive(Deserialize)]
516 struct Verdicts {
517 author_id: String,
518 author_name: String,
519 verdict: Verdict,
520 created_at: String,
521 }
522 let rows = self
523 .db
524 .prepare(
525 "SELECT author_id, author_name, verdict, created_at FROM comments
526 WHERE repo_id = ? AND number = ? AND verdict IS NOT NULL
527 AND author_id != ? AND author_id != ?
528 ORDER BY id",
529 )
530 .bind(&[
531 pull.repo_id.as_str().into(),
532 pull.number.into(),
533 pull.author.id.as_str().into(),
534 AGENT_ID.into(),
535 ])?
536 .all()
537 .await?
538 .results::<Verdicts>()?;
539 // Each person's latest verdict is the one that stands.
540 let mut latest: HashMap<String, Verdicts> = HashMap::new();
541 for row in rows {
542 latest.insert(row.author_id.clone(), row);
543 }
544 Ok(latest
545 .into_values()
546 .filter(|row| row.verdict == Verdict::RequestChanges)
547 .filter(|row| revised_at.is_none_or(|revised| row.created_at.as_str() > revised))
548 .max_by(|a, b| a.created_at.cmp(&b.created_at))
549 .map(|row| PersonRequest {
550 author_id: row.author_id,
551 author_name: row.author_name,
552 created_at: row.created_at,
553 }))
554 }
555
556 /// Marks a pull request a g1t agent has just opened as one g1t sees
557 /// through.
558 pub(crate) async fn manage(&self, pull: &Pull) -> Result<()> {
559 if !made_by_g1t(pull) {
560 return Ok(());
561 }
562 self.db
563 .prepare("UPDATE pulls SET managed = 1 WHERE id = ?")
564 .bind(&[pull.id.as_str().into()])?
565 .run()
566 .await?;
567 Ok(())
568 }
569
570 /// Takes a step for a pull request, if nobody else has. One statement,
571 /// so that two callers cannot both take it.
572 pub(crate) async fn claim(&self, pull_id: &str, step: &str, minutes: u64, revising: bool) -> Result<bool> {
573 let now = now_ms();
574 let revision = if revising {
575 ", revisions = revisions + 1, revised_at = ?1"
576 } else {
577 ""
578 };
579 Ok(self
580 .db
581 .prepare(format!(
582 "UPDATE pulls SET working_on = ?2, working_until = ?3{revision}
583 WHERE id = ?4 AND status = 'open' AND stalled IS NULL
584 AND (working_until IS NULL OR working_until < ?1)
585 RETURNING id AS value"
586 ))
587 .bind(&[
588 rfc3339(now).into(),
589 step.into(),
590 rfc3339(now + minutes * 60 * 1000).into(),
591 pull_id.into(),
592 ])?
593 .first::<ValueRow>(None)
594 .await?
595 .is_some())
596 }
597
598 /// What the author is told when sent back: the checks that failed and
599 /// what they printed, or the review and its comments on lines.
600 async fn feedback(&self, pull: &Pull, feedback: &Feedback) -> Result<String> {
601 match feedback {
602 Feedback::FailedChecks => {
603 let run = self.latest_checks(&pull.id).await?;
604 // Why it failed, when that is more than a list of commands:
605 // the merge queue saying what broke in the combined state.
606 let why = run.as_ref().and_then(|run| run.error.clone()).map(|error| format!("{error}\n\n")).unwrap_or_default();
607 let failed: Vec<String> = run
608 .map(|run| run.results)
609 .unwrap_or_default()
610 .into_iter()
611 .filter(|result| !result.passed)
612 .map(|result| {
613 let length = result.output.chars().count();
614 let output: String = result
615 .output
616 .chars()
617 .skip(length.saturating_sub(MAX_CHECK_OUTPUT_CHARS))
618 .collect();
619 let exit = result
620 .exit_code
621 .map_or("it was stopped for taking too long".to_owned(), |code| {
622 format!("exit code {code}")
623 });
624 format!("`{}` failed ({exit}):\n\n{}", result.command, output.trim())
625 })
626 .collect();
627 if failed.is_empty() {
628 return Ok(format!(
629 "{why}Find the cause, fix it in your change, and push. Use get_workflow_run and get_job_logs for any workflow named above."
630 ));
631 }
632 Ok(format!(
633 "{why}These acceptance checks were run against your change in a clean sandbox and failed.\n\n{}",
634 failed.join("\n\n")
635 ))
636 }
637 Feedback::FailedWorkflows => {
638 let statuses = self.statuses(&pull.repo_id, pull.head_commit.as_deref()).await?;
639 let failed: Vec<String> = statuses
640 .iter()
641 .filter(|s| s.state == "failure" || s.state == "error")
642 .map(|s| {
643 let run = s.target_url.as_deref().and_then(|url| url.rsplit('/').next()).unwrap_or_default();
644 format!(
645 "- {} ({}): run `{run}`, {}",
646 s.context,
647 s.description.as_deref().unwrap_or("failed"),
648 s.target_url.as_deref().unwrap_or_default()
649 )
650 })
651 .collect();
652 // Named outright: the agent cannot guess it from its fork.
653 let repo = g1t_kit::call::<_, Option<RepoPath>>(
654 &self.repos,
655 "path_by_id",
656 &g1t_contracts::repos::PathByIdArgs { id: pull.repo_id.clone() },
657 )
658 .await?
659 .map(|path| format!("{}/{}", path.namespace, path.name))
660 .unwrap_or_default();
661 Ok(format!(
662 "These GitHub Actions workflows failed on your latest commit to {repo}:\n\n{}\n\n\
663 Read why with the `get_workflow_run` tool (repo `{repo}` and the run's id), \
664 then `get_job_logs` for the job that failed. Fix the cause in the code, not the workflow, \
665 unless the workflow itself is wrong.",
666 failed.join("\n")
667 ))
668 }
669 Feedback::Review(finished_at) => {
670 // Everything a review says is recorded at the moment it finished.
671 let notes = self
672 .db
673 .prepare(
674 "SELECT body, path, line FROM comments
675 WHERE repo_id = ? AND number = ? AND author_id = ? AND created_at = ?
676 ORDER BY id",
677 )
678 .bind(&[
679 pull.repo_id.as_str().into(),
680 pull.number.into(),
681 AGENT_ID.into(),
682 finished_at.as_str().into(),
683 ])?
684 .all()
685 .await?
686 .results::<ReviewNote>()?;
687 let mut on_lines = Vec::new();
688 let mut summary = String::new();
689 for note in notes {
690 match (note.path, note.line) {
691 (Some(path), Some(line)) => {
692 on_lines.push(format!("- `{path}` line {line}: {}", note.body));
693 }
694 (Some(path), None) => on_lines.push(format!("- `{path}`: {}", note.body)),
695 (None, _) => summary = note.body,
696 }
697 }
698 let mut text = format!(
699 "Another agent reviewed your change and asked for changes.\n\n{summary}"
700 );
701 if !on_lines.is_empty() {
702 text.push_str("\n\nIts comments on lines:\n");
703 text.push_str(&on_lines.join("\n"));
704 }
705 Ok(text)
706 }
707 Feedback::Person(request) => {
708 // What they wrote since the agent last revised, which their
709 // request for changes closes.
710 let revised: Option<String> = self
711 .db
712 .prepare("SELECT revised_at AS value FROM pulls WHERE id = ?")
713 .bind(&[pull.id.as_str().into()])?
714 .first::<Option<String>>(Some("value"))
715 .await?
716 .flatten();
717 let notes = self
718 .db
719 .prepare(
720 "SELECT body, path, line FROM comments
721 WHERE repo_id = ? AND number = ? AND author_id = ?
722 AND created_at > ? AND created_at <= ?
723 ORDER BY id",
724 )
725 .bind(&[
726 pull.repo_id.as_str().into(),
727 pull.number.into(),
728 request.author_id.as_str().into(),
729 revised.unwrap_or_default().into(),
730 request.created_at.as_str().into(),
731 ])?
732 .all()
733 .await?
734 .results::<ReviewNote>()?;
735 let mut on_lines = Vec::new();
736 let mut said = Vec::new();
737 for note in notes {
738 match (note.path, note.line) {
739 (Some(path), Some(line)) => {
740 on_lines.push(format!("- `{path}` line {line}: {}", note.body));
741 }
742 (Some(path), None) => on_lines.push(format!("- `{path}`: {}", note.body)),
743 (None, _) => said.push(note.body),
744 }
745 }
746 let mut text = format!(
747 "{} reviewed your change and asked for changes.\n\n{}",
748 request.author_name,
749 said.join("\n\n")
750 );
751 if !on_lines.is_empty() {
752 text.push_str("\n\nTheir comments on lines:\n");
753 text.push_str(&on_lines.join("\n"));
754 }
755 Ok(text)
756 }
757 }
758 }
759
760 pub(crate) async fn advance(&self, a: AdvanceArgs) -> Result<Advance> {
761 let Some(pull) = self.pull_by_id(&a.pull_id).await? else {
762 return Ok(Advance::None);
763 };
764 if pull.status != PullStatus::Open {
765 return Ok(Advance::None);
766 }
767 // As a member: a private repository would look missing otherwise,
768 // and the pull request would never move.
769 let viewer: Viewer = self.author_viewer(&pull).await?;
770 let repo: Outcome<Repo> = g1t_kit::call(
771 &self.repos,
772 "get_by_id",
773 &GetByIdArgs {
774 id: pull.repo_id.clone(),
775 viewer,
776 },
777 )
778 .await?;
779 let (Outcome::Ok(repo), Some(source)) = (repo, pull.fork.clone()) else {
780 return Ok(Advance::None);
781 };
782 let issue = match pull.issue {
783 Some(number) => self.issue(&pull.repo_id, number).await?,
784 None => None,
785 };
786 let behind = self.is_behind(&repo.id, &pull).await?;
787 let Some((lifecycle, next)) = self.assess(&pull, &issue, behind).await? else {
788 return Ok(Advance::None);
789 };
790
791 if matches!(next, Next::Merge) {
792 self.merge_by_policy(&repo, &pull).await?;
793 return Ok(Advance::None);
794 }
795 let (step, minutes) = match &next {
796 Next::Wait | Next::Merge => return Ok(Advance::None),
797 Next::Review => ("review", REVIEW_MINUTES),
798 Next::Revise(_) => ("revision", REVISION_MINUTES),
799 Next::CatchUp => ("catch_up", CATCH_UP_MINUTES),
800 };
801 let feedback = match &next {
802 Next::Revise(feedback) => self.feedback(&pull, feedback).await?,
803 _ => String::new(),
804 };
805 if !self
806 .claim(&pull.id, step, minutes, matches!(next, Next::Revise(_)))
807 .await?
808 {
809 return Ok(Advance::None);
810 }
811 // Said in the conversation, so nobody has to wonder why a review or
812 // a new commit appeared.
813 let told = match &next {
814 Next::Review => {
815 self.db
816 .prepare(
817 "UPDATE pulls SET reviewers = json_insert(reviewers, '$[#]', ?1)
818 WHERE id = ?2 AND NOT EXISTS (
819 SELECT 1 FROM json_each(pulls.reviewers) WHERE json_each.value = ?1)",
820 )
821 .bind(&[AGENT_NAME.into(), pull.id.as_str().into()])?
822 .run()
823 .await?;
824 "requested a review from g1t-agent".to_owned()
825 }
826 Next::Revise(Feedback::FailedChecks) => {
827 "sent g1t-agent back to fix the failed checks".to_owned()
828 }
829 Next::Revise(Feedback::FailedWorkflows) => {
830 "sent g1t-agent back to fix the failed workflows".to_owned()
831 }
832 Next::Revise(Feedback::Review(_)) => {
833 "sent g1t-agent back to address the review".to_owned()
834 }
835 _ => format!(
836 "asked g1t-agent to bring this up to date with {}",
837 repo.default_branch
838 ),
839 };
840 self.note(
841 &pull.repo_id,
842 pull.number,
843 (POLICY_ACTOR_ID, POLICY_ACTOR_NAME),
844 &told,
845 )
846 .await?;
847 let job = LifecycleJob {
848 pull_id: pull.id,
849 repo: RepoPath {
850 namespace: repo.namespace,
851 name: repo.name,
852 },
853 number: pull.number,
854 author: pull.author,
855 source,
856 branch: None,
857 default_branch: repo.default_branch,
858 title: pull.title,
859 description: pull.body.unwrap_or_default(),
860 issue,
861 feedback,
862 round: lifecycle.revisions + 1,
863 };
864 Ok(match next {
865 Next::Review => Advance::Review { job },
866 Next::Revise(_) => Advance::Revise { job },
867 Next::CatchUp => Advance::CatchUp { job },
868 Next::Wait | Next::Merge => Advance::None,
869 })
870 }
871
872 /// Lands a pull request that is ready, on the authority of the
873 /// repository's settings instead of a person's click.
874 async fn merge_by_policy(&self, repo: &Repo, pull: &Pull) -> Result<()> {
875 if !self.claim(&pull.id, "merge", MERGE_MINUTES, false).await? {
876 return Ok(());
877 }
878 // g1t acts for the workspace whose members turned this on.
879 let actor = User {
880 id: POLICY_ACTOR_ID.to_owned(),
881 username: POLICY_ACTOR_NAME.to_owned(),
882 verified: true,
883 workspaces: vec![Membership {
884 slug: repo.namespace.clone(),
885 role: Role::Member,
886 }],
887 ..User::default()
888 };
889 let merged = self
890 .merge_pull(PullActionArgs {
891 actor,
892 repo: RepoPath {
893 namespace: repo.namespace.clone(),
894 name: repo.name.clone(),
895 },
896 number: pull.number,
897 summary: String::new(),
898 keep_issue_open: false,
899 ignore_checks: false,
900 })
901 .await?;
902 match merged {
903 Outcome::Ok(_) => Ok(()),
904 // Most likely the branch moved in the moment between: let go, and
905 // the next look at it will catch up and try again.
906 Outcome::Fail(failure) if failure.code == FailureCode::Conflict => {
907 self.db
908 .prepare(
909 "UPDATE pulls SET working_on = NULL, working_until = NULL
910 WHERE id = ? AND working_on = 'merge'",
911 )
912 .bind(&[pull.id.as_str().into()])?
913 .run()
914 .await?;
915 Ok(())
916 }
917 Outcome::Fail(failure) => {
918 self.stall(StallArgs {
919 pull_id: pull.id.clone(),
920 reason: format!("g1t could not merge this: {}", failure.message),
921 })
922 .await?;
923 Ok(())
924 }
925 }
926 }
927
928 /// Records that a merge was asked for while the pull request was
929 /// behind, and announces it so that the runner brings it up to date.
930 pub(crate) async fn request_landing(
931 &self,
932 pull: &Pull,
933 actor: &User,
934 keep_issue_open: bool,
935 ) -> Result<()> {
936 let now = now_ms();
937 let request = serde_json::to_string(&LandRequest {
938 actor: actor.clone(),
939 keep_issue_open,
940 })?;
941 self.db
942 .prepare(
943 "UPDATE pulls
944 SET land_requested = ?, land_requested_at = ?, stalled = NULL,
945 working_on = 'catch_up', working_until = ?
946 WHERE id = ?",
947 )
948 .bind(&[
949 request.into(),
950 rfc3339(now).into(),
951 rfc3339(now + CATCH_UP_MINUTES * 60 * 1000).into(),
952 pull.id.as_str().into(),
953 ])?
954 .run()
955 .await?;
956 self.publish(
957 "pull.merge_requested",
958 &pull.repo_id,
959 actor,
960 PullEvent {
961 pull_id: pull.id.clone(),
962 repo_id: pull.repo_id.clone(),
963 number: pull.number,
964 issue: pull.issue,
965 ..PullEvent::default()
966 },
967 )
968 .await
969 }
970
971 /// The merge waiting on a pull request, if one was asked for recently
972 /// enough to still stand.
973 async fn land_request(&self, pull_id: &str) -> Result<Option<LandRequest>> {
974 let row = self
975 .db
976 .prepare("SELECT land_requested, land_requested_at, stalled FROM pulls WHERE id = ?")
977 .bind(&[pull_id.into()])?
978 .first::<LandRow>(None)
979 .await?;
980 let oldest = rfc3339(now_ms().saturating_sub(CATCH_UP_MINUTES * 60 * 1000));
981 Ok(row
982 .filter(|row| {
983 row.land_requested_at
984 .as_deref()
985 .is_some_and(|at| at >= oldest.as_str())
986 })
987 .and_then(|row| row.land_requested)
988 .and_then(|request| serde_json::from_str(&request).ok()))
989 }
990
991 /// Whether a merge is waiting on a pull request, and why g1t stopped
992 /// working on it if it did.
993 pub(crate) async fn landing_state(&self, pull_id: &str) -> Result<(bool, Option<String>)> {
994 let stalled = self
995 .db
996 .prepare("SELECT land_requested, land_requested_at, stalled FROM pulls WHERE id = ?")
997 .bind(&[pull_id.into()])?
998 .first::<LandRow>(None)
999 .await?
1000 .and_then(|row| row.stalled);
1001 Ok((self.land_request(pull_id).await?.is_some(), stalled))
1002 }
1003
1004 async fn forget_landing(&self, pull_id: &str) -> Result<()> {
1005 self.db
1006 .prepare(
1007 "UPDATE pulls SET land_requested = NULL, land_requested_at = NULL WHERE id = ?",
1008 )
1009 .bind(&[pull_id.into()])?
1010 .run()
1011 .await?;
1012 Ok(())
1013 }
1014
1015 /// Lands a pull request whose head has just moved, if a merge of it was
1016 /// waiting for exactly that. What it was caught up to was already
1017 /// checked and reviewed apart from the merge, so the checks are not
1018 /// waited for again; a repository that wants them rerun turns on
1019 /// "require up to date", and then nothing is landed this way.
1020 pub(crate) async fn land_if_requested(&self, pull_id: &str) -> Result<()> {
1021 let Some(request) = self.land_request(pull_id).await? else {
1022 return Ok(());
1023 };
1024 self.forget_landing(pull_id).await?;
1025 let Some(pull) = self.pull_by_id(pull_id).await? else {
1026 return Ok(());
1027 };
1028 let repo: Outcome<Repo> = g1t_kit::call(
1029 &self.repos,
1030 "get_by_id",
1031 &GetByIdArgs {
1032 id: pull.repo_id.clone(),
1033 viewer: Some(request.actor.clone()),
1034 },
1035 )
1036 .await?;
1037 let Outcome::Ok(repo) = repo else {
1038 return Ok(());
1039 };
1040 let merged = self
1041 .merge_pull(PullActionArgs {
1042 actor: request.actor,
1043 repo: RepoPath {
1044 namespace: repo.namespace,
1045 name: repo.name,
1046 },
1047 number: pull.number,
1048 summary: String::new(),
1049 keep_issue_open: request.keep_issue_open,
1050 ignore_checks: true,
1051 })
1052 .await?;
1053 if let Outcome::Fail(failure) = merged {
1054 self.stall(StallArgs {
1055 pull_id: pull.id,
1056 reason: format!(
1057 "It was brought up to date but could not be merged: {}",
1058 failure.message
1059 ),
1060 })
1061 .await?;
1062 }
1063 Ok(())
1064 }
1065
1066 /// What the runner needs to bring a pull request up to date for a merge
1067 /// that is waiting on it.
1068 pub(crate) async fn catch_up_job(&self, a: CatchUpJobArgs) -> Result<Option<LifecycleJob>> {
1069 if self.land_request(&a.pull_id).await?.is_none() {
1070 return Ok(None);
1071 }
1072 let Some(pull) = self.pull_by_id(&a.pull_id).await? else {
1073 return Ok(None);
1074 };
1075 // Its author can read both the repository and the pull request's source.
1076 let repo: Outcome<Repo> = g1t_kit::call(
1077 &self.repos,
1078 "get_by_id",
1079 &GetByIdArgs {
1080 id: pull.repo_id.clone(),
1081 viewer: self.author_viewer(&pull).await?,
1082 },
1083 )
1084 .await?;
1085 let Outcome::Ok(repo) = repo else {
1086 return Ok(None);
1087 };
1088 let path = RepoPath {
1089 namespace: repo.namespace,
1090 name: repo.name,
1091 };
1092 let issue = match pull.issue {
1093 Some(number) => self.issue(&pull.repo_id, number).await?,
1094 None => None,
1095 };
1096 Ok(Some(LifecycleJob {
1097 pull_id: pull.id,
1098 source: pull.fork.unwrap_or_else(|| path.clone()),
1099 repo: path,
1100 number: pull.number,
1101 author: pull.author,
1102 branch: pull.branch,
1103 default_branch: repo.default_branch,
1104 title: pull.title,
1105 description: pull.body.unwrap_or_default(),
1106 issue,
1107 feedback: String::new(),
1108 round: 0,
1109 }))
1110 }
1111
1112 pub(crate) async fn stall(&self, a: StallArgs) -> Result<bool> {
1113 self.db
1114 .prepare(
1115 "UPDATE pulls
1116 SET stalled = ?, working_on = NULL, working_until = NULL,
1117 land_requested = NULL, land_requested_at = NULL
1118 WHERE id = ? AND status = 'open'",
1119 )
1120 .bind(&[a.reason.trim().into(), a.pull_id.as_str().into()])?
1121 .run()
1122 .await?;
1123 self.db
1124 .prepare("UPDATE pulls SET stage = 'needs_you', stage_detail = ? WHERE id = ?")
1125 .bind(&[a.reason.trim().into(), a.pull_id.into()])?
1126 .run()
1127 .await?;
1128 Ok(true)
1129 }
1130
1131 pub(crate) async fn managed_pulls(&self, a: ManagedPullsArgs) -> Result<Vec<String>> {
1132 let rows = self
1133 .db
1134 .prepare(
1135 "SELECT id AS value FROM pulls
1136 WHERE status = 'open' AND managed = 1 AND stalled IS NULL
1137 AND (?1 IS NULL OR repo_id = ?1)
1138 ORDER BY updated_at DESC LIMIT ?2",
1139 )
1140 .bind(&[
1141 a.repo_id.map_or(JsValue::NULL, JsValue::from),
1142 MANAGED_PAGE.into(),
1143 ])?
1144 .all()
1145 .await?
1146 .results::<ValueRow>()?;
1147 Ok(rows.into_iter().map(|row| row.value).collect())
1148 }
1149}
1150
1151#[cfg(test)]
1152mod tests {
1153 use super::*;
1154
1155 const MAX_REVISIONS: u32 = 2;
1156
1157 #[test]
1158 fn approvals_the_repository_wants_are_waited_for() {
1159 let short = || Facts {
1160 review: reviewed(Some(Verdict::Approve)),
1161 approvals_missing: Some("This repository requires 1 approving review.".to_owned()),
1162 ..facts()
1163 };
1164 let (lifecycle, next) = decide(short());
1165 assert_eq!(lifecycle.stage, Stage::NeedsYou);
1166 assert_eq!(
1167 lifecycle.detail,
1168 "This repository requires 1 approving review."
1169 );
1170 assert!(matches!(next, Next::Wait));
1171 // Not even a repository that merges by itself merges without them.
1172 let automatic = Facts {
1173 auto_merge: true,
1174 ..short()
1175 };
1176 assert_eq!(outcome(automatic), (Stage::NeedsYou, "wait"));
1177 }
1178
1179 #[test]
1180 fn a_repository_can_leave_review_to_people() {
1181 let unreviewed = Facts {
1182 agent_review: false,
1183 ..facts()
1184 };
1185 assert_eq!(outcome(unreviewed), (Stage::Ready, "wait"));
1186 let failing = Facts {
1187 agent_review: false,
1188 check_status: Some(CheckStatus::Failed),
1189 ..facts()
1190 };
1191 assert_eq!(outcome(failing), (Stage::Revising, "revise for checks"));
1192 }
1193
1194 #[test]
1195 fn a_repository_sets_how_often_the_author_is_sent_back() {
1196 let never = Facts {
1197 max_revisions: 0,
1198 check_status: Some(CheckStatus::Failed),
1199 ..facts()
1200 };
1201 assert_eq!(outcome(never), (Stage::NeedsYou, "wait"));
1202 }
1203
1204 /// A pull request that is ready for review, with checks that passed
1205 /// and nothing else yet.
1206 fn facts() -> Facts {
1207 Facts {
1208 draft: false,
1209 stalled: None,
1210 working_on: None,
1211 check_status: Some(CheckStatus::Passed),
1212 review_pending: false,
1213 has_checks: true,
1214 revisions: 0,
1215 review: None,
1216 behind: false,
1217 auto_merge: false,
1218 require_up_to_date: false,
1219 agent_review: true,
1220 max_revisions: MAX_REVISIONS,
1221 approvals_missing: None,
1222 person_request: None,
1223 queued: None,
1224 workflows: WorkflowFacts::default(),
1225 }
1226 }
1227
1228 #[test]
1229 fn failed_workflows_send_the_agent_back_and_running_ones_wait() {
1230 let failed = Facts {
1231 workflows: WorkflowFacts { pending: vec![], failed: vec!["CI / pull_request".into()] },
1232 ..facts()
1233 };
1234 let (lifecycle, next) = decide(failed);
1235 assert!(matches!(next, Next::Revise(Feedback::FailedWorkflows)));
1236 assert!(lifecycle.detail.contains("CI / pull_request failed"));
1237 let running = Facts {
1238 workflows: WorkflowFacts { pending: vec!["CI / pull_request".into()], failed: vec![] },
1239 ..facts()
1240 };
1241 let (lifecycle, next) = decide(running);
1242 assert!(matches!(next, Next::Wait));
1243 assert!(lifecycle.detail.contains("Waiting for CI / pull_request"));
1244 }
1245
1246 #[test]
1247 fn a_queued_pull_request_waits_in_the_queue() {
1248 let queued = Facts {
1249 review: reviewed(Some(Verdict::Approve)),
1250 queued: Some((QueueState::Testing, vec![12, 14])),
1251 auto_merge: true,
1252 ..facts()
1253 };
1254 let (lifecycle, next) = decide(queued);
1255 assert_eq!(lifecycle.stage, Stage::Queued);
1256 assert!(lifecycle.detail.contains("#12, #14"));
1257 assert!(matches!(next, Next::Wait));
1258 }
1259
1260 fn asked_by_a_person() -> Option<PersonRequest> {
1261 Some(PersonRequest {
1262 author_id: "usr_reviewer".to_owned(),
1263 author_name: "g1t-reviewer".to_owned(),
1264 created_at: "2026-10-02T11:00:00.000Z".to_owned(),
1265 })
1266 }
1267
1268 #[test]
1269 fn a_person_asking_for_changes_sends_the_agent_back() {
1270 let asked = Facts {
1271 review: reviewed(Some(Verdict::Approve)),
1272 approvals_missing: Some("A reviewer has asked for changes.".to_owned()),
1273 person_request: asked_by_a_person(),
1274 ..facts()
1275 };
1276 let (lifecycle, _) = decide(Facts {
1277 person_request: asked_by_a_person(),
1278 ..facts()
1279 });
1280 assert!(lifecycle.detail.starts_with("g1t-reviewer asked for changes"));
1281 assert_eq!(outcome(asked), (Stage::Revising, "revise for a person"));
1282 }
1283
1284 #[test]
1285 fn a_person_is_asked_once_the_revisions_run_out() {
1286 let exhausted = Facts {
1287 person_request: asked_by_a_person(),
1288 revisions: MAX_REVISIONS,
1289 ..facts()
1290 };
1291 assert_eq!(outcome(exhausted), (Stage::NeedsYou, "wait"));
1292 }
1293
1294 fn reviewed(verdict: Option<Verdict>) -> Option<FinishedReview> {
1295 Some(FinishedReview {
1296 finished_at: "2026-10-02T10:00:00.000Z".to_owned(),
1297 verdict,
1298 })
1299 }
1300
1301 /// The stage, and a word for the step to take.
1302 fn outcome(facts: Facts) -> (Stage, &'static str) {
1303 let (lifecycle, next) = decide(facts);
1304 let step = match next {
1305 Next::Wait => "wait",
1306 Next::Review => "review",
1307 Next::Revise(Feedback::FailedChecks) => "revise for checks",
1308 Next::Revise(Feedback::FailedWorkflows) => "revise for workflows",
1309 Next::Revise(Feedback::Review(_)) => "revise for review",
1310 Next::Revise(Feedback::Person(_)) => "revise for a person",
1311 Next::CatchUp => "catch up",
1312 Next::Merge => "merge",
1313 };
1314 (lifecycle.stage, step)
1315 }
1316
1317 #[test]
1318 fn nothing_is_started_while_the_agent_is_still_working() {
1319 let draft = Facts {
1320 draft: true,
1321 check_status: None,
1322 ..facts()
1323 };
1324 assert_eq!(outcome(draft), (Stage::Working, "wait"));
1325 }
1326
1327 #[test]
1328 fn checks_come_before_review() {
1329 let unchecked = Facts {
1330 check_status: None,
1331 ..facts()
1332 };
1333 assert_eq!(outcome(unchecked), (Stage::Checking, "wait"));
1334 let running = Facts {
1335 check_status: Some(CheckStatus::Running),
1336 ..facts()
1337 };
1338 assert_eq!(outcome(running), (Stage::Checking, "wait"));
1339 assert_eq!(outcome(facts()), (Stage::Reviewing, "review"));
1340 }
1341
1342 #[test]
1343 fn an_issue_without_checks_goes_straight_to_review() {
1344 let unchecked = Facts {
1345 has_checks: false,
1346 check_status: None,
1347 ..facts()
1348 };
1349 assert_eq!(outcome(unchecked), (Stage::Reviewing, "review"));
1350 }
1351
1352 #[test]
1353 fn failed_checks_send_the_author_back() {
1354 let failed = Facts {
1355 check_status: Some(CheckStatus::Failed),
1356 ..facts()
1357 };
1358 assert_eq!(outcome(failed), (Stage::Revising, "revise for checks"));
1359 }
1360
1361 #[test]
1362 fn checks_that_could_not_run_are_a_persons_problem() {
1363 let errored = Facts {
1364 check_status: Some(CheckStatus::Errored),
1365 ..facts()
1366 };
1367 assert_eq!(outcome(errored), (Stage::NeedsYou, "wait"));
1368 }
1369
1370 #[test]
1371 fn a_review_asking_for_changes_sends_the_author_back() {
1372 let changes = Facts {
1373 review: reviewed(Some(Verdict::RequestChanges)),
1374 ..facts()
1375 };
1376 assert_eq!(outcome(changes), (Stage::Revising, "revise for review"));
1377 }
1378
1379 #[test]
1380 fn the_author_is_sent_back_only_so_many_times() {
1381 let failing = Facts {
1382 check_status: Some(CheckStatus::Failed),
1383 revisions: MAX_REVISIONS,
1384 ..facts()
1385 };
1386 assert_eq!(outcome(failing), (Stage::NeedsYou, "wait"));
1387 let unconvinced = Facts {
1388 review: reviewed(Some(Verdict::RequestChanges)),
1389 revisions: MAX_REVISIONS,
1390 ..facts()
1391 };
1392 assert_eq!(outcome(unconvinced), (Stage::NeedsYou, "wait"));
1393 // One short of the limit still gets another go.
1394 let once = Facts {
1395 check_status: Some(CheckStatus::Failed),
1396 revisions: MAX_REVISIONS - 1,
1397 ..facts()
1398 };
1399 assert_eq!(outcome(once), (Stage::Revising, "revise for checks"));
1400 }
1401
1402 #[test]
1403 fn a_review_that_could_not_be_written_is_not_retried() {
1404 let broken = Facts {
1405 review: reviewed(None),
1406 ..facts()
1407 };
1408 assert_eq!(outcome(broken), (Stage::NeedsYou, "wait"));
1409 }
1410
1411 #[test]
1412 fn being_behind_only_holds_a_change_up_where_the_repository_says_so() {
1413 let behind = || Facts {
1414 review: reviewed(Some(Verdict::Approve)),
1415 behind: true,
1416 ..facts()
1417 };
1418 // By default it is ready as it is; merging brings it up to date.
1419 assert_eq!(outcome(behind()), (Stage::Ready, "wait"));
1420 let strict = Facts {
1421 require_up_to_date: true,
1422 ..behind()
1423 };
1424 assert_eq!(outcome(strict), (Stage::CatchingUp, "catch up"));
1425 let current = Facts {
1426 review: reviewed(Some(Verdict::Approve)),
1427 ..facts()
1428 };
1429 assert_eq!(outcome(current), (Stage::Ready, "wait"));
1430 }
1431
1432 #[test]
1433 fn a_ready_change_lands_by_itself_only_where_the_repository_says_so() {
1434 let ready = || Facts {
1435 review: reviewed(Some(Verdict::Approve)),
1436 ..facts()
1437 };
1438 assert_eq!(outcome(ready()), (Stage::Ready, "wait"));
1439 let automatic = Facts {
1440 auto_merge: true,
1441 ..ready()
1442 };
1443 assert_eq!(outcome(automatic), (Stage::Ready, "merge"));
1444 // One that is behind is merged too: merging brings it up to date.
1445 let behind = Facts {
1446 auto_merge: true,
1447 behind: true,
1448 ..ready()
1449 };
1450 assert_eq!(outcome(behind), (Stage::Ready, "merge"));
1451 // Unless the repository wants it caught up and checked again first.
1452 let strict = Facts {
1453 auto_merge: true,
1454 behind: true,
1455 require_up_to_date: true,
1456 ..ready()
1457 };
1458 assert_eq!(outcome(strict), (Stage::CatchingUp, "catch up"));
1459 // Nothing short of approved is merged, whatever the setting.
1460 let failing = Facts {
1461 auto_merge: true,
1462 check_status: Some(CheckStatus::Failed),
1463 ..ready()
1464 };
1465 assert_eq!(outcome(failing), (Stage::Revising, "revise for checks"));
1466 let unreviewed = Facts {
1467 auto_merge: true,
1468 ..facts()
1469 };
1470 assert_eq!(outcome(unreviewed), (Stage::Reviewing, "review"));
1471 }
1472
1473 #[test]
1474 fn catching_up_reruns_the_checks_but_not_the_review() {
1475 // The merge moved the head, so the checks are waited for again.
1476 let merged_in = Facts {
1477 review: reviewed(Some(Verdict::Approve)),
1478 check_status: None,
1479 ..facts()
1480 };
1481 assert_eq!(outcome(merged_in), (Stage::Checking, "wait"));
1482 }
1483
1484 #[test]
1485 fn a_step_under_way_is_not_started_again() {
1486 for (step, stage) in [
1487 ("review", Stage::Reviewing),
1488 ("revision", Stage::Revising),
1489 ("catch_up", Stage::CatchingUp),
1490 ] {
1491 let busy = Facts {
1492 working_on: Some(step.to_owned()),
1493 // Whatever else is true, the step in hand comes first.
1494 check_status: Some(CheckStatus::Failed),
1495 ..facts()
1496 };
1497 assert_eq!(outcome(busy), (stage, "wait"));
1498 }
1499 let asked = Facts {
1500 review_pending: true,
1501 ..facts()
1502 };
1503 assert_eq!(outcome(asked), (Stage::Reviewing, "wait"));
1504 }
1505
1506 #[test]
1507 fn once_stopped_it_stays_stopped() {
1508 let (lifecycle, next) = decide(Facts {
1509 stalled: Some("The agent could not catch up.".to_owned()),
1510 review: reviewed(Some(Verdict::Approve)),
1511 behind: true,
1512 ..facts()
1513 });
1514 assert_eq!(lifecycle.stage, Stage::NeedsYou);
1515 assert_eq!(lifecycle.detail, "The agent could not catch up.");
1516 assert!(matches!(next, Next::Wait));
1517 }
1518}