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
//! Issue comment, PR review, and PR review comment event handlers.
use crate::domain::{
models::{GithubError, MacroTaskId, ValidatedGithubWebhookEvent},
ports::{GithubSyncClient, GithubSyncRealtime, GithubSyncRepo},
};
use documents::domain::ports::DocumentService;
use github_pull_requests::domain::ports::GithubPullRequestService;
use notification::domain::service::NotificationIngress;
use super::GithubSyncServiceImpl;
impl<
D: DocumentService,
R: GithubSyncRepo,
C: GithubSyncClient,
G: GithubPullRequestService,
N: NotificationIngress,
P: GithubSyncRealtime,
> GithubSyncServiceImpl<D, R, C, G, N, P>
{
/// Handle `issue_comment`, `pull_request_review`, and
/// `pull_request_review_comment` events.
///
/// Extracts task IDs from the new comment/review text and deduplicates
/// against tasks already tracked in the repo for this PR.
#[tracing::instrument(skip(self, event), err)]
pub(crate) async fn handle_comment_event(
&self,
event: &ValidatedGithubWebhookEvent,
) -> Result<(), GithubError> {
// Extract task IDs from the new comment/review text only
let searchable_texts = event.extract_searchable_text();
let combined = searchable_texts.join(" ");
let comment_task_ids = self.extract_task_ids_from_text(event, &combined).await;
tracing::trace!(
new_task_id_count = comment_task_ids.len(),
task_ids = ?comment_task_ids.iter().map(|t| t.to_task_id_string()).collect::<Vec<_>>(),
"extracted task IDs from comment/review text"
);
if comment_task_ids.is_empty() {
tracing::debug!("no task IDs found in comment/review text");
return Ok(());
}
let github_key = Self::github_key(event);
// Validate comment task IDs against the document service
let resolved = self.resolve_tasks(&comment_task_ids).await;
if resolved.validated_task_ids.is_empty() {
tracing::debug!("no valid task documents found for extracted IDs");
return Ok(());
}
// Ensure PR context task IDs are tracked in the repo (handles PRs
// that existed before the tracking table was introduced).
if let Some(ref key) = github_key {
let pr_context_tasks = {
let pr_context = event.extract_pr_context_text().join(" ");
let extracted = self.extract_task_ids_from_text(event, &pr_context).await;
// Validate PR context task IDs too
self.resolve_tasks(&extracted).await.validated_task_ids
};
if !pr_context_tasks.is_empty() {
tracing::trace!(
pr_context_task_count = pr_context_tasks.len(),
"upserting PR context task IDs"
);
self.repo
.upsert_task_ids(key.clone(), &pr_context_tasks)
.await
.inspect_err(
|e| tracing::error!(error=?e, "failed to upsert PR context task IDs"),
)
.ok();
}
}
// Filter to only truly new validated task IDs using the repo
let truly_new = if let Some(ref key) = github_key {
self.repo
.filter_duplicate_tasks(key.clone(), &resolved.validated_task_ids)
.await
.inspect_err(|e| tracing::error!(error=?e, "failed to filter duplicate tasks"))
.unwrap_or_else(|_| resolved.validated_task_ids.clone())
} else {
resolved.validated_task_ids.clone()
};
tracing::trace!(
truly_new_count = truly_new.len(),
truly_new_task_ids = ?truly_new.iter().map(|t| t.to_task_id_string()).collect::<Vec<_>>(),
"filtered to truly new task IDs"
);
if !truly_new.is_empty() {
let new_task_id_set: std::collections::HashSet<&MacroTaskId> =
truly_new.iter().collect();
let new_task_links: Vec<_> = resolved
.validated_task_ids
.iter()
.zip(resolved.task_links.iter())
.filter(|(id, _)| new_task_id_set.contains(id))
.map(|(_, link)| link.clone())
.collect();
let pr_meta = self.acquire_pr_meta(event).await;
if let Some(ref meta) = pr_meta {
self.post_task_comment(meta, &new_task_links).await;
}
}
// Track only validated task IDs in the repo
if let Some(ref key) = github_key {
self.repo
.upsert_task_ids(key.clone(), &resolved.validated_task_ids)
.await
.inspect_err(|e| tracing::error!(error=?e, "failed to upsert comment task IDs"))
.ok();
}
// Only update status if the PR is open
if let Some(status) = event.task_status_for_event() {
self.update_task_statuses(event, &resolved.doc_ids, status)
.await;
} else {
tracing::trace!("no status update warranted for this event");
}
tracing::trace!("comment event handler complete");
Ok(())
}
}