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

2.6 KB
Raw Blame
import {
  createTrigger,
  MarkdownVariant,
  Property,
  TriggerStrategy,
} from '@activepieces/pieces-framework';
import { githubApiCall, githubCommon } from '../common';
import { githubAuth } from '../auth';
import { HttpMethod } from '@activepieces/pieces-common';

export const githubRegisterTrigger = ({
  name,
  displayName,
  description,
  activityNote,
  sampleData,
}: {
  name: string;
  displayName: string;
  description: string;
  activityNote: string;
  sampleData: object;
}) =>
  createTrigger({
    auth: githubAuth,
    name: `trigger_${name}`,
    classification: 'READ',
    displayName,
    description,
    aiMetadata: {
      description:
        'Fires on activity for a configured GitHub event type on a chosen repository (one of: pull request, star, issue, push, discussion, or discussion comment, depending on which trigger variant is used). Represents a repository webhook event delivering the raw GitHub event payload.',
    },
    props: {
      activityNote: Property.MarkDown({
        value: activityNote,
        variant: MarkdownVariant.INFO,
      }),
      repository: githubCommon.repositoryDropdown,
    },
    sampleData,
    type: TriggerStrategy.WEBHOOK,
    async onEnable(context) {
      const { repo, owner } = context.propsValue.repository!;

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

      await context.store.put<WebhookInformation>(`github_${name}_trigger`, {
        webhookId: response.body.id,
        owner: owner,
        repo: repo,
      });
    },
    async onDisable(context) {
      const response = await context.store.get<WebhookInformation>(
        `github_${name}_trigger`
      );
      if (response !== null && response !== undefined) {
        await githubApiCall({
          auth: context.auth,
          method: HttpMethod.DELETE,
          resourceUri: `/repos/${response.owner}/${response.repo}/hooks/${response.webhookId}`,
        });
      }
    },
    async run(context) {
      if (isVerificationCall(context.payload.body as Record<string, unknown>)) {
        return [];
      }
      return [context.payload.body];
    },
  });

function isVerificationCall(payload: Record<string, any>) {
  return payload['zen'] !== undefined;
}

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