Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

refactor(github_pull_requests): match fef: me against the typed participant column (#7107)

dev-rb6c80fe006299
diff --git a/.sqlx/query-75ae44e1f1bf0d633a1e1ee896cd23178455849db387d141f7486727cbe967be.json b/.sqlx/query-b67db6378b33fa74c21608013ba7227b9b81766f526770c4b65c9a72b0e5e535.json
similarity index 53%
rename from .sqlx/query-75ae44e1f1bf0d633a1e1ee896cd23178455849db387d141f7486727cbe967be.json
rename to .sqlx/query-b67db6378b33fa74c21608013ba7227b9b81766f526770c4b65c9a72b0e5e535.json
index 781591a07c..d702f45c40 100644
--- a/.sqlx/query-75ae44e1f1bf0d633a1e1ee896cd23178455849db387d141f7486727cbe967be.json
+++ b/.sqlx/query-b67db6378b33fa74c21608013ba7227b9b81766f526770c4b65c9a72b0e5e535.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n WITH source_ids AS (\n SELECT DISTINCT stored_for_id, stored_for_auth_entity\n FROM UNNEST($1::text[], $2::text[])\n AS source_rows(stored_for_id, stored_for_auth_entity)\n ),\n deduped AS (\n SELECT DISTINCT ON (fe.foreign_entity_source, fe.foreign_entity_id)\n fe.id,\n fe.foreign_entity_id,\n fe.foreign_entity_source,\n fe.metadata,\n fe.stored_for_id,\n fe.stored_for_auth_entity,\n fe.created_at,\n COALESCE(gpr.github_updated_at, fe.updated_at) AS updated_at,\n CASE $3::text\n WHEN 'created_at' THEN fe.created_at\n ELSE COALESCE(gpr.github_updated_at, fe.updated_at)\n END AS sort_at\n FROM foreign_entity fe\n LEFT JOIN github_pull_request gpr ON gpr.github_key = fe.foreign_entity_id\n WHERE fe.foreign_entity_source = $11::text\n AND EXISTS (\n SELECT 1\n FROM source_ids s\n WHERE s.stored_for_id = fe.stored_for_id\n AND s.stored_for_auth_entity = fe.stored_for_auth_entity\n )\n AND (\n $4::text IS NULL\n OR jsonb_path_match(\n jsonb_build_object(\n 'id', fe.id::text,\n 'foreignEntityId', fe.foreign_entity_id,\n 'foreignEntitySource', fe.foreign_entity_source\n ),\n ($4::text)::jsonpath\n )\n )\n AND (\n $8::text[] IS NULL\n OR (fe.metadata -> 'participantGithubUserIds') ?| $8::text[]\n )\n AND (\n $12::text IS NULL\n OR (gpr.github_key IS NOT NULL AND jsonb_path_match(\n jsonb_build_object(\n 'repositoryId', gpr.repository_id,\n 'authorId', gpr.author_github_user_id,\n 'status', gpr.status,\n 'draft', gpr.draft,\n 'requestedReviewers', to_jsonb(gpr.requested_reviewer_github_user_ids),\n 'participants', to_jsonb(gpr.participant_github_user_ids)\n ),\n ($12::text)::jsonpath\n ))\n )\n AND (\n $9::int[] IS NULL\n OR ($10::text IS NOT NULL AND (\n SELECT COALESCE(bit_or(CASE un.state\n WHEN 'unseen' THEN 1 WHEN 'seen' THEN 2 WHEN 'done' THEN 4 END), 0)\n FROM notification n\n JOIN user_notification un ON un.notification_id = n.id\n WHERE un.user_id = $10::text\n AND un.deleted_at IS NULL\n AND n.event_item_type = 'foreign_entity'\n AND n.event_item_id = fe.id::text\n ) = ANY($9::int[]))\n )\n ORDER BY\n fe.foreign_entity_source,\n fe.foreign_entity_id,\n sort_at DESC,\n fe.updated_at DESC,\n fe.id DESC\n )\n SELECT\n id as \"id!: Uuid\",\n foreign_entity_id as \"foreign_entity_id!: String\",\n foreign_entity_source as \"foreign_entity_source!: String\",\n metadata as \"metadata!: serde_json::Value\",\n stored_for_id as \"stored_for_id!: String\",\n stored_for_auth_entity as \"stored_for_auth_entity!: String\",\n created_at as \"created_at!: DateTime<Utc>\",\n updated_at as \"updated_at!: DateTime<Utc>\"\n FROM deduped\n WHERE $5::timestamptz IS NULL\n OR (sort_at, id) < ($5::timestamptz, $6::uuid)\n ORDER BY sort_at DESC, id DESC\n LIMIT $7\n ",
+ "query": "\n WITH source_ids AS (\n SELECT DISTINCT stored_for_id, stored_for_auth_entity\n FROM UNNEST($1::text[], $2::text[])\n AS source_rows(stored_for_id, stored_for_auth_entity)\n ),\n deduped AS (\n SELECT DISTINCT ON (fe.foreign_entity_source, fe.foreign_entity_id)\n fe.id,\n fe.foreign_entity_id,\n fe.foreign_entity_source,\n fe.metadata,\n fe.stored_for_id,\n fe.stored_for_auth_entity,\n fe.created_at,\n COALESCE(gpr.github_updated_at, fe.updated_at) AS updated_at,\n CASE $3::text\n WHEN 'created_at' THEN fe.created_at\n ELSE COALESCE(gpr.github_updated_at, fe.updated_at)\n END AS sort_at\n FROM foreign_entity fe\n LEFT JOIN github_pull_request gpr ON gpr.github_key = fe.foreign_entity_id\n WHERE fe.foreign_entity_source = $10::text\n AND EXISTS (\n SELECT 1\n FROM source_ids s\n WHERE s.stored_for_id = fe.stored_for_id\n AND s.stored_for_auth_entity = fe.stored_for_auth_entity\n )\n AND (\n $4::text IS NULL\n OR jsonb_path_match(\n jsonb_build_object(\n 'id', fe.id::text,\n 'foreignEntityId', fe.foreign_entity_id,\n 'foreignEntitySource', fe.foreign_entity_source\n ),\n ($4::text)::jsonpath\n )\n )\n AND (\n $11::text IS NULL\n OR (gpr.github_key IS NOT NULL AND jsonb_path_match(\n jsonb_build_object(\n 'repositoryId', gpr.repository_id,\n 'authorId', gpr.author_github_user_id,\n 'status', gpr.status,\n 'draft', gpr.draft,\n 'requestedReviewers', to_jsonb(gpr.requested_reviewer_github_user_ids),\n 'participants', to_jsonb(gpr.participant_github_user_ids)\n ),\n ($11::text)::jsonpath\n ))\n )\n AND (\n $8::int[] IS NULL\n OR ($9::text IS NOT NULL AND (\n SELECT COALESCE(bit_or(CASE un.state\n WHEN 'unseen' THEN 1 WHEN 'seen' THEN 2 WHEN 'done' THEN 4 END), 0)\n FROM notification n\n JOIN user_notification un ON un.notification_id = n.id\n WHERE un.user_id = $9::text\n AND un.deleted_at IS NULL\n AND n.event_item_type = 'foreign_entity'\n AND n.event_item_id = fe.id::text\n ) = ANY($8::int[]))\n )\n ORDER BY\n fe.foreign_entity_source,\n fe.foreign_entity_id,\n sort_at DESC,\n fe.updated_at DESC,\n fe.id DESC\n )\n SELECT\n id as \"id!: Uuid\",\n foreign_entity_id as \"foreign_entity_id!: String\",\n foreign_entity_source as \"foreign_entity_source!: String\",\n metadata as \"metadata!: serde_json::Value\",\n stored_for_id as \"stored_for_id!: String\",\n stored_for_auth_entity as \"stored_for_auth_entity!: String\",\n created_at as \"created_at!: DateTime<Utc>\",\n updated_at as \"updated_at!: DateTime<Utc>\"\n FROM deduped\n WHERE $5::timestamptz IS NULL\n OR (sort_at, id) < ($5::timestamptz, $6::uuid)\n ORDER BY sort_at DESC, id DESC\n LIMIT $7\n ",
"describe": {
"columns": [
{
@@ -53,7 +53,6 @@
"Timestamptz",
"Uuid",
"Int8",
- "TextArray",
"Int4Array",
"Text",
"Text",
@@ -71,5 +70,5 @@
null
]
},
- "hash": "75ae44e1f1bf0d633a1e1ee896cd23178455849db387d141f7486727cbe967be"
+ "hash": "b67db6378b33fa74c21608013ba7227b9b81766f526770c4b65c9a72b0e5e535"
}
diff --git a/crates/github_pull_requests/src/outbound/pg_github_pull_request_repo/listing.rs b/crates/github_pull_requests/src/outbound/pg_github_pull_request_repo/listing.rs
index 9b3d90c72b..0663059317 100644
--- a/crates/github_pull_requests/src/outbound/pg_github_pull_request_repo/listing.rs
+++ b/crates/github_pull_requests/src/outbound/pg_github_pull_request_repo/listing.rs
@@ -3,6 +3,8 @@
#[cfg(test)]
mod test;
+use std::sync::Arc;
+
use chrono::{DateTime, Utc};
use filter_ast::Expr;
use foreign_entity::domain::{
@@ -26,7 +28,6 @@ struct ListingQuery<'a> {
source_auth_entities: &'a [String],
sort_method: SimpleSortMethod,
filter_jsonpath: Option<&'a str>,
- participant_github_user_ids: Option<&'a [String]>,
/// Macro user id used to scope the per-user notification state predicates.
/// When a notification filter is requested but this is `None`, nothing matches.
notification_user_id: Option<&'a str>,
@@ -252,7 +253,6 @@ impl PgGithubPullRequestRepo {
source_auth_entities,
sort_method,
filter_jsonpath,
- participant_github_user_ids,
notification_user_id,
notification_sets,
cursor_id,
@@ -286,7 +286,7 @@ impl PgGithubPullRequestRepo {
END AS sort_at
FROM foreign_entity fe
LEFT JOIN github_pull_request gpr ON gpr.github_key = fe.foreign_entity_id
- WHERE fe.foreign_entity_source = $11::text
+ WHERE fe.foreign_entity_source = $10::text
AND EXISTS (
SELECT 1
FROM source_ids s
@@ -305,11 +305,7 @@ impl PgGithubPullRequestRepo {
)
)
AND (
- $8::text[] IS NULL
- OR (fe.metadata -> 'participantGithubUserIds') ?| $8::text[]
- )
- AND (
- $12::text IS NULL
+ $11::text IS NULL
OR (gpr.github_key IS NOT NULL AND jsonb_path_match(
jsonb_build_object(
'repositoryId', gpr.repository_id,
@@ -319,21 +315,21 @@ impl PgGithubPullRequestRepo {
'requestedReviewers', to_jsonb(gpr.requested_reviewer_github_user_ids),
'participants', to_jsonb(gpr.participant_github_user_ids)
),
- ($12::text)::jsonpath
+ ($11::text)::jsonpath
))
)
AND (
- $9::int[] IS NULL
- OR ($10::text IS NOT NULL AND (
+ $8::int[] IS NULL
+ OR ($9::text IS NOT NULL AND (
SELECT COALESCE(bit_or(CASE un.state
WHEN 'unseen' THEN 1 WHEN 'seen' THEN 2 WHEN 'done' THEN 4 END), 0)
FROM notification n
JOIN user_notification un ON un.notification_id = n.id
- WHERE un.user_id = $10::text
+ WHERE un.user_id = $9::text
AND un.deleted_at IS NULL
AND n.event_item_type = 'foreign_entity'
AND n.event_item_id = fe.id::text
- ) = ANY($9::int[]))
+ ) = ANY($8::int[]))
)
ORDER BY
fe.foreign_entity_source,
@@ -364,7 +360,6 @@ impl PgGithubPullRequestRepo {
cursor_value,
cursor_id,
limit,
- participant_github_user_ids,
notification_sets,
notification_user_id,
GITHUB_PULL_REQUEST_FOREIGN_ENTITY_SOURCE,
@@ -432,19 +427,29 @@ impl GithubPullRequestListingRepository for PgGithubPullRequestRepo {
return Ok(Vec::new());
}
- let participant_github_user_ids = if includes_me {
+ // Match `fef: me` against every GitHub identity linked to the requesting user.
+ let involves_me = if includes_me {
let Some(requesting_user) = requesting_user.as_deref() else {
return Ok(Vec::new());
};
let github_user_ids = self.github_user_ids_for_macro_user(requesting_user).await?;
- if github_user_ids.is_empty() {
- // No linked GitHub identity: the user participates in nothing.
+ let Some(involves_me) = github_user_ids
+ .into_iter()
+ .map(|id| Expr::val(GithubPullRequestLiteral::Involves(id)))
+ .reduce(Expr::or)
+ else {
return Ok(Vec::new());
- }
- Some(github_user_ids)
+ };
+ Some(involves_me)
} else {
None
};
+ let github_pull_request_filter = match (github_pull_request_filter, involves_me) {
+ (Some(filter), Some(involves_me)) => {
+ Some(Expr::and(Arc::unwrap_or_clone(filter), involves_me))
+ }
+ (filter, involves_me) => filter.map(Arc::unwrap_or_clone).or(involves_me),
+ };
let notification_sets: Option<Vec<i32>> = notification_matches.map(|table| {
(0..8)
@@ -453,13 +458,13 @@ impl GithubPullRequestListingRepository for PgGithubPullRequestRepo {
});
// Notification states are scoped to the requesting user's per-user notification row.
// Without a requesting user the predicate matches nothing, so an active notification
- // filter yields no results (consistent with the participant filter above).
+ // filter yields no results (consistent with `fef: me` above).
let notification_user_id = requesting_user.as_deref();
let (source_ids, source_auth_entities) = source_id_parts(&source_ids);
let (cursor_id, cursor_value) = query.vals();
let github_pull_request_jsonpath = github_pull_request_filter
- .as_deref()
+ .as_ref()
.map(github_pull_request_expr_jsonpath);
self.fetch_listing(ListingQuery {
@@ -467,7 +472,6 @@ impl GithubPullRequestListingRepository for PgGithubPullRequestRepo {
source_auth_entities: &source_auth_entities,
sort_method: *query.sort_method(),
filter_jsonpath: filter_jsonpath.as_deref(),
- participant_github_user_ids: participant_github_user_ids.as_deref(),
notification_user_id,
notification_sets: notification_sets.as_deref(),
cursor_id: cursor_id.copied(),
diff --git a/crates/github_pull_requests/src/outbound/pg_github_pull_request_repo/listing/test.rs b/crates/github_pull_requests/src/outbound/pg_github_pull_request_repo/listing/test.rs
index a12c0adf33..47cd864314 100644
--- a/crates/github_pull_requests/src/outbound/pg_github_pull_request_repo/listing/test.rs
+++ b/crates/github_pull_requests/src/outbound/pg_github_pull_request_repo/listing/test.rs
@@ -118,29 +118,43 @@ async fn set_timestamps(
.expect("updated foreign entity should exist")
}
+/// A pull request record, with a typed row listing `participant_github_user_ids` when given.
async fn insert_pr_with_participants(
+ pool: &PgPool,
repo: &PgForeignEntityRepo,
foreign_entity_id: &str,
stored_for_id: &str,
participant_github_user_ids: Option<&[&str]>,
) -> ForeignEntity {
- let mut metadata = json!({ "displayName": foreign_entity_id });
+ let entity = repo
+ .create_foreign_entity(
+ Uuid::now_v7(),
+ CreateForeignEntity {
+ foreign_entity_id: foreign_entity_id.into(),
+ foreign_entity_source: "github_pull_request".into(),
+ metadata: json!({ "displayName": foreign_entity_id }),
+ stored_for_id: stored_for_id.into(),
+ stored_for_auth_entity: "user".into(),
+ },
+ )
+ .await
+ .expect("pull request foreign entity should be inserted");
+
if let Some(participants) = participant_github_user_ids {
- metadata["participantGithubUserIds"] = json!(participants);
+ sqlx::query(
+ r#"
+ INSERT INTO github_pull_request (github_key, number, owner, repo, participant_github_user_ids)
+ VALUES ($1, 1, 'macro', 'app', $2)
+ "#,
+ )
+ .bind(foreign_entity_id)
+ .bind(participants.iter().map(|id| (*id).to_owned()).collect::<Vec<_>>())
+ .execute(pool)
+ .await
+ .expect("pull request row should be inserted");
}
- repo.create_foreign_entity(
- Uuid::now_v7(),
- CreateForeignEntity {
- foreign_entity_id: foreign_entity_id.into(),
- foreign_entity_source: "github_pull_request".into(),
- metadata,
- stored_for_id: stored_for_id.into(),
- stored_for_auth_entity: "user".into(),
- },
- )
- .await
- .expect("pull request foreign entity should be inserted")
+ entity
}
async fn insert_github_link(pool: &PgPool, macro_id: &str, github_user_id: &str) {
@@ -592,16 +606,17 @@ async fn list_applies_foreign_entity_filters(pool: PgPool) {
}
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
-async fn list_includes_me_filters_to_participant_metadata(pool: PgPool) {
+async fn list_includes_me_matches_the_typed_participants(pool: PgPool) {
let repo = PgForeignEntityRepo::new(pool.clone());
let listing = PgGithubPullRequestRepo::new(pool.clone());
let macro_id = "macro|[email protected]";
insert_github_link(&pool, macro_id, "42").await;
let involved =
- insert_pr_with_participants(&repo, "involved-pr", macro_id, Some(&["7", "42"])).await;
- insert_pr_with_participants(&repo, "other-pr", macro_id, Some(&["7"])).await;
- insert_pr_with_participants(&repo, "legacy-pr", macro_id, None).await;
+ insert_pr_with_participants(&pool, &repo, "involved-pr", macro_id, Some(&["7", "42"]))
+ .await;
+ insert_pr_with_participants(&pool, &repo, "other-pr", macro_id, Some(&["7"])).await;
+ insert_pr_with_participants(&pool, &repo, "legacy-pr", macro_id, None).await;
let entities = listing
.list_pull_requests(
@@ -638,10 +653,12 @@ async fn list_includes_me_matches_every_linked_github_identity(pool: PgPool) {
.await
.expect("second github link should be inserted");
- let first = insert_pr_with_participants(&repo, "first-pr", macro_id, Some(&["42"])).await;
- let second = insert_pr_with_participants(&repo, "second-pr", macro_id, Some(&["99"])).await;
- insert_pr_with_participants(&repo, "unrelated-pr", macro_id, Some(&["7"])).await;
- insert_pr_with_participants(&repo, "legacy-pr", macro_id, None).await;
+ let first =
+ insert_pr_with_participants(&pool, &repo, "first-pr", macro_id, Some(&["42"])).await;
+ let second =
+ insert_pr_with_participants(&pool, &repo, "second-pr", macro_id, Some(&["99"])).await;
+ insert_pr_with_participants(&pool, &repo, "unrelated-pr", macro_id, Some(&["7"])).await;
+ insert_pr_with_participants(&pool, &repo, "legacy-pr", macro_id, None).await;
let entities = listing
.list_pull_requests(
@@ -664,7 +681,7 @@ async fn list_includes_me_without_github_link_returns_empty(pool: PgPool) {
let repo = PgForeignEntityRepo::new(pool.clone());
let listing = PgGithubPullRequestRepo::new(pool.clone());
let macro_id = "macro|[email protected]";
- insert_pr_with_participants(&repo, "involved-pr", macro_id, Some(&["42"])).await;
+ insert_pr_with_participants(&pool, &repo, "involved-pr", macro_id, Some(&["42"])).await;
let entities = listing
.list_pull_requests(
@@ -686,7 +703,7 @@ async fn list_includes_me_without_requesting_user_returns_empty(pool: PgPool) {
let listing = PgGithubPullRequestRepo::new(pool.clone());
let macro_id = "macro|[email protected]";
insert_github_link(&pool, macro_id, "42").await;
- insert_pr_with_participants(&repo, "involved-pr", macro_id, Some(&["42"])).await;
+ insert_pr_with_participants(&pool, &repo, "involved-pr", macro_id, Some(&["42"])).await;
let entities = listing
.list_pull_requests(
@@ -709,8 +726,9 @@ async fn list_includes_me_composes_with_other_filters(pool: PgPool) {
let macro_id = "macro|[email protected]";
insert_github_link(&pool, macro_id, "42").await;
- let involved = insert_pr_with_participants(&repo, "involved-pr", macro_id, Some(&["42"])).await;
- insert_pr_with_participants(&repo, "other-pr", macro_id, Some(&["7"])).await;
+ let involved =
+ insert_pr_with_participants(&pool, &repo, "involved-pr", macro_id, Some(&["42"])).await;
+ insert_pr_with_participants(&pool, &repo, "other-pr", macro_id, Some(&["7"])).await;
insert_foreign_entity_for_source(&repo, "linear-issue", "linear_issue", macro_id, "user").await;
let filter = Some(Arc::new(Expr::and(
@@ -733,14 +751,37 @@ async fn list_includes_me_composes_with_other_filters(pool: PgPool) {
assert_eq!(entities, vec![involved]);
}
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn list_includes_me_composes_with_the_pull_request_filter(pool: PgPool) {
+ let repo = PgForeignEntityRepo::new(pool.clone());
+ let listing = PgGithubPullRequestRepo::new(pool.clone());
+ let macro_id = "macro|[email protected]";
+ insert_github_link(&pool, macro_id, "42").await;
+ let involved =
+ insert_pr_with_participants(&pool, &repo, "involved-pr", macro_id, Some(&["42"])).await;
+
+ let list = |draft: bool| {
+ listing.list_pull_requests(
+ Some(macro_id.to_string()),
+ vec![SourceId::user(macro_id)],
+ 10,
+ filter_query(includes_me_filter()),
+ Some(Arc::new(Expr::val(GithubPullRequestLiteral::Draft(draft)))),
+ )
+ };
+
+ assert_eq!(list(false).await.unwrap(), vec![involved]);
+ assert!(list(true).await.unwrap().is_empty());
+}
+
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn list_includes_me_under_not_fails_closed(pool: PgPool) {
let repo = PgForeignEntityRepo::new(pool.clone());
let listing = PgGithubPullRequestRepo::new(pool.clone());
let macro_id = "macro|[email protected]";
insert_github_link(&pool, macro_id, "42").await;
- insert_pr_with_participants(&repo, "involved-pr", macro_id, Some(&["42"])).await;
- insert_pr_with_participants(&repo, "other-pr", macro_id, Some(&["7"])).await;
+ insert_pr_with_participants(&pool, &repo, "involved-pr", macro_id, Some(&["42"])).await;
+ insert_pr_with_participants(&pool, &repo, "other-pr", macro_id, Some(&["7"])).await;
let filter = Some(Arc::new(Expr::is_not(Expr::val(
ForeignEntityLiteral::IncludesMe,
diff --git a/crates/item_filters/src/ast/foreign_entity.rs b/crates/item_filters/src/ast/foreign_entity.rs
index 0596ad5235..113e4b3333 100644
--- a/crates/item_filters/src/ast/foreign_entity.rs
+++ b/crates/item_filters/src/ast/foreign_entity.rs
@@ -16,8 +16,9 @@ pub enum ForeignEntityLiteral {
/// Filter by the external source name.
#[serde(rename = "fes")]
ForeignEntitySource(String),
- /// The requesting user takes part in the record. The GitHub pull request listing matches it
- /// against a pull request's participants; the generic foreign entity listing matches nothing.
+ /// The requesting user takes part in the record. For GitHub pull requests this is the pull
+ /// request filter's `inv` literal for the user's linked GitHub id; the generic foreign entity
+ /// listing matches nothing.
#[serde(rename = "me")]
IncludesMe,
/// An entity has a non-deleted notification in this exact state.