// Helper function to construct component references import { Component, ComponentReferenceInput, } from '@atlassian/forge-graphql-types'; import { CompassYaml } from '../../../types'; import { isValidARI } from '../../../helpers/parseAri'; import { CompassRequests } from '../../../compass-requests'; import { validTypeIdValues } from './validate-config-file/models/compass-yaml-types'; import { InvalidConfigFileError } from './validate-config-file/models/errors'; import { DEFAULT_COMPONENT_TYPE_ID } from './createOrUpdateComponent/constants'; import { reportHumanReadableIds } from './metrics'; /** * Builds references input from a component yaml to * lookup components by slug. * * @param cloudId - The cloud id. * @param componentYaml - The component yaml. * @returns The component references input. */ export const buildSlugRefsFromYaml = ( cloudId: string, componentYaml: CompassYaml, ): ComponentReferenceInput[] => { const references: ComponentReferenceInput[] = []; if (componentYaml.relationships?.DEPENDS_ON) { componentYaml.relationships.DEPENDS_ON.forEach((relationship) => { if (!isValidARI(relationship)) { references.push({ slug: { cloudId, slug: relationship } }); } }); } return references; }; // Helper function to look up the component-type ARI from the type name, if one was provided in the yaml export const buildComponentTypeFromYaml = async ( cloudId: string, typeId: string, requestApi: CompassRequests, currentComponent?: Component, ): Promise => { if (!typeId) { if (currentComponent) { return currentComponent.typeId ?? currentComponent.type; } // if no current component was provided, we are creating the component so put the default here return DEFAULT_COMPONENT_TYPE_ID; } if ( Object.values(validTypeIdValues).includes(typeId as validTypeIdValues) || isValidARI(typeId) ) { return typeId; } const componentTypeResponse = await requestApi.getAllComponentTypes({ cloudId, }); if (componentTypeResponse.data?.componentTypes) { for (const componentTypeNode of componentTypeResponse.data.componentTypes) { if (componentTypeNode.name === typeId) { reportHumanReadableIds(1); return componentTypeNode.id; } } } throw new InvalidConfigFileError([ `typeId must be a Built-In component type, or a valid ARI or Name of a custom component type.`, ]); };