Skip to content

Commit

Permalink
refactor, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ezhong2 committed Aug 22, 2024
1 parent ca9c104 commit e9305f1
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 71 deletions.
2 changes: 1 addition & 1 deletion src/client/compass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { AggClientError, GraphqlGatewayError } from '../models/errors';
import { getTenantContextQuery } from './get-tenat-context-query';
import { aggQuery } from './agg';
import { getTeamsQuery } from './get-teams-query';
import { formatLabels } from '../utils/format-labels';
import { formatLabels } from '../utils/labels-utils';

const throwIfErrors = function throwIfSdkErrors(method: string, errors: SdkError[]) {
// Checking if any invalid config errors to report.
Expand Down
2 changes: 1 addition & 1 deletion src/resolvers/import-queue-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { appendLink } from '../utils/append-link';
import { ImportableProject } from '../resolverTypes';
import { sleep } from '../utils/time-utils';
import { createMRWithCompassYML } from '../services/create-mr-with-compass-yml';
import { formatLabels } from '../utils/format-labels';
import { formatLabels } from '../utils/labels-utils';

const backOffConfig: Partial<IBackOffOptions> = {
startingDelay: BACK_OFF.startingDelay,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ describe('syncComponent', () => {

await syncComponent(...syncPayload);

const expectedLabels = [...MOCK_COMPONENT_LABELS, IMPORT_LABEL, ...MOCK_GET_PROJECT_LABELS];
const expectedLabels = ['source:gitlab', 'koko', 'label', 'language:javascript', 'momo'];

expect(mockUpdateComponent).toBeCalledWith(
expect.objectContaining({
Expand Down
25 changes: 3 additions & 22 deletions src/services/sync-component-with-file/sync-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,17 @@ import { CompassLinkType, Component, ConfigFileMetadata } from '@atlassian/forge
import yaml from 'js-yaml';
import { ComponentSyncDetails, ComponentSyncPayload, PushEvent } from '../../types';
import { reportSyncError } from './report-sync-error';
import { EXTERNAL_SOURCE, IMPORT_LABEL } from '../../constants';
import { EXTERNAL_SOURCE } from '../../constants';
import { syncComponentWithFile, updateComponent } from '../../client/compass';
import { getProjectLabels } from '../get-labels';
import { getProjectById } from '../../client/gitlab';
import { hasLastSyncEvent } from '../../utils/push-event-utils';
import { formatLabels } from '../../utils/format-labels';
import { mergeLabels } from '../../utils/labels-utils';

const getFileUrl = (filePath: string, event: PushEvent, branchName: string) => {
return `${event.project.web_url}/blob/${branchName}/${filePath}`;
};

// Helper function to merge the project labels with the current labels
function mergeLabels(projectLabels: string[], currentLabels: string[], importLabel: string): string[] {
const formattedLabels = formatLabels(projectLabels);
const labels = currentLabels ? [...currentLabels, ...formattedLabels] : formattedLabels;

// Deduplicate and sort the labels for consistency
const uniqueLabels = Array.from(new Set(labels));
uniqueLabels.sort();

// If first20Labels doesn't include IMPORT_LABEL, add it to the first 20 labels
const first20Labels = uniqueLabels.slice(0, 20);
if (!first20Labels.includes(importLabel)) {
first20Labels.pop();
first20Labels.unshift(importLabel);
}

return first20Labels;
}

export const syncComponent = async (
componentSyncPayload: ComponentSyncPayload,
componentSyncDetails: ComponentSyncDetails,
Expand Down Expand Up @@ -76,7 +57,7 @@ export const syncComponent = async (
const { topics } = await getProjectById(token, event.project.id);
const projectLabels = await getProjectLabels(event.project.id, token, topics);

const labels = mergeLabels(projectLabels, currentComponent.labels, IMPORT_LABEL);
const labels = mergeLabels(projectLabels, currentComponent.labels);

await updateComponent({
currentComponent,
Expand Down
39 changes: 0 additions & 39 deletions src/utils/format-labels.test.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/utils/format-labels.ts

This file was deleted.

88 changes: 88 additions & 0 deletions src/utils/labels-utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { IMPORT_LABEL } from '../constants';
import { formatLabels, mergeLabels } from './labels-utils';

describe('formatLabels', () => {
it('should format labels correctly', () => {
const input = ['Example Label', 'AnotherExampleLabelThatIsWayTooLongAndShouldBeTruncated'];
const expectedOutput = ['example-label', 'anotherexamplelabelthatiswaytoolongandsh'];

const result = formatLabels(input);

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

it('should handle empty array', () => {
const input: string[] = [];
const expectedOutput: string[] = [];

const result = formatLabels(input);

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

it('should not alter labels shorter than 40 characters', () => {
const input = ['short', 'medium length label'];
const expectedOutput = ['short', 'medium-length-label'];

const result = formatLabels(input);

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

it('should convert spaces to hyphens and lowercase all characters', () => {
const input = ['Mixed CASE Label', 'Label With Spaces'];
const expectedOutput = ['mixed-case-label', 'label-with-spaces'];

const result = formatLabels(input);

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

describe('mergeLabels', () => {
it('should return an array with IMPORT_LABEL when both projectLabels and currentLabels are empty', () => {
const result = mergeLabels([], []);
expect(result).toEqual([IMPORT_LABEL]);
});

it('should return formatted projectLabels with IMPORT_LABEL when currentLabels is empty', () => {
const projectLabels = ['Label1', 'Label2'];
const result = mergeLabels(projectLabels, []);
expect(result).toEqual([IMPORT_LABEL, 'label1', 'label2']);
});

it('should return formatted currentLabels with IMPORT_LABEL when projectLabels is empty', () => {
const currentLabels = ['label2', 'label3'];
const result = mergeLabels([], currentLabels);
expect(result).toEqual([IMPORT_LABEL, 'label2', 'label3']);
});

it('should merge and deduplicate labels from projectLabels and currentLabels, including IMPORT_LABEL', () => {
const projectLabels = ['Label1', 'Label2'];
const currentLabels = ['label2', 'label3'];
const result = mergeLabels(projectLabels, currentLabels);
expect(result).toEqual([IMPORT_LABEL, 'label1', 'label2', 'label3']);
});

it('should return up to 20 labels including IMPORT_LABEL', () => {
const projectLabels = Array.from({ length: 15 }, (_, i) => `Label${i + 1}`);
const currentLabels = Array.from({ length: 10 }, (_, i) => `label${i + 16}`);
const result = mergeLabels(projectLabels, currentLabels);
expect(result.length).toBe(20);
expect(result).toContain(IMPORT_LABEL);
});

it('should include IMPORT_LABEL if not present in the combined labels', () => {
const projectLabels = ['Label1', 'Label2'];
const currentLabels = ['label3', 'label4'];
const result = mergeLabels(projectLabels, currentLabels);
expect(result).toContain(IMPORT_LABEL);
});

it('should not duplicate IMPORT_LABEL if it is already present', () => {
const projectLabels = ['Label1', 'Label2'];
const currentLabels = ['label3', IMPORT_LABEL];
const result = mergeLabels(projectLabels, currentLabels);
expect(result.filter((label) => label === IMPORT_LABEL).length).toBe(1);
});
});
29 changes: 29 additions & 0 deletions src/utils/labels-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { IMPORT_LABEL } from '../constants';

// Converts the topics from a GitLab project to a list of formatted labels for a Compass component.
export const formatLabels = (labels: string[]): string[] => {
return labels.map((label) => {
const transformedLabel = label.split(' ').join('-').toLowerCase();
return transformedLabel.length > 40 ? transformedLabel.slice(0, 40) : transformedLabel;
});
};

// Helper function to merge the project labels with the current labels
export function mergeLabels(projectLabels: string[], currentLabels: string[]): string[] {
const formattedLabels = formatLabels(projectLabels);
const labels = currentLabels ? [...currentLabels, ...formattedLabels] : formattedLabels;

// Deduplicate and sort the labels for consistency
const uniqueLabels = Array.from(new Set(labels));
uniqueLabels.sort();

// Get up to 20 labels
let trimmedLabels = uniqueLabels.slice(0, 20);

// If trimmedLabels doesn't include IMPORT_LABEL, add it to the trimmed labels
if (!trimmedLabels.includes(IMPORT_LABEL)) {
trimmedLabels = [IMPORT_LABEL, ...trimmedLabels].slice(0, 20);
}

return trimmedLabels;
}

0 comments on commit e9305f1

Please sign in to comment.