From 75335fbdc6a7e845f103e493983193d4e3cefe91 Mon Sep 17 00:00:00 2001 From: vbihun Date: Tue, 30 Apr 2024 15:12:32 +0300 Subject: [PATCH] Does not delete external alias after removing compass yaml --- .../handle-push-event.test.ts | 35 ++++++++++++++++++- .../handle-push-event.ts | 4 ++- .../config-file-changes-transformer.ts | 2 +- .../find-config-file-changes.test.ts | 5 +++ src/types.ts | 1 + 5 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/entry/webtriggers/gitlab-event-handlers/handle-push-event.test.ts b/src/entry/webtriggers/gitlab-event-handlers/handle-push-event.test.ts index 3a31bed..7680997 100644 --- a/src/entry/webtriggers/gitlab-event-handlers/handle-push-event.test.ts +++ b/src/entry/webtriggers/gitlab-event-handlers/handle-push-event.test.ts @@ -3,7 +3,7 @@ import { mockAgg, mockUnlinkComponent } from '../../../__tests__/helpers/mock-ag mockAgg(); -import { ConfigFileActions } from '@atlassian/forge-graphql'; +import { ConfigFileActions, UnLinkComponentInput } from '@atlassian/forge-graphql'; import { mocked } from 'jest-mock'; import { generatePushEvent } from '../../../__tests__/helpers/gitlab-helper'; import { handlePushEvent } from './handle-push-event'; @@ -37,6 +37,16 @@ describe('Gitlab push events', () => { componentYaml: { id: 'test2' }, filePath: '/compass.yml', deduplicationId: '1', + shouldRemoveExternalAlias: true, + }, + ]; + + const mockComponentsToUnlinkWithoutExternalAliasesToRemove = [ + { + componentYaml: { id: 'test2' }, + filePath: '/compass.yml', + deduplicationId: '1', + shouldRemoveExternalAlias: false, }, ]; @@ -55,6 +65,16 @@ describe('Gitlab push events', () => { }, ]; + const mockUnlinkComponentDataWithoutExternalAliasesToRemove: UnLinkComponentInput[] = [ + { + componentId: 'test2', + cloudId: MOCK_CLOUD_ID, + filePath: `/compass.yml`, + deduplicationId: '1', + additionalExternalAliasesToRemove: [], + }, + ]; + beforeEach(() => { jest.clearAllMocks(); }); @@ -172,4 +192,17 @@ describe('Gitlab push events', () => { }); expect(removals).toBeCalledWith(mockUnlinkComponentData[0]); }); + + it('does not delete externalAlias for the current component', async () => { + getNonDefaultBranchNameMock.mockResolvedValue(event.project.default_branch); + findConfigChanges.mockResolvedValue({ + componentsToCreate: [], + componentsToUpdate: [], + componentsToUnlink: mockComponentsToUnlinkWithoutExternalAliasesToRemove, + }); + + await handlePushEvent(event, TEST_TOKEN, MOCK_CLOUD_ID); + + expect(removals).toBeCalledWith(mockUnlinkComponentDataWithoutExternalAliasesToRemove[0]); + }); }); diff --git a/src/entry/webtriggers/gitlab-event-handlers/handle-push-event.ts b/src/entry/webtriggers/gitlab-event-handlers/handle-push-event.ts index e0b8791..11d9e4d 100644 --- a/src/entry/webtriggers/gitlab-event-handlers/handle-push-event.ts +++ b/src/entry/webtriggers/gitlab-event-handlers/handle-push-event.ts @@ -61,7 +61,9 @@ export const handlePushEvent = async (event: PushEvent, groupToken: string, clou filePath: componentToUnlink.filePath, componentId: componentToUnlink.componentYaml.id, deduplicationId: componentToUnlink.deduplicationId, - additionalExternalAliasesToRemove: [{ externalId: event.project.id.toString(), externalSource: EXTERNAL_SOURCE }], + additionalExternalAliasesToRemove: componentToUnlink.shouldRemoveExternalAlias + ? [{ externalId: event.project.id.toString(), externalSource: EXTERNAL_SOURCE }] + : [], }), ); await Promise.all([...creates, ...updates, ...removals]); diff --git a/src/services/sync-component-with-file/config-file-changes-transformer.ts b/src/services/sync-component-with-file/config-file-changes-transformer.ts index d08932e..bc31c35 100644 --- a/src/services/sync-component-with-file/config-file-changes-transformer.ts +++ b/src/services/sync-component-with-file/config-file-changes-transformer.ts @@ -81,7 +81,7 @@ export const handleModifiedFilesAndUpdateComponentChanges = for (const { oldFile, newFile } of modifiedFiles) { if (isFileIdentifierChanged(oldFile, newFile)) { - unlinks.push(oldFile); + unlinks.push({ ...oldFile, shouldRemoveExternalAlias: true }); creates.push(newFile); } else { updates.push(newFile); diff --git a/src/services/sync-component-with-file/find-config-file-changes.test.ts b/src/services/sync-component-with-file/find-config-file-changes.test.ts index 75cf98d..3413d03 100644 --- a/src/services/sync-component-with-file/find-config-file-changes.test.ts +++ b/src/services/sync-component-with-file/find-config-file-changes.test.ts @@ -226,6 +226,7 @@ describe('findConfigAsCodeFileChanges', () => { componentYaml: compassYamlBefore, filePath: '/compass.yml', deduplicationId: baseEvent.project.id.toString(), + shouldRemoveExternalAlias: true, }, ], }; @@ -352,6 +353,7 @@ describe('findConfigAsCodeFileChanges', () => { componentYaml: compassYamlBefore, filePath: '/compass.yml', deduplicationId: event.project.id.toString(), + shouldRemoveExternalAlias: true, }, ], }; @@ -380,6 +382,7 @@ describe('findConfigAsCodeFileChanges', () => { componentYaml: compassYamlBefore, filePath: '/compass.yml', deduplicationId: event.project.id.toString(), + shouldRemoveExternalAlias: true, }, ], }; @@ -653,6 +656,7 @@ describe('findConfigAsCodeFileChanges', () => { componentYaml: compassYamlBefore, filePath: '/compass.yml', deduplicationId: event.project.id.toString(), + shouldRemoveExternalAlias: true, }, ], }; @@ -708,6 +712,7 @@ describe('findConfigAsCodeFileChanges', () => { componentYaml: compassYamlBefore, filePath: '/compass.yml', deduplicationId: event.project.id.toString(), + shouldRemoveExternalAlias: true, }, ], }; diff --git a/src/types.ts b/src/types.ts index 7a2fbd7..084bb85 100644 --- a/src/types.ts +++ b/src/types.ts @@ -149,6 +149,7 @@ export type ComponentUnlinkPayload = { componentYaml: CompassYaml; deduplicationId?: string; filePath?: string; + shouldRemoveExternalAlias?: boolean; }; type ModifiedFilePayload = {