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.2 KB
Raw Blame
import { TriggerStrategy, createTrigger, isNil } from '@activepieces/pieces-framework';
import { jiraCloudAuth } from '../../auth';
import { getProjects } from '../common';

import { projectTriggerOutputSchema } from '../output-schemas';
export const newProject = createTrigger({
  name: 'new_project',
  classification: 'READ',
  displayName: 'New Project',
  description: 'Triggers when a new project is created',
  aiMetadata: {
    description:
      'Fires when a new Jira project is created. Each event represents one newly created project with its full metadata (name, key, type, privacy setting, style). Polling-based; events arrive on the next poll, not instantly.',
  },
  auth: jiraCloudAuth,
  type: TriggerStrategy.POLLING,
  outputSchema: projectTriggerOutputSchema,
  props: {},
  sampleData: {
    id: '10000',
    key: 'PROJ',
    name: 'My Project',
    projectTypeKey: 'software',
    simplified: false,
    style: 'classic',
    isPrivate: false,
    self: 'https://instance.atlassian.net/rest/api/3/project/10000',
  },
  async onEnable(context) {
    if (context.isRepublish && !isNil(await context.store.get('projectIds'))) {
      return;
    }
    const projects = await getProjects(context.auth);
    const ids = projects.map((p) => p.id);
    await context.store.put('projectIds', JSON.stringify(ids));
  },
  async onDisable(context) {
    if (context.isRepublish) {
      return;
    }
    await context.store.delete('projectIds');
  },
  async run(context) {
    const projects = await getProjects(context.auth);

    if (isNil(projects) || projects.length === 0) {
      return [];
    }

    const existingIds = (await context.store.get<string>('projectIds')) ?? '[]';
    const parsedExistingIds = JSON.parse(existingIds) as string[];

    const newProjects = projects.filter(
      (p) => !parsedExistingIds.includes(p.id)
    );

    if (newProjects.length === 0) {
      return [];
    }

    const newIds = newProjects.map((p) => p.id);
    await context.store.put(
      'projectIds',
      JSON.stringify([...newIds, ...parsedExistingIds])
    );

    return newProjects;
  },
  async test(context) {
    const projects = await getProjects(context.auth);

    if (isNil(projects)) {
      return [];
    }

    return projects;
  },
});