From 5cbdd322b8003348066fc7460e410bb41590b763 Mon Sep 17 00:00:00 2001 From: Samiya Akhtar Date: Tue, 7 Apr 2020 15:49:54 -0700 Subject: [PATCH 1/5] Explaining logic in deploymenttable.ts --- src/lib/azure/deploymenttable.ts | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/lib/azure/deploymenttable.ts b/src/lib/azure/deploymenttable.ts index 294d0640c..e072b588e 100644 --- a/src/lib/azure/deploymenttable.ts +++ b/src/lib/azure/deploymenttable.ts @@ -182,7 +182,7 @@ export const addSrcToACRPipeline = async ( * @param env environment name * @param pr Pull request Id (if available) */ -export const updateMatchingArcToHLDPipelineEntry = async ( +export const updateMatchingACRToHLDPipelineEntry = async ( entries: EntryACRToHLDPipeline[], tableInfo: DeploymentTable, pipelineId: string, @@ -240,7 +240,7 @@ export const updateMatchingArcToHLDPipelineEntry = async ( * @param env environment name * @param pr Pull request Id (if available) */ -export const updateLastRowOfArcToHLDPipelines = async ( +export const updateLastRowOfACRToHLDPipelines = async ( entries: EntryACRToHLDPipeline[], tableInfo: DeploymentTable, pipelineId: string, @@ -288,7 +288,7 @@ export const updateLastRowOfArcToHLDPipelines = async ( * @param env environment name * @param pr Pull request Id (if available) */ -export const addNewRowToArcToHLDPipelines = async ( +export const addNewRowToACRToHLDPipelines = async ( tableInfo: DeploymentTable, pipelineId: string, imageTag: string, @@ -345,9 +345,9 @@ export const updateACRToHLDPipeline = async ( imageTag ); - // 1. try to find the matching entry. if (entries && entries.length > 0) { - const found = await updateMatchingArcToHLDPipelineEntry( + // If there is a corresponding src -> acr pipeline, update it + const found = await updateMatchingACRToHLDPipelineEntry( entries, tableInfo, pipelineId, @@ -362,9 +362,9 @@ export const updateACRToHLDPipeline = async ( return found; } - // 2. when cannot find the entry, we take the last row and INSERT it. - // TODO: rethink this logic. - return await updateLastRowOfArcToHLDPipelines( + // If there's no src -> acr but a matching image tag and/or multiple p1 pipelines for this, + // copy one of them and amend info to create a new instance of deployment + return await updateLastRowOfACRToHLDPipelines( entries, tableInfo, pipelineId, @@ -376,10 +376,8 @@ export const updateACRToHLDPipeline = async ( ); } - // Fallback: Ideally we should not be getting here, because there should - // always be a p1 for any p2 being created. - // TODO: rethink this logic. - return await addNewRowToArcToHLDPipelines( + // If a corresponding src -> acr is not found, insert a new entry + return await addNewRowToACRToHLDPipelines( tableInfo, pipelineId, imageTag, @@ -639,6 +637,7 @@ export const updateHLDtoManifestHelper = async ( repository?: string ): Promise => { if (entries && entries.length > 0) { + // If a src -> acr and acr -> hld pipeline is found for this run, update it const updated = await updateHLDtoManifestEntry( entries, tableInfo, @@ -653,8 +652,8 @@ export const updateHLDtoManifestHelper = async ( return updated; } - // 2. when cannot find the entry, we take the last row and INSERT it. - // TODO: rethink this logic. + // If there are multiple acr -> hld pipelines or some information is missing, + // copy one of them and amend info to create a new instance of deployment return await updateLastHLDtoManifestEntry( entries, tableInfo, @@ -666,9 +665,8 @@ export const updateHLDtoManifestHelper = async ( ); } - // Fallback: Ideally we should not be getting here, because there should - // always matching entry - // TODO: rethink this logic. + // When no matching entry exists in storage for this hld -> manifest pipeline, it must be + // a manual change merged into HLD. Add new entry to storage. return await addNewRowToHLDtoManifestPipeline( tableInfo, hldCommitId, From 400645ef45bdbb11113568bcb6a6d11cdd7ed3a8 Mon Sep 17 00:00:00 2001 From: Samiya Akhtar Date: Tue, 7 Apr 2020 15:59:24 -0700 Subject: [PATCH 2/5] fix test --- src/lib/azure/deploymenttable.test.ts | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/lib/azure/deploymenttable.test.ts b/src/lib/azure/deploymenttable.test.ts index eaeda1829..9c32e43df 100644 --- a/src/lib/azure/deploymenttable.test.ts +++ b/src/lib/azure/deploymenttable.test.ts @@ -2,7 +2,7 @@ import uuid from "uuid"; import { disableVerboseLogging, enableVerboseLogging } from "../../logger"; import * as deploymenttable from "./deploymenttable"; import { - addNewRowToArcToHLDPipelines, + addNewRowToACRToHLDPipelines, addNewRowToHLDtoManifestPipeline, addSrcToACRPipeline, deleteFromTable, @@ -19,9 +19,9 @@ import { updateHLDtoManifestHelper, updateHLDToManifestPipeline, updateLastHLDtoManifestEntry, - updateLastRowOfArcToHLDPipelines, + updateLastRowOfACRToHLDPipelines, updateManifestCommitId, - updateMatchingArcToHLDPipelineEntry, + updateMatchingACRToHLDPipelineEntry, } from "./deploymenttable"; const mockedTableInfo: DeploymentTable = { @@ -211,13 +211,13 @@ describe("test addSrcToACRPipeline function", () => { }); }); -describe("test updateMatchingArcToHLDPipelineEntry function", () => { +describe("test updateMatchingACRToHLDPipelineEntry function", () => { it("positive test: matching entry", async (done) => { jest .spyOn(deploymenttable, "updateEntryInTable") .mockReturnValueOnce(Promise.resolve()); const entries: EntryACRToHLDPipeline[] = [mockedEntryACRToHLDPipeline]; - const result = await updateMatchingArcToHLDPipelineEntry( + const result = await updateMatchingACRToHLDPipelineEntry( entries, mockedTableInfo, mockedPipelineId, @@ -230,7 +230,7 @@ describe("test updateMatchingArcToHLDPipelineEntry function", () => { done(); }); it("positive test: no matching entries", async (done) => { - const result = await updateMatchingArcToHLDPipelineEntry( + const result = await updateMatchingACRToHLDPipelineEntry( [], mockedTableInfo, mockedPipelineId, @@ -261,7 +261,7 @@ const testUpdateLastRowOfArcToHLDPipelines = async ( ): Promise => { mockInsertIntoTable(positive); const entries: EntryACRToHLDPipeline[] = [mockedEntryACRToHLDPipeline]; - return await updateLastRowOfArcToHLDPipelines( + return await updateLastRowOfACRToHLDPipelines( entries, mockedTableInfo, mockedPipelineId, @@ -272,7 +272,7 @@ const testUpdateLastRowOfArcToHLDPipelines = async ( ); }; -describe("test updateLastRowOfArcToHLDPipelines function", () => { +describe("test updateLastRowOfACRToHLDPipelines function", () => { it("positive test", async (done) => { const result = await testUpdateLastRowOfArcToHLDPipelines(); expect(result).toBeDefined(); @@ -289,7 +289,7 @@ const testAddNewRowToArcToHLDPipelines = async ( ): Promise => { mockInsertIntoTable(positive); - return await addNewRowToArcToHLDPipelines( + return await addNewRowToACRToHLDPipelines( mockedTableInfo, mockedPipelineId, mockedImageTag, @@ -299,7 +299,7 @@ const testAddNewRowToArcToHLDPipelines = async ( ); }; -describe("test addNewRowToArcToHLDPipelines function", () => { +describe("test addNewRowToACRToHLDPipelines function", () => { it("positive test", async (done) => { const result = await testAddNewRowToArcToHLDPipelines(); expect(result).toBeDefined(); @@ -317,13 +317,13 @@ const testUpdateACRToHLDPipeline = async ( ): Promise => { const fnUpdateFound = jest.spyOn( deploymenttable, - "updateMatchingArcToHLDPipelineEntry" + "updateMatchingACRToHLDPipelineEntry" ); const fnUpdateLastEntry = jest.spyOn( deploymenttable, - "updateLastRowOfArcToHLDPipelines" + "updateLastRowOfACRToHLDPipelines" ); - const addNewRow = jest.spyOn(deploymenttable, "addNewRowToArcToHLDPipelines"); + const addNewRow = jest.spyOn(deploymenttable, "addNewRowToACRToHLDPipelines"); if (noEntry) { jest From 7c6d73c89dd644e806d7e808f8308a78ba1d0cc1 Mon Sep 17 00:00:00 2001 From: Samiya Akhtar Date: Tue, 7 Apr 2020 20:04:53 -0700 Subject: [PATCH 3/5] Remove duplicate code --- src/lib/azure/deploymenttable.test.ts | 96 ++++++++-------- src/lib/azure/deploymenttable.ts | 154 ++++++-------------------- 2 files changed, 78 insertions(+), 172 deletions(-) diff --git a/src/lib/azure/deploymenttable.test.ts b/src/lib/azure/deploymenttable.test.ts index 9c32e43df..c2fc72adf 100644 --- a/src/lib/azure/deploymenttable.test.ts +++ b/src/lib/azure/deploymenttable.test.ts @@ -18,8 +18,6 @@ import { updateHLDtoManifestEntry, updateHLDtoManifestHelper, updateHLDToManifestPipeline, - updateLastHLDtoManifestEntry, - updateLastRowOfACRToHLDPipelines, updateManifestCommitId, updateMatchingACRToHLDPipelineEntry, } from "./deploymenttable"; @@ -40,6 +38,7 @@ const mockedHldCommitId = uuid(); const mockedEnv = uuid(); const mockedPr = uuid(); const mockedManifestCommitId = uuid(); +const mockedRepository = uuid(); const mockedEntryACRPipeline: deploymenttable.EntrySRCToACRPipeline = { PartitionKey: uuid(), @@ -186,7 +185,8 @@ describe("test addSrcToACRPipeline function", () => { mockedPipelineId, mockedImageTag, mockedServiceName, - mockedCommitId + mockedCommitId, + mockedRepository ); expect(entry.commitId).toBe(mockedCommitId); expect(entry.p1).toBe(mockedPipelineId); @@ -224,7 +224,8 @@ describe("test updateMatchingACRToHLDPipelineEntry function", () => { mockedImageTag, mockedHldCommitId, mockedEnv, - mockedPr + mockedPr, + mockedRepository ); expect(result).toBeDefined(); done(); @@ -237,7 +238,8 @@ describe("test updateMatchingACRToHLDPipelineEntry function", () => { mockedImageTag, mockedHldCommitId, mockedEnv, - mockedPr + mockedPr, + mockedRepository ); expect(result).toBeNull(); done(); @@ -256,35 +258,38 @@ const mockInsertIntoTable = (positive = true): void => { } }; -const testUpdateLastRowOfArcToHLDPipelines = async ( +const testAddNewRowToACRToHLDPipelinesWithSimilarEntry = async ( positive = true ): Promise => { mockInsertIntoTable(positive); const entries: EntryACRToHLDPipeline[] = [mockedEntryACRToHLDPipeline]; - return await updateLastRowOfACRToHLDPipelines( - entries, + return await addNewRowToACRToHLDPipelines( mockedTableInfo, mockedPipelineId, mockedImageTag, mockedHldCommitId, mockedEnv, - mockedPr + mockedPr, + mockedRepository, + entries[entries.length - 1] ); }; -describe("test updateLastRowOfACRToHLDPipelines function", () => { +describe("test addNewRowToACRToHLDPipelines function with similar", () => { it("positive test", async (done) => { - const result = await testUpdateLastRowOfArcToHLDPipelines(); + const result = await testAddNewRowToACRToHLDPipelinesWithSimilarEntry(); expect(result).toBeDefined(); done(); }); it("negative test", async (done) => { - await expect(testUpdateLastRowOfArcToHLDPipelines(false)).rejects.toThrow(); + await expect( + testAddNewRowToACRToHLDPipelinesWithSimilarEntry(false) + ).rejects.toThrow(); done(); }); }); -const testAddNewRowToArcToHLDPipelines = async ( +const testAddNewRowToACRToHLDPipelines = async ( positive = true ): Promise => { mockInsertIntoTable(positive); @@ -301,12 +306,12 @@ const testAddNewRowToArcToHLDPipelines = async ( describe("test addNewRowToACRToHLDPipelines function", () => { it("positive test", async (done) => { - const result = await testAddNewRowToArcToHLDPipelines(); + const result = await testAddNewRowToACRToHLDPipelines(); expect(result).toBeDefined(); done(); }); it("negative test", async (done) => { - await expect(testAddNewRowToArcToHLDPipelines(false)).rejects.toThrow(); + await expect(testAddNewRowToACRToHLDPipelines(false)).rejects.toThrow(); done(); }); }); @@ -319,10 +324,6 @@ const testUpdateACRToHLDPipeline = async ( deploymenttable, "updateMatchingACRToHLDPipelineEntry" ); - const fnUpdateLastEntry = jest.spyOn( - deploymenttable, - "updateLastRowOfACRToHLDPipelines" - ); const addNewRow = jest.spyOn(deploymenttable, "addNewRowToACRToHLDPipelines"); if (noEntry) { @@ -345,9 +346,6 @@ const testUpdateACRToHLDPipeline = async ( Promise.resolve([mockedNonMatchEntryACRToHLDPipeline]) ); fnUpdateFound.mockReturnValueOnce(Promise.resolve(null)); - fnUpdateLastEntry.mockReturnValueOnce( - Promise.resolve(mockedRowACRToHLDPipeline) - ); } } @@ -357,26 +355,24 @@ const testUpdateACRToHLDPipeline = async ( mockedImageTag, mockedHldCommitId, mockedEnv, - mockedPr + mockedPr, + mockedRepository ); if (noEntry) { expect(fnUpdateFound).toBeCalledTimes(0); - expect(fnUpdateLastEntry).toBeCalledTimes(0); expect(addNewRow).toBeCalledTimes(1); } else { expect(fnUpdateFound).toBeCalledTimes(1); - expect(addNewRow).toBeCalledTimes(0); if (matched) { - expect(fnUpdateLastEntry).toBeCalledTimes(0); + expect(addNewRow).toBeCalledTimes(0); } else { - expect(fnUpdateLastEntry).toBeCalledTimes(1); + expect(addNewRow).toBeCalledTimes(1); } } fnUpdateFound.mockReset(); - fnUpdateLastEntry.mockReset(); addNewRow.mockReset(); }; @@ -452,7 +448,8 @@ describe("test updateHLDtoManifestEntry function", () => { mockedHldCommitId, mockedPipelineId3, mockedManifestCommitId, - mockedPr + mockedPr, + mockedRepository ); expect(res).toBeDefined(); done(); @@ -464,7 +461,8 @@ describe("test updateHLDtoManifestEntry function", () => { mockedHldCommitId, mockedPipelineId2, mockedManifestCommitId, - mockedPr + mockedPr, + mockedRepository ); expect(res).toBeNull(); done(); @@ -480,26 +478,28 @@ describe("test updateHLDtoManifestEntry function", () => { mockedHldCommitId, mockedPipelineId3, mockedManifestCommitId, - mockedPr + mockedPr, + mockedRepository ) ).rejects.toThrow(); done(); }); }); -describe("test updateLastHLDtoManifestEntry function", () => { +describe("test addNewRowToHLDtoManifestPipeline function with similar entry", () => { it("positive test", async (done) => { jest .spyOn(deploymenttable, "insertToTable") .mockReturnValueOnce(Promise.resolve()); - const res = await updateLastHLDtoManifestEntry( - [mockedEntryHLDToManifestPipeline], + const res = await addNewRowToHLDtoManifestPipeline( mockedTableInfo, mockedHldCommitId, mockedPipelineId3, mockedManifestCommitId, - mockedPr + mockedPr, + mockedRepository, + mockedEntryHLDToManifestPipeline ); expect(res).toBeDefined(); done(); @@ -510,13 +510,14 @@ describe("test updateLastHLDtoManifestEntry function", () => { .mockReturnValueOnce(Promise.reject(new Error("Fake"))); await expect( - updateLastHLDtoManifestEntry( - [mockedEntryHLDToManifestPipeline], + addNewRowToHLDtoManifestPipeline( mockedTableInfo, mockedHldCommitId, mockedPipelineId3, mockedManifestCommitId, - mockedPr + mockedPr, + mockedRepository, + mockedEntryHLDToManifestPipeline ) ).rejects.toThrow(); done(); @@ -560,10 +561,6 @@ const testUpdateHLDtoManifestHelper = async ( match: boolean ): Promise => { const updateFn = jest.spyOn(deploymenttable, "updateHLDtoManifestEntry"); - const updateLastFn = jest.spyOn( - deploymenttable, - "updateLastHLDtoManifestEntry" - ); const addFn = jest.spyOn(deploymenttable, "addNewRowToHLDtoManifestPipeline"); if (empty) { @@ -575,7 +572,7 @@ const testUpdateHLDtoManifestHelper = async ( ); } else { updateFn.mockReturnValueOnce(Promise.resolve(null)); - updateLastFn.mockReturnValueOnce( + addFn.mockReturnValueOnce( Promise.resolve(mockedRowHLDToManifestPipeline) ); } @@ -594,20 +591,17 @@ const testUpdateHLDtoManifestHelper = async ( if (empty) { expect(updateFn).toBeCalledTimes(0); - expect(updateLastFn).toBeCalledTimes(0); expect(addFn).toBeCalledTimes(1); } else { expect(updateFn).toBeCalledTimes(1); - expect(addFn).toBeCalledTimes(0); if (match) { - expect(updateLastFn).toBeCalledTimes(0); + expect(addFn).toBeCalledTimes(0); } else { - expect(updateLastFn).toBeCalledTimes(1); + expect(addFn).toBeCalledTimes(1); } } updateFn.mockReset(); - updateLastFn.mockReset(); addFn.mockReset(); }; @@ -637,7 +631,8 @@ describe("test updateManifestCommitId function", () => { const res = await updateManifestCommitId( mockedTableInfo, mockedPipelineId3, - mockedManifestCommitId + mockedManifestCommitId, + mockedRepository ); expect(res).toBeDefined(); done(); @@ -650,7 +645,8 @@ describe("test updateManifestCommitId function", () => { updateManifestCommitId( mockedTableInfo, mockedPipelineId3, - mockedManifestCommitId + mockedManifestCommitId, + mockedRepository ) ).rejects.toThrow(); done(); diff --git a/src/lib/azure/deploymenttable.ts b/src/lib/azure/deploymenttable.ts index e072b588e..4ef70aed6 100644 --- a/src/lib/azure/deploymenttable.ts +++ b/src/lib/azure/deploymenttable.ts @@ -228,54 +228,6 @@ export const updateMatchingACRToHLDPipelineEntry = async ( return null; }; -/** - * Creates a new copy of an existing SRC -> ACR entry when a release is created for a - * corresponding entry that already has an existing release - * For eg. when the user manually creates a ACR -> HLD release for an existing image tag. - * @param entries list of entries found - * @param tableInfo table info object - * @param pipelineId Id of the ACR -> HLD pipeline - * @param imageTag image tag name - * @param hldCommitId HLD commit Id - * @param env environment name - * @param pr Pull request Id (if available) - */ -export const updateLastRowOfACRToHLDPipelines = async ( - entries: EntryACRToHLDPipeline[], - tableInfo: DeploymentTable, - pipelineId: string, - imageTag: string, - hldCommitId: string, - env: string, - pr?: string, - repository?: string -): Promise => { - const lastEntry = entries[entries.length - 1]; - const last: RowACRToHLDPipeline = { - PartitionKey: lastEntry.PartitionKey, - RowKey: getRowKey(), - commitId: lastEntry.commitId, - env: env.toLowerCase(), - hldCommitId: hldCommitId.toLowerCase(), - imageTag: lastEntry.imageTag, - p1: lastEntry.p1, - p2: pipelineId.toLowerCase(), - service: lastEntry.service, - sourceRepo: lastEntry.sourceRepo, - }; - if (pr) { - last.pr = pr.toLowerCase(); - } - if (repository) { - last.hldRepo = repository.toLowerCase(); - } - await insertToTable(tableInfo, last); - logger.info( - `Added new p2 entry for imageTag ${imageTag} by finding a similar entry` - ); - return last; -}; - /** * Adds a new entry for ACR -> HLD pipeline when no corresponding SRC -> ACR pipeline was found * to be associated @@ -295,18 +247,20 @@ export const addNewRowToACRToHLDPipelines = async ( hldCommitId: string, env: string, pr?: string, - repository?: string + repository?: string, + similarEntry?: EntryACRToHLDPipeline ): Promise => { const newEntry: RowACRToHLDPipeline = { PartitionKey: tableInfo.partitionKey, RowKey: getRowKey(), - commitId: "", + commitId: similarEntry?.commitId ? similarEntry.commitId : "", env: env.toLowerCase(), hldCommitId: hldCommitId.toLowerCase(), imageTag: imageTag.toLowerCase(), - p1: "", + p1: similarEntry?.p1 ? similarEntry.p1 : "", p2: pipelineId.toLowerCase(), - service: "", + service: similarEntry?.service ? similarEntry.service : "", + sourceRepo: similarEntry?.sourceRepo ? similarEntry.sourceRepo : "", }; if (pr) { newEntry.pr = pr.toLowerCase(); @@ -316,7 +270,11 @@ export const addNewRowToACRToHLDPipelines = async ( } await insertToTable(tableInfo, newEntry); logger.info( - `Added new p2 entry for imageTag ${imageTag} - no matching entry was found.` + `Added new p2 entry for imageTag ${imageTag} - ${ + similarEntry + ? "by finding a similar entry" + : "no matching entry was found." + }` ); return newEntry; }; @@ -364,15 +322,15 @@ export const updateACRToHLDPipeline = async ( // If there's no src -> acr but a matching image tag and/or multiple p1 pipelines for this, // copy one of them and amend info to create a new instance of deployment - return await updateLastRowOfACRToHLDPipelines( - entries, + return await addNewRowToACRToHLDPipelines( tableInfo, pipelineId, imageTag, hldCommitId, env, pr, - repository + repository, + entries[entries.length - 1] ); } @@ -512,61 +470,6 @@ export const updateHLDtoManifestEntry = async ( return null; }; -/** - * Creates a new copy of an existing ACR -> HLD entry when a build is created for a - * corresponding entry that already has an existing build - * For eg. when the user manually triggers a HLD -> Manifest build for the last existing HLD - * commit. - * @param entries list of matching entries based on PR / hld commit - * @param tableInfo table info interface containing information about - * the deployment storage table - * @param hldCommitId commit identifier into the HLD repo, used as a - * filter to find corresponding deployments - * @param pipelineId identifier of the HLD to manifest pipeline - * @param manifestCommitId manifest commit identifier - * @param pr pull request identifier - */ -export const updateLastHLDtoManifestEntry = async ( - entries: EntryHLDToManifestPipeline[], - tableInfo: DeploymentTable, - hldCommitId: string, - pipelineId: string, - manifestCommitId?: string, - pr?: string, - repository?: string -): Promise => { - const lastEntry = entries[entries.length - 1]; - const newEntry: RowHLDToManifestPipeline = { - PartitionKey: lastEntry.PartitionKey, - RowKey: getRowKey(), - commitId: lastEntry.commitId, - env: lastEntry.env, - hldCommitId: hldCommitId.toLowerCase(), - hldRepo: lastEntry.hldRepo, - imageTag: lastEntry.imageTag, - p1: lastEntry.p1, - p2: lastEntry.p2, - p3: pipelineId.toLowerCase(), - service: lastEntry.service, - sourceRepo: lastEntry.sourceRepo, - }; - if (manifestCommitId) { - newEntry.manifestCommitId = manifestCommitId.toLowerCase(); - } - if (pr) { - newEntry.pr = pr.toLowerCase(); - } - if (repository) { - newEntry.manifestRepo = repository.toLowerCase(); - } - - await insertToTable(tableInfo, newEntry); - logger.info( - `Added new p3 entry for hldCommitId ${hldCommitId} by finding a similar entry` - ); - return newEntry; -}; - /** * Adds a new row to the table when the HLD -> Manifest pipeline is triggered by * manually committing into the HLD @@ -584,19 +487,22 @@ export const addNewRowToHLDtoManifestPipeline = async ( pipelineId: string, manifestCommitId?: string, pr?: string, - repository?: string + repository?: string, + similarEntry?: EntryHLDToManifestPipeline ): Promise => { const newEntry: RowHLDToManifestPipeline = { PartitionKey: tableInfo.partitionKey, RowKey: getRowKey(), - commitId: "", - env: "", + commitId: similarEntry?.commitId ? similarEntry.commitId : "", + env: similarEntry?.env ? similarEntry.env : "", hldCommitId: hldCommitId.toLowerCase(), - imageTag: "", - p1: "", - p2: "", + hldRepo: similarEntry?.hldRepo ? similarEntry.hldRepo : "", + imageTag: similarEntry?.imageTag ? similarEntry.imageTag : "", + p1: similarEntry?.p1 ? similarEntry.p1 : "", + p2: similarEntry?.p2 ? similarEntry.p2 : "", p3: pipelineId.toLowerCase(), - service: "", + service: similarEntry?.service ? similarEntry.service : "", + sourceRepo: similarEntry?.sourceRepo ? similarEntry.sourceRepo : "", }; if (manifestCommitId) { newEntry.manifestCommitId = manifestCommitId.toLowerCase(); @@ -609,7 +515,11 @@ export const addNewRowToHLDtoManifestPipeline = async ( } await insertToTable(tableInfo, newEntry); logger.info( - `Added new p3 entry for hldCommitId ${hldCommitId} - no matching entry was found.` + `Added new p3 entry for hldCommitId ${hldCommitId} - ${ + similarEntry + ? "by finding a similar entry" + : "no matching entry was found." + }` ); return newEntry; }; @@ -654,14 +564,14 @@ export const updateHLDtoManifestHelper = async ( // If there are multiple acr -> hld pipelines or some information is missing, // copy one of them and amend info to create a new instance of deployment - return await updateLastHLDtoManifestEntry( - entries, + return await addNewRowToHLDtoManifestPipeline( tableInfo, hldCommitId, pipelineId, manifestCommitId, pr, - repository + repository, + entries[entries.length - 1] ); } From 1b1a5e9ebfb423bfa572dedd7842994821648f94 Mon Sep 17 00:00:00 2001 From: Samiya Akhtar Date: Tue, 7 Apr 2020 22:17:23 -0700 Subject: [PATCH 4/5] Remove duplicate interfaces for deployment entry --- src/commands/deployment/validate.test.ts | 55 ++----- src/commands/deployment/validate.ts | 6 +- src/lib/azure/deploymenttable.test.ts | 66 +++----- src/lib/azure/deploymenttable.ts | 196 ++++++----------------- 4 files changed, 89 insertions(+), 234 deletions(-) diff --git a/src/commands/deployment/validate.test.ts b/src/commands/deployment/validate.test.ts index fe7e5bf79..315a39a2f 100644 --- a/src/commands/deployment/validate.test.ts +++ b/src/commands/deployment/validate.test.ts @@ -2,10 +2,7 @@ import uuid from "uuid/v4"; import * as deploymenttable from "../../lib/azure/deploymenttable"; import { DeploymentTable, - RowACRToHLDPipeline, - RowHLDToManifestPipeline, - RowManifest, - RowSrcToACRPipeline, + DeploymentEntry, } from "../../lib/azure/deploymenttable"; import * as storage from "../../lib/azure/storage"; import { deepClone } from "../../lib/util"; @@ -69,16 +66,11 @@ jest.spyOn(storage, "isStorageAccountNameAvailable").mockImplementation( } ); -let mockedDB: Array< - | RowSrcToACRPipeline - | RowACRToHLDPipeline - | RowHLDToManifestPipeline - | RowManifest -> = []; +let mockedDB: Array = []; jest.spyOn(deploymenttable, "findMatchingDeployments").mockImplementation( - (): Promise => { - const array: RowSrcToACRPipeline[] = []; + (): Promise => { + const array: DeploymentEntry[] = []; return new Promise((resolve) => { mockedDB.forEach((row) => { if (row.p1 === "500") { @@ -93,13 +85,7 @@ jest.spyOn(deploymenttable, "findMatchingDeployments").mockImplementation( jest .spyOn(deploymenttable, "insertToTable") .mockImplementation( - ( - tableInfo: deploymenttable.DeploymentTable, - entry: - | RowSrcToACRPipeline - | RowACRToHLDPipeline - | RowHLDToManifestPipeline - ) => { + (tableInfo: deploymenttable.DeploymentTable, entry: DeploymentEntry) => { // eslint-disable-next-line @typescript-eslint/no-explicit-any return new Promise((resolve) => { mockedDB.push(entry); @@ -120,26 +106,17 @@ jest.spyOn(deploymenttable, "deleteFromTable").mockImplementation(async () => { jest .spyOn(deploymenttable, "updateEntryInTable") - .mockImplementation( - ( - tableInfo: DeploymentTable, - entry: - | RowSrcToACRPipeline - | RowACRToHLDPipeline - | RowHLDToManifestPipeline - | RowManifest - ) => { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - return new Promise((resolve) => { - mockedDB.forEach((row, index: number) => { - if (row.RowKey === entry.RowKey) { - mockedDB[index] = entry; - resolve(entry); - } - }, mockedDB); - }); - } - ); + .mockImplementation((tableInfo: DeploymentTable, entry: DeploymentEntry) => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + return new Promise((resolve) => { + mockedDB.forEach((row, index: number) => { + if (row.RowKey === entry.RowKey) { + mockedDB[index] = entry; + resolve(entry); + } + }, mockedDB); + }); + }); jest.spyOn(Math, "random").mockImplementation((): number => { return 0.5; diff --git a/src/commands/deployment/validate.ts b/src/commands/deployment/validate.ts index d14a8b49f..32a698c59 100644 --- a/src/commands/deployment/validate.ts +++ b/src/commands/deployment/validate.ts @@ -5,7 +5,7 @@ import { deleteFromTable, findMatchingDeployments, DeploymentTable, - EntrySRCToACRPipeline, + DeploymentEntry, updateACRToHLDPipeline, } from "../../lib/azure/deploymenttable"; import { build as buildCmd, exit as exitCmd } from "../../lib/commandBuilder"; @@ -176,10 +176,10 @@ export const deleteSelfTestData = async ( ).then(async (results) => { logger.info("Deleting test data..."); let foundEntry = false; - const entries = results as EntrySRCToACRPipeline[]; + const entries = results as DeploymentEntry[]; try { for (const entry of entries) { - if (entry.p1 && entry.p1._ === buildId) { + if (entry.p1 && entry.p1 === buildId) { foundEntry = true; } await deleteFromTable(tableInfo, entry); diff --git a/src/lib/azure/deploymenttable.test.ts b/src/lib/azure/deploymenttable.test.ts index c2fc72adf..fa19620e0 100644 --- a/src/lib/azure/deploymenttable.test.ts +++ b/src/lib/azure/deploymenttable.test.ts @@ -9,10 +9,8 @@ import { findMatchingDeployments, getTableService, DeploymentTable, - EntryACRToHLDPipeline, + DeploymentEntry, insertToTable, - RowHLDToManifestPipeline, - RowManifest, updateACRToHLDPipeline, updateEntryInTable, updateHLDtoManifestEntry, @@ -40,57 +38,41 @@ const mockedPr = uuid(); const mockedManifestCommitId = uuid(); const mockedRepository = uuid(); -const mockedEntryACRPipeline: deploymenttable.EntrySRCToACRPipeline = { +const mockedEntryACRPipeline: DeploymentEntry = { PartitionKey: uuid(), RowKey: uuid(), commitId: mockedCommitId, - env: { - _: mockedEnv, - }, + env: mockedEnv, imageTag: mockedImageTag, - p1: { - _: mockedPipelineId, - }, + p1: mockedPipelineId, service: mockedServiceName, }; -const mockedEntryACRToHLDPipeline = { +const mockedEntryACRToHLDPipeline: DeploymentEntry = { PartitionKey: uuid(), RowKey: uuid(), commitId: mockedCommitId, - env: { - _: mockedEnv, - }, - hldCommitId: { - _: mockedHldCommitId, - }, + env: mockedEnv, + hldCommitId: mockedHldCommitId, imageTag: mockedImageTag, p1: mockedPipelineId, - p2: { - _: mockedPipelineId, - }, + p2: mockedPipelineId, service: mockedServiceName, }; -const mockedNonMatchEntryACRToHLDPipeline = { +const mockedNonMatchEntryACRToHLDPipeline: DeploymentEntry = { PartitionKey: uuid(), RowKey: uuid(), commitId: mockedCommitId, - env: { - _: mockedEnv, - }, - hldCommitId: { - _: mockedHldCommitId, - }, + env: mockedEnv, + hldCommitId: mockedHldCommitId, imageTag: mockedImageTag, p1: mockedPipelineId, - p2: { - _: uuid(), - }, + p2: uuid(), service: mockedServiceName, }; -const mockedRowACRToHLDPipeline = { +const mockedRowACRToHLDPipeline: DeploymentEntry = { PartitionKey: uuid(), RowKey: uuid(), commitId: mockedCommitId, @@ -109,27 +91,23 @@ const mockedRowHLDToManifestPipeline = Object.assign( p3: mockedPipelineId3, }, mockedRowACRToHLDPipeline -) as RowHLDToManifestPipeline; +) as DeploymentEntry; -const mockedEntryHLDToManifestPipeline = { +const mockedEntryHLDToManifestPipeline: DeploymentEntry = { PartitionKey: uuid(), RowKey: uuid(), commitId: mockedCommitId, env: mockedEnv, hldCommitId: mockedHldCommitId, imageTag: mockedImageTag, - manifestCommitId: { - _: mockedManifestCommitId, - }, + manifestCommitId: mockedManifestCommitId, p1: mockedPipelineId, p2: mockedPipelineId2, - p3: { - _: mockedPipelineId3, - }, + p3: mockedPipelineId3, service: mockedServiceName, }; -const mockedManifestRow: RowManifest = Object.assign( +const mockedManifestRow: DeploymentEntry = Object.assign( { manifestCommitId: uuid(), }, @@ -216,7 +194,7 @@ describe("test updateMatchingACRToHLDPipelineEntry function", () => { jest .spyOn(deploymenttable, "updateEntryInTable") .mockReturnValueOnce(Promise.resolve()); - const entries: EntryACRToHLDPipeline[] = [mockedEntryACRToHLDPipeline]; + const entries: DeploymentEntry[] = [mockedEntryACRToHLDPipeline]; const result = await updateMatchingACRToHLDPipelineEntry( entries, mockedTableInfo, @@ -260,9 +238,9 @@ const mockInsertIntoTable = (positive = true): void => { const testAddNewRowToACRToHLDPipelinesWithSimilarEntry = async ( positive = true -): Promise => { +): Promise => { mockInsertIntoTable(positive); - const entries: EntryACRToHLDPipeline[] = [mockedEntryACRToHLDPipeline]; + const entries: DeploymentEntry[] = [mockedEntryACRToHLDPipeline]; return await addNewRowToACRToHLDPipelines( mockedTableInfo, mockedPipelineId, @@ -291,7 +269,7 @@ describe("test addNewRowToACRToHLDPipelines function with similar", () => { const testAddNewRowToACRToHLDPipelines = async ( positive = true -): Promise => { +): Promise => { mockInsertIntoTable(positive); return await addNewRowToACRToHLDPipelines( diff --git a/src/lib/azure/deploymenttable.ts b/src/lib/azure/deploymenttable.ts index 4ef70aed6..d40a28b27 100644 --- a/src/lib/azure/deploymenttable.ts +++ b/src/lib/azure/deploymenttable.ts @@ -15,110 +15,22 @@ export interface DeploymentTable { partitionKey: string; } -/** - * Row interface to hold necessary information about SRC -> ACR entry - */ -export interface RowSrcToACRPipeline { - PartitionKey: string; - RowKey: string; - commitId: string; - imageTag: string; - p1: string; - service: string; - sourceRepo?: string; -} - -/** - * Row interface to add ACR -> HLD entry - */ -export interface RowACRToHLDPipeline extends RowSrcToACRPipeline { - p2: string; - hldCommitId: string; - env: string; - pr?: string; - hldRepo?: string; -} - -/** - * Row interface to hold necessary information about SRC -> ACR entry - */ -export interface EntrySRCToACRPipeline { +export interface DeploymentEntry { RowKey: string; PartitionKey: string; - commitId: string; - imageTag: string; - service: string; - sourceRepo?: string; - p1: { - _: string; - }; - env: { - _: string; - }; -} - -/** - * Row interface to hold necessary information about ACR -> HLD entry - */ -export interface EntryACRToHLDPipeline { - RowKey: string; - PartitionKey: string; - commitId: string; - imageTag: string; - sourceRepo?: string; - hldRepo?: string; - p1: string; - service: string; - p2: { - _: string; - }; - hldCommitId: { - _: string; - }; - env: { - _: string; - }; -} - -/** - * Row interface to hold necessary information about HLD -> Manifest entry - */ -export interface RowHLDToManifestPipeline extends RowACRToHLDPipeline { - p3: string; + commitId?: string; + env?: string; + imageTag?: string; + p1?: string; + service?: string; + p2?: string; + hldCommitId?: string; + p3?: string; manifestCommitId?: string; - manifestRepo?: string; -} - -/** - * Row interface to hold necessary information about HLD -> Manifest entry - */ -export interface EntryHLDToManifestPipeline { - RowKey: string; - PartitionKey: string; - commitId: string; - env: string; - imageTag: string; - p1: string; - service: string; - p2: string; - hldCommitId: string; - p3: { - _: string; - }; - manifestCommitId: { - _: string; - }; sourceRepo?: string; hldRepo?: string; manifestRepo?: string; -} - -/** - * Row interface to hold necessary information Manifest update entry - */ -export interface RowManifest extends RowHLDToManifestPipeline { - manifestCommitId: string; - manifestRepo?: string; + pr?: string; } /** @@ -147,9 +59,9 @@ export const addSrcToACRPipeline = async ( serviceName: string, commitId: string, repository?: string -): Promise => { +): Promise => { try { - const entry: RowSrcToACRPipeline = { + const entry: DeploymentEntry = { PartitionKey: tableInfo.partitionKey, RowKey: getRowKey(), commitId, @@ -183,7 +95,7 @@ export const addSrcToACRPipeline = async ( * @param pr Pull request Id (if available) */ export const updateMatchingACRToHLDPipelineEntry = async ( - entries: EntryACRToHLDPipeline[], + entries: DeploymentEntry[], tableInfo: DeploymentTable, pipelineId: string, imageTag: string, @@ -191,17 +103,17 @@ export const updateMatchingACRToHLDPipelineEntry = async ( env: string, pr?: string, repository?: string -): Promise => { - const found = (entries || []).find((entry: EntryACRToHLDPipeline) => { +): Promise => { + const found = (entries || []).find((entry: DeploymentEntry) => { return ( - (entry.p2 ? entry.p2._ === pipelineId : true) && - (entry.hldCommitId ? entry.hldCommitId._ === hldCommitId : true) && - (entry.env ? entry.env._ === env : true) + (entry.p2 ? entry.p2 === pipelineId : true) && + (entry.hldCommitId ? entry.hldCommitId === hldCommitId : true) && + (entry.env ? entry.env === env : true) ); }); if (found) { - const updateEntry: RowACRToHLDPipeline = { + const updateEntry: DeploymentEntry = { PartitionKey: found.PartitionKey, RowKey: found.RowKey, commitId: found.commitId, @@ -221,7 +133,7 @@ export const updateMatchingACRToHLDPipelineEntry = async ( } await updateEntryInTable(tableInfo, updateEntry); logger.info( - `Added new p2 entry for imageTag ${imageTag} by finding corresponding entry` + `Updated p2 entry for imageTag ${imageTag} by finding corresponding entry` ); return updateEntry; } @@ -248,9 +160,9 @@ export const addNewRowToACRToHLDPipelines = async ( env: string, pr?: string, repository?: string, - similarEntry?: EntryACRToHLDPipeline -): Promise => { - const newEntry: RowACRToHLDPipeline = { + similarEntry?: DeploymentEntry +): Promise => { + const newEntry: DeploymentEntry = { PartitionKey: tableInfo.partitionKey, RowKey: getRowKey(), commitId: similarEntry?.commitId ? similarEntry.commitId : "", @@ -295,9 +207,9 @@ export const updateACRToHLDPipeline = async ( env: string, pr?: string, repository?: string -): Promise => { +): Promise => { try { - const entries = await findMatchingDeployments( + const entries = await findMatchingDeployments( tableInfo, "imageTag", imageTag @@ -374,9 +286,9 @@ export const updateHLDToManifestPipeline = async ( manifestCommitId?: string, pr?: string, repository?: string -): Promise => { +): Promise => { try { - let entries = await findMatchingDeployments( + let entries = await findMatchingDeployments( tableInfo, "hldCommitId", hldCommitId @@ -385,11 +297,7 @@ export const updateHLDToManifestPipeline = async ( // cannot find entries by hldCommitId. // attempt to find entries by pr if ((!entries || entries.length === 0) && pr) { - entries = await findMatchingDeployments( - tableInfo, - "pr", - pr - ); + entries = await findMatchingDeployments(tableInfo, "pr", pr); } return updateHLDtoManifestHelper( entries, @@ -421,24 +329,24 @@ export const updateHLDToManifestPipeline = async ( * @param pr pull request identifier */ export const updateHLDtoManifestEntry = async ( - entries: EntryHLDToManifestPipeline[], + entries: DeploymentEntry[], tableInfo: DeploymentTable, hldCommitId: string, pipelineId: string, manifestCommitId?: string, pr?: string, repository?: string -): Promise => { +): Promise => { const found = entries.find( - (entry: EntryHLDToManifestPipeline) => - (entry.p3 ? entry.p3._ === pipelineId : true) && + (entry: DeploymentEntry) => + (entry.p3 ? entry.p3 === pipelineId : true) && (entry.manifestCommitId - ? entry.manifestCommitId._ === manifestCommitId + ? entry.manifestCommitId === manifestCommitId : true) ); if (found) { - const entry: RowHLDToManifestPipeline = { + const entry: DeploymentEntry = { PartitionKey: found.PartitionKey, RowKey: found.RowKey, commitId: found.commitId, @@ -488,9 +396,9 @@ export const addNewRowToHLDtoManifestPipeline = async ( manifestCommitId?: string, pr?: string, repository?: string, - similarEntry?: EntryHLDToManifestPipeline -): Promise => { - const newEntry: RowHLDToManifestPipeline = { + similarEntry?: DeploymentEntry +): Promise => { + const newEntry: DeploymentEntry = { PartitionKey: tableInfo.partitionKey, RowKey: getRowKey(), commitId: similarEntry?.commitId ? similarEntry.commitId : "", @@ -538,14 +446,14 @@ export const addNewRowToHLDtoManifestPipeline = async ( * @param pr pull request identifier */ export const updateHLDtoManifestHelper = async ( - entries: EntryHLDToManifestPipeline[], + entries: DeploymentEntry[], tableInfo: DeploymentTable, hldCommitId: string, pipelineId: string, manifestCommitId?: string, pr?: string, repository?: string -): Promise => { +): Promise => { if (entries && entries.length > 0) { // If a src -> acr and acr -> hld pipeline is found for this run, update it const updated = await updateHLDtoManifestEntry( @@ -598,13 +506,9 @@ export const updateManifestCommitId = async ( pipelineId: string, manifestCommitId: string, repository?: string -): Promise => { +): Promise => { try { - const entries = await findMatchingDeployments( - tableInfo, - "p3", - pipelineId - ); + const entries = await findMatchingDeployments(tableInfo, "p3", pipelineId); // Ideally there should only be one entry for every pipeline id if (entries.length > 0) { const entry = entries[0]; @@ -637,11 +541,11 @@ export const updateManifestCommitId = async ( * @param filterName name of the filter, such as `imageTag` * @param filterValue value of the filter, such as `hello-spk-master-1234` */ -export const findMatchingDeployments = ( +export const findMatchingDeployments = ( tableInfo: DeploymentTable, filterName: string, filterValue: string -): Promise => { +): Promise => { const tableService = getTableService(tableInfo); const query: azure.TableQuery = new azure.TableQuery().where( `PartitionKey eq '${tableInfo.partitionKey}'` @@ -655,13 +559,13 @@ export const findMatchingDeployments = ( | any = null; return new Promise((resolve, reject) => { - tableService.queryEntities( + tableService.queryEntities( tableInfo.tableName, query, nextContinuationToken, (error, result) => { if (!error) { - resolve(result.entries as T[]); + resolve(result.entries); } else { reject(error); } @@ -678,7 +582,7 @@ export const findMatchingDeployments = ( */ export const insertToTable = ( tableInfo: DeploymentTable, - entry: RowSrcToACRPipeline | RowACRToHLDPipeline | RowHLDToManifestPipeline + entry: DeploymentEntry ): Promise => { const tableService = getTableService(tableInfo); @@ -700,7 +604,7 @@ export const insertToTable = ( */ export const deleteFromTable = ( tableInfo: DeploymentTable, - entry: EntrySRCToACRPipeline + entry: DeploymentEntry ): Promise => { const tableService = getTableService(tableInfo); @@ -723,11 +627,7 @@ export const deleteFromTable = ( */ export const updateEntryInTable = ( tableInfo: DeploymentTable, - entry: - | RowSrcToACRPipeline - | RowACRToHLDPipeline - | RowHLDToManifestPipeline - | RowManifest + entry: DeploymentEntry ): Promise => { const tableService = getTableService(tableInfo); From 213b27137fe62bf293a7feef0fe7d7130d9ae09f Mon Sep 17 00:00:00 2001 From: Samiya Akhtar Date: Wed, 8 Apr 2020 15:00:58 -0700 Subject: [PATCH 5/5] Remove eslint disable no use before define --- src/lib/azure/deploymenttable.ts | 333 +++++++++++++++---------------- 1 file changed, 166 insertions(+), 167 deletions(-) diff --git a/src/lib/azure/deploymenttable.ts b/src/lib/azure/deploymenttable.ts index d40a28b27..72f3ed042 100644 --- a/src/lib/azure/deploymenttable.ts +++ b/src/lib/azure/deploymenttable.ts @@ -1,4 +1,3 @@ -/* eslint-disable @typescript-eslint/no-use-before-define */ import * as azure from "azure-storage"; import uuid from "uuid/v4"; import { logger } from "../../logger"; @@ -33,6 +32,13 @@ export interface DeploymentEntry { pr?: string; } +/** + * Generates a RowKey GUID 12 characters long + */ +export const getRowKey = (): string => { + return uuid().replace("-", "").substring(0, 12); +}; + /** * Gets the azure table service * @param tableInfo tableInfo object containing necessary table info @@ -43,6 +49,113 @@ export const getTableService = ( return azure.createTableService(tableInfo.accountName, tableInfo.accountKey); }; +/** + * Finds matching deployments for a filter name and filter value in the storage + * @param tableInfo table info interface containing information about the deployment storage table + * @param filterName name of the filter, such as `imageTag` + * @param filterValue value of the filter, such as `hello-spk-master-1234` + */ +export const findMatchingDeployments = ( + tableInfo: DeploymentTable, + filterName: string, + filterValue: string +): Promise => { + const tableService = getTableService(tableInfo); + const query: azure.TableQuery = new azure.TableQuery().where( + `PartitionKey eq '${tableInfo.partitionKey}'` + ); + query.and(`${filterName} eq '${filterValue}'`); + + // To get around issue https://github.com/Azure/azure-storage-node/issues/545, set below to null + const nextContinuationToken: + | azure.TableService.TableContinuationToken + // eslint-disable-next-line @typescript-eslint/no-explicit-any + | any = null; + + return new Promise((resolve, reject) => { + tableService.queryEntities( + tableInfo.tableName, + query, + nextContinuationToken, + (error, result) => { + if (!error) { + resolve(result.entries); + } else { + reject(error); + } + } + ); + }); +}; + +/** + * Inserts a new entry into the table. + * + * @param tableInfo Table Information + * @param entry entry to insert + */ +export const insertToTable = ( + tableInfo: DeploymentTable, + entry: DeploymentEntry +): Promise => { + const tableService = getTableService(tableInfo); + + return new Promise((resolve, reject) => { + tableService.insertEntity(tableInfo.tableName, entry, (err) => { + if (!err) { + resolve(); + } else { + reject(err); + } + }); + }); +}; + +/** + * Deletes self test data from table + * @param tableInfo table info object + * @param entry entry to be deleted + */ +export const deleteFromTable = ( + tableInfo: DeploymentTable, + entry: DeploymentEntry +): Promise => { + const tableService = getTableService(tableInfo); + + return new Promise((resolve, reject) => { + tableService.deleteEntity(tableInfo.tableName, entry, {}, (err) => { + if (!err) { + resolve(); + } else { + reject(err); + } + }); + }); +}; + +/** + * Updates an entry in the table. + * + * @param tableInfo Table Information + * @param entry entry to update + */ +export const updateEntryInTable = ( + tableInfo: DeploymentTable, + entry: DeploymentEntry +): Promise => { + const tableService = getTableService(tableInfo); + + return new Promise((resolve, reject) => { + tableService.replaceEntity(tableInfo.tableName, entry, (err) => { + if (!err) { + resolve(); + } else { + reject(err); + } + }); + }); +}; + /** * Adds a new deployment in storage for SRC to ACR pipeline. * @@ -265,58 +378,6 @@ export const updateACRToHLDPipeline = async ( } }; -/** - * Updates the HLD to manifest pipeline in storage by finding its - * corresponding SRC to ACR and ACR to HLD pipelines - * Depending on whether PR is specified or not, it performs a lookup - * on commit Id and PR to link it to the previous release. - * - * @param tableInfo table info interface containing information about - * the deployment storage table - * @param hldCommitId commit identifier into the HLD repo, used as a - * filter to find corresponding deployments - * @param pipelineId identifier of the HLD to manifest pipeline - * @param manifestCommitId manifest commit identifier - * @param pr pull request identifier - */ -export const updateHLDToManifestPipeline = async ( - tableInfo: DeploymentTable, - hldCommitId: string, - pipelineId: string, - manifestCommitId?: string, - pr?: string, - repository?: string -): Promise => { - try { - let entries = await findMatchingDeployments( - tableInfo, - "hldCommitId", - hldCommitId - ); - - // cannot find entries by hldCommitId. - // attempt to find entries by pr - if ((!entries || entries.length === 0) && pr) { - entries = await findMatchingDeployments(tableInfo, "pr", pr); - } - return updateHLDtoManifestHelper( - entries, - tableInfo, - hldCommitId, - pipelineId, - manifestCommitId, - pr, - repository - ); - } catch (err) { - throw buildError( - errorStatusCode.AZURE_STORAGE_OP_ERR, - "deployment-table-update-hld-manifest-pipeline-failed", - err - ); - } -}; - /** * Updates HLD -> Manifest build for its corresponding ACR -> HLD release * @param entries list of matching entries based on PR / hld commit @@ -495,6 +556,58 @@ export const updateHLDtoManifestHelper = async ( ); }; +/** + * Updates the HLD to manifest pipeline in storage by finding its + * corresponding SRC to ACR and ACR to HLD pipelines + * Depending on whether PR is specified or not, it performs a lookup + * on commit Id and PR to link it to the previous release. + * + * @param tableInfo table info interface containing information about + * the deployment storage table + * @param hldCommitId commit identifier into the HLD repo, used as a + * filter to find corresponding deployments + * @param pipelineId identifier of the HLD to manifest pipeline + * @param manifestCommitId manifest commit identifier + * @param pr pull request identifier + */ +export const updateHLDToManifestPipeline = async ( + tableInfo: DeploymentTable, + hldCommitId: string, + pipelineId: string, + manifestCommitId?: string, + pr?: string, + repository?: string +): Promise => { + try { + let entries = await findMatchingDeployments( + tableInfo, + "hldCommitId", + hldCommitId + ); + + // cannot find entries by hldCommitId. + // attempt to find entries by pr + if ((!entries || entries.length === 0) && pr) { + entries = await findMatchingDeployments(tableInfo, "pr", pr); + } + return updateHLDtoManifestHelper( + entries, + tableInfo, + hldCommitId, + pipelineId, + manifestCommitId, + pr, + repository + ); + } catch (err) { + throw buildError( + errorStatusCode.AZURE_STORAGE_OP_ERR, + "deployment-table-update-hld-manifest-pipeline-failed", + err + ); + } +}; + /** * Updates manifest commit identifier in the storage for a pipeline identifier in HLD to manifest pipeline * @param tableInfo table info interface containing information about the deployment storage table @@ -534,117 +647,3 @@ export const updateManifestCommitId = async ( values: [manifestCommitId], }); }; - -/** - * Finds matching deployments for a filter name and filter value in the storage - * @param tableInfo table info interface containing information about the deployment storage table - * @param filterName name of the filter, such as `imageTag` - * @param filterValue value of the filter, such as `hello-spk-master-1234` - */ -export const findMatchingDeployments = ( - tableInfo: DeploymentTable, - filterName: string, - filterValue: string -): Promise => { - const tableService = getTableService(tableInfo); - const query: azure.TableQuery = new azure.TableQuery().where( - `PartitionKey eq '${tableInfo.partitionKey}'` - ); - query.and(`${filterName} eq '${filterValue}'`); - - // To get around issue https://github.com/Azure/azure-storage-node/issues/545, set below to null - const nextContinuationToken: - | azure.TableService.TableContinuationToken - // eslint-disable-next-line @typescript-eslint/no-explicit-any - | any = null; - - return new Promise((resolve, reject) => { - tableService.queryEntities( - tableInfo.tableName, - query, - nextContinuationToken, - (error, result) => { - if (!error) { - resolve(result.entries); - } else { - reject(error); - } - } - ); - }); -}; - -/** - * Inserts a new entry into the table. - * - * @param tableInfo Table Information - * @param entry entry to insert - */ -export const insertToTable = ( - tableInfo: DeploymentTable, - entry: DeploymentEntry -): Promise => { - const tableService = getTableService(tableInfo); - - return new Promise((resolve, reject) => { - tableService.insertEntity(tableInfo.tableName, entry, (err) => { - if (!err) { - resolve(); - } else { - reject(err); - } - }); - }); -}; - -/** - * Deletes self test data from table - * @param tableInfo table info object - * @param entry entry to be deleted - */ -export const deleteFromTable = ( - tableInfo: DeploymentTable, - entry: DeploymentEntry -): Promise => { - const tableService = getTableService(tableInfo); - - return new Promise((resolve, reject) => { - tableService.deleteEntity(tableInfo.tableName, entry, {}, (err) => { - if (!err) { - resolve(); - } else { - reject(err); - } - }); - }); -}; - -/** - * Updates an entry in the table. - * - * @param tableInfo Table Information - * @param entry entry to update - */ -export const updateEntryInTable = ( - tableInfo: DeploymentTable, - entry: DeploymentEntry -): Promise => { - const tableService = getTableService(tableInfo); - - return new Promise((resolve, reject) => { - tableService.replaceEntity(tableInfo.tableName, entry, (err) => { - if (!err) { - resolve(); - } else { - reject(err); - } - }); - }); -}; - -/** - * Generates a RowKey GUID 12 characters long - */ -export const getRowKey = (): string => { - return uuid().replace("-", "").substring(0, 12); -};