Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
feat(github_pull_requests): add a crate that owns stored GitHub pull requests (#7102)
dev-rbd5a4b4600ba4
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.jsonindex cc86dba845..a03a3fa936 100644--- a/.github/workspace-dep-closures.json+++ b/.github/workspace-dep-closures.json@@ -93,6 +93,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/github_pull_requests","crates/google_token","crates/harness_id","crates/harness_token",@@ -209,6 +210,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/github_pull_requests","crates/google_token","crates/harness_id","crates/harness_token",@@ -343,6 +345,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/github_pull_requests","crates/google_token","crates/harness_id","crates/harness_token",@@ -481,6 +484,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/github_pull_requests","crates/google_token","crates/harness_id","crates/harness_token",@@ -639,6 +643,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/github_pull_requests","crates/google_token","crates/harness_id","crates/harness_token",@@ -1230,6 +1235,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/github_pull_requests","crates/google_token","crates/harness_id","crates/harness_token",@@ -1472,6 +1478,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/github_pull_requests","crates/google_token","crates/harness_id","crates/harness_token",@@ -1754,6 +1761,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/github_pull_requests","crates/google_token","crates/gtm_invite","crates/harness_id",@@ -2021,6 +2029,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/github_pull_requests","crates/google_token","crates/graphql_common","crates/graphql_soup_filter_input",@@ -2477,6 +2486,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/github_pull_requests","crates/google_token","crates/harness_id","crates/harness_token",@@ -3002,6 +3012,7 @@"crates/frecency","crates/genai_telemetry","crates/generic_email_domains",+ "crates/github_pull_requests","crates/google_token","crates/harness_id","crates/harness_token",@@ -3270,6 +3281,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/github_pull_requests","crates/google_token","crates/graphql_activity","crates/graphql_channel",@@ -3970,6 +3982,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/github_pull_requests","crates/google_token","crates/harness_id","crates/harness_token",@@ -4140,6 +4153,7 @@"crates/frecency","crates/genai_telemetry","crates/generic_email_domains",+ "crates/github_pull_requests","crates/google_token","crates/harness_id","crates/harness_token",@@ -4292,6 +4306,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/github_pull_requests","crates/google_token","crates/harness_id","crates/harness_token",@@ -4461,6 +4476,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/github_pull_requests","crates/google_token","crates/graphql_activity","crates/graphql_channel",@@ -4704,6 +4720,7 @@"crates/frecency","crates/genai_telemetry","crates/generic_email_domains",+ "crates/github_pull_requests","crates/google_token","crates/harness_id","crates/harness_token",@@ -4816,6 +4833,7 @@"crates/frecency","crates/genai_telemetry","crates/generic_email_domains",+ "crates/github_pull_requests","crates/google_token","crates/harness_id","crates/harness_token",@@ -5987,6 +6005,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/github_pull_requests","crates/google_token","crates/harness_id","crates/harness_token",@@ -6055,6 +6074,47 @@"crates/user_quota","crates/workspace-hack"],+ "github_pull_requests": [+ "crates/agent",+ "crates/ai_toolset",+ "crates/ai_usage",+ "crates/attachment",+ "crates/bot_id",+ "crates/bot_token",+ "crates/cowlike",+ "crates/database_env_vars",+ "crates/document_sub_type",+ "crates/entity_access",+ "crates/filter_ast",+ "crates/foreign_entity",+ "crates/genai_telemetry",+ "crates/github_pull_requests",+ "crates/harness_id",+ "crates/harness_token",+ "crates/item_filters",+ "crates/macro_auth",+ "crates/macro_authorization",+ "crates/macro_entrypoint",+ "crates/macro_env",+ "crates/macro_env_var",+ "crates/macro_user_id",+ "crates/macro_uuid",+ "crates/model",+ "crates/model-entity",+ "crates/model-error-response",+ "crates/model_file_type",+ "crates/model_owner",+ "crates/model_user",+ "crates/models_bulk_upload",+ "crates/models_entity_access_management",+ "crates/models_pagination",+ "crates/models_permissions",+ "crates/non_empty",+ "crates/notification_state",+ "crates/prompt",+ "crates/remote_env_var",+ "crates/workspace-hack"+ ],"gmail_client": ["crates/cowlike","crates/email_utils",@@ -6383,6 +6443,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/github_pull_requests","crates/google_token","crates/graphql_common","crates/graphql_email",@@ -6519,6 +6580,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/github_pull_requests","crates/google_token","crates/graphql_common","crates/graphql_email",@@ -6656,6 +6718,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/github_pull_requests","crates/google_token","crates/graphql_common","crates/graphql_email",@@ -6937,6 +7000,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/github_pull_requests","crates/google_token","crates/graphql_scheduled_action","crates/harness_id",@@ -7087,6 +7151,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/github_pull_requests","crates/google_token","crates/graphql_common","crates/graphql_email",@@ -7591,6 +7656,7 @@"crates/frecency","crates/genai_telemetry","crates/generic_email_domains",+ "crates/github_pull_requests","crates/google_token","crates/harness_id","crates/harness_token",@@ -8308,6 +8374,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/github_pull_requests","crates/google_token","crates/harness_id","crates/harness_token",@@ -8471,6 +8538,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/github_pull_requests","crates/google_token","crates/harness_id","crates/harness_token",@@ -9616,6 +9684,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/github_pull_requests","crates/google_token","crates/harness_id","crates/harness_token",@@ -9734,6 +9803,7 @@"crates/frecency","crates/genai_telemetry","crates/generic_email_domains",+ "crates/github_pull_requests","crates/google_token","crates/harness_id","crates/harness_token",@@ -10130,6 +10200,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/github_pull_requests","crates/google_token","crates/harness_id","crates/harness_token",@@ -10277,6 +10348,7 @@"crates/frecency","crates/genai_telemetry","crates/generic_email_domains",+ "crates/github_pull_requests","crates/google_token","crates/harness_id","crates/harness_token",@@ -10791,6 +10863,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/github_pull_requests","crates/google_token","crates/harness_id","crates/harness_token",@@ -11126,6 +11199,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/github_pull_requests","crates/google_token","crates/harness_id","crates/harness_token",@@ -11249,6 +11323,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/github_pull_requests","crates/google_token","crates/graphql_common","crates/graphql_soup_filter_input",@@ -11378,6 +11453,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/github_pull_requests","crates/google_token","crates/harness_id","crates/harness_token",@@ -11499,6 +11575,7 @@"crates/frecency","crates/genai_telemetry","crates/generic_email_domains",+ "crates/github_pull_requests","crates/google_token","crates/harness_id","crates/harness_token",@@ -12299,6 +12376,7 @@"crates/frecency","crates/genai_telemetry","crates/generic_email_domains",+ "crates/github_pull_requests","crates/google_token","crates/harness_id","crates/harness_token",diff --git a/Cargo.lock b/Cargo.lockindex d719c21fa0..27092a8b99 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -1866,6 +1866,7 @@ dependencies = ["fusionauth","futures","github",+ "github_pull_requests","gtm_invite","http-body-util","initiative",@@ -6095,6 +6096,7 @@ dependencies = ["frecency","futures","github",+ "github_pull_requests","graphql_initiative","graphql_scheduled_action","graphql_soup",@@ -6315,6 +6317,7 @@ dependencies = ["entity_registry_db_utils","foreign_entity","futures",+ "github_pull_requests","hex","http-body-util","jsonwebtoken 10.3.0",@@ -8093,6 +8096,7 @@ dependencies = ["entity_access","foreign_entity","fusionauth",+ "github_pull_requests","hex","hmac 0.12.1","http-body-util",@@ -8134,6 +8138,23 @@ dependencies = ["workspace-hack",]+[[package]]+name = "github_pull_requests"+version = "0.1.0"+dependencies = [+ "chrono",+ "entity_access",+ "foreign_entity",+ "serde",+ "serde_json",+ "thiserror 2.0.18",+ "tokio",+ "tracing",+ "utoipa",+ "uuid",+ "workspace-hack",+]+[[package]]name = "glob"version = "0.3.3"@@ -17446,6 +17467,7 @@ dependencies = ["foreign_entity","frecency","futures",+ "github_pull_requests","http-body-util","indexmap 2.14.0","item_filters",diff --git a/apps/web/src/features/block-spreadsheet/components/SpreadsheetGrid.test.tsx b/apps/web/src/features/block-spreadsheet/components/SpreadsheetGrid.test.tsxindex 6b87851572..fe042e4f90 100644--- a/apps/web/src/features/block-spreadsheet/components/SpreadsheetGrid.test.tsx+++ b/apps/web/src/features/block-spreadsheet/components/SpreadsheetGrid.test.tsx@@ -1011,15 +1011,14 @@ describe('cell context menu', () => {show: vi.fn(),};const view = renderGrid({}, { comments, hiddenColumns: [0] });+ // Keep full-grid role queries outside the polling callback so they cannot+ // starve the menu's deferred close/focus handlers on slower CI runners.+ const anchor = view.getByRole('gridcell', { name: 'B2' });view.controller.selectRange({ row: 1, column: 0 }, { row: 1, column: 25 });fireEvent.keyDown(view.element, { key: 'ContextMenu' });const comment = await screen.findByRole('menuitem', { name: 'Comment' });fireEvent.keyDown(comment, { key: 'Enter' });- await waitFor(() =>- expect(comments.add).toHaveBeenCalledWith(- view.getByRole('gridcell', { name: 'B2' })- )- );+ await waitFor(() => expect(comments.add).toHaveBeenCalledWith(anchor));});it('keeps the native text editing menu and does not open the cell menu over headers', async () => {diff --git a/crates/documents/Cargo.toml b/crates/documents/Cargo.tomlindex 47cbff8470..0d5d719220 100644--- a/crates/documents/Cargo.toml+++ b/crates/documents/Cargo.toml@@ -82,7 +82,12 @@ outbound = []ports = ["dep:tokio", "dep:tokio-retry"]schema = ["dep:utoipa", "macro_user_id/schema"]-service = ["dep:entity_access_management", "dep:foreign_entity", "ports"]+service = [+ "dep:entity_access_management",+ "dep:foreign_entity",+ "dep:github_pull_requests",+ "ports",+][dependencies]macro_db_client = { path = "../macro_db_client", optional = true }@@ -114,6 +119,7 @@ entity_access_management = { path = "../entity_access_management", default-featuforeign_entity = { path = "../foreign_entity", default-features = false, features = ["ports",], optional = true }+github_pull_requests = { path = "../github_pull_requests", default-features = false, optional = true }macro_authorization = { path = "../macro_authorization", optional = true, default-features = false, features = ["axum",] }diff --git a/crates/documents/src/domain/service.rs b/crates/documents/src/domain/service.rsindex 791cbd34ce..aabfccd29f 100644--- a/crates/documents/src/domain/service.rs+++ b/crates/documents/src/domain/service.rs@@ -34,6 +34,7 @@ use entity_access::domain::models::{};use foreign_entity::domain::models::{ForeignEntity, SourceId};use foreign_entity::domain::ports::ForeignEntityService;+use github_pull_requests::domain::models::GITHUB_PULL_REQUEST_FOREIGN_ENTITY_SOURCE;use macro_event_broker::MacroEventBroker;use macro_user_id::user_id::MacroUserIdStr;use model::document::response::{DocumentResponseMetadata, LocationResponseData};@@ -210,8 +211,6 @@ fn published_document_actors(auth: &EntityAccessAuth) -> PublishedDocumentActors}}-const GITHUB_PULL_REQUEST_FOREIGN_ENTITY_SOURCE: &str = "github_pull_request";-const MAX_DOCUMENT_NAME_GRAPHEMES: usize = 200;fn short_id_for_entity_id(entity_id: &str) -> Result<String, DocumentError> {diff --git a/crates/github/Cargo.toml b/crates/github/Cargo.tomlindex a54cd73490..b29f2a4102 100644--- a/crates/github/Cargo.toml+++ b/crates/github/Cargo.toml@@ -12,10 +12,11 @@ axum = ["dep:macro_authorization","dep:model-error-response","dep:utoipa",+ "github_pull_requests/schema",]default = ["axum", "inbound", "link", "outbound", "ports", "sync"]inbound = ["axum", "ports"]-link = ["dep:entity_access", "dep:foreign_entity", "ports"]+link = ["dep:entity_access", "github_pull_requests/ports", "ports"]outbound = ["dep:connection_gateway_client","dep:fusionauth",@@ -35,6 +36,7 @@ sync = ["dep:model_notifications","dep:notification","dep:reqwest",+ "github_pull_requests/ports","ports",]@@ -55,6 +57,7 @@ entity_access = { path = "../entity_access", default-features = false, featuresforeign_entity = { path = "../foreign_entity", default-features = false, features = ["ports",], optional = true }+github_pull_requests = { path = "../github_pull_requests", default-features = false }hex = { workspace = true }hmac = { workspace = true }jsonwebtoken = { workspace = true }@@ -95,6 +98,12 @@ workspace-hack = { version = "0.1", path = "../workspace-hack" }[dev-dependencies]document_sub_type = { path = "../document_sub_type" }+foreign_entity = { path = "../foreign_entity", default-features = false, features = [+ "ports",+] }+github_pull_requests = { path = "../github_pull_requests", default-features = false, features = [+ "service",+] }http-body-util = { workspace = true }macro_db_migrator = { path = "../macro_db_migrator" }mockall = { workspace = true }diff --git a/crates/github/src/domain/models.rs b/crates/github/src/domain/models.rsindex 453a553413..109e2ea759 100644--- a/crates/github/src/domain/models.rs+++ b/crates/github/src/domain/models.rs@@ -7,7 +7,6 @@ mod test;mod app_jwt;mod installation_state;mod link;-mod pull_request;mod repository;mod sync;@@ -15,20 +14,20 @@ mod sync;pub use app_jwt::AppJwt;#[cfg(feature = "sync")]pub(crate) use app_jwt::app_jwt;-pub use installation_state::{- InstallationState, InstallationStateError, sign_installation_state, verify_installation_state,-};-pub use link::{GithubAccessToken, GithubExchangeTokenResponse, GithubLink, GithubUserInfo};-pub use pull_request::{+pub use github_pull_requests::domain::models::{EnrichGithubPullRequestsProxyRequest, EnrichGithubPullRequestsResponse,- EnrichedGithubPullRequest, GITHUB_PULL_REQUEST_FOREIGN_ENTITY_SOURCE,+ EnrichedGithubPullRequest, GITHUB_PULL_REQUEST_FOREIGN_ENTITY_SOURCE, GithubKey,GithubPullRequestCheckRun, GithubPullRequestComment, GithubPullRequestDetails,GithubPullRequestRef, GithubPullRequestStatus,};+pub use installation_state::{+ InstallationState, InstallationStateError, sign_installation_state, verify_installation_state,+};+pub use link::{GithubAccessToken, GithubExchangeTokenResponse, GithubLink, GithubUserInfo};pub use repository::GithubRepository;pub use sync::{GithubAppInstallationSource, GithubAuthenticatedUser, GithubInstallationAccessToken,- GithubInstallationSetupAction, GithubKey, GithubSetupAccessToken, GithubUserInstallation,+ GithubInstallationSetupAction, GithubSetupAccessToken, GithubUserInstallation,GithubUserInstallationsPage, GithubWebhookEventType, MacroTaskId, ResolvedTeamTaskReference,TeamTaskReference, ValidatedGithubWebhookEvent, extract_github_mentions, strip_markdown_code,};diff --git a/crates/github/src/domain/models/sync.rs b/crates/github/src/domain/models/sync.rsindex b848bb8262..c561822be9 100644--- a/crates/github/src/domain/models/sync.rs+++ b/crates/github/src/domain/models/sync.rs@@ -33,29 +33,6 @@ impl TryFrom<&str> for GithubInstallationSetupAction {}}-/// Github key used for tracking tasks-#[derive(Debug, Clone)]-pub struct GithubKey(String);--impl GithubKey {- /// Create a new github key- pub fn new(org: &str, repo: &str, pr: u64) -> Self {- Self(format!("{org}/{repo}/pull/{pr}"))- }-}--impl fmt::Display for GithubKey {- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {- write!(f, "{}", self.0)- }-}--impl AsRef<str> for GithubKey {- fn as_ref(&self) -> &str {- &self.0- }-}-/// OAuth access token returned after exchanging a GitHub App setup code.#[derive(Clone, Deserialize)]pub struct GithubSetupAccessToken {diff --git a/crates/github/src/domain/models/test.rs b/crates/github/src/domain/models/test.rsindex 1b432f3a7f..b7cd65022f 100644--- a/crates/github/src/domain/models/test.rs+++ b/crates/github/src/domain/models/test.rs@@ -1,582 +1,5 @@use super::*;-#[test]-fn test_github_key() {- let key = GithubKey::new("rust-lang", "rust", 12345);- assert_eq!(key.as_ref(), "rust-lang/rust/pull/12345");- assert_eq!(key.to_string(), "rust-lang/rust/pull/12345");-}--fn pull_request_reference() -> GithubPullRequestRef {- GithubPullRequestRef {- github_key: "macro/app/pull/7".to_string(),- owner: "macro".to_string(),- repo: "app".to_string(),- number: 7,- url: "https://github.com/macro/app/pull/7".to_string(),- display_name: "macro/app#7".to_string(),- }-}--fn pull_request_details(- state: &str,- merged_at: Option<chrono::DateTime<chrono::Utc>>,-) -> GithubPullRequestDetails {- GithubPullRequestDetails {- title: "Add pull request enrichment".to_string(),- state: state.to_string(),- merged_at,- additions: 42,- deletions: 12,- author_login: Some("octocat".to_string()),- author_id: Some(583231),- description: Some("Enrich pull requests with GitHub data".to_string()),- comments: None,- checks: None,- participant_github_user_ids: None,- }-}--fn utc_datetime(value: &str) -> chrono::DateTime<chrono::Utc> {- chrono::DateTime::parse_from_rfc3339(value)- .unwrap()- .with_timezone(&chrono::Utc)-}--fn pull_request_comment() -> GithubPullRequestComment {- GithubPullRequestComment {- id: 101,- body: "Looks good to me".to_string(),- author_id: None,- author_login: Some("octocat".to_string()),- author_association: Some("MEMBER".to_string()),- url: Some("https://github.com/macro/app/pull/7#issuecomment-101".to_string()),- created_at: Some(utc_datetime("2026-05-25T18:54:21Z")),- updated_at: Some(utc_datetime("2026-05-25T19:00:00Z")),- source: "issue_comment".to_string(),- in_reply_to_id: None,- pull_request_review_id: None,- path: None,- line: None,- original_line: None,- }-}--fn pull_request_check_run() -> GithubPullRequestCheckRun {- GithubPullRequestCheckRun {- id: 202,- name: "ci".to_string(),- status: "completed".to_string(),- conclusion: Some("success".to_string()),- url: Some("https://github.com/macro/app/actions/runs/202".to_string()),- started_at: Some(utc_datetime("2026-05-25T18:55:00Z")),- completed_at: Some(utc_datetime("2026-05-25T18:59:00Z")),- }-}--#[test]-fn pull_request_status_maps_open_pr() {- let details = pull_request_details("open", None);-- assert_eq!(details.status(), GithubPullRequestStatus::Open);-}--#[test]-fn pull_request_status_maps_closed_unmerged_pr() {- let details = pull_request_details("closed", None);-- assert_eq!(details.status(), GithubPullRequestStatus::Closed);-}--#[test]-fn pull_request_status_maps_closed_merged_pr() {- let merged_at = utc_datetime("2026-05-25T18:54:21Z");- let details = pull_request_details("closed", Some(merged_at));-- assert_eq!(details.status(), GithubPullRequestStatus::Merged);-}--#[test]-fn pull_request_details_deserializes_github_comment_count() {- let details_json = serde_json::json!({- "title": "Add pull request enrichment",- "state": "open",- "merged_at": null,- "additions": 42,- "deletions": 12,- "comments": 3- });-- let details: GithubPullRequestDetails = serde_json::from_value(details_json).unwrap();-- assert_eq!(details.comments, None);- assert_eq!(details.checks, None);-}--#[test]-fn pull_request_proxy_request_serializes_with_only_pull_requests() {- let reference = pull_request_reference();- let request = EnrichGithubPullRequestsProxyRequest {- pull_requests: vec![reference],- };-- let request_json = serde_json::to_value(&request).unwrap();-- assert_eq!(- request_json,- serde_json::json!({- "pullRequests": [- {- "githubKey": "macro/app/pull/7",- "owner": "macro",- "repo": "app",- "number": 7,- "url": "https://github.com/macro/app/pull/7",- "displayName": "macro/app#7"- }- ]- })- );- assert!(request_json.get("macroUserId").is_none());-- let decoded_request: EnrichGithubPullRequestsProxyRequest =- serde_json::from_value(request_json).unwrap();- assert_eq!(decoded_request, request);-}--#[test]-fn pull_request_response_serializes_with_camel_case_fields() {- let reference = pull_request_reference();- let response = EnrichGithubPullRequestsResponse {- pull_requests: vec![EnrichedGithubPullRequest {- github_key: reference.github_key,- owner: reference.owner,- repo: reference.repo,- number: reference.number,- url: reference.url,- display_name: reference.display_name,- name: Some("Add pull request enrichment".to_string()),- status: Some(GithubPullRequestStatus::Merged),- additions: Some(42),- deletions: Some(12),- author_login: Some("octocat".to_string()),- author_id: Some(583231),- description: Some("Enrich pull requests with GitHub data".to_string()),- comments: Some(vec![pull_request_comment()]),- checks: Some(vec![pull_request_check_run()]),- participant_github_user_ids: None,- }],- };-- let response_json = serde_json::to_value(&response).unwrap();-- assert_eq!(- response_json,- serde_json::json!({- "pullRequests": [- {- "githubKey": "macro/app/pull/7",- "owner": "macro",- "repo": "app",- "number": 7,- "url": "https://github.com/macro/app/pull/7",- "displayName": "macro/app#7",- "name": "Add pull request enrichment",- "status": "merged",- "additions": 42,- "deletions": 12,- "authorLogin": "octocat",- "authorId": 583231,- "description": "Enrich pull requests with GitHub data",- "comments": [- {- "id": 101,- "body": "Looks good to me",- "authorLogin": "octocat",- "authorAssociation": "MEMBER",- "url": "https://github.com/macro/app/pull/7#issuecomment-101",- "createdAt": "2026-05-25T18:54:21Z",- "updatedAt": "2026-05-25T19:00:00Z",- "source": "issue_comment"- }- ],- "checks": [- {- "id": 202,- "name": "ci",- "status": "completed",- "conclusion": "success",- "url": "https://github.com/macro/app/actions/runs/202",- "startedAt": "2026-05-25T18:55:00Z",- "completedAt": "2026-05-25T18:59:00Z"- }- ]- }- ]- })- );-- let decoded_response: EnrichGithubPullRequestsResponse =- serde_json::from_value(response_json).unwrap();- assert_eq!(decoded_response, response);-}--#[test]-fn pull_request_response_deserializes_without_comments_and_checks() {- let response_json = serde_json::json!({- "pullRequests": [- {- "githubKey": "macro/app/pull/7",- "owner": "macro",- "repo": "app",- "number": 7,- "url": "https://github.com/macro/app/pull/7",- "displayName": "macro/app#7",- "name": "Add pull request enrichment",- "status": "open",- "additions": 42,- "deletions": 12- }- ]- });-- let decoded_response: EnrichGithubPullRequestsResponse =- serde_json::from_value(response_json).unwrap();- let pull_request = decoded_response.pull_requests.first().unwrap();-- assert_eq!(pull_request.comments, None);- assert_eq!(pull_request.checks, None);-}--#[test]-fn pull_request_enrichment_preserves_reference_fields() {- let reference = pull_request_reference();- let enriched = EnrichedGithubPullRequest::from_reference(reference.clone());-- assert_eq!(enriched.github_key, reference.github_key);- assert_eq!(enriched.owner, reference.owner);- assert_eq!(enriched.repo, reference.repo);- assert_eq!(enriched.number, reference.number);- assert_eq!(enriched.url, reference.url);- assert_eq!(enriched.display_name, reference.display_name);- assert_eq!(enriched.name, None);- assert_eq!(enriched.status, None);- assert_eq!(enriched.additions, None);- assert_eq!(enriched.deletions, None);- assert_eq!(enriched.author_login, None);- assert_eq!(enriched.author_id, None);- assert_eq!(enriched.description, None);- assert_eq!(enriched.comments, None);- assert_eq!(enriched.checks, None);-- let enriched_json = serde_json::to_value(&enriched).unwrap();- assert!(enriched_json.get("comments").is_none());- assert!(enriched_json.get("checks").is_none());- assert!(enriched_json.get("authorLogin").is_none());- assert!(enriched_json.get("authorId").is_none());- assert!(enriched_json.get("description").is_none());-}--#[test]-fn pull_request_enrichment_copies_details_fields() {- let reference = pull_request_reference();- let comments = vec![pull_request_comment()];- let checks = vec![pull_request_check_run()];- let details = GithubPullRequestDetails {- title: "Add pull request enrichment".to_string(),- state: "closed".to_string(),- merged_at: Some(utc_datetime("2026-05-25T18:54:21Z")),- additions: 42,- deletions: 12,- author_login: Some("octocat".to_string()),- author_id: Some(583231),- description: Some("Enrich pull requests with GitHub data".to_string()),- comments: Some(comments.clone()),- checks: Some(checks.clone()),- participant_github_user_ids: Some(vec!["42".to_string(), "583231".to_string()]),- };-- let enriched = EnrichedGithubPullRequest::from_details(reference.clone(), details);-- assert_eq!(enriched.github_key, reference.github_key);- assert_eq!(enriched.owner, reference.owner);- assert_eq!(enriched.repo, reference.repo);- assert_eq!(enriched.number, reference.number);- assert_eq!(enriched.url, reference.url);- assert_eq!(enriched.display_name, reference.display_name);- assert_eq!(- enriched.name,- Some("Add pull request enrichment".to_string())- );- assert_eq!(enriched.status, Some(GithubPullRequestStatus::Merged));- assert_eq!(enriched.additions, Some(42));- assert_eq!(enriched.deletions, Some(12));- assert_eq!(enriched.author_login, Some("octocat".to_string()));- assert_eq!(enriched.author_id, Some(583231));- assert_eq!(- enriched.description,- Some("Enrich pull requests with GitHub data".to_string())- );- assert_eq!(enriched.comments, Some(comments));- assert_eq!(enriched.checks, Some(checks));- assert_eq!(- enriched.participant_github_user_ids,- Some(vec!["42".to_string(), "583231".to_string()])- );-}--#[test]-fn pull_request_foreign_entity_metadata_serializes_enriched_pull_request() {- assert_eq!(- GITHUB_PULL_REQUEST_FOREIGN_ENTITY_SOURCE,- "github_pull_request"- );-- let reference = pull_request_reference();- let details = GithubPullRequestDetails {- title: "Add pull request enrichment".to_string(),- state: "closed".to_string(),- merged_at: Some(utc_datetime("2026-05-25T18:54:21Z")),- additions: 42,- deletions: 12,- author_login: Some("octocat".to_string()),- author_id: Some(583231),- description: Some("Enrich pull requests with GitHub data".to_string()),- comments: Some(vec![pull_request_comment()]),- checks: Some(vec![pull_request_check_run()]),- participant_github_user_ids: None,- };- let enriched = EnrichedGithubPullRequest::from_details(reference, details);-- let metadata = enriched.foreign_entity_metadata(None).unwrap();-- assert_eq!(- metadata,- serde_json::json!({- "githubKey": "macro/app/pull/7",- "owner": "macro",- "repo": "app",- "number": 7,- "url": "https://github.com/macro/app/pull/7",- "displayName": "macro/app#7",- "name": "Add pull request enrichment",- "status": "merged",- "additions": 42,- "deletions": 12,- "authorLogin": "octocat",- "authorId": 583231,- "description": "Enrich pull requests with GitHub data",- "comments": [- {- "id": 101,- "body": "Looks good to me",- "authorLogin": "octocat",- "authorAssociation": "MEMBER",- "url": "https://github.com/macro/app/pull/7#issuecomment-101",- "createdAt": "2026-05-25T18:54:21Z",- "updatedAt": "2026-05-25T19:00:00Z",- "source": "issue_comment"- }- ],- "checks": [- {- "id": 202,- "name": "ci",- "status": "completed",- "conclusion": "success",- "url": "https://github.com/macro/app/actions/runs/202",- "startedAt": "2026-05-25T18:55:00Z",- "completedAt": "2026-05-25T18:59:00Z"- }- ]- })- );-}--#[test]-fn pull_request_foreign_entity_metadata_preserves_existing_arrays_when_refresh_omits_them() {- let enriched = EnrichedGithubPullRequest::from_details(- pull_request_reference(),- pull_request_details("open", None),- );- let existing_metadata = serde_json::json!({- "comments": [- {- "id": 303,- "body": "Existing comment"- }- ],- "checks": [- {- "id": 404,- "name": "existing-ci"- }- ]- });-- let metadata = enriched- .foreign_entity_metadata(Some(&existing_metadata))- .unwrap();-- assert_eq!(- metadata,- serde_json::json!({- "githubKey": "macro/app/pull/7",- "owner": "macro",- "repo": "app",- "number": 7,- "url": "https://github.com/macro/app/pull/7",- "displayName": "macro/app#7",- "name": "Add pull request enrichment",- "status": "open",- "additions": 42,- "deletions": 12,- "authorLogin": "octocat",- "authorId": 583231,- "description": "Enrich pull requests with GitHub data",- "comments": [- {- "id": 303,- "body": "Existing comment"- }- ],- "checks": [- {- "id": 404,- "name": "existing-ci"- }- ]- })- );-}--#[test]-fn pull_request_foreign_entity_metadata_keeps_fresh_arrays() {- let comments = vec![pull_request_comment()];- let checks = vec![pull_request_check_run()];- let details = GithubPullRequestDetails {- title: "Add pull request enrichment".to_string(),- state: "open".to_string(),- merged_at: None,- additions: 42,- deletions: 12,- author_login: None,- author_id: None,- description: None,- comments: Some(comments.clone()),- checks: Some(checks.clone()),- participant_github_user_ids: None,- };- let enriched = EnrichedGithubPullRequest::from_details(pull_request_reference(), details);- let existing_metadata = serde_json::json!({- "comments": [{ "id": 303, "body": "Existing comment" }],- "checks": [{ "id": 404, "name": "existing-ci" }]- });- let fresh_comments = serde_json::to_value(&comments).unwrap();- let fresh_checks = serde_json::to_value(&checks).unwrap();-- let metadata = enriched- .foreign_entity_metadata(Some(&existing_metadata))- .unwrap();-- assert_eq!(metadata.get("comments"), Some(&fresh_comments));- assert_eq!(metadata.get("checks"), Some(&fresh_checks));-}--#[test]-fn pull_request_foreign_entity_metadata_carries_existing_author_and_description_forward() {- let mut details = pull_request_details("open", None);- details.author_login = None;- details.author_id = None;- details.description = None;- let enriched = EnrichedGithubPullRequest::from_details(pull_request_reference(), details);- let existing_metadata = serde_json::json!({- "authorLogin": "octocat",- "authorId": 583231,- "description": "Existing description"- });-- let metadata = enriched- .foreign_entity_metadata(Some(&existing_metadata))- .unwrap();-- assert_eq!(- metadata.get("authorLogin"),- Some(&serde_json::json!("octocat"))- );- assert_eq!(metadata.get("authorId"), Some(&serde_json::json!(583231)));- assert_eq!(- metadata.get("description"),- Some(&serde_json::json!("Existing description"))- );-}--#[test]-fn pull_request_foreign_entity_metadata_keeps_fresh_author_and_description() {- let enriched = EnrichedGithubPullRequest::from_details(- pull_request_reference(),- pull_request_details("open", None),- );- let existing_metadata = serde_json::json!({- "authorLogin": "stale-login",- "authorId": 1,- "description": "Stale description"- });-- let metadata = enriched- .foreign_entity_metadata(Some(&existing_metadata))- .unwrap();-- assert_eq!(- metadata.get("authorLogin"),- Some(&serde_json::json!("octocat"))- );- assert_eq!(metadata.get("authorId"), Some(&serde_json::json!(583231)));- assert_eq!(- metadata.get("description"),- Some(&serde_json::json!("Enrich pull requests with GitHub data"))- );-}--#[test]-fn pull_request_foreign_entity_metadata_unions_participants_with_existing() {- let mut details = pull_request_details("open", None);- details.participant_github_user_ids = Some(vec!["42".to_string(), "99".to_string()]);- let enriched = EnrichedGithubPullRequest::from_details(pull_request_reference(), details);- let existing_metadata = serde_json::json!({- "participantGithubUserIds": ["7", "42"]- });-- let metadata = enriched- .foreign_entity_metadata(Some(&existing_metadata))- .unwrap();-- assert_eq!(- metadata.get("participantGithubUserIds"),- Some(&serde_json::json!(["42", "7", "99"]))- );-}--#[test]-fn pull_request_foreign_entity_metadata_carries_existing_participants_forward() {- let enriched = EnrichedGithubPullRequest::from_details(- pull_request_reference(),- pull_request_details("open", None),- );- let existing_metadata = serde_json::json!({- "participantGithubUserIds": ["7"]- });-- let metadata = enriched- .foreign_entity_metadata(Some(&existing_metadata))- .unwrap();-- assert_eq!(- metadata.get("participantGithubUserIds"),- Some(&serde_json::json!(["7"]))- );-}-// ---------------------------------------------------------------------------// MacroTaskId::from_short_uuid// ---------------------------------------------------------------------------diff --git a/crates/github/src/domain/service/link.rs b/crates/github/src/domain/service/link.rsindex 29c24d3498..96caad1fc9 100644--- a/crates/github/src/domain/service/link.rs+++ b/crates/github/src/domain/service/link.rs@@ -1,7 +1,7 @@//! Github Link Service implemenationuse chrono::Utc;-use foreign_entity::domain::{models::PatchForeignEntity, ports::ForeignEntityService};+use github_pull_requests::domain::ports::GithubPullRequestService;use macro_user_id::{lowercased::Lowercase,user_id::{MacroUserId, MacroUserIdStr},@@ -9,8 +9,7 @@ use macro_user_id::{use crate::domain::{models::{- EnrichedGithubPullRequest, GITHUB_PULL_REQUEST_FOREIGN_ENTITY_SOURCE, GithubAccessToken,- GithubError, GithubLink, GithubPullRequestRef,+ EnrichedGithubPullRequest, GithubAccessToken, GithubError, GithubLink, GithubPullRequestRef,},ports::{Auth, GithubLinkService, GithubOauth, GithubRepo},};@@ -27,15 +26,20 @@ pub struct GithubLinkConfig {}/// The concrete github link service implementation.-pub struct GithubLinkServiceImpl<R: GithubRepo, U: GithubOauth, F: Auth, E: ForeignEntityService> {+pub struct GithubLinkServiceImpl<+ R: GithubRepo,+ U: GithubOauth,+ F: Auth,+ E: GithubPullRequestService,+> {repo: R,oauth: U,auth: F,- foreign_entity_service: E,+ pull_request_service: E,config: super::GithubLinkConfig,}-impl<R: GithubRepo, U: GithubOauth, F: Auth, E: ForeignEntityService>+impl<R: GithubRepo, U: GithubOauth, F: Auth, E: GithubPullRequestService>GithubLinkServiceImpl<R, U, F, E>{/// Create a new github link service.@@ -43,14 +47,14 @@ impl<R: GithubRepo, U: GithubOauth, F: Auth, E: ForeignEntityService>repo: R,oauth: U,auth: F,- foreign_entity_service: E,+ pull_request_service: E,config: super::GithubLinkConfig,) -> Self {Self {repo,oauth,auth,- foreign_entity_service,+ pull_request_service,config,}}@@ -100,65 +104,22 @@ impl<R: GithubRepo, U: GithubOauth, F: Auth, E: ForeignEntityService>Ok(access_token)}- async fn patch_pull_request_foreign_entities(&self, pull_request: &EnrichedGithubPullRequest) {- let foreign_entities = match self- .foreign_entity_service- .get_foreign_entities_by_foreign_entity_id(- &pull_request.github_key,- Some(GITHUB_PULL_REQUEST_FOREIGN_ENTITY_SOURCE),- )+ async fn refresh_stored_pull_request(&self, pull_request: &EnrichedGithubPullRequest) {+ if let Err(error) = self+ .pull_request_service+ .refresh_pull_request(pull_request).await{- Ok(foreign_entities) => foreign_entities,- Err(error) => {- tracing::warn!(- error=?error,- github_key=%pull_request.github_key,- source=%GITHUB_PULL_REQUEST_FOREIGN_ENTITY_SOURCE,- "failed to fetch GitHub pull request foreign entities"- );- return;- }- };-- for foreign_entity in foreign_entities {- let foreign_entity_record_id = foreign_entity.id;- let metadata =- match pull_request.foreign_entity_metadata(Some(&foreign_entity.metadata)) {- Ok(metadata) => metadata,- Err(error) => {- tracing::warn!(- error=?error,- foreign_entity_record_id=%foreign_entity_record_id,- github_key=%pull_request.github_key,- "failed to serialize GitHub pull request foreign entity metadata"- );- continue;- }- };-- let patch = PatchForeignEntity {- metadata: Some(metadata),- ..PatchForeignEntity::default()- };-- if let Err(error) = self- .foreign_entity_service- .patch_foreign_entity(foreign_entity_record_id, patch)- .await- {- tracing::warn!(- error=?error,- foreign_entity_record_id=%foreign_entity_record_id,- github_key=%pull_request.github_key,- "failed to patch GitHub pull request foreign entity"- );- }+ tracing::warn!(+ error=?error,+ github_key=%pull_request.github_key,+ "failed to refresh stored GitHub pull request"+ );}}}-impl<R: GithubRepo, U: GithubOauth, F: Auth, E: ForeignEntityService> GithubLinkService+impl<R: GithubRepo, U: GithubOauth, F: Auth, E: GithubPullRequestService> GithubLinkServicefor GithubLinkServiceImpl<R, U, F, E>{#[tracing::instrument(skip(self), err)]@@ -221,7 +182,7 @@ impl<R: GithubRepo, U: GithubOauth, F: Auth, E: ForeignEntityService> GithubLinkOk(details) => {let enriched_pull_request =EnrichedGithubPullRequest::from_details(pull_request, details);- self.patch_pull_request_foreign_entities(&enriched_pull_request)+ self.refresh_stored_pull_request(&enriched_pull_request).await;enriched_pull_request}diff --git a/crates/github/src/domain/service/link_test.rs b/crates/github/src/domain/service/link_test.rsindex 61af15bef0..e35d20e0d5 100644--- a/crates/github/src/domain/service/link_test.rs+++ b/crates/github/src/domain/service/link_test.rs@@ -8,6 +8,7 @@ use foreign_entity::domain::{},ports::{ForeignEntityListQuery, ForeignEntityService},};+use github_pull_requests::domain::service::GithubPullRequestServiceImpl;use macro_user_id::{lowercased::Lowercase,user_id::{MacroUserId, MacroUserIdStr},@@ -592,11 +593,14 @@ fn foreign_entity(}}-fn service(- repo: StubGithubRepo,- oauth: StubGithubOauth,- auth: StubAuth,-) -> GithubLinkServiceImpl<StubGithubRepo, StubGithubOauth, StubAuth, StubForeignEntityService> {+type TestGithubLinkService = GithubLinkServiceImpl<+ StubGithubRepo,+ StubGithubOauth,+ StubAuth,+ GithubPullRequestServiceImpl<StubForeignEntityService>,+>;++fn service(repo: StubGithubRepo, oauth: StubGithubOauth, auth: StubAuth) -> TestGithubLinkService {service_with_foreign_entities(repo, oauth, auth, StubForeignEntityService::default())}@@ -605,12 +609,12 @@ fn service_with_foreign_entities(oauth: StubGithubOauth,auth: StubAuth,foreign_entity_service: StubForeignEntityService,-) -> GithubLinkServiceImpl<StubGithubRepo, StubGithubOauth, StubAuth, StubForeignEntityService> {+) -> TestGithubLinkService {GithubLinkServiceImpl::new(repo,oauth,auth,- foreign_entity_service,+ GithubPullRequestServiceImpl::new(foreign_entity_service),GithubLinkConfig {client_id: "client-id".to_string(),client_secret: "client-secret".to_string(),diff --git a/crates/github/src/domain/service/sync.rs b/crates/github/src/domain/service/sync.rsindex e3a658bc47..4fdb79403d 100644--- a/crates/github/src/domain/service/sync.rs+++ b/crates/github/src/domain/service/sync.rs@@ -13,20 +13,19 @@ mod realtime;use crate::domain::{models::{- EnrichedGithubPullRequest, GITHUB_PULL_REQUEST_FOREIGN_ENTITY_SOURCE,- GithubAppInstallationSource, GithubError, GithubInstallationAccessToken,- GithubInstallationSetupAction, GithubKey, GithubPullRequestDetails,- GithubPullRequestStatus, GithubWebhookEventType, InstallationState, MacroTaskId,- ResolvedTeamTaskReference, TeamTaskReference, ValidatedGithubWebhookEvent,- sign_installation_state, verify_installation_state,+ EnrichedGithubPullRequest, GithubAppInstallationSource, GithubError,+ GithubInstallationAccessToken, GithubInstallationSetupAction, GithubKey,+ GithubPullRequestDetails, GithubPullRequestStatus, GithubWebhookEventType,+ InstallationState, MacroTaskId, ResolvedTeamTaskReference, TeamTaskReference,+ ValidatedGithubWebhookEvent, sign_installation_state, verify_installation_state,},ports::{GithubSyncClient, GithubSyncRealtime, GithubSyncRepo, GithubSyncService},};use documents::domain::{models::DocumentError, ports::DocumentService};use entity_access::domain::models::{EditAccessLevel, ViewAccessLevel};-use foreign_entity::domain::{- models::{CreateForeignEntity, ForeignEntity, PatchForeignEntity},- ports::ForeignEntityService,+use foreign_entity::domain::models::SourceId;+use github_pull_requests::domain::{+ models::UpsertGithubPullRequest, ports::GithubPullRequestService,};use hmac::{Hmac, Mac};use macro_env_var::maybe_env_vars;@@ -66,13 +65,13 @@ pub struct GithubSyncServiceImpl<D: DocumentService,R: GithubSyncRepo,C: GithubSyncClient,- F: ForeignEntityService,+ G: GithubPullRequestService,N: NotificationIngress,P: GithubSyncRealtime,> {config: GithubSyncConfig,document_service: Arc<D>,- foreign_entity_service: Arc<F>,+ pull_request_service: Arc<G>,notification_ingress: N,realtime: P,repo: R,@@ -83,16 +82,16 @@ impl<D: DocumentService,R: GithubSyncRepo,C: GithubSyncClient,- F: ForeignEntityService,+ G: GithubPullRequestService,N: NotificationIngress,P: GithubSyncRealtime,-> GithubSyncServiceImpl<D, R, C, F, N, P>+> GithubSyncServiceImpl<D, R, C, G, N, P>{/// Create a new github sync service.pub fn new(config: GithubSyncConfig,document_service: Arc<D>,- foreign_entity_service: Arc<F>,+ pull_request_service: Arc<G>,notification_ingress: N,repo: R,client: C,@@ -101,7 +100,7 @@ impl<Self {config,document_service,- foreign_entity_service,+ pull_request_service,notification_ingress,realtime,repo,@@ -147,10 +146,10 @@ impl<D: DocumentService,R: GithubSyncRepo,C: GithubSyncClient,- F: ForeignEntityService,+ G: GithubPullRequestService,N: NotificationIngress,P: GithubSyncRealtime,-> GithubSyncServiceImpl<D, R, C, F, N, P>+> GithubSyncServiceImpl<D, R, C, G, N, P>{/// Extract PR metadata and generate an installation access token./// Returns `None` if any required field is missing or token generation fails.@@ -478,171 +477,57 @@ impl<Some((pull_request, upserts))}- /// Create or refresh foreign entity rows from already-enriched pull request metadata.+ /// Create or refresh one foreign entity record per installation source from already-enriched+ /// pull request metadata.#[tracing::instrument(skip(self, pull_request, stored_for_sources))]async fn upsert_enriched_pull_request_foreign_entities(&self,pull_request: EnrichedGithubPullRequest,stored_for_sources: &[GithubAppInstallationSource],) -> Vec<PullRequestForeignEntityUpsert> {- let existing = match self- .foreign_entity_service- .get_foreign_entities_by_foreign_entity_id(- &pull_request.github_key,- Some(GITHUB_PULL_REQUEST_FOREIGN_ENTITY_SOURCE),- )- .await- {- Ok(existing) => existing,- Err(error) => {- tracing::error!(error=?error, "failed to fetch existing PR foreign entities");- return Vec::new();- }- };-let mut upserts = Vec::new();let mut seen_sources = HashSet::new();for source in stored_for_sources {- let stored_for_id = source.source_id();- let stored_for_auth_entity = source.source_type().to_string();- if !seen_sources.insert((stored_for_id.clone(), stored_for_auth_entity.clone())) {+ let stored_for = SourceId::new(source.source_id(), source.source_type());+ if !seen_sources.insert(stored_for.clone()) {continue;}- let existing_entity = existing.iter().find(|entity| {- entity.stored_for_id.as_str() == stored_for_id.as_str()- && entity.stored_for_auth_entity.as_str() == stored_for_auth_entity.as_str()- });- let previous_status = existing_entity- .and_then(|entity| Self::pull_request_status_from_metadata(&entity.metadata));- let existing_metadata = existing_entity- .map(|entity| &entity.metadata)- .or_else(|| existing.first().map(|entity| &entity.metadata));- let metadata = match pull_request.foreign_entity_metadata(existing_metadata) {- Ok(metadata) => metadata,+ let upserted = match self+ .pull_request_service+ .upsert_pull_request(UpsertGithubPullRequest {+ pull_request: pull_request.clone(),+ stored_for,+ })+ .await+ {+ Ok(upserted) => upserted,Err(error) => {- tracing::error!(error=?error, "failed to serialize PR foreign entity metadata");- return Vec::new();+ tracing::error!(+ error=?error,+ source_id=%source.source_id(),+ source_type=%source.source_type(),+ "failed to store PR foreign entity"+ );+ continue;}};- let participant_github_user_ids =- Self::participant_github_user_ids_from_metadata(&metadata);-- let foreign_entity = if let Some(entity) = existing_entity {- self.patch_pull_request_foreign_entity(- entity,- metadata,- &pull_request.github_key,- &stored_for_id,- &stored_for_auth_entity,- )- .await- } else {- self.create_pull_request_foreign_entity(- &pull_request.github_key,- metadata,- &stored_for_id,- &stored_for_auth_entity,- )- .await- };- let Some(foreign_entity) = foreign_entity else {- continue;- };-- self.publish_pull_request(source, &foreign_entity).await;+ self.publish_pull_request(source, &upserted.foreign_entity)+ .await;upserts.push(PullRequestForeignEntityUpsert {source: source.clone(),- foreign_entity_id: foreign_entity.id,- previous_status,+ foreign_entity_id: upserted.foreign_entity.id,+ previous_status: upserted.previous_status,status: pull_request.status,- participant_github_user_ids,+ participant_github_user_ids: upserted.participant_github_user_ids,});}upserts}- async fn patch_pull_request_foreign_entity(- &self,- entity: &ForeignEntity,- metadata: serde_json::Value,- github_key: &str,- stored_for_id: &str,- stored_for_auth_entity: &str,- ) -> Option<ForeignEntity> {- self.foreign_entity_service- .patch_foreign_entity(- entity.id,- PatchForeignEntity {- metadata: Some(metadata),- ..PatchForeignEntity::default()- },- )- .await- .inspect_err(|error| {- tracing::error!(- error=?error,- foreign_entity_id=%github_key,- stored_for_id=%stored_for_id,- stored_for_auth_entity=%stored_for_auth_entity,- "failed to patch PR foreign entity"- );- })- .ok()- }-- async fn create_pull_request_foreign_entity(- &self,- github_key: &str,- metadata: serde_json::Value,- stored_for_id: &str,- stored_for_auth_entity: &str,- ) -> Option<ForeignEntity> {- self.foreign_entity_service- .create_foreign_entity(CreateForeignEntity {- foreign_entity_id: github_key.to_string(),- foreign_entity_source: GITHUB_PULL_REQUEST_FOREIGN_ENTITY_SOURCE.to_string(),- metadata,- stored_for_id: stored_for_id.to_string(),- stored_for_auth_entity: stored_for_auth_entity.to_string(),- })- .await- .inspect_err(|error| {- tracing::error!(- error=?error,- foreign_entity_id=%github_key,- stored_for_id=%stored_for_id,- stored_for_auth_entity=%stored_for_auth_entity,- "failed to create PR foreign entity"- );- })- .ok()- }-- fn pull_request_status_from_metadata(- metadata: &serde_json::Value,- ) -> Option<GithubPullRequestStatus> {- metadata- .get("status")- .and_then(|status| serde_json::from_value(status.clone()).ok())- }-- fn participant_github_user_ids_from_metadata(metadata: &serde_json::Value) -> Vec<String> {- let Some(ids) = metadata- .get("participantGithubUserIds")- .and_then(|value| value.as_array())- else {- return Vec::new();- };-- ids.iter()- .filter_map(|value| value.as_str().map(str::to_string))- .collect()- }-/// Extract both legacy `MACRO-{short_uuid}` IDs and team-scoped/// `{team_slug}-{team_task_id}` references from text.#[tracing::instrument(skip(self, event, text))]@@ -873,10 +758,10 @@ impl<D: DocumentService,R: GithubSyncRepo,C: GithubSyncClient,- F: ForeignEntityService,+ G: GithubPullRequestService,N: NotificationIngress,P: GithubSyncRealtime,-> GithubSyncService for GithubSyncServiceImpl<D, R, C, F, N, P>+> GithubSyncService for GithubSyncServiceImpl<D, R, C, G, N, P>{#[tracing::instrument(skip(self, body), err)]async fn validate_webhook_event(diff --git a/crates/github/src/domain/service/sync/handle_comment.rs b/crates/github/src/domain/service/sync/handle_comment.rsindex 19b64c3cfa..3992c11727 100644--- a/crates/github/src/domain/service/sync/handle_comment.rs+++ b/crates/github/src/domain/service/sync/handle_comment.rs@@ -5,7 +5,7 @@ use crate::domain::{ports::{GithubSyncClient, GithubSyncRealtime, GithubSyncRepo},};use documents::domain::ports::DocumentService;-use foreign_entity::domain::ports::ForeignEntityService;+use github_pull_requests::domain::ports::GithubPullRequestService;use notification::domain::service::NotificationIngress;use super::GithubSyncServiceImpl;@@ -14,10 +14,10 @@ impl<D: DocumentService,R: GithubSyncRepo,C: GithubSyncClient,- F: ForeignEntityService,+ G: GithubPullRequestService,N: NotificationIngress,P: GithubSyncRealtime,-> GithubSyncServiceImpl<D, R, C, F, N, P>+> GithubSyncServiceImpl<D, R, C, G, N, P>{/// Handle `issue_comment`, `pull_request_review`, and/// `pull_request_review_comment` events.diff --git a/crates/github/src/domain/service/sync/handle_installation.rs b/crates/github/src/domain/service/sync/handle_installation.rsindex 75089e36ff..817f4462d5 100644--- a/crates/github/src/domain/service/sync/handle_installation.rs+++ b/crates/github/src/domain/service/sync/handle_installation.rs@@ -5,7 +5,7 @@ use crate::domain::{ports::{GithubSyncClient, GithubSyncRealtime, GithubSyncRepo},};use documents::domain::ports::DocumentService;-use foreign_entity::domain::ports::ForeignEntityService;+use github_pull_requests::domain::ports::GithubPullRequestService;use notification::domain::service::NotificationIngress;use super::GithubSyncServiceImpl;@@ -14,10 +14,10 @@ impl<D: DocumentService,R: GithubSyncRepo,C: GithubSyncClient,- F: ForeignEntityService,+ G: GithubPullRequestService,N: NotificationIngress,P: GithubSyncRealtime,-> GithubSyncServiceImpl<D, R, C, F, N, P>+> GithubSyncServiceImpl<D, R, C, G, N, P>{/// Handle `installation` events with action `created`.///diff --git a/crates/github/src/domain/service/sync/handle_pr.rs b/crates/github/src/domain/service/sync/handle_pr.rsindex e034f5e59b..def70db8f6 100644--- a/crates/github/src/domain/service/sync/handle_pr.rs+++ b/crates/github/src/domain/service/sync/handle_pr.rs@@ -5,7 +5,7 @@ use crate::domain::{ports::{GithubSyncClient, GithubSyncRealtime, GithubSyncRepo},};use documents::domain::ports::DocumentService;-use foreign_entity::domain::ports::ForeignEntityService;+use github_pull_requests::domain::ports::GithubPullRequestService;use notification::domain::service::NotificationIngress;use std::collections::HashSet;@@ -15,10 +15,10 @@ impl<D: DocumentService,R: GithubSyncRepo,C: GithubSyncClient,- F: ForeignEntityService,+ G: GithubPullRequestService,N: NotificationIngress,P: GithubSyncRealtime,-> GithubSyncServiceImpl<D, R, C, F, N, P>+> GithubSyncServiceImpl<D, R, C, G, N, P>{/// Handle `pull_request` events with action `opened` or `reopened`.#[tracing::instrument(skip(self, event), err)]diff --git a/crates/github/src/domain/service/sync/notify_pr.rs b/crates/github/src/domain/service/sync/notify_pr.rsindex cf2aad8e65..7eac31cafc 100644--- a/crates/github/src/domain/service/sync/notify_pr.rs+++ b/crates/github/src/domain/service/sync/notify_pr.rs@@ -4,7 +4,7 @@ use std::collections::HashSet;use chrono::{DateTime, Utc};use documents::domain::ports::DocumentService;-use foreign_entity::domain::ports::ForeignEntityService;+use github_pull_requests::domain::ports::GithubPullRequestService;use macro_user_id::user_id::MacroUserIdStr;use model_entity::EntityType;use model_notifications::{@@ -46,10 +46,10 @@ impl<D: DocumentService,R: GithubSyncRepo,C: GithubSyncClient,- F: ForeignEntityService,+ G: GithubPullRequestService,N: NotificationIngress,P: GithubSyncRealtime,-> GithubSyncServiceImpl<D, R, C, F, N, P>+> GithubSyncServiceImpl<D, R, C, G, N, P>{pub(super) async fn notify_pr_status_transitions(&self,diff --git a/crates/github/src/domain/service/sync/notify_pr_activity.rs b/crates/github/src/domain/service/sync/notify_pr_activity.rsindex b57360b555..bfb16bd451 100644--- a/crates/github/src/domain/service/sync/notify_pr_activity.rs+++ b/crates/github/src/domain/service/sync/notify_pr_activity.rs@@ -11,7 +11,7 @@use std::collections::HashSet;use documents::domain::ports::DocumentService;-use foreign_entity::domain::ports::ForeignEntityService;+use github_pull_requests::domain::ports::GithubPullRequestService;use macro_user_id::user_id::MacroUserIdStr;use model_notifications::{GithubPrComment, GithubPrCommentKind, GithubPrMention, GithubPrMentionLocation,@@ -33,10 +33,10 @@ impl<D: DocumentService,R: GithubSyncRepo,C: GithubSyncClient,- F: ForeignEntityService,+ G: GithubPullRequestService,N: NotificationIngress,P: GithubSyncRealtime,-> GithubSyncServiceImpl<D, R, C, F, N, P>+> GithubSyncServiceImpl<D, R, C, G, N, P>{/// Notify the requested reviewer that their review was requested on a/// pull request.diff --git a/crates/github/src/domain/service/sync/notify_pr_checks.rs b/crates/github/src/domain/service/sync/notify_pr_checks.rsindex ed157f677a..726635c4d8 100644--- a/crates/github/src/domain/service/sync/notify_pr_checks.rs+++ b/crates/github/src/domain/service/sync/notify_pr_checks.rs@@ -2,7 +2,7 @@use chrono::{DateTime, Utc};use documents::domain::ports::DocumentService;-use foreign_entity::domain::ports::ForeignEntityService;+use github_pull_requests::domain::ports::GithubPullRequestService;use model_notifications::{GithubPrCheckRun, GithubPrCheckRunState};use notification::domain::service::NotificationIngress;@@ -27,10 +27,10 @@ impl<D: DocumentService,R: GithubSyncRepo,C: GithubSyncClient,- F: ForeignEntityService,+ G: GithubPullRequestService,N: NotificationIngress,P: GithubSyncRealtime,-> GithubSyncServiceImpl<D, R, C, F, N, P>+> GithubSyncServiceImpl<D, R, C, G, N, P>{/// Notify pull request participants when an associated GitHub check run finishes.///diff --git a/crates/github/src/domain/service/sync/realtime.rs b/crates/github/src/domain/service/sync/realtime.rsindex 7eb633aeaf..636b33c5d4 100644--- a/crates/github/src/domain/service/sync/realtime.rs+++ b/crates/github/src/domain/service/sync/realtime.rs@@ -1,7 +1,8 @@//! Live updates after a PR mapping has been persisted.use documents::domain::ports::DocumentService;-use foreign_entity::domain::{models::ForeignEntity, ports::ForeignEntityService};+use foreign_entity::domain::models::ForeignEntity;+use github_pull_requests::domain::ports::GithubPullRequestService;use notification::domain::service::NotificationIngress;use super::GithubSyncServiceImpl;@@ -14,10 +15,10 @@ impl<D: DocumentService,R: GithubSyncRepo,C: GithubSyncClient,- F: ForeignEntityService,+ G: GithubPullRequestService,N: NotificationIngress,P: GithubSyncRealtime,-> GithubSyncServiceImpl<D, R, C, F, N, P>+> GithubSyncServiceImpl<D, R, C, G, N, P>{pub(super) async fn publish_pull_request(&self,diff --git a/crates/github/src/domain/service/sync/test.rs b/crates/github/src/domain/service/sync/test.rsindex 9920644bb7..87e074ebf5 100644--- a/crates/github/src/domain/service/sync/test.rs+++ b/crates/github/src/domain/service/sync/test.rs@@ -3,8 +3,9 @@ use std::sync::{Arc, Mutex};use crate::domain::{models::{- EnrichedGithubPullRequest, GithubAppInstallationSource, GithubAuthenticatedUser,- GithubError, GithubInstallationAccessToken, GithubKey, GithubPullRequestCheckRun,+ EnrichedGithubPullRequest, GITHUB_PULL_REQUEST_FOREIGN_ENTITY_SOURCE,+ GithubAppInstallationSource, GithubAuthenticatedUser, GithubError,+ GithubInstallationAccessToken, GithubKey, GithubPullRequestCheckRun,GithubPullRequestComment, GithubPullRequestDetails, GithubPullRequestStatus,GithubSetupAccessToken, GithubUserInstallation, MacroTaskId, ResolvedTeamTaskReference,TeamTaskReference, ValidatedGithubWebhookEvent,@@ -32,6 +33,7 @@ use foreign_entity::domain::{},ports::{ForeignEntityListQuery, ForeignEntityService},};+use github_pull_requests::domain::service::GithubPullRequestServiceImpl;use macro_user_id::user_id::MacroUserIdStr;use model::document::{DocumentBasic, DocumentMetadata};use model_entity::Entity;@@ -962,18 +964,19 @@ fn foreign_entity_id_from_receipt(})}+#[derive(Clone)]struct StubForeignEntityService {- foreign_entities: Mutex<Vec<ForeignEntity>>,- create_calls: Mutex<Vec<CreateForeignEntity>>,- patch_calls: Mutex<Vec<(uuid::Uuid, PatchForeignEntity)>>,+ foreign_entities: Arc<Mutex<Vec<ForeignEntity>>>,+ create_calls: Arc<Mutex<Vec<CreateForeignEntity>>>,+ patch_calls: Arc<Mutex<Vec<(uuid::Uuid, PatchForeignEntity)>>>,}impl StubForeignEntityService {fn new() -> Self {Self {- foreign_entities: Mutex::new(Vec::new()),- create_calls: Mutex::new(Vec::new()),- patch_calls: Mutex::new(Vec::new()),+ foreign_entities: Arc::new(Mutex::new(Vec::new())),+ create_calls: Arc::new(Mutex::new(Vec::new())),+ patch_calls: Arc::new(Mutex::new(Vec::new())),}}@@ -1170,15 +1173,44 @@ impl NotificationIngress for StubNotificationIngress {}}-type TestGithubSyncService = GithubSyncServiceImpl<+type TestSyncServiceImpl = GithubSyncServiceImpl<StubDocumentService,StubSyncRepo,StubSyncClient,- StubForeignEntityService,+ GithubPullRequestServiceImpl<StubForeignEntityService>,StubNotificationIngress,StubRealtime,>;-type TestServiceWithForeignEntityService = (TestGithubSyncService, Arc<StubForeignEntityService>);++/// The sync service under test, with a handle on the foreign entities it stores.+struct TestGithubSyncService {+ service: TestSyncServiceImpl,+ foreign_entity_service: StubForeignEntityService,+}++impl std::ops::Deref for TestGithubSyncService {+ type Target = TestSyncServiceImpl;++ fn deref(&self) -> &Self::Target {+ &self.service+ }+}++impl std::ops::DerefMut for TestGithubSyncService {+ fn deref_mut(&mut self) -> &mut Self::Target {+ &mut self.service+ }+}++type TestServiceWithForeignEntityService = (TestGithubSyncService, StubForeignEntityService);++fn pull_request_service(+ foreign_entity_service: &StubForeignEntityService,+) -> Arc<GithubPullRequestServiceImpl<StubForeignEntityService>> {+ Arc::new(GithubPullRequestServiceImpl::new(+ foreign_entity_service.clone(),+ ))+}fn make_sync_service() -> TestGithubSyncService {make_sync_service_with_doc_service().0@@ -1193,9 +1225,9 @@ fn make_sync_service_with_repo_and_notification_ingress(notification_ingress: StubNotificationIngress,) -> TestGithubSyncService {let doc_service = Arc::new(StubDocumentService::new());- let foreign_entity_service = Arc::new(StubForeignEntityService::new());+ let foreign_entity_service = StubForeignEntityService::new();- GithubSyncServiceImpl::new(+ let service = GithubSyncServiceImpl::new(GithubSyncConfig {webhook_secret: "test-webhook-secret".to_string(),github_sync_app_url: "https://github.com/apps/test/installations/new?existing=1"@@ -1206,17 +1238,21 @@ fn make_sync_service_with_repo_and_notification_ingress(installation_state_secret: "test-installation-state-secret".to_string(),},doc_service,- foreign_entity_service,+ pull_request_service(&foreign_entity_service),notification_ingress,repo,StubSyncClient::new(),StubRealtime::default(),- )+ );+ TestGithubSyncService {+ service,+ foreign_entity_service,+ }}fn make_sync_service_with_doc_service() -> (TestGithubSyncService, Arc<StubDocumentService>) {let doc_service = Arc::new(StubDocumentService::new());- let foreign_entity_service = Arc::new(StubForeignEntityService::new());+ let foreign_entity_service = StubForeignEntityService::new();let service = GithubSyncServiceImpl::new(GithubSyncConfig {@@ -1229,13 +1265,19 @@ fn make_sync_service_with_doc_service() -> (TestGithubSyncService, Arc<StubDocuminstallation_state_secret: "test-installation-state-secret".to_string(),},doc_service.clone(),- foreign_entity_service,+ pull_request_service(&foreign_entity_service),StubNotificationIngress::new(),StubSyncRepo::new(),StubSyncClient::new(),StubRealtime::default(),);- (service, doc_service)+ (+ TestGithubSyncService {+ service,+ foreign_entity_service,+ },+ doc_service,+ )}fn make_sync_service_with_foreign_entity_service() -> TestServiceWithForeignEntityService {diff --git a/crates/github_pull_requests/Cargo.toml b/crates/github_pull_requests/Cargo.tomlnew file mode 100644index 0000000000..5798457bc1--- /dev/null+++ b/crates/github_pull_requests/Cargo.toml@@ -0,0 +1,26 @@+[package]+edition = "2024"+name = "github_pull_requests"+publish = false+version = "0.1.0"++[features]+default = ["ports", "service"]+ports = []+schema = ["dep:utoipa"]+service = ["foreign_entity/ports", "ports"]++[dependencies]+chrono = { workspace = true }+foreign_entity = { path = "../foreign_entity", default-features = false }+serde = { workspace = true }+serde_json = { workspace = true }+thiserror = { workspace = true }+tracing = { workspace = true }+utoipa = { workspace = true, optional = true }+workspace-hack = { version = "0.1", path = "../workspace-hack" }++[dev-dependencies]+entity_access = { path = "../entity_access", default-features = false, features = ["ports"] }+tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }+uuid = { workspace = true }diff --git a/crates/github_pull_requests/src/domain.rs b/crates/github_pull_requests/src/domain.rsnew file mode 100644index 0000000000..6e34618b64--- /dev/null+++ b/crates/github_pull_requests/src/domain.rs@@ -0,0 +1,9 @@+//! Domain layer: models, ports, and service implementation.++pub mod models;++#[cfg(feature = "ports")]+pub mod ports;++#[cfg(feature = "service")]+pub mod service;diff --git a/crates/github_pull_requests/src/domain/models.rs b/crates/github_pull_requests/src/domain/models.rsnew file mode 100644index 0000000000..8ca24bf8cf--- /dev/null+++ b/crates/github_pull_requests/src/domain/models.rs@@ -0,0 +1,48 @@+//! Domain models for GitHub pull request records.++#[cfg(test)]+mod test;++mod key;+mod pull_request;++use foreign_entity::domain::models::{ForeignEntity, ForeignEntityError, SourceId};++pub use key::GithubKey;+pub use pull_request::{+ EnrichGithubPullRequestsProxyRequest, EnrichGithubPullRequestsResponse,+ EnrichedGithubPullRequest, GITHUB_PULL_REQUEST_FOREIGN_ENTITY_SOURCE,+ GithubPullRequestCheckRun, GithubPullRequestComment, GithubPullRequestDetails,+ GithubPullRequestRef, GithubPullRequestStatus,+};++/// A pull request's latest data, for the record stored for one source.+#[derive(Debug, Clone)]+pub struct UpsertGithubPullRequest {+ /// The pull request's latest data. Fields it leaves out keep their stored values.+ pub pull_request: EnrichedGithubPullRequest,+ /// The user or team the record is stored for.+ pub stored_for: SourceId,+}++/// A pull request's record for one source after an upsert.+#[derive(Debug, Clone)]+pub struct UpsertedGithubPullRequest {+ /// The pull request's record for the source.+ pub foreign_entity: ForeignEntity,+ /// The status stored for the source before this upsert, when its record already existed.+ pub previous_status: Option<GithubPullRequestStatus>,+ /// Stable numeric GitHub user ids of everyone involved in the pull request, after merging.+ pub participant_github_user_ids: Vec<String>,+}++/// Errors from storing GitHub pull requests.+#[derive(Debug, thiserror::Error)]+pub enum GithubPullRequestError {+ /// The pull request could not be serialized into record metadata.+ #[error("failed to serialize pull request metadata: {0}")]+ Metadata(#[from] serde_json::Error),+ /// The foreign entity store failed.+ #[error(transparent)]+ ForeignEntity(#[from] ForeignEntityError),+}diff --git a/crates/github_pull_requests/src/domain/models/key.rs b/crates/github_pull_requests/src/domain/models/key.rsnew file mode 100644index 0000000000..1dac3bcec3--- /dev/null+++ b/crates/github_pull_requests/src/domain/models/key.rs@@ -0,0 +1,26 @@+//! The key identifying a GitHub pull request.++use std::fmt;++/// Identifies a pull request as `owner/repo/pull/number`.+#[derive(Debug, Clone)]+pub struct GithubKey(String);++impl GithubKey {+ /// Create the key for pull request `pr` in `org/repo`.+ pub fn new(org: &str, repo: &str, pr: u64) -> Self {+ Self(format!("{org}/{repo}/pull/{pr}"))+ }+}++impl fmt::Display for GithubKey {+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+ write!(f, "{}", self.0)+ }+}++impl AsRef<str> for GithubKey {+ fn as_ref(&self) -> &str {+ &self.0+ }+}diff --git a/crates/github/src/domain/models/pull_request.rs b/crates/github_pull_requests/src/domain/models/pull_request.rssimilarity index 96%rename from crates/github/src/domain/models/pull_request.rsrename to crates/github_pull_requests/src/domain/models/pull_request.rsindex e647b5af39..e85966d27f 100644--- a/crates/github/src/domain/models/pull_request.rs+++ b/crates/github_pull_requests/src/domain/models/pull_request.rs@@ -1,15 +1,15 @@-//! Domain models for GitHub pull request enrichment.+//! GitHub pull request models and the rules for merging them into stored metadata.use std::fmt;use serde::{Deserialize, Serialize};-/// Foreign entity source used for GitHub pull request metadata rows.+/// Foreign entity source used for GitHub pull request records.pub const GITHUB_PULL_REQUEST_FOREIGN_ENTITY_SOURCE: &str = "github_pull_request";/// A pull request reference that can be enriched with live GitHub data.#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]-#[cfg_attr(feature = "axum", derive(utoipa::ToSchema))]+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]#[serde(rename_all = "camelCase")]pub struct GithubPullRequestRef {/// The stored GitHub association key, in `owner/repo/pull/number` format.@@ -28,7 +28,7 @@ pub struct GithubPullRequestRef {/// The normalized lifecycle status for a GitHub pull request.#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]-#[cfg_attr(feature = "axum", derive(utoipa::ToSchema))]+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]#[serde(rename_all = "lowercase")]pub enum GithubPullRequestStatus {/// The pull request is open.@@ -91,7 +91,7 @@ where/// A comment associated with a GitHub pull request.#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]-#[cfg_attr(feature = "axum", derive(utoipa::ToSchema))]+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]#[serde(rename_all = "camelCase")]pub struct GithubPullRequestComment {/// The unique GitHub identifier for the comment or review.@@ -137,7 +137,7 @@ pub struct GithubPullRequestComment {/// A check run associated with a GitHub pull request.#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]-#[cfg_attr(feature = "axum", derive(utoipa::ToSchema))]+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]#[serde(rename_all = "camelCase")]pub struct GithubPullRequestCheckRun {/// The unique GitHub identifier for the check run.@@ -158,7 +158,7 @@ pub struct GithubPullRequestCheckRun {/// GitHub API pull request details used to enrich a pull request reference.#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]-#[cfg_attr(feature = "axum", derive(utoipa::ToSchema))]+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]pub struct GithubPullRequestDetails {/// The GitHub pull request title.pub title: String,@@ -212,7 +212,7 @@ impl GithubPullRequestDetails {/// A pull request reference enriched with live GitHub details when available.#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]-#[cfg_attr(feature = "axum", derive(utoipa::ToSchema))]+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]#[serde(rename_all = "camelCase")]pub struct EnrichedGithubPullRequest {/// The stored GitHub association key, in `owner/repo/pull/number` format.@@ -382,7 +382,7 @@ impl EnrichedGithubPullRequest {/// Request body for the authenticated pull request enrichment proxy.#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]-#[cfg_attr(feature = "axum", derive(utoipa::ToSchema))]+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]#[serde(rename_all = "camelCase")]pub struct EnrichGithubPullRequestsProxyRequest {/// The pull requests to enrich for the authenticated user.@@ -391,7 +391,7 @@ pub struct EnrichGithubPullRequestsProxyRequest {/// Response body for pull request enrichment.#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]-#[cfg_attr(feature = "axum", derive(utoipa::ToSchema))]+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]#[serde(rename_all = "camelCase")]pub struct EnrichGithubPullRequestsResponse {/// Pull requests with enrichment fields populated when GitHub data was available.diff --git a/crates/github_pull_requests/src/domain/models/test.rs b/crates/github_pull_requests/src/domain/models/test.rsnew file mode 100644index 0000000000..e7be9e3d45--- /dev/null+++ b/crates/github_pull_requests/src/domain/models/test.rs@@ -0,0 +1,578 @@+use super::*;++#[test]+fn test_github_key() {+ let key = GithubKey::new("rust-lang", "rust", 12345);+ assert_eq!(key.as_ref(), "rust-lang/rust/pull/12345");+ assert_eq!(key.to_string(), "rust-lang/rust/pull/12345");+}++fn pull_request_reference() -> GithubPullRequestRef {+ GithubPullRequestRef {+ github_key: "macro/app/pull/7".to_string(),+ owner: "macro".to_string(),+ repo: "app".to_string(),+ number: 7,+ url: "https://github.com/macro/app/pull/7".to_string(),+ display_name: "macro/app#7".to_string(),+ }+}++fn pull_request_details(+ state: &str,+ merged_at: Option<chrono::DateTime<chrono::Utc>>,+) -> GithubPullRequestDetails {+ GithubPullRequestDetails {+ title: "Add pull request enrichment".to_string(),+ state: state.to_string(),+ merged_at,+ additions: 42,+ deletions: 12,+ author_login: Some("octocat".to_string()),+ author_id: Some(583231),+ description: Some("Enrich pull requests with GitHub data".to_string()),+ comments: None,+ checks: None,+ participant_github_user_ids: None,+ }+}++fn utc_datetime(value: &str) -> chrono::DateTime<chrono::Utc> {+ chrono::DateTime::parse_from_rfc3339(value)+ .unwrap()+ .with_timezone(&chrono::Utc)+}++fn pull_request_comment() -> GithubPullRequestComment {+ GithubPullRequestComment {+ id: 101,+ body: "Looks good to me".to_string(),+ author_id: None,+ author_login: Some("octocat".to_string()),+ author_association: Some("MEMBER".to_string()),+ url: Some("https://github.com/macro/app/pull/7#issuecomment-101".to_string()),+ created_at: Some(utc_datetime("2026-05-25T18:54:21Z")),+ updated_at: Some(utc_datetime("2026-05-25T19:00:00Z")),+ source: "issue_comment".to_string(),+ in_reply_to_id: None,+ pull_request_review_id: None,+ path: None,+ line: None,+ original_line: None,+ }+}++fn pull_request_check_run() -> GithubPullRequestCheckRun {+ GithubPullRequestCheckRun {+ id: 202,+ name: "ci".to_string(),+ status: "completed".to_string(),+ conclusion: Some("success".to_string()),+ url: Some("https://github.com/macro/app/actions/runs/202".to_string()),+ started_at: Some(utc_datetime("2026-05-25T18:55:00Z")),+ completed_at: Some(utc_datetime("2026-05-25T18:59:00Z")),+ }+}++#[test]+fn pull_request_status_maps_open_pr() {+ let details = pull_request_details("open", None);++ assert_eq!(details.status(), GithubPullRequestStatus::Open);+}++#[test]+fn pull_request_status_maps_closed_unmerged_pr() {+ let details = pull_request_details("closed", None);++ assert_eq!(details.status(), GithubPullRequestStatus::Closed);+}++#[test]+fn pull_request_status_maps_closed_merged_pr() {+ let merged_at = utc_datetime("2026-05-25T18:54:21Z");+ let details = pull_request_details("closed", Some(merged_at));++ assert_eq!(details.status(), GithubPullRequestStatus::Merged);+}++#[test]+fn pull_request_details_deserializes_github_comment_count() {+ let details_json = serde_json::json!({+ "title": "Add pull request enrichment",+ "state": "open",+ "merged_at": null,+ "additions": 42,+ "deletions": 12,+ "comments": 3+ });++ let details: GithubPullRequestDetails = serde_json::from_value(details_json).unwrap();++ assert_eq!(details.comments, None);+ assert_eq!(details.checks, None);+}++#[test]+fn pull_request_proxy_request_serializes_with_only_pull_requests() {+ let reference = pull_request_reference();+ let request = EnrichGithubPullRequestsProxyRequest {+ pull_requests: vec![reference],+ };++ let request_json = serde_json::to_value(&request).unwrap();++ assert_eq!(+ request_json,+ serde_json::json!({+ "pullRequests": [+ {+ "githubKey": "macro/app/pull/7",+ "owner": "macro",+ "repo": "app",+ "number": 7,+ "url": "https://github.com/macro/app/pull/7",+ "displayName": "macro/app#7"+ }+ ]+ })+ );+ assert!(request_json.get("macroUserId").is_none());++ let decoded_request: EnrichGithubPullRequestsProxyRequest =+ serde_json::from_value(request_json).unwrap();+ assert_eq!(decoded_request, request);+}++#[test]+fn pull_request_response_serializes_with_camel_case_fields() {+ let reference = pull_request_reference();+ let response = EnrichGithubPullRequestsResponse {+ pull_requests: vec![EnrichedGithubPullRequest {+ github_key: reference.github_key,+ owner: reference.owner,+ repo: reference.repo,+ number: reference.number,+ url: reference.url,+ display_name: reference.display_name,+ name: Some("Add pull request enrichment".to_string()),+ status: Some(GithubPullRequestStatus::Merged),+ additions: Some(42),+ deletions: Some(12),+ author_login: Some("octocat".to_string()),+ author_id: Some(583231),+ description: Some("Enrich pull requests with GitHub data".to_string()),+ comments: Some(vec![pull_request_comment()]),+ checks: Some(vec![pull_request_check_run()]),+ participant_github_user_ids: None,+ }],+ };++ let response_json = serde_json::to_value(&response).unwrap();++ assert_eq!(+ response_json,+ serde_json::json!({+ "pullRequests": [+ {+ "githubKey": "macro/app/pull/7",+ "owner": "macro",+ "repo": "app",+ "number": 7,+ "url": "https://github.com/macro/app/pull/7",+ "displayName": "macro/app#7",+ "name": "Add pull request enrichment",+ "status": "merged",+ "additions": 42,+ "deletions": 12,+ "authorLogin": "octocat",+ "authorId": 583231,+ "description": "Enrich pull requests with GitHub data",+ "comments": [+ {+ "id": 101,+ "body": "Looks good to me",+ "authorLogin": "octocat",+ "authorAssociation": "MEMBER",+ "url": "https://github.com/macro/app/pull/7#issuecomment-101",+ "createdAt": "2026-05-25T18:54:21Z",+ "updatedAt": "2026-05-25T19:00:00Z",+ "source": "issue_comment"+ }+ ],+ "checks": [+ {+ "id": 202,+ "name": "ci",+ "status": "completed",+ "conclusion": "success",+ "url": "https://github.com/macro/app/actions/runs/202",+ "startedAt": "2026-05-25T18:55:00Z",+ "completedAt": "2026-05-25T18:59:00Z"+ }+ ]+ }+ ]+ })+ );++ let decoded_response: EnrichGithubPullRequestsResponse =+ serde_json::from_value(response_json).unwrap();+ assert_eq!(decoded_response, response);+}++#[test]+fn pull_request_response_deserializes_without_comments_and_checks() {+ let response_json = serde_json::json!({+ "pullRequests": [+ {+ "githubKey": "macro/app/pull/7",+ "owner": "macro",+ "repo": "app",+ "number": 7,+ "url": "https://github.com/macro/app/pull/7",+ "displayName": "macro/app#7",+ "name": "Add pull request enrichment",+ "status": "open",+ "additions": 42,+ "deletions": 12+ }+ ]+ });++ let decoded_response: EnrichGithubPullRequestsResponse =+ serde_json::from_value(response_json).unwrap();+ let pull_request = decoded_response.pull_requests.first().unwrap();++ assert_eq!(pull_request.comments, None);+ assert_eq!(pull_request.checks, None);+}++#[test]+fn pull_request_enrichment_preserves_reference_fields() {+ let reference = pull_request_reference();+ let enriched = EnrichedGithubPullRequest::from_reference(reference.clone());++ assert_eq!(enriched.github_key, reference.github_key);+ assert_eq!(enriched.owner, reference.owner);+ assert_eq!(enriched.repo, reference.repo);+ assert_eq!(enriched.number, reference.number);+ assert_eq!(enriched.url, reference.url);+ assert_eq!(enriched.display_name, reference.display_name);+ assert_eq!(enriched.name, None);+ assert_eq!(enriched.status, None);+ assert_eq!(enriched.additions, None);+ assert_eq!(enriched.deletions, None);+ assert_eq!(enriched.author_login, None);+ assert_eq!(enriched.author_id, None);+ assert_eq!(enriched.description, None);+ assert_eq!(enriched.comments, None);+ assert_eq!(enriched.checks, None);++ let enriched_json = serde_json::to_value(&enriched).unwrap();+ assert!(enriched_json.get("comments").is_none());+ assert!(enriched_json.get("checks").is_none());+ assert!(enriched_json.get("authorLogin").is_none());+ assert!(enriched_json.get("authorId").is_none());+ assert!(enriched_json.get("description").is_none());+}++#[test]+fn pull_request_enrichment_copies_details_fields() {+ let reference = pull_request_reference();+ let comments = vec![pull_request_comment()];+ let checks = vec![pull_request_check_run()];+ let details = GithubPullRequestDetails {+ title: "Add pull request enrichment".to_string(),+ state: "closed".to_string(),+ merged_at: Some(utc_datetime("2026-05-25T18:54:21Z")),+ additions: 42,+ deletions: 12,+ author_login: Some("octocat".to_string()),+ author_id: Some(583231),+ description: Some("Enrich pull requests with GitHub data".to_string()),+ comments: Some(comments.clone()),+ checks: Some(checks.clone()),+ participant_github_user_ids: Some(vec!["42".to_string(), "583231".to_string()]),+ };++ let enriched = EnrichedGithubPullRequest::from_details(reference.clone(), details);++ assert_eq!(enriched.github_key, reference.github_key);+ assert_eq!(enriched.owner, reference.owner);+ assert_eq!(enriched.repo, reference.repo);+ assert_eq!(enriched.number, reference.number);+ assert_eq!(enriched.url, reference.url);+ assert_eq!(enriched.display_name, reference.display_name);+ assert_eq!(+ enriched.name,+ Some("Add pull request enrichment".to_string())+ );+ assert_eq!(enriched.status, Some(GithubPullRequestStatus::Merged));+ assert_eq!(enriched.additions, Some(42));+ assert_eq!(enriched.deletions, Some(12));+ assert_eq!(enriched.author_login, Some("octocat".to_string()));+ assert_eq!(enriched.author_id, Some(583231));+ assert_eq!(+ enriched.description,+ Some("Enrich pull requests with GitHub data".to_string())+ );+ assert_eq!(enriched.comments, Some(comments));+ assert_eq!(enriched.checks, Some(checks));+ assert_eq!(+ enriched.participant_github_user_ids,+ Some(vec!["42".to_string(), "583231".to_string()])+ );+}++#[test]+fn pull_request_foreign_entity_metadata_serializes_enriched_pull_request() {+ assert_eq!(+ GITHUB_PULL_REQUEST_FOREIGN_ENTITY_SOURCE,+ "github_pull_request"+ );++ let reference = pull_request_reference();+ let details = GithubPullRequestDetails {+ title: "Add pull request enrichment".to_string(),+ state: "closed".to_string(),+ merged_at: Some(utc_datetime("2026-05-25T18:54:21Z")),+ additions: 42,+ deletions: 12,+ author_login: Some("octocat".to_string()),+ author_id: Some(583231),+ description: Some("Enrich pull requests with GitHub data".to_string()),+ comments: Some(vec![pull_request_comment()]),+ checks: Some(vec![pull_request_check_run()]),+ participant_github_user_ids: None,+ };+ let enriched = EnrichedGithubPullRequest::from_details(reference, details);++ let metadata = enriched.foreign_entity_metadata(None).unwrap();++ assert_eq!(+ metadata,+ serde_json::json!({+ "githubKey": "macro/app/pull/7",+ "owner": "macro",+ "repo": "app",+ "number": 7,+ "url": "https://github.com/macro/app/pull/7",+ "displayName": "macro/app#7",+ "name": "Add pull request enrichment",+ "status": "merged",+ "additions": 42,+ "deletions": 12,+ "authorLogin": "octocat",+ "authorId": 583231,+ "description": "Enrich pull requests with GitHub data",+ "comments": [+ {+ "id": 101,+ "body": "Looks good to me",+ "authorLogin": "octocat",+ "authorAssociation": "MEMBER",+ "url": "https://github.com/macro/app/pull/7#issuecomment-101",+ "createdAt": "2026-05-25T18:54:21Z",+ "updatedAt": "2026-05-25T19:00:00Z",+ "source": "issue_comment"+ }+ ],+ "checks": [+ {+ "id": 202,+ "name": "ci",+ "status": "completed",+ "conclusion": "success",+ "url": "https://github.com/macro/app/actions/runs/202",+ "startedAt": "2026-05-25T18:55:00Z",+ "completedAt": "2026-05-25T18:59:00Z"+ }+ ]+ })+ );+}++#[test]+fn pull_request_foreign_entity_metadata_preserves_existing_arrays_when_refresh_omits_them() {+ let enriched = EnrichedGithubPullRequest::from_details(+ pull_request_reference(),+ pull_request_details("open", None),+ );+ let existing_metadata = serde_json::json!({+ "comments": [+ {+ "id": 303,+ "body": "Existing comment"+ }+ ],+ "checks": [+ {+ "id": 404,+ "name": "existing-ci"+ }+ ]+ });++ let metadata = enriched+ .foreign_entity_metadata(Some(&existing_metadata))+ .unwrap();++ assert_eq!(+ metadata,+ serde_json::json!({+ "githubKey": "macro/app/pull/7",+ "owner": "macro",+ "repo": "app",+ "number": 7,+ "url": "https://github.com/macro/app/pull/7",+ "displayName": "macro/app#7",+ "name": "Add pull request enrichment",+ "status": "open",+ "additions": 42,+ "deletions": 12,+ "authorLogin": "octocat",+ "authorId": 583231,+ "description": "Enrich pull requests with GitHub data",+ "comments": [+ {+ "id": 303,+ "body": "Existing comment"+ }+ ],+ "checks": [+ {+ "id": 404,+ "name": "existing-ci"+ }+ ]+ })+ );+}++#[test]+fn pull_request_foreign_entity_metadata_keeps_fresh_arrays() {+ let comments = vec![pull_request_comment()];+ let checks = vec![pull_request_check_run()];+ let details = GithubPullRequestDetails {+ title: "Add pull request enrichment".to_string(),+ state: "open".to_string(),+ merged_at: None,+ additions: 42,+ deletions: 12,+ author_login: None,+ author_id: None,+ description: None,+ comments: Some(comments.clone()),+ checks: Some(checks.clone()),+ participant_github_user_ids: None,+ };+ let enriched = EnrichedGithubPullRequest::from_details(pull_request_reference(), details);+ let existing_metadata = serde_json::json!({+ "comments": [{ "id": 303, "body": "Existing comment" }],+ "checks": [{ "id": 404, "name": "existing-ci" }]+ });+ let fresh_comments = serde_json::to_value(&comments).unwrap();+ let fresh_checks = serde_json::to_value(&checks).unwrap();++ let metadata = enriched+ .foreign_entity_metadata(Some(&existing_metadata))+ .unwrap();++ assert_eq!(metadata.get("comments"), Some(&fresh_comments));+ assert_eq!(metadata.get("checks"), Some(&fresh_checks));+}++#[test]+fn pull_request_foreign_entity_metadata_carries_existing_author_and_description_forward() {+ let mut details = pull_request_details("open", None);+ details.author_login = None;+ details.author_id = None;+ details.description = None;+ let enriched = EnrichedGithubPullRequest::from_details(pull_request_reference(), details);+ let existing_metadata = serde_json::json!({+ "authorLogin": "octocat",+ "authorId": 583231,+ "description": "Existing description"+ });++ let metadata = enriched+ .foreign_entity_metadata(Some(&existing_metadata))+ .unwrap();++ assert_eq!(+ metadata.get("authorLogin"),+ Some(&serde_json::json!("octocat"))+ );+ assert_eq!(metadata.get("authorId"), Some(&serde_json::json!(583231)));+ assert_eq!(+ metadata.get("description"),+ Some(&serde_json::json!("Existing description"))+ );+}++#[test]+fn pull_request_foreign_entity_metadata_keeps_fresh_author_and_description() {+ let enriched = EnrichedGithubPullRequest::from_details(+ pull_request_reference(),+ pull_request_details("open", None),+ );+ let existing_metadata = serde_json::json!({+ "authorLogin": "stale-login",+ "authorId": 1,+ "description": "Stale description"+ });++ let metadata = enriched+ .foreign_entity_metadata(Some(&existing_metadata))+ .unwrap();++ assert_eq!(+ metadata.get("authorLogin"),+ Some(&serde_json::json!("octocat"))+ );+ assert_eq!(metadata.get("authorId"), Some(&serde_json::json!(583231)));+ assert_eq!(+ metadata.get("description"),+ Some(&serde_json::json!("Enrich pull requests with GitHub data"))+ );+}++#[test]+fn pull_request_foreign_entity_metadata_unions_participants_with_existing() {+ let mut details = pull_request_details("open", None);+ details.participant_github_user_ids = Some(vec!["42".to_string(), "99".to_string()]);+ let enriched = EnrichedGithubPullRequest::from_details(pull_request_reference(), details);+ let existing_metadata = serde_json::json!({+ "participantGithubUserIds": ["7", "42"]+ });++ let metadata = enriched+ .foreign_entity_metadata(Some(&existing_metadata))+ .unwrap();++ assert_eq!(+ metadata.get("participantGithubUserIds"),+ Some(&serde_json::json!(["42", "7", "99"]))+ );+}++#[test]+fn pull_request_foreign_entity_metadata_carries_existing_participants_forward() {+ let enriched = EnrichedGithubPullRequest::from_details(+ pull_request_reference(),+ pull_request_details("open", None),+ );+ let existing_metadata = serde_json::json!({+ "participantGithubUserIds": ["7"]+ });++ let metadata = enriched+ .foreign_entity_metadata(Some(&existing_metadata))+ .unwrap();++ assert_eq!(+ metadata.get("participantGithubUserIds"),+ Some(&serde_json::json!(["7"]))+ );+}diff --git a/crates/github_pull_requests/src/domain/ports.rs b/crates/github_pull_requests/src/domain/ports.rsnew file mode 100644index 0000000000..28a1e25ad1--- /dev/null+++ b/crates/github_pull_requests/src/domain/ports.rs@@ -0,0 +1,31 @@+//! Ports for storing GitHub pull requests.++use std::future::Future;++use foreign_entity::domain::models::ForeignEntity;++use super::models::{+ EnrichedGithubPullRequest, GithubPullRequestError, UpsertGithubPullRequest,+ UpsertedGithubPullRequest,+};++/// Stores GitHub pull requests as foreign entity records, one record per user or team the pull+/// request is synced for.+pub trait GithubPullRequestService: Send + Sync + 'static {+ /// Store a pull request's latest data on the record for+ /// [`UpsertGithubPullRequest::stored_for`], creating it when that source has none.+ fn upsert_pull_request(+ &self,+ upsert: UpsertGithubPullRequest,+ ) -> impl Future<Output = Result<UpsertedGithubPullRequest, GithubPullRequestError>> + Send;++ /// Store a pull request's latest data on every record already stored for it, without+ /// creating any. Returns the updated records.+ ///+ /// Attempts every record even when an individual refresh fails. Logs each failed record+ /// and returns the first error after completing the remaining refreshes.+ fn refresh_pull_request(+ &self,+ pull_request: &EnrichedGithubPullRequest,+ ) -> impl Future<Output = Result<Vec<ForeignEntity>, GithubPullRequestError>> + Send;+}diff --git a/crates/github_pull_requests/src/domain/service.rs b/crates/github_pull_requests/src/domain/service.rsnew file mode 100644index 0000000000..1070b7c710--- /dev/null+++ b/crates/github_pull_requests/src/domain/service.rs@@ -0,0 +1,173 @@+//! Service implementation for storing GitHub pull requests.++#[cfg(test)]+mod test;++use foreign_entity::domain::{+ models::{CreateForeignEntity, ForeignEntity, PatchForeignEntity},+ ports::ForeignEntityService,+};++use super::{+ models::{+ EnrichedGithubPullRequest, GITHUB_PULL_REQUEST_FOREIGN_ENTITY_SOURCE,+ GithubPullRequestError, GithubPullRequestStatus, UpsertGithubPullRequest,+ UpsertedGithubPullRequest,+ },+ ports::GithubPullRequestService,+};++/// Stores pull requests through the foreign entity service.+pub struct GithubPullRequestServiceImpl<F> {+ foreign_entity_service: F,+}++impl<F: ForeignEntityService> GithubPullRequestServiceImpl<F> {+ /// Create a service that stores pull requests through `foreign_entity_service`.+ pub fn new(foreign_entity_service: F) -> Self {+ Self {+ foreign_entity_service,+ }+ }++ async fn stored_records(+ &self,+ github_key: &str,+ ) -> Result<Vec<ForeignEntity>, GithubPullRequestError> {+ Ok(self+ .foreign_entity_service+ .get_foreign_entities_by_foreign_entity_id(+ github_key,+ Some(GITHUB_PULL_REQUEST_FOREIGN_ENTITY_SOURCE),+ )+ .await?)+ }++ async fn refresh_record(+ &self,+ pull_request: &EnrichedGithubPullRequest,+ record: &ForeignEntity,+ ) -> Result<ForeignEntity, GithubPullRequestError> {+ let metadata = pull_request.foreign_entity_metadata(Some(&record.metadata))?;+ Ok(self+ .foreign_entity_service+ .patch_foreign_entity(+ record.id,+ PatchForeignEntity {+ metadata: Some(metadata),+ ..PatchForeignEntity::default()+ },+ )+ .await?)+ }+}++impl<F: ForeignEntityService> GithubPullRequestService for GithubPullRequestServiceImpl<F> {+ #[tracing::instrument(+ err,+ skip(self, upsert),+ fields(github_key = %upsert.pull_request.github_key)+ )]+ async fn upsert_pull_request(+ &self,+ upsert: UpsertGithubPullRequest,+ ) -> Result<UpsertedGithubPullRequest, GithubPullRequestError> {+ let UpsertGithubPullRequest {+ pull_request,+ stored_for,+ } = upsert;++ let records = self.stored_records(&pull_request.github_key).await?;+ let existing = records.iter().find(|record| {+ record.stored_for_id == stored_for.id+ && record.stored_for_auth_entity == stored_for.auth_entity+ });+ let previous_status = existing.and_then(|record| status_from_metadata(&record.metadata));+ // A source's first record starts from what another source already stored.+ let existing_metadata = existing+ .or_else(|| records.first())+ .map(|record| &record.metadata);+ let metadata = pull_request.foreign_entity_metadata(existing_metadata)?;+ let participant_github_user_ids = participant_github_user_ids(&metadata);++ let foreign_entity = match existing {+ Some(record) => {+ self.foreign_entity_service+ .patch_foreign_entity(+ record.id,+ PatchForeignEntity {+ metadata: Some(metadata),+ ..PatchForeignEntity::default()+ },+ )+ .await?+ }+ None => {+ self.foreign_entity_service+ .create_foreign_entity(CreateForeignEntity {+ foreign_entity_id: pull_request.github_key.clone(),+ foreign_entity_source: GITHUB_PULL_REQUEST_FOREIGN_ENTITY_SOURCE+ .to_string(),+ metadata,+ stored_for_id: stored_for.id,+ stored_for_auth_entity: stored_for.auth_entity,+ })+ .await?+ }+ };++ Ok(UpsertedGithubPullRequest {+ foreign_entity,+ previous_status,+ participant_github_user_ids,+ })+ }++ #[tracing::instrument(+ err,+ skip(self, pull_request),+ fields(github_key = %pull_request.github_key)+ )]+ async fn refresh_pull_request(+ &self,+ pull_request: &EnrichedGithubPullRequest,+ ) -> Result<Vec<ForeignEntity>, GithubPullRequestError> {+ let mut refreshed = Vec::new();+ let mut first_error = None;+ for record in self.stored_records(&pull_request.github_key).await? {+ match self.refresh_record(pull_request, &record).await {+ Ok(record) => refreshed.push(record),+ Err(error) => {+ tracing::error!(+ error=?error,+ record_id=%record.id,+ "failed to refresh pull request record"+ );+ first_error.get_or_insert(error);+ }+ }+ }+ match first_error {+ Some(error) => Err(error),+ None => Ok(refreshed),+ }+ }+}++fn status_from_metadata(metadata: &serde_json::Value) -> Option<GithubPullRequestStatus> {+ metadata+ .get("status")+ .and_then(|status| serde_json::from_value(status.clone()).ok())+}++fn participant_github_user_ids(metadata: &serde_json::Value) -> Vec<String> {+ metadata+ .get("participantGithubUserIds")+ .and_then(|value| value.as_array())+ .map(|ids| {+ ids.iter()+ .filter_map(|value| value.as_str().map(str::to_string))+ .collect()+ })+ .unwrap_or_default()+}diff --git a/crates/github_pull_requests/src/domain/service/test.rs b/crates/github_pull_requests/src/domain/service/test.rsnew file mode 100644index 0000000000..bdbb540a7e--- /dev/null+++ b/crates/github_pull_requests/src/domain/service/test.rs@@ -0,0 +1,391 @@+use std::sync::{Arc, Mutex};++use chrono::Utc;+use entity_access::domain::models::{EntityAccessReceipt, ViewAccessLevel};+use foreign_entity::domain::{+ models::{+ CreateForeignEntity, ForeignEntity, ForeignEntityError, PatchForeignEntity, SourceId,+ },+ ports::{ForeignEntityListQuery, ForeignEntityService},+};+use uuid::Uuid;++use super::GithubPullRequestServiceImpl;+use crate::domain::{+ models::{+ EnrichedGithubPullRequest, GITHUB_PULL_REQUEST_FOREIGN_ENTITY_SOURCE,+ GithubPullRequestStatus, UpsertGithubPullRequest,+ },+ ports::GithubPullRequestService,+};++const GITHUB_KEY: &str = "macro/app/pull/7";+const USER_ID: &str = "macro|[email protected]";+const TEAM_ID: &str = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa";++#[derive(Clone, Default)]+struct StubForeignEntityService {+ state: Arc<Mutex<StubState>>,+}++#[derive(Default)]+struct StubState {+ records: Vec<ForeignEntity>,+ creates: Vec<CreateForeignEntity>,+ patches: Vec<(Uuid, PatchForeignEntity)>,+ failing_patches: Vec<Uuid>,+}++impl StubForeignEntityService {+ fn with_records(records: Vec<ForeignEntity>) -> Self {+ let service = Self::default();+ service.state.lock().unwrap().records = records;+ service+ }++ fn records(&self) -> Vec<ForeignEntity> {+ self.state.lock().unwrap().records.clone()+ }++ fn creates(&self) -> Vec<CreateForeignEntity> {+ self.state.lock().unwrap().creates.clone()+ }++ fn patches(&self) -> Vec<(Uuid, PatchForeignEntity)> {+ self.state.lock().unwrap().patches.clone()+ }+}++impl ForeignEntityService for StubForeignEntityService {+ async fn get_foreign_entity(+ &self,+ _receipt: EntityAccessReceipt<ViewAccessLevel>,+ ) -> Result<ForeignEntity, ForeignEntityError> {+ unreachable!("pull request storage does not read records by receipt")+ }++ async fn get_foreign_entity_by_id(+ &self,+ id: Uuid,+ ) -> Result<ForeignEntity, ForeignEntityError> {+ self.records()+ .into_iter()+ .find(|record| record.id == id)+ .ok_or(ForeignEntityError::NotFound(id))+ }++ async fn get_foreign_entities_by_foreign_entity_id(+ &self,+ foreign_entity_id: &str,+ foreign_entity_source: Option<&str>,+ ) -> Result<Vec<ForeignEntity>, ForeignEntityError> {+ Ok(self+ .records()+ .into_iter()+ .filter(|record| {+ record.foreign_entity_id == foreign_entity_id+ && foreign_entity_source+ .is_none_or(|source| record.foreign_entity_source == source)+ })+ .collect())+ }++ async fn get_foreign_entities_for_user(+ &self,+ _requesting_user: Option<String>,+ _source_ids: Vec<SourceId>,+ _limit: u32,+ _query: ForeignEntityListQuery,+ ) -> Result<Vec<ForeignEntity>, ForeignEntityError> {+ unreachable!("pull request storage does not list records")+ }++ async fn create_foreign_entity(+ &self,+ create: CreateForeignEntity,+ ) -> Result<ForeignEntity, ForeignEntityError> {+ let now = Utc::now();+ let record = ForeignEntity {+ id: Uuid::new_v4(),+ foreign_entity_id: create.foreign_entity_id.clone(),+ foreign_entity_source: create.foreign_entity_source.clone(),+ metadata: create.metadata.clone(),+ stored_for_id: create.stored_for_id.clone(),+ stored_for_auth_entity: create.stored_for_auth_entity.clone(),+ created_at: now,+ updated_at: now,+ };+ let mut state = self.state.lock().unwrap();+ state.creates.push(create);+ state.records.push(record.clone());+ Ok(record)+ }++ async fn delete_foreign_entity(&self, _id: Uuid) -> Result<(), ForeignEntityError> {+ unreachable!("pull request storage does not delete records")+ }++ async fn patch_foreign_entity(+ &self,+ id: Uuid,+ patch: PatchForeignEntity,+ ) -> Result<ForeignEntity, ForeignEntityError> {+ let mut state = self.state.lock().unwrap();+ state.patches.push((id, patch.clone()));+ if state.failing_patches.contains(&id) {+ return Err(ForeignEntityError::NotFound(id));+ }+ let record = state+ .records+ .iter_mut()+ .find(|record| record.id == id)+ .ok_or(ForeignEntityError::NotFound(id))?;+ if let Some(metadata) = patch.metadata {+ record.metadata = metadata;+ }+ record.updated_at = Utc::now();+ Ok(record.clone())+ }+}++fn pull_request(status: GithubPullRequestStatus) -> EnrichedGithubPullRequest {+ EnrichedGithubPullRequest {+ github_key: GITHUB_KEY.to_string(),+ owner: "macro".to_string(),+ repo: "app".to_string(),+ number: 7,+ url: "https://github.com/macro/app/pull/7".to_string(),+ display_name: "macro/app#7".to_string(),+ name: Some("Add pull request storage".to_string()),+ status: Some(status),+ additions: Some(10),+ deletions: Some(2),+ author_login: None,+ author_id: None,+ description: None,+ comments: None,+ checks: None,+ participant_github_user_ids: Some(vec!["42".to_string()]),+ }+}++fn stored_record(stored_for: &SourceId, metadata: serde_json::Value) -> ForeignEntity {+ let now = Utc::now();+ ForeignEntity {+ id: Uuid::new_v4(),+ foreign_entity_id: GITHUB_KEY.to_string(),+ foreign_entity_source: GITHUB_PULL_REQUEST_FOREIGN_ENTITY_SOURCE.to_string(),+ metadata,+ stored_for_id: stored_for.id.clone(),+ stored_for_auth_entity: stored_for.auth_entity.clone(),+ created_at: now,+ updated_at: now,+ }+}++fn team() -> SourceId {+ SourceId::new(TEAM_ID, "team")+}++fn user() -> SourceId {+ SourceId::user(USER_ID)+}++#[tokio::test]+async fn upsert_creates_the_record_for_a_source_without_one() {+ let foreign_entities = StubForeignEntityService::default();+ let service = GithubPullRequestServiceImpl::new(foreign_entities.clone());++ let upserted = service+ .upsert_pull_request(UpsertGithubPullRequest {+ pull_request: pull_request(GithubPullRequestStatus::Open),+ stored_for: team(),+ })+ .await+ .unwrap();++ let creates = foreign_entities.creates();+ assert_eq!(creates.len(), 1);+ assert_eq!(creates[0].foreign_entity_id, GITHUB_KEY);+ assert_eq!(+ creates[0].foreign_entity_source,+ GITHUB_PULL_REQUEST_FOREIGN_ENTITY_SOURCE+ );+ assert_eq!(creates[0].stored_for_id, TEAM_ID);+ assert_eq!(creates[0].stored_for_auth_entity, "team");+ assert_eq!(upserted.previous_status, None);+ assert_eq!(upserted.participant_github_user_ids, vec!["42".to_string()]);+ assert_eq!(foreign_entities.records(), vec![upserted.foreign_entity]);+}++#[tokio::test]+async fn upsert_merges_into_the_sources_own_record() {+ let team_record = stored_record(+ &team(),+ serde_json::json!({+ "status": "open",+ "comments": [{ "id": 1, "body": "kept", "source": "issue_comment" }],+ "participantGithubUserIds": ["7"],+ }),+ );+ let user_record = stored_record(&user(), serde_json::json!({ "status": "closed" }));+ let foreign_entities =+ StubForeignEntityService::with_records(vec![user_record.clone(), team_record.clone()]);+ let service = GithubPullRequestServiceImpl::new(foreign_entities.clone());++ let upserted = service+ .upsert_pull_request(UpsertGithubPullRequest {+ pull_request: pull_request(GithubPullRequestStatus::Merged),+ stored_for: team(),+ })+ .await+ .unwrap();++ assert!(foreign_entities.creates().is_empty());+ assert_eq!(upserted.foreign_entity.id, team_record.id);+ assert_eq!(+ upserted.previous_status,+ Some(GithubPullRequestStatus::Open)+ );+ assert_eq!(+ upserted.participant_github_user_ids,+ vec!["42".to_string(), "7".to_string()]+ );+ assert_eq!(upserted.foreign_entity.metadata["status"], "merged");+ assert_eq!(+ upserted.foreign_entity.metadata["comments"],+ team_record.metadata["comments"]+ );+ let patches = foreign_entities.patches();+ assert_eq!(patches.len(), 1);+ assert_eq!(patches[0].0, team_record.id);+}++#[tokio::test]+async fn a_sources_first_record_starts_from_another_sources_metadata() {+ let team_record = stored_record(+ &team(),+ serde_json::json!({+ "status": "open",+ "participantGithubUserIds": ["7"],+ }),+ );+ let foreign_entities = StubForeignEntityService::with_records(vec![team_record]);+ let service = GithubPullRequestServiceImpl::new(foreign_entities.clone());++ let upserted = service+ .upsert_pull_request(UpsertGithubPullRequest {+ pull_request: pull_request(GithubPullRequestStatus::Open),+ stored_for: user(),+ })+ .await+ .unwrap();++ assert_eq!(foreign_entities.creates().len(), 1);+ assert_eq!(upserted.foreign_entity.stored_for_id, USER_ID);+ assert_eq!(upserted.previous_status, None);+ assert_eq!(+ upserted.participant_github_user_ids,+ vec!["42".to_string(), "7".to_string()]+ );+}++#[tokio::test]+async fn refresh_updates_every_stored_record() {+ let team_record = stored_record(&team(), serde_json::json!({ "status": "open" }));+ let user_record = stored_record(&user(), serde_json::json!({ "status": "open" }));+ let foreign_entities =+ StubForeignEntityService::with_records(vec![team_record.clone(), user_record.clone()]);+ let service = GithubPullRequestServiceImpl::new(foreign_entities.clone());++ let refreshed = service+ .refresh_pull_request(&pull_request(GithubPullRequestStatus::Closed))+ .await+ .unwrap();++ assert_eq!(+ refreshed.iter().map(|record| record.id).collect::<Vec<_>>(),+ vec![team_record.id, user_record.id]+ );+ assert!(+ refreshed+ .iter()+ .all(|record| record.metadata["status"] == "closed")+ );+ assert!(foreign_entities.creates().is_empty());+}++#[tokio::test]+async fn refresh_without_stored_records_does_nothing() {+ let foreign_entities = StubForeignEntityService::default();+ let service = GithubPullRequestServiceImpl::new(foreign_entities.clone());++ let refreshed = service+ .refresh_pull_request(&pull_request(GithubPullRequestStatus::Open))+ .await+ .unwrap();++ assert!(refreshed.is_empty());+ assert!(foreign_entities.patches().is_empty());+}++#[tokio::test]+async fn refresh_attempts_later_records_after_a_patch_failure() {+ let team_record = stored_record(&team(), serde_json::json!({ "status": "open" }));+ let user_record = stored_record(&user(), serde_json::json!({ "status": "open" }));+ let foreign_entities =+ StubForeignEntityService::with_records(vec![team_record.clone(), user_record.clone()]);+ foreign_entities.state.lock().unwrap().failing_patches = vec![team_record.id];+ let service = GithubPullRequestServiceImpl::new(foreign_entities.clone());++ let result = service+ .refresh_pull_request(&pull_request(GithubPullRequestStatus::Closed))+ .await;++ assert!(matches!(+ result,+ Err(crate::domain::models::GithubPullRequestError::ForeignEntity(+ ForeignEntityError::NotFound(id)+ )) if id == team_record.id+ ));+ assert_eq!(+ foreign_entities+ .patches()+ .iter()+ .map(|(id, _)| *id)+ .collect::<Vec<_>>(),+ vec![team_record.id, user_record.id]+ );+ let records = foreign_entities.records();+ assert_eq!(records[0].metadata["status"], "open");+ assert_eq!(records[1].metadata["status"], "closed");+ assert!(foreign_entities.creates().is_empty());+}++#[tokio::test]+async fn refresh_attempts_every_record_when_all_patches_fail() {+ let records = vec![+ stored_record(&team(), serde_json::json!({ "status": "open" })),+ stored_record(&user(), serde_json::json!({ "status": "open" })),+ ];+ let ids = records.iter().map(|record| record.id).collect::<Vec<_>>();+ let foreign_entities = StubForeignEntityService::with_records(records.clone());+ foreign_entities.state.lock().unwrap().failing_patches = ids.clone();+ let service = GithubPullRequestServiceImpl::new(foreign_entities.clone());++ assert!(+ service+ .refresh_pull_request(&pull_request(GithubPullRequestStatus::Closed))+ .await+ .is_err()+ );+ assert_eq!(+ foreign_entities+ .patches()+ .iter()+ .map(|(id, _)| *id)+ .collect::<Vec<_>>(),+ ids+ );+ assert_eq!(foreign_entities.records(), records);+}diff --git a/crates/github_pull_requests/src/lib.rs b/crates/github_pull_requests/src/lib.rsnew file mode 100644index 0000000000..fee5934598--- /dev/null+++ b/crates/github_pull_requests/src/lib.rs@@ -0,0 +1,12 @@+//! GitHub pull requests stored as foreign entity records.+//!+//! Owns the pull request model, the rules for merging refreshed pull request data into what is+//! already stored, and the service that writes pull request records.+//!+//! # Architecture+//!+//! - **domain**: Contains domain models, ports, and service implementation.++#![deny(missing_docs)]++pub mod domain;diff --git a/crates/soup/Cargo.toml b/crates/soup/Cargo.tomlindex 4320b10df4..1f01a62d90 100644--- a/crates/soup/Cargo.toml+++ b/crates/soup/Cargo.toml@@ -69,6 +69,7 @@ foreign_entity = { path = "../foreign_entity", default-features = false, feature"ports",] }frecency = { path = "../frecency" }+github_pull_requests = { path = "../github_pull_requests", default-features = false }indexmap = { workspace = true }item_filters = { path = "../item_filters" }macro_authorization = { path = "../macro_authorization", default-features = false, features = ["axum"], optional = true }diff --git a/crates/soup/src/domain/service/agent_metadata.rs b/crates/soup/src/domain/service/agent_metadata.rsindex b0a606a976..5dc37b140b 100644--- a/crates/soup/src/domain/service/agent_metadata.rs+++ b/crates/soup/src/domain/service/agent_metadata.rs@@ -6,12 +6,11 @@ use agent_changes::domain::{model::{AgentSessionId, PullRequestRef},ports::SessionBranchReader,};+use github_pull_requests::domain::models::GITHUB_PULL_REQUEST_FOREIGN_ENTITY_SOURCE;use models_soup::agent_session::AgentPullRequestState;use super::*;-const PULL_REQUEST_SOURCE: &str = "github_pull_request";-fn pull_request_key(url: &str) -> Option<String> {let reference = PullRequestRef::parse(url)?;Some(format!(@@ -77,7 +76,7 @@ pub(super) async fn enrich<F: ForeignEntityService>(};let filter = Expr::and(Expr::Literal(ForeignEntityLiteral::ForeignEntitySource(- PULL_REQUEST_SOURCE.to_owned(),+ GITHUB_PULL_REQUEST_FOREIGN_ENTITY_SOURCE.to_owned(),)),Arc::unwrap_or_clone(ids),);diff --git a/services/authentication_service/Cargo.toml b/services/authentication_service/Cargo.tomlindex ff22c3f26b..26c69950b7 100644--- a/services/authentication_service/Cargo.toml+++ b/services/authentication_service/Cargo.toml@@ -84,6 +84,9 @@ github = { path = "../../crates/github", default-features = false, features = ["link","outbound",] }+github_pull_requests = { path = "../../crates/github_pull_requests", default-features = false, features = [+ "service",+] }http-body-util = { workspace = true }jsonwebtoken = { workspace = true }last_online_tracker = { path = "../../crates/last_online_tracker" }diff --git a/services/authentication_service/src/api/context.rs b/services/authentication_service/src/api/context.rsindex 5e7a04a836..0d832129c1 100644--- a/services/authentication_service/src/api/context.rs+++ b/services/authentication_service/src/api/context.rs@@ -24,6 +24,7 @@ use github::domain::service::GithubLinkServiceImpl;use github::outbound::github_auth_client::GithubAuthImpl;use github::outbound::github_oauth_client::GithubOauthImpl;use github::outbound::pg_github_repo::PgGithubRepo;+use github_pull_requests::domain::service::GithubPullRequestServiceImpl;use gtm_invite::{domain::service::GtmInviteServiceImpl, outbound::pg_gtm_invite_repo::PgGtmInviteRepo,};@@ -117,7 +118,7 @@ pub(crate) type GithubLinkServiceType = GithubLinkServiceImpl<PgGithubRepo,GithubOauthImpl,GithubAuthImpl,- ForeignEntityServiceImpl<PgForeignEntityRepo>,+ GithubPullRequestServiceImpl<ForeignEntityServiceImpl<PgForeignEntityRepo>>,>;pub(crate) type EntityAccessServiceType = EntityAccessServiceImpl<PgAccessRepository>;diff --git a/services/authentication_service/src/main.rs b/services/authentication_service/src/main.rsindex feb22234a2..adec62a2f3 100644--- a/services/authentication_service/src/main.rs+++ b/services/authentication_service/src/main.rs@@ -32,6 +32,7 @@ use github::{pg_github_repo::PgGithubRepo,},};+use github_pull_requests::domain::service::GithubPullRequestServiceImpl;use loops_client::LoopsClient;use macro_auth::middleware::decode_jwt::JwtValidationArgs;use macro_authorization::{@@ -453,7 +454,7 @@ async fn main() -> anyhow::Result<()> {PgGithubRepo::new(db.clone()),GithubOauthImpl::default(),GithubAuthImpl::new(auth_client.clone(), redis_multiplexed_conn),- foreign_entity_service,+ GithubPullRequestServiceImpl::new(foreign_entity_service),GithubLinkConfig {client_id: config.github_client_id.to_string(),client_secret: config.github_client_secret.to_string(),diff --git a/services/document_storage_service/Cargo.toml b/services/document_storage_service/Cargo.tomlindex 90cdddc069..ab41e9e51a 100644--- a/services/document_storage_service/Cargo.toml+++ b/services/document_storage_service/Cargo.toml@@ -130,6 +130,9 @@ github = { path = "../../crates/github", default-features = false, features = ["outbound","sync",] }+github_pull_requests = { path = "../../crates/github_pull_requests", default-features = false, features = [+ "service",+] }graphql_soup = { path = "../../crates/graphql_soup" }graphql_initiative = { path = "../../crates/graphql_initiative" }graphql_scheduled_action = { path = "../../crates/graphql_scheduled_action" }diff --git a/services/document_storage_service/src/api/context.rs b/services/document_storage_service/src/api/context.rsindex d2e73a4096..ff03cdf01d 100644--- a/services/document_storage_service/src/api/context.rs+++ b/services/document_storage_service/src/api/context.rs@@ -91,6 +91,7 @@ use github::domain::service::GithubSyncServiceImpl;use github::outbound::connection_gateway_realtime::ConnectionGatewayGithubRealtime;use github::outbound::github_sync_client::GithubSyncClientImpl;use github::outbound::pg_github_sync_repo::PgGithubSyncRepo;+use github_pull_requests::domain::service::GithubPullRequestServiceImpl;use initiative::{domain::service::InitiativeServiceImpl, inbound::axum_router::InitiativeRouterState,outbound::PgInitiativeRepo,@@ -663,7 +664,7 @@ pub(crate) type GithubSyncServiceType = GithubSyncServiceImpl<DocumentService,PgGithubSyncRepo,GithubSyncClientImpl,- ForeignEntityServiceType,+ GithubPullRequestServiceImpl<ForeignEntityServiceType>,NotificationIngressType,ConnectionGatewayGithubRealtime,>;diff --git a/services/document_storage_service/src/main.rs b/services/document_storage_service/src/main.rsindex 27b690c991..041b18332b 100644--- a/services/document_storage_service/src/main.rs+++ b/services/document_storage_service/src/main.rs@@ -93,6 +93,7 @@ use github::domain::service::{GithubSyncConfig, GithubSyncServiceImpl};use github::outbound::connection_gateway_realtime::ConnectionGatewayGithubRealtime;use github::outbound::github_sync_client::GithubSyncClientImpl;use github::outbound::pg_github_sync_repo::PgGithubSyncRepo;+use github_pull_requests::domain::service::GithubPullRequestServiceImpl;use graphql_scheduled_action::ScheduledActionGraphqlContext;use harnesses::outbound::pg_harness_repo::PgHarnessRepo;use initiative::{@@ -574,7 +575,9 @@ async fn run() -> anyhow::Result<()> {installation_state_secret: config.github_installation_state_secret.to_string(),},document_service.clone(),- foreign_entity_service.clone(),+ Arc::new(GithubPullRequestServiceImpl::new(+ ForeignEntityServiceImpl::new(PgForeignEntityRepo::new(db.clone())),+ )),(*notification_ingress_service).clone(),PgGithubSyncRepo::new(db.clone()),GithubSyncClientImpl::default(),