Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

3.4 KB
Raw Blame
import { createTrigger, TriggerStrategy } from '@activepieces/pieces-framework';
import { githubAuth } from '../auth';
import { githubApiCall, githubCommon } from '../common';
import { getAuthenticatedLogin, GithubAuthValue } from '../common/auth-helpers';
import { HttpMethod } from '@activepieces/pieces-common';

interface WebhookInformation {
  webhookId: number;
  repo: string;
  owner: string;
  username: string;
}

interface MentionPayload {
  action?: string;
  comment?: {
    body?: string;
  };
}

const STORE_KEY = 'github_new_mention_trigger';

function escapeRegExp(value: string): string {
  return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

function containsMention(comment: string, username: string): boolean {
  const pattern = new RegExp(
    `(^|[^A-Za-z0-9-])@${escapeRegExp(username)}(?![A-Za-z0-9-])`,
    'i'
  );
  return pattern.test(comment);
}

export const newMentionTrigger = createTrigger({
  auth: githubAuth,
  name: 'new_mention',
  classification: 'READ',
  displayName: 'New Mention',
  description:
    'Triggers when your account is @mentioned in an issue or PR comment.',
  aiMetadata: {
    description:
      'Fires when the authenticated user is @-mentioned in a newly created or edited issue or pull-request review comment in the chosen repository. Represents someone tagging you in a comment.',
  },

  props: {
    repository: githubCommon.repositoryDropdown,
  },

  type: TriggerStrategy.WEBHOOK,

  sampleData: {
    action: 'created',
    comment: {
      body: '@octocat Please review this.',
      user: { login: 'octocat' },
      created_at: '2026-02-15T20:09:31Z',
    },
    repository: { full_name: 'octocat/Hello-World' },
    sender: { login: 'octocat' },
  },

  async onEnable(context) {
    const { repo, owner } = context.propsValue.repository!;
    const username = await getAuthenticatedLogin(
      context.auth as GithubAuthValue
    );

    const response = await githubApiCall<{ id: number }>({
      auth: context.auth,
      method: HttpMethod.POST,
      resourceUri: `/repos/${owner}/${repo}/hooks`,
      body: {
        name: 'web',
        active: true,
        events: ['issue_comment', 'pull_request_review_comment'],
        config: {
          url: context.webhookUrl,
          content_type: 'json',
        },
      },
    });

    await context.store.put<WebhookInformation>(STORE_KEY, {
      webhookId: response.body.id,
      repo,
      owner,
      username,
    });
  },

  async onDisable(context) {
    const webhook = await context.store.get<WebhookInformation>(STORE_KEY);

    if (webhook !== null && webhook !== undefined) {
      await githubApiCall({
        auth: context.auth,
        method: HttpMethod.DELETE,
        resourceUri: `/repos/${webhook.owner}/${webhook.repo}/hooks/${webhook.webhookId}`,
      });
    }
  },

  async run(context) {
    const body = context.payload.body as MentionPayload;
    if (body.action !== 'created' && body.action !== 'edited') {
      return [];
    }

    const comment = body.comment?.body ?? '';
    if (!comment) {
      return [];
    }

    const webhook = await context.store.get<WebhookInformation>(STORE_KEY);
    let username = webhook?.username;
    if (!username) {
      username = await getAuthenticatedLogin(context.auth as GithubAuthValue);
    }

    if (username && containsMention(comment, username)) {
      return [context.payload.body];
    }

    return [];
  },
});