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

5.3 KB
Raw Blame
import { createAction, MarkdownVariant, Property } from '@activepieces/pieces-framework';
import { linearAuth } from '../../..';
import { props } from '../../common/props';
import { makeClient } from '../../common/client';
import { LinearAuth, linearGraphql } from '../../common/graphql';
import { PROJECT_TEAM_IDS_QUERY } from '../../common/queries';
import { projectMutationOutputSchema } from '../../output-schemas';

export const linearUpdateProject = createAction({
  auth: linearAuth,
  name: 'linear_update_project',
  classification: 'WRITE',
  displayName: 'Update Project',
  description: 'Change a project. Only the fields you fill in are changed.',
  audience: 'human',
  aiMetadata: {
    description: 'Updates an existing Linear project identified by its project ID, changing fields such as name, description, icon, color, start/target dates, or status, and adds the selected team to the project without removing its other teams. Use to modify a project already created. Repeating the same update is idempotent.',
    idempotent: true,
  },
  propertyGroups: [
    { key: 'target', display: 'section', label: 'Project to update', icon: 'file', props: ['team_id', 'project_id'] },
    {
      key: 'changes',
      display: 'section',
      label: 'Changes',
      icon: 'sliders',
      props: ['changes_info', 'name', 'description', 'state'],
    },
    { key: 'timeline', display: 'section', label: 'Timeline', icon: 'calendar', props: ['startDate', 'targetDate'] },
  ],
  props: {
    team_id: props.team_id(true, "Added to the project's teams if it is not one already."),
    project_id: props.project_id(),
    changes_info: Property.MarkDown({
      value: 'Empty fields keep their current value.',
      variant: MarkdownVariant.INFO,
    }),
    name: Property.ShortText({
      displayName: 'Name',
      required: false,
    }),
    description: Property.LongText({
      displayName: 'Description',
      required: false,
    }),
    icon: Property.ShortText({
      displayName: 'Icon',
      required: false,
      advanced: true,
    }),
    color: Property.ShortText({
      displayName: 'Color',
      description: 'A hex color code.',
      placeholder: '#4cb782',
      required: false,
      advanced: true,
    }),
    startDate: Property.DateTime({
      displayName: 'Start Date',
      placeholder: '2026-10-15',
      required: false,
      width: 'half',
    }),
    targetDate: Property.DateTime({
      displayName: 'Target Date',
      placeholder: '2026-12-15',
      required: false,
      width: 'half',
    }),
    state: props.project_status(false),
  },
  outputSchema: projectMutationOutputSchema,
  async run({ auth, propsValue }) {
    const client = makeClient(auth);
    const currentTeamIds = await listProjectTeamIds({
      auth,
      projectId: propsValue.project_id!,
    });
    const teamId = propsValue.team_id!;
    const input: Record<string, unknown> = {
      teamIds: currentTeamIds.includes(teamId) ? undefined : [...currentTeamIds, teamId],
      name: propsValue.name,
      description: propsValue.description,
      icon: propsValue.icon,
      color: propsValue.color,
      startDate: propsValue.startDate,
      targetDate: propsValue.targetDate,
    };
    const selectedState = propsValue['state'];
    if (selectedState != null && selectedState !== '') {
      const statuses = await client.listProjectStatuses();
      const match = statuses.find(
        (s: { type: string }) => s.type === selectedState,
      );
      if (match) {
        input['statusId'] = match.id;
      }
    }
    const query = `
      mutation UpdateProject($id: String!, $input: ProjectUpdateInput!) {
        projectUpdate(id: $id, input: $input) {
          success
          lastSyncId
          project {
            id
            name
            description
            color
            icon
            state
            startDate
            targetDate
            progress
            url
            createdAt
            updatedAt
          }
        }
      }
    `;
    const result = await client.rawRequest(query, {
      id: propsValue.project_id!,
      input,
    }) as { data: { projectUpdate: { success: boolean; lastSyncId: number; project: unknown } } };
    if (result.data.projectUpdate.success) {
      return {
        success: result.data.projectUpdate.success,
        lastSyncId: result.data.projectUpdate.lastSyncId,
        project: result.data.projectUpdate.project,
      };
    } else {
      throw new Error(`Unexpected error updating project`);
    }
  },
});

async function listProjectTeamIds({
  auth,
  projectId,
}: {
  auth: LinearAuth;
  projectId: string;
}): Promise<string[]> {
  const teamIds: string[] = [];
  let after: string | undefined;
  let hasNextPage = false;
  do {
    const data = await linearGraphql.request<{
      project: {
        id: string;
        teams: {
          pageInfo: { hasNextPage: boolean; endCursor?: string | null };
          nodes: Array<{ id: string }>;
        };
      } | null;
    }>({ auth, query: PROJECT_TEAM_IDS_QUERY, variables: { id: projectId, after } });
    const teams = data.project?.teams;
    teamIds.push(...(teams?.nodes.map((team) => team.id) ?? []));
    hasNextPage = teams?.pageInfo.hasNextPage === true && typeof teams.pageInfo.endCursor === 'string';
    after = teams?.pageInfo.endCursor ?? undefined;
  } while (hasNextPage);
  return teamIds;
}