Skip to content

Commit

Permalink
COMPASS-21110: Disable Document link type
Browse files Browse the repository at this point in the history
  • Loading branch information
alitvin committed Jul 31, 2024
1 parent e1b7807 commit 3723fdb
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/features.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export enum GitlabFeaturesEnum {
SEND_STAGING_EVENTS = 'isSendStagingEventsEnabled',
DATA_COMPONENT_TYPES = 'isDataComponentTypesEnabled',
DISABLE_DOCUMENT_COMPONENT_LINKS = 'isDocumentComponentLinksDisabled',
}

export type FeaturesList = { [key in GitlabFeaturesEnum]: boolean };
5 changes: 5 additions & 0 deletions src/services/feature-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ export const isSendStagingEventsEnabled = (defaultValue = false): boolean => {
const isDataComponentTypesEnabled = (defaultValue = false): boolean =>
process.env.FF_DATA_COMPONENT_TYPES === 'true' || defaultValue;

const isDocumentComponentLinksDisabled = (defaultValue = false): boolean => {
return process.env.DISABLE_DOCUMENT_COMPONENT_LINKS === 'true' || defaultValue;
};

export const listFeatures = (): FeaturesList => {
return {
[GitlabFeaturesEnum.SEND_STAGING_EVENTS]: isSendStagingEventsEnabled(),
[GitlabFeaturesEnum.DATA_COMPONENT_TYPES]: isDataComponentTypesEnabled(),
[GitlabFeaturesEnum.DISABLE_DOCUMENT_COMPONENT_LINKS]: isDocumentComponentLinksDisabled(),
};
};
55 changes: 53 additions & 2 deletions src/utils/create-compass-yaml.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { mockForgeApi } from '../__tests__/helpers/forge-helper';
mockForgeApi();

import { CompassLinkType, Component, CustomField, CustomFields, CustomFieldType, Link } from '@atlassian/forge-graphql';
import { CompassYaml, ImportableProject } from '../types';
import { generateCompassYamlData } from './create-compass-yaml';
import { CompassYaml, ImportableProject, YamlLink } from '../types';
import { formatLinks, generateCompassYamlData } from './create-compass-yaml';
import { DEFAULT_CONFIG_VERSION } from '../constants';
import * as featureFlags from '../services/feature-flags';

const getMockComponent = (override: Partial<Component> = {}): Component => {
return {
Expand Down Expand Up @@ -211,3 +212,53 @@ describe('generateCompassYamlData', () => {
expect(result).toEqual(expectedYamlData);
});
});

describe('formatLinks', () => {
const inputLinks: Array<YamlLink> = [
{ name: undefined, type: CompassLinkType.Repository, url: 'url1' },
{ name: 'Link 2', type: CompassLinkType.Document, url: 'url2' },
{ name: 'Link 3', type: CompassLinkType.Dashboard, url: 'url3' },
{ name: 'Link 4', type: CompassLinkType.Document, url: 'url4' },
];

it('should filter out Document links and format remaining links correctly', async () => {
jest.spyOn(featureFlags, 'listFeatures').mockReturnValueOnce({
isDataComponentTypesEnabled: false,
isSendStagingEventsEnabled: false,
isDocumentComponentLinksDisabled: true,
});

const result = formatLinks(inputLinks);

expect(result).toHaveLength(2);
expect(result).toEqual([
{ type: CompassLinkType.Repository, url: 'url1' },
{ name: 'Link 3', type: CompassLinkType.Dashboard, url: 'url3' },
]);
});

it('should return an empty array if input links are null', async () => {
jest.spyOn(featureFlags, 'listFeatures').mockReturnValueOnce({
isDataComponentTypesEnabled: false,
isSendStagingEventsEnabled: false,
isDocumentComponentLinksDisabled: true,
});

const result = formatLinks(null);

expect(result).toEqual(null);
});

it('Should allow all types of Component Links if DISABLE_DOCUMENT_COMPONENT_LINKS FF is off', async () => {
jest.spyOn(featureFlags, 'listFeatures').mockReturnValueOnce({
isDataComponentTypesEnabled: false,
isSendStagingEventsEnabled: false,
isDocumentComponentLinksDisabled: false,
});

const result = formatLinks(inputLinks);

expect(result).toHaveLength(inputLinks.length);
expect(result).toEqual(inputLinks);
});
});
28 changes: 20 additions & 8 deletions src/utils/create-compass-yaml.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import yaml from 'js-yaml';
import { Component } from '@atlassian/forge-graphql';
import { CompassLinkType, Component } from '@atlassian/forge-graphql';
import {
CompassYaml,
ComponentLifecycleField,
Expand All @@ -10,6 +10,7 @@ import {
} from '../types';
import { formatCustomFieldsToYamlFormat } from './format-custom-fields-to-yaml';
import { DEFAULT_CONFIG_VERSION } from '../constants';
import { listFeatures } from '../services/feature-flags';

function getFields(fields?: Record<string, unknown>): YamlFields | null {
if (fields) {
Expand All @@ -27,12 +28,23 @@ function getFields(fields?: Record<string, unknown>): YamlFields | null {
return null;
}

function formatLink(link: YamlLink) {
return {
...(link.name !== undefined ? { name: link.name } : {}),
type: link.type,
url: link.url,
};
export function formatLinks(links: Array<YamlLink>) {
const featuresList = listFeatures();

return (
links
?.filter((link) => {
if (featuresList.isDocumentComponentLinksDisabled) {
return link.type !== CompassLinkType.Document;
}
return true;
})
.map((link) => ({
...(link.name !== undefined ? { name: link.name } : {}),
type: link.type,
url: link.url,
})) ?? null
);
}

export const generateCompassYamlData = (component: Component, project: ImportableProject): CompassYaml => {
Expand Down Expand Up @@ -60,7 +72,7 @@ export const generateCompassYamlData = (component: Component, project: Importabl
typeId: typeId || type,
ownerId: ownerId || selectedOwnerId,
fields: getFields(fields),
links: links?.map(formatLink) || null,
links: formatLinks(links),
relationships: {
DEPENDS_ON: relationships ? relationships.map((relationship) => relationship.nodeId) : [],
},
Expand Down

0 comments on commit 3723fdb

Please sign in to comment.