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

4.6 KB
Raw Blame
import {
  createTrigger,
  TriggerStrategy,
  StaticPropsValue,
} from '@activepieces/pieces-framework';
import { DedupeStrategy, Polling, pollingHelper, HttpMethod } from '@activepieces/pieces-common';
import {
  azureDevOpsAuth,
  AzureDevOpsAuth,
  azureDevOpsCommon,
  FlatWorkItem,
  WiqlResponse,
} from '../common';

const props = {
  project: azureDevOpsCommon.projectDropdown,
  work_item_type: azureDevOpsCommon.workItemTypeDropdownOptional,
  state_filter: azureDevOpsCommon.stateDropdownOptional,
};

const POLLING_FIRST_RUN_LOOKBACK_DAYS = 14;

const polling: Polling<AzureDevOpsAuth, StaticPropsValue<typeof props>> = {
  strategy: DedupeStrategy.TIMEBASED,
  items: async ({ auth, propsValue, lastFetchEpochMS }) => {
    const orgUrl = azureDevOpsCommon.sanitizeOrgUrl(auth.props.organizationUrl);
    const { project, work_item_type, state_filter } = propsValue;
    const projectName = azureDevOpsCommon.narrowString(project, 'project');

    const sinceEpoch =
      lastFetchEpochMS && lastFetchEpochMS > 0
        ? lastFetchEpochMS + 1
        : Date.now() - POLLING_FIRST_RUN_LOOKBACK_DAYS * 24 * 60 * 60 * 1000;
    const sinceDate = new Date(sinceEpoch).toISOString().slice(0, 10);

    const escapedProject = azureDevOpsCommon.escapeWiqlString(projectName);
    let wiqlQuery =
      `SELECT [System.Id], [System.ChangedDate] FROM WorkItems ` +
      `WHERE [System.TeamProject] = '${escapedProject}' ` +
      `AND [System.ChangedDate] >= '${sinceDate}'`;

    if (work_item_type) {
      const escapedType = azureDevOpsCommon.escapeWiqlString(
        azureDevOpsCommon.narrowString(work_item_type, 'work_item_type'),
      );
      wiqlQuery += ` AND [System.WorkItemType] = '${escapedType}'`;
    }

    if (state_filter) {
      const escapedState = azureDevOpsCommon.escapeWiqlString(
        azureDevOpsCommon.narrowString(state_filter, 'state_filter'),
      );
      wiqlQuery += ` AND [System.State] = '${escapedState}'`;
    }

    wiqlQuery += ' ORDER BY [System.ChangedDate] DESC';

    const wiqlResponse = await azureDevOpsCommon.apiCall<WiqlResponse>({
      organizationUrl: orgUrl,
      pat: auth.props.pat,
      method: HttpMethod.POST,
      endpoint: `/${encodeURIComponent(projectName)}/_apis/wit/wiql`,
      queryParams: {
        '$top': '200',
        'api-version': '7.1',
      },
      body: { query: wiqlQuery },
    });

    if (!wiqlResponse.workItems || wiqlResponse.workItems.length === 0) {
      return [];
    }

    const ids = wiqlResponse.workItems.map((wi) => wi.id);

    const workItems = await azureDevOpsCommon.fetchWorkItemsByIds({
      organizationUrl: orgUrl,
      pat: auth.props.pat,
      ids,
    });

    return workItems.map((item) => ({
      epochMilliSeconds: item.changed_date
        ? new Date(item.changed_date).getTime()
        : Date.now(),
      data: item,
    }));
  },
};

const sampleWorkItem: FlatWorkItem = {
  id: 123,
  rev: 1,
  url: 'https://dev.azure.com/myorg/myproject/_apis/wit/workItems/123',
  title: 'Fix login page bug',
  work_item_type: 'Bug',
  state: 'Active',
  reason: 'New',
  assigned_to: 'John Doe',
  assigned_to_email: '[email protected]',
  created_date: '2024-03-15T10:30:00Z',
  created_by: 'Jane Smith',
  changed_date: '2024-03-15T14:45:00Z',
  changed_by: 'John Doe',
  area_path: 'MyProject\\Backend',
  iteration_path: 'MyProject\\Sprint 5',
  priority: 2,
  description: '<p>Users cannot log in after password reset.</p>',
  project: 'MyProject',
};

export const newUpdatedWorkItemTrigger = createTrigger({
  auth: azureDevOpsAuth,
  name: 'new_updated_work_item',
  classification: 'READ',
  displayName: 'New or Updated Work Item (Polling)',
  description:
    'Polls Azure DevOps every few minutes and fires when a work item is created or updated. Use this when your Activepieces instance is not reachable from the public internet; otherwise prefer the Instant trigger. Pick a work item type to unlock the state filter.',
  aiMetadata: {
    description: 'Fires for each Azure DevOps work item created or changed since the last poll in the chosen project, discovered by periodic polling rather than instantly. Optionally narrow to a single work item type and, once a type is set, to a specific state. Prefer the Instant trigger when the Activepieces instance is publicly reachable.',
  },
  props,
  sampleData: sampleWorkItem,
  type: TriggerStrategy.POLLING,
  async test(context) {
    return await pollingHelper.test(polling, context);
  },
  async onEnable(context) {
    await pollingHelper.onEnable(polling, context);
  },
  async onDisable(context) {
    await pollingHelper.onDisable(polling, context);
  },
  async run(context) {
    return await pollingHelper.poll(polling, context);
  },
});