fix(forge,claw): forge keys separate the issue from the pull

The watch key encoded source, repo, and number but not the thread
kind, and on GitLab an issue and a merge request share one number
space: iid 7 twice produced one key, one snapshot, and comments on
the merge request landing in the issue (or a 404 fallback that
misrouted the other way). The key carries the kind now —
{source}:{kind}:{owner}/{name}#{number}, with GitLab spelling the
pull variant mr — comment and snapshot route outright by kind, and a
kind-less payload parses to the unparsable key instead of guessing
issue. The claw's callers thread the kind from the payload and strip
the segment where the bare repo is wanted; pre-kind stored keys parse
as None rather than misroute. Four route-pinning wiremock tests cover
the GitLab split, and the GitHub/Gitea clients take the kind without
using it (their issues and PRs share the same REST space).
This commit is contained in:
vhaudiquet committed 2026-11-05 19:03:52 +01:00
1 parent 10efe85f38
commit 00adf092ff
15 files changed
+478 -178

No files matched your search

@@ -55,8 +55,26 @@ pub enum ThreadState {
Merged,
}
/// Repository coordinates. `owner` + `name` + a thread [`ForgeEvent::number`]
/// is the resource key the RSD dispatcher forms (`host:owner/repo#number`).
/// Which of a forge's thread sequences a number names. GitLab numbers
/// issues and merge requests from separate sequences, so the kind is part
/// of the thread's identity there; GitHub and Gitea share one sequence,
/// where the kind rides along for a uniform vocabulary.
///
/// `PullRequest` is the crate's umbrella term for the issue's
/// counterpart: GitHub's and Gitea's pull request, GitLab's merge
/// request.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ForgeThreadKind {
/// A thread in the forge's issue sequence.
Issue,
/// A thread in the forge's pull/merge-request sequence.
PullRequest,
}
/// Repository coordinates. `owner` + `name` + a thread's [`ForgeEvent::number`]
/// and [`ForgeThreadKind`] are the resource key the RSD dispatcher forms
/// (`host:issue:owner/repo#number`).
///
/// GitLab's `owner` is the whole namespace path (`group/subgroup`), so
/// [`RepoRef::path`] is always the full project path the forge addresses.
@@ -16,7 +16,7 @@
use async_trait::async_trait;
use crate::error::ForgeError;
use crate::event::{ForgeEvent, NotificationPage, RepoRef, Snapshot, Timestamp};
use crate::event::{ForgeEvent, ForgeThreadKind, NotificationPage, RepoRef, Snapshot, Timestamp};
/// One poll's worth of activity, with the cursor to continue from.
#[derive(Debug, Clone, PartialEq)]
@@ -61,12 +61,33 @@ pub trait Forge: Send + Sync {
since: Option<&Timestamp>,
) -> Result<ForgePage, ForgeError>;
/// Posts `body` as a new comment on thread `number`.
async fn comment(&self, repo: &RepoRef, number: u64, body: &str) -> Result<(), ForgeError>;
/// Posts `body` as a new comment on the thread `kind` + `number`
/// name.
///
/// On GitLab the kind picks the sequence the iid is read against:
/// issues and merge requests are numbered independently, and only
/// the named one is addressed — a miss surfaces as a 404, never a
/// silent post to the other sequence. GitHub and Gitea share one
/// number space across issues and pull requests (every pull request
/// is an issue there), so they accept the kind for signature
/// uniformity and either value addresses the same thread.
async fn comment(
&self,
repo: &RepoRef,
kind: ForgeThreadKind,
number: u64,
body: &str,
) -> Result<(), ForgeError>;
/// The thread's current state, so a handler can orient without a
/// second API round-trip.
async fn snapshot(&self, repo: &RepoRef, number: u64) -> Result<Snapshot, ForgeError>;
/// second API round-trip. `kind` selects the sequence `number` is
/// read against, under the same rules as [`Forge::comment`].
async fn snapshot(
&self,
repo: &RepoRef,
kind: ForgeThreadKind,
number: u64,
) -> Result<Snapshot, ForgeError>;
/// The account's notification feed: everything addressed to the
/// account this client authenticates as, across every repository it
@@ -40,8 +40,8 @@ use serde_json::Value;
use crate::error::{ForgeError, ensure_success, required};
use crate::event::{
EventKind, ForgeEvent, Notification, NotificationPage, NotificationReason, RepoRef, Snapshot,
ThreadState, Timestamp,
EventKind, ForgeEvent, ForgeThreadKind, Notification, NotificationPage, NotificationReason,
RepoRef, Snapshot, ThreadState, Timestamp,
};
use crate::forge::{Forge, ForgePage};
@@ -612,8 +612,18 @@ impl Forge for GiteaForge {
Ok(ForgePage::finish(events, since))
}
async fn comment(&self, repo: &RepoRef, number: u64, body: &str) -> Result<(), ForgeError> {
async fn comment(
&self,
repo: &RepoRef,
_kind: ForgeThreadKind,
number: u64,
body: &str,
) -> Result<(), ForgeError> {
// The issues endpoint covers PRs too: every PR is an issue here.
// Issues and pull requests share one number space, so `_kind`
// cannot change the route — either value addresses the same
// thread, and the parameter exists only for the trait's uniform
// signature (GitLab is the forge whose sequences differ).
let path = format!(
"/repos/{}/{}/issues/{number}/comments",
repo.owner, repo.name
@@ -628,7 +638,14 @@ impl Forge for GiteaForge {
Ok(())
}
async fn snapshot(&self, repo: &RepoRef, number: u64) -> Result<Snapshot, ForgeError> {
async fn snapshot(
&self,
repo: &RepoRef,
_kind: ForgeThreadKind,
number: u64,
) -> Result<Snapshot, ForgeError> {
// One number space across issues and pulls: `_kind` cannot
// change the route (see `comment`).
let issue_path = format!("/repos/{}/{}/issues/{number}", repo.owner, repo.name);
let response = self
.get(&issue_path)
@@ -41,8 +41,8 @@ use serde_json::Value;
use crate::error::{ForgeError, ensure_success, required};
use crate::event::{
EventKind, ForgeEvent, Notification, NotificationPage, NotificationReason, RepoRef, Snapshot,
ThreadState, Timestamp,
EventKind, ForgeEvent, ForgeThreadKind, Notification, NotificationPage, NotificationReason,
RepoRef, Snapshot, ThreadState, Timestamp,
};
use crate::forge::{Forge, ForgePage};
@@ -620,8 +620,18 @@ impl Forge for GitHubForge {
Ok(ForgePage::finish(events, since))
}
async fn comment(&self, repo: &RepoRef, number: u64, body: &str) -> Result<(), ForgeError> {
async fn comment(
&self,
repo: &RepoRef,
_kind: ForgeThreadKind,
number: u64,
body: &str,
) -> Result<(), ForgeError> {
// The issues endpoint covers PRs too: every PR is an issue here.
// Issues and pull requests share one number space, so `_kind`
// cannot change the route — either value addresses the same
// thread, and the parameter exists only for the trait's uniform
// signature (GitLab is the forge whose sequences differ).
let path = format!(
"/repos/{}/{}/issues/{number}/comments",
repo.owner, repo.name
@@ -636,7 +646,14 @@ impl Forge for GitHubForge {
Ok(())
}
async fn snapshot(&self, repo: &RepoRef, number: u64) -> Result<Snapshot, ForgeError> {
async fn snapshot(
&self,
repo: &RepoRef,
_kind: ForgeThreadKind,
number: u64,
) -> Result<Snapshot, ForgeError> {
// One number space across issues and pulls: `_kind` cannot
// change the route (see `comment`).
let issue_path = format!("/repos/{}/{}/issues/{number}", repo.owner, repo.name);
let response = self
.get(&issue_path)
@@ -9,9 +9,12 @@
//! `updated_after=<RFC 3339>&order_by=updated_at&sort=asc`: two separate
//! feeds, since GitLab has no combined thread feed.
//! - Issue iids and merge-request iids are separate sequences: a number
//! can exist as both. Wherever a bare number must resolve, the issue
//! feed is tried first and the merge-request feed is the 404 fallback;
//! an iid that is neither surfaces the merge-request 404.
//! can exist as both. Every bare number arrives with its
//! [`ForgeThreadKind`] (the resource key carries it, and the
//! [`Forge`] write methods take it), and the kind picks the
//! `{issues,merge_requests}` path outright — there is no
//! try-issues-then-fallback guessing, so a reply meant for one
//! sequence can never land on the other.
//! - GitLab has no repo-wide notes feed and the per-thread notes endpoint
//! has no updated filter, so every updated thread gets one paginated
//! `GET .../{issues,merge_requests}/{iid}/notes?sort=asc&order_by=created_at`
@@ -41,8 +44,8 @@ use serde_json::Value;
use crate::error::{ForgeError, ensure_success, required};
use crate::event::{
EventKind, ForgeEvent, Notification, NotificationPage, NotificationReason, RepoRef, Snapshot,
ThreadState, Timestamp,
EventKind, ForgeEvent, ForgeThreadKind, Notification, NotificationPage, NotificationReason,
RepoRef, Snapshot, ThreadState, Timestamp,
};
use crate::forge::{Forge, ForgePage};
@@ -169,6 +172,15 @@ async fn decode<T: DeserializeOwned>(response: reqwest::Response) -> Result<T, F
.map_err(|err| ForgeError::Parse(format!("malformed forge response: {err}")))
}
/// The URL path segment GitLab addresses `kind`'s sequence with:
/// `issues` or `merge_requests`.
fn thread_segment(kind: ForgeThreadKind) -> &'static str {
match kind {
ForgeThreadKind::Issue => "issues",
ForgeThreadKind::PullRequest => "merge_requests",
}
}
/// The project path (`owner/name`, possibly with subgroups) as GitLab's
/// API needs it: the whole path percent-encoded, slashes included.
fn encode_project_path(path: &str) -> String {
@@ -488,53 +500,45 @@ impl Forge for GitLabForge {
Ok(ForgePage::finish(events, since))
}
async fn comment(&self, repo: &RepoRef, number: u64, body: &str) -> Result<(), ForgeError> {
async fn comment(
&self,
repo: &RepoRef,
kind: ForgeThreadKind,
number: u64,
body: &str,
) -> Result<(), ForgeError> {
let project = encode_project_path(&repo.path());
// Issue iids and MR iids are separate sequences: try the issue
// feed first, fall back on its 404. An iid that is neither
// surfaces the merge-request 404.
let notes_path = |segment: &str| format!("/projects/{project}/{segment}/{number}/notes");
// The kind names the sequence outright: iid sequences are
// independent, so guessing (issues first, 404 fallback) could
// land a merge-request reply on the same-numbered issue.
let segment = thread_segment(kind);
let notes_path = format!("/projects/{project}/{segment}/{number}/notes");
let response = self
.post(&notes_path("issues"))
.post(&notes_path)
.json(&serde_json::json!({ "body": body }))
.send()
.await
.map_err(ForgeError::network)?;
let response = if response.status() == reqwest::StatusCode::NOT_FOUND {
self.post(&notes_path("merge_requests"))
.json(&serde_json::json!({ "body": body }))
.send()
.await
.map_err(ForgeError::network)?
} else {
response
};
ensure_success(response).await?;
Ok(())
}
async fn snapshot(&self, repo: &RepoRef, number: u64) -> Result<Snapshot, ForgeError> {
async fn snapshot(
&self,
repo: &RepoRef,
kind: ForgeThreadKind,
number: u64,
) -> Result<Snapshot, ForgeError> {
let project = encode_project_path(&repo.path());
let resource = |segment: &str| format!("/projects/{project}/{segment}/{number}");
// Same sequences as `comment`: the kind picks the path.
let is_pr = kind == ForgeThreadKind::PullRequest;
let resource = format!("/projects/{project}/{}/{number}", thread_segment(kind));
let response = self
.get(&resource("issues"))
.get(&resource)
.send()
.await
.map_err(ForgeError::network)?;
// Same iid sequences as `comment`: issues first, 404 fallback.
let (row, is_pr) = match decode::<WireThread>(response).await {
Ok(row) => (row, false),
Err(ForgeError::Api { status: 404, .. }) => {
let response = self
.get(&resource("merge_requests"))
.send()
.await
.map_err(ForgeError::network)?;
let row: WireThread = decode(response).await?;
(row, true)
}
Err(other) => return Err(other),
};
let row: WireThread = decode(response).await?;
Ok(snapshot_from_row(&row, is_pr)?)
}
@@ -8,8 +8,8 @@
//! [`crate::Watcher`] polls a [`crate::Forge`] for activity and feeds
//! the events to a [`rhema_rsd::Dispatcher`](rhema_rsd) (the forge's
//! stable dedup id becomes the event id, and the resource key is spec
//! §8's `host:owner/repo#number` shape, so re-polling unchanged history
//! dedups away and a changed row re-dispatches), and a dispatched
//! §8's `host:kind:owner/repo#number` shape, so re-polling unchanged
//! history dedups away and a changed row re-dispatches), and a dispatched
//! thread is answered through the same [`crate::Forge`] capabilities,
//! with [`crate::decode`] recovering the event behind an inbox item.
//! Polling cadence, handler policy, and the loop itself are the
@@ -63,8 +63,8 @@ pub mod watcher;
pub use async_trait::async_trait;
pub use error::ForgeError;
pub use event::{
EventKind, ForgeEvent, Notification, NotificationPage, NotificationReason, RepoRef, Snapshot,
ThreadState, Timestamp,
EventKind, ForgeEvent, ForgeThreadKind, Notification, NotificationPage, NotificationReason,
RepoRef, Snapshot, ThreadState, Timestamp,
};
pub use forge::{Forge, ForgePage};
pub use gitea::GiteaForge;
@@ -6,7 +6,7 @@
//! [`rhema_rsd::Event`]s: the forge's stable dedup id stays the event id,
//! the serialized [`ForgeEvent`](crate::event::ForgeEvent) is the payload,
//! and the resource key is the spec §8 shape —
//! `{source}:{owner}/{name}#{number}` (see [`resource_key`]). Re-polling
//! `{source}:{kind}:{owner}/{name}#{number}` (see [`resource_key`]). Re-polling
//! unchanged history therefore dedups away in the dispatcher, and a
//! changed row re-dispatches.
//!
@@ -19,7 +19,7 @@ use std::sync::{Arc, Mutex};
use rhema_rsd::{Clock, Dispatcher, Event, InboxItem, KeyFn, ResourceKey, SystemClock};
use crate::error::ForgeError;
use crate::event::{ForgeEvent, RepoRef, Timestamp};
use crate::event::{ForgeEvent, ForgeThreadKind, RepoRef, Timestamp};
use crate::forge::Forge;
/// Why a watch step failed: the forge call, or the dispatcher seam behind
@@ -34,20 +34,71 @@ pub enum WatchError {
Dispatch(#[from] rhema_rsd::DispatchError),
}
/// The resource key of a thread on `source`: `{source}:{path}#{number}` —
/// spec §8's `github:acme/widgets#142` shape.
pub fn resource_key(source: &str, repo: &RepoRef, number: u64) -> ResourceKey {
ResourceKey::from(format!("{source}:{}#{number}", repo.path()))
/// The resource key of a thread on `source`:
/// `{source}:{kind}:{owner}/{name}#{number}` — spec §8's shape with the
/// thread kind spelled between source and path, as `github:issue:acme/
/// widgets#142` or `gitlab:mr:acme/widgets#7`. The kind is part of the
/// key because GitLab numbers issues and merge requests from separate
/// sequences: without it, issue #7 and merge request #7 would share one
/// key, one dedup lane, and one agent session.
pub fn resource_key(
source: &str,
repo: &RepoRef,
kind: ForgeThreadKind,
number: u64,
) -> ResourceKey {
ResourceKey::from(format!(
"{source}:{}:{}#{number}",
key_segment(source, kind),
repo.path()
))
}
/// The key segment `source` spells `kind` with: GitLab says `mr` where
/// the rest say `pull`, so a key reads like the forge's own dedup ids
/// (`github:pull:…`, `gitlab:mr:…`) it rides beside.
fn key_segment(source: &str, kind: ForgeThreadKind) -> &'static str {
match (kind, source == "gitlab") {
(ForgeThreadKind::Issue, _) => "issue",
(ForgeThreadKind::PullRequest, true) => "mr",
(ForgeThreadKind::PullRequest, false) => "pull",
}
}
/// The kind a key segment names: `issue`, or the pull variant in either
/// spelling (`pull` on GitHub and Gitea, `mr` on GitLab). `None` for
/// anything else — an old-format key has no segment and parses as none.
fn segment_kind(segment: &str) -> Option<ForgeThreadKind> {
match segment {
"issue" => Some(ForgeThreadKind::Issue),
"pull" | "mr" => Some(ForgeThreadKind::PullRequest),
_ => None,
}
}
/// Recovers the thread coordinates from [`resource_key`]'s shape. The
/// inverse of the key builder, so a dispatch never needs the original
/// event to reach the forge.
pub fn parse_key(key: &ResourceKey) -> Option<(RepoRef, u64)> {
/// event to reach the forge. Pre-kind keys (`gitlab:acme/w#7`) parse as
/// `None` rather than guess a sequence.
pub fn parse_key(key: &ResourceKey) -> Option<(RepoRef, ForgeThreadKind, u64)> {
let (head, number) = key.as_str().rsplit_once('#')?;
let path = head.split_once(':')?.1;
let (_, rest) = head.split_once(':')?;
let (segment, path) = rest.split_once(':')?;
let kind = segment_kind(segment)?;
let (owner, name) = path.rsplit_once('/')?;
Some((RepoRef::new(owner, name), number.parse().ok()?))
Some((RepoRef::new(owner, name), kind, number.parse().ok()?))
}
/// The thread kind a payload's `is_pr` field names. Both payload builders
/// this crate ships (the watcher's serialized [`ForgeEvent`] and the claw's
/// triaged notification) carry `is_pr` at the top level, so the key
/// function reads the wire field instead of redefining the payload.
fn thread_kind(is_pr: bool) -> ForgeThreadKind {
if is_pr {
ForgeThreadKind::PullRequest
} else {
ForgeThreadKind::Issue
}
}
/// The payload a watcher builds for every event: just the coordinates the
@@ -56,24 +107,29 @@ pub fn parse_key(key: &ResourceKey) -> Option<(RepoRef, u64)> {
struct KeyParts {
repo: RepoRef,
number: u64,
/// The thread kind, in its wire spelling. No default: a payload
/// without it cannot name a GitLab sequence, and guessing `issue`
/// would rebuild the ambiguity this key shape exists to prevent —
/// such a payload lands on the unparsable key instead.
is_pr: bool,
}
/// Registers `source` on `dispatcher` with the canonical key function —
/// the one [`resource_key`] implements. Do this once before the first
/// ingest.
pub fn register_forge(dispatcher: &Dispatcher, source: &str) {
let prefix = format!("{source}:");
let source = source.to_owned();
dispatcher.register_source(
source.to_owned(),
source.clone(),
Arc::new(move |event: &Event| {
match serde_json::from_value::<KeyParts>(event.payload.clone()) {
Ok(parts) => {
ResourceKey::from(format!("{prefix}{}#{}", parts.repo.path(), parts.number))
resource_key(&source, &parts.repo, thread_kind(parts.is_pr), parts.number)
}
// Infallible seam: a payload the watcher itself did not
// build still gets a unique key, so it can neither clobber
// another thread nor wedge the dispatcher.
Err(_) => ResourceKey::from(format!("{prefix}unparsable#{}", event.id)),
Err(_) => ResourceKey::from(format!("{source}:unparsable#{}", event.id)),
}
}),
);
@@ -82,13 +138,11 @@ pub fn register_forge(dispatcher: &Dispatcher, source: &str) {
/// The `KeyFn` [`register_forge`] installs, for embedders wiring the
/// source themselves.
pub fn forge_key_fn(source: &str) -> Arc<KeyFn> {
let prefix = format!("{source}:");
let source = source.to_owned();
Arc::new(
move |event: &Event| match serde_json::from_value::<KeyParts>(event.payload.clone()) {
Ok(parts) => {
ResourceKey::from(format!("{prefix}{}#{}", parts.repo.path(), parts.number))
}
Err(_) => ResourceKey::from(format!("{prefix}unparsable#{}", event.id)),
Ok(parts) => resource_key(&source, &parts.repo, thread_kind(parts.is_pr), parts.number),
Err(_) => ResourceKey::from(format!("{source}:unparsable#{}", event.id)),
},
)
}
@@ -167,16 +221,85 @@ mod tests {
#[test]
fn the_key_shape_round_trips_through_parse() {
let repo = RepoRef::new("acme", "widgets");
let key = resource_key("github", &repo, 142);
assert_eq!(key.as_str(), "github:acme/widgets#142");
assert_eq!(parse_key(&key), Some((repo, 142)));
let key = resource_key("github", &repo, ForgeThreadKind::Issue, 142);
assert_eq!(key.as_str(), "github:issue:acme/widgets#142");
assert_eq!(parse_key(&key), Some((repo, ForgeThreadKind::Issue, 142)));
}
#[test]
fn a_gitlab_subgroup_path_survives_the_round_trip() {
let repo = RepoRef::new("group/subgroup", "widgets");
let key = resource_key("gitlab", &repo, 7);
assert_eq!(parse_key(&key), Some((repo, 7)));
let key = resource_key("gitlab", &repo, ForgeThreadKind::Issue, 7);
assert_eq!(parse_key(&key), Some((repo, ForgeThreadKind::Issue, 7)));
}
#[test]
fn a_gitlab_issue_and_merge_request_produce_distinct_keys() {
// GitLab numbers the two sequences independently: iid 7 exists
// twice, and the keys must not collapse them onto one session.
let repo = RepoRef::new("acme", "widgets");
let issue = resource_key("gitlab", &repo, ForgeThreadKind::Issue, 7);
let mr = resource_key("gitlab", &repo, ForgeThreadKind::PullRequest, 7);
assert_eq!(issue.as_str(), "gitlab:issue:acme/widgets#7");
assert_eq!(mr.as_str(), "gitlab:mr:acme/widgets#7");
assert_ne!(issue, mr);
assert_eq!(
parse_key(&mr),
Some((repo, ForgeThreadKind::PullRequest, 7))
);
}
#[test]
fn a_github_pull_request_key_spells_the_pull_segment() {
let repo = RepoRef::new("acme", "widgets");
let pull = resource_key("github", &repo, ForgeThreadKind::PullRequest, 142);
assert_eq!(pull.as_str(), "github:pull:acme/widgets#142");
assert_eq!(
parse_key(&pull),
Some((repo, ForgeThreadKind::PullRequest, 142))
);
}
#[test]
fn a_pre_kind_key_parses_as_none_instead_of_guessing() {
assert_eq!(parse_key(&ResourceKey::from("gitlab:acme/widgets#7")), None);
}
#[test]
fn the_key_fn_separates_issue_and_pull_payloads() {
let event = |is_pr: bool, number: u64| Event {
id: format!("gitlab:thread:{number}"),
source: "gitlab".to_owned(),
payload: serde_json::to_value(ForgeEvent {
id: format!("gitlab:thread:{number}@2026-10-08T00:00:00Z"),
kind: crate::EventKind::Comment,
repo: RepoRef::new("acme", "widgets"),
number,
is_pr,
actor: None,
body: None,
url: None,
timestamp: Timestamp::parse("2026-10-08T00:00:00Z").expect("parses"),
raw: serde_json::json!({}),
})
.expect("a forge event serializes"),
received_at: 0,
};
let key_fn = forge_key_fn("gitlab");
assert_eq!(
key_fn(&event(false, 7)).as_str(),
"gitlab:issue:acme/widgets#7"
);
assert_eq!(key_fn(&event(true, 7)).as_str(), "gitlab:mr:acme/widgets#7");
// A payload without the kind cannot name a sequence: the
// unparsable key keeps it off every thread's lane.
let key = key_fn(&Event {
id: "foreign".to_owned(),
source: "gitlab".to_owned(),
payload: serde_json::json!({ "repo": {"owner": "acme", "name": "widgets"}, "number": 7 }),
received_at: 0,
});
assert_eq!(key.as_str(), "gitlab:unparsable#foreign");
}
#[test]