diff --git a/services/common/src/lib.rs b/services/common/src/lib.rs index 07ce9a481..ef8fdb1c2 100644 --- a/services/common/src/lib.rs +++ b/services/common/src/lib.rs @@ -248,6 +248,9 @@ pub mod review_live; pub mod review_overview; pub mod review_session; pub mod review_stats; +/// CoderHelm's earlier reviews on a pull request, collapsed once it posts a +/// new one: one line each, hidden comments, resolved threads, dismissed. +pub mod review_supersede; pub mod reviewer_prefs; /// Learning from outcomes: reward events, scores, calibration, variants, /// examples and evidence-backed lessons, per team. diff --git a/services/common/src/review_supersede.rs b/services/common/src/review_supersede.rs new file mode 100644 index 000000000..197e8d7e3 --- /dev/null +++ b/services/common/src/review_supersede.rs @@ -0,0 +1,300 @@ +//! CoderHelm's earlier reviews on a pull request, out of the way once it +//! posts a new one. +//! +//! GitHub can't delete a submitted review. A pull request CoderHelm reviewed +//! three times showed three full review bodies, each with its inline +//! comments, and a reader had to work out which one was current. The +//! cleanest equivalent, decided here and carried out by the conversation +//! layer (`worker::passes::conversation::supersede_reviews`): +//! +//! - every earlier review of CoderHelm's is rewritten to [`OUTDATED_BODY`]; +//! - its earlier inline comments are hidden as outdated: only CoderHelm's +//! own, only in threads it opened, and only where no person's reply is +//! left unanswered; +//! - its own threads whose finding the new review no longer raises are +//! resolved, never one a person opened or replied to without an answer; +//! - its own earlier request for changes is dismissed with +//! [`DISMISS_MESSAGE`]. +//! +//! A person's review, comment or thread is never touched. + +/// What an earlier review of CoderHelm's says once superseded. +pub const OUTDATED_BODY: &str = "Outdated: see CoderHelm's latest review"; +/// The dismissal message on CoderHelm's own superseded request for changes. +pub const DISMISS_MESSAGE: &str = "Superseded by CoderHelm's latest review"; + +/// A submitted review on the pull request. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct Review { + pub id: u64, + /// Written by CoderHelm's app. + pub own: bool, + /// `APPROVED`, `CHANGES_REQUESTED`, `COMMENTED`, `DISMISSED`, `PENDING`. + pub state: String, + pub body: String, +} + +/// One comment in a review thread, oldest first. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct ThreadComment { + /// GraphQL node id (what hiding takes). + pub node_id: String, + pub own: bool, + /// The review it was posted with (0 for a reply outside a review). + pub review_id: u64, + pub minimized: bool, +} + +/// A review thread on the pull request. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct Thread { + /// GraphQL node id (what resolving takes). + pub id: String, + pub resolved: bool, + pub path: String, + /// The first comment's text (the finding). + pub first_body: String, + pub comments: Vec, +} + +impl Thread { + /// CoderHelm opened it. + pub fn own(&self) -> bool { + self.comments.first().is_some_and(|c| c.own) + } + + /// A person wrote the last word: their reply waits for an answer. + pub fn person_waiting(&self) -> bool { + self.comments.last().is_some_and(|c| !c.own) + } +} + +/// What to do with the earlier reviews. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct Plan { + /// Reviews whose body becomes [`OUTDATED_BODY`]. + pub edit: Vec, + /// Requests for changes to dismiss. + pub dismiss: Vec, + /// Inline comments to hide as outdated (node ids). + pub minimize: Vec, + /// Threads to resolve (node ids). + pub resolve: Vec, +} + +/// A finding's key: its file and its first line, in lower case with the +/// punctuation left out, so the same finding raised again matches. +pub fn finding_key(path: &str, body: &str) -> String { + let first = body + .lines() + .map(str::trim) + .find(|l| !l.is_empty() && !l.starts_with("