Skip to content

Commit

Permalink
Merge pull request #107 from atlassian-labs/COMPASS-21110-disable-doc…
Browse files Browse the repository at this point in the history
…ument-link

COMPASS-21110: Disable Document link type
  • Loading branch information
AlekseiLitvin authored Aug 5, 2024
2 parents e1b7807 + be246b9 commit 69dbc82
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"dependencies": {
"@atlaskit/tokens": "^1.29.1",
"@atlassian/forge-graphql": "13.3.14",
"@atlassian/forge-graphql": "13.8.0",
"@forge/api": "^2.8.1",
"@forge/bridge": "^2.6.0",
"@forge/events": "^0.5.3",
Expand Down
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
2 changes: 1 addition & 1 deletion ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@atlaskit/theme": "^12.1.6",
"@atlaskit/tokens": "^1.29.1",
"@atlaskit/tooltip": "^17.5.9",
"@atlassian/forge-graphql": "13.3.14",
"@atlassian/forge-graphql": "13.8.0",
"@forge/api": "^2.8.1",
"@forge/bridge": "^2.6.0",
"escape-string-regexp": "^5.0.0",
Expand Down
1 change: 1 addition & 0 deletions ui/src/hooks/useFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const useFeatures = (): [FeaturesList, boolean, ErrorTypes | undefined] =
const [features, setFeatures] = useState<FeaturesList>({
[GitlabFeaturesEnum.SEND_STAGING_EVENTS]: false,
[GitlabFeaturesEnum.DATA_COMPONENT_TYPES]: false,
[GitlabFeaturesEnum.DISABLE_DOCUMENT_COMPONENT_LINKS]: false,
});
const [loading, setLoading] = useState<boolean>(false);
const [error, setError] = useState<ErrorTypes>();
Expand Down
8 changes: 4 additions & 4 deletions ui/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -847,10 +847,10 @@
"@babel/runtime" "^7.0.0"
"@emotion/react" "^11.7.1"

"@atlassian/forge-graphql@13.3.14":
version "13.3.14"
resolved "https://registry.npmjs.com/@atlassian/forge-graphql/-/forge-graphql-13.3.14.tgz#dd50bff6df9db473ca9e53a708fd71cafd6af0eb"
integrity sha512-VP9TGql8WHEZcQg5wFrTeN1uwH9xJu3IdKIkleLngCF54i3qvGelYYPcYg2Vn6tV8l6IcqJZ2gJ3kFtxiZk38Q==
"@atlassian/forge-graphql@13.8.0":
version "13.8.0"
resolved "https://packages.atlassian.com/api/npm/atlassian-npm/@atlassian/forge-graphql/-/forge-graphql-13.8.0.tgz#42a0a27bc3d58f7d5039edfa54ad630cb0441594"
integrity sha512-S/3KJeg2bypghg8KMRgHvr+JWWlZZs/XyNGMcQQfq3zMt3V9c7bxn0ZHTnhk/OcFEHr/J5kRm7WJlTUWbVgRBw==
dependencies:
"@forge/api" "^2.21.0"
fs "^0.0.1-security"
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@
"@babel/types" "^7.20.0"
bind-event-listener "^2.1.1"

"@atlassian/forge-graphql@13.3.14":
version "13.3.14"
resolved "https://registry.npmjs.com/@atlassian/forge-graphql/-/forge-graphql-13.3.14.tgz#dd50bff6df9db473ca9e53a708fd71cafd6af0eb"
integrity sha512-VP9TGql8WHEZcQg5wFrTeN1uwH9xJu3IdKIkleLngCF54i3qvGelYYPcYg2Vn6tV8l6IcqJZ2gJ3kFtxiZk38Q==
"@atlassian/forge-graphql@13.8.0":
version "13.8.0"
resolved "https://packages.atlassian.com/api/npm/atlassian-npm/@atlassian/forge-graphql/-/forge-graphql-13.8.0.tgz#42a0a27bc3d58f7d5039edfa54ad630cb0441594"
integrity sha512-S/3KJeg2bypghg8KMRgHvr+JWWlZZs/XyNGMcQQfq3zMt3V9c7bxn0ZHTnhk/OcFEHr/J5kRm7WJlTUWbVgRBw==
dependencies:
"@forge/api" "^2.21.0"
fs "^0.0.1-security"
Expand Down

0 comments on commit 69dbc82

Please sign in to comment.