Skip to content

Commit

Permalink
truncate gitlab project labels to 40 chars, create util method
Browse files Browse the repository at this point in the history
  • Loading branch information
ezhong2 committed Jul 19, 2024
1 parent be7d854 commit cc67abf
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/client/compass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +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';

const throwIfErrors = function throwIfSdkErrors(method: string, errors: SdkError[]) {
// Checking if any invalid config errors to report.
Expand All @@ -38,7 +39,7 @@ const throwIfErrors = function throwIfSdkErrors(method: string, errors: SdkError

export const createComponent = async (cloudId: string, project: ImportableProject): Promise<Component | never> => {
const { name, description, typeId, labels, url, ownerId } = project;
const formattedLabels = labels.map((label) => label.split(' ').join('-').toLowerCase());
const formattedLabels = formatLabels(labels);
const component = {
name,
description,
Expand Down
3 changes: 2 additions & 1 deletion src/resolvers/import-queue-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +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';

const backOffConfig: Partial<IBackOffOptions> = {
startingDelay: BACK_OFF.startingDelay,
Expand Down Expand Up @@ -67,7 +68,7 @@ resolver.define('import', async (req) => {
await createMRWithCompassYML(project, component.id, groupId);
}
} else if (hasComponent && !(isCompassFilePrOpened && isManaged)) {
const formattedLabels = labels.map((label: string) => label.split(' ').join('-').toLowerCase());
const formattedLabels = formatLabels(labels);
const component = {
name,
description,
Expand Down
3 changes: 2 additions & 1 deletion src/services/sync-component-with-file/sync-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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';

const getFileUrl = (filePath: string, event: PushEvent, branchName: string) => {
return `${event.project.web_url}/blob/${branchName}/${filePath}`;
Expand Down Expand Up @@ -56,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 formattedLabels = projectLabels.map((label) => label.split(' ').join('-').toLowerCase());
const formattedLabels = formatLabels(projectLabels);

const labels = currentComponent.labels
? [...currentComponent.labels, IMPORT_LABEL, ...formattedLabels]
Expand Down
39 changes: 39 additions & 0 deletions src/utils/format-labels.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { formatLabels } from './format-labels';

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);
});
});
7 changes: 7 additions & 0 deletions src/utils/format-labels.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// 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;
});
};

0 comments on commit cc67abf

Please sign in to comment.