From a0f07db7fc52cc3925c0e514635b1b3d099213f0 Mon Sep 17 00:00:00 2001 From: Edaena Salinas Date: Tue, 17 Mar 2020 14:56:22 -0700 Subject: [PATCH 1/5] Add version to generated yaml --- src/lib/constants.ts | 2 ++ src/lib/fileutils.test.ts | 25 +++++++++++++++++++++++++ src/lib/fileutils.ts | 29 ++++++++++++++++++++++++++--- 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/lib/constants.ts b/src/lib/constants.ts index e4f382d2b..a47cc2e06 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -23,3 +23,5 @@ export const RENDER_HLD_PIPELINE_FILENAME = "manifest-generation.yaml"; export const SERVICE_PIPELINE_FILENAME = "build-update-hld.yaml"; export const VM_IMAGE = "ubuntu-latest"; + +export const VERSION_MESSAGE = "# GENERATED WITH SPK VERSION "; diff --git a/src/lib/fileutils.test.ts b/src/lib/fileutils.test.ts index f3f807fc1..6e504ff4e 100644 --- a/src/lib/fileutils.test.ts +++ b/src/lib/fileutils.test.ts @@ -43,6 +43,7 @@ import { generateHldLifecyclePipelineYaml, generateServiceBuildAndUpdatePipelineYaml, generateYamlScript, + getVersionMessage, sanitizeTriggerPath, serviceBuildAndUpdatePipeline, updateTriggerBranchesForServiceBuildAndUpdatePipeline @@ -60,6 +61,10 @@ beforeEach(() => { jest.clearAllMocks(); }); +jest.mock("../../package.json", () => { + return { version: "0.5" }; +}); + describe("generateAccessYaml", () => { const targetDirectory = "hld-repository"; const serviceDirectory = "my-service"; @@ -158,6 +163,7 @@ describe("generateServiceBuildAndUpdatePipelineYaml", () => { const targetDirectory = "app-repository"; const serviceDirectory = "my-service"; const writeSpy = jest.spyOn(fs, "writeFileSync"); + const appendSpy = jest.spyOn(fs, "appendFileSync"); beforeEach(() => { mockFs({ @@ -198,7 +204,13 @@ describe("generateServiceBuildAndUpdatePipelineYaml", () => { path.join(targetDirectory, serviceDirectory), [] ); + expect(writeSpy).toBeCalledWith( + expectedFilePath, + getVersionMessage(), + "utf8" + ); + expect(appendSpy).toBeCalledWith( expectedFilePath, createTestServiceBuildAndUpdatePipelineYaml( true, @@ -322,6 +334,7 @@ describe("updateTriggerBranchesForServiceBuildAndUpdatePipeline", () => { describe("generateHldLifecyclePipelineYaml", () => { const targetDirectory = "app-repository"; const writeSpy = jest.spyOn(fs, "writeFileSync"); + const appendSpy = jest.spyOn(fs, "appendFileSync"); beforeEach(() => { mockFs({ @@ -348,6 +361,11 @@ describe("generateHldLifecyclePipelineYaml", () => { generateHldLifecyclePipelineYaml(targetDirectory); expect(writeSpy).toBeCalledWith( + expectedFilePath, + getVersionMessage(), + "utf8" + ); + expect(appendSpy).toBeCalledWith( expectedFilePath, createTestHldLifecyclePipelineYaml(), "utf8" @@ -359,6 +377,8 @@ describe("generateHldLifecyclePipelineYaml", () => { describe("generateHldAzurePipelinesYaml", () => { const targetDirectory = "hld-repository"; const writeSpy = jest.spyOn(fs, "writeFileSync"); + const appendSpy = jest.spyOn(fs, "appendFileSync"); + beforeEach(() => { mockFs({ "hld-repository": {} @@ -384,6 +404,11 @@ describe("generateHldAzurePipelinesYaml", () => { generateHldAzurePipelinesYaml(targetDirectory); expect(writeSpy).toBeCalledWith( + expectedFilePath, + getVersionMessage(), + "utf8" + ); + expect(appendSpy).toBeCalledWith( expectedFilePath, createTestHldAzurePipelinesYaml(), "utf8" diff --git a/src/lib/fileutils.ts b/src/lib/fileutils.ts index 70d7b9bd5..8e6bdec50 100644 --- a/src/lib/fileutils.ts +++ b/src/lib/fileutils.ts @@ -1,4 +1,5 @@ /* eslint-disable @typescript-eslint/no-use-before-define */ +import EOL from "os"; import fs from "fs"; import yaml from "js-yaml"; import path from "path"; @@ -8,6 +9,7 @@ import { PROJECT_PIPELINE_FILENAME, RENDER_HLD_PIPELINE_FILENAME, SERVICE_PIPELINE_FILENAME, + VERSION_MESSAGE, VM_IMAGE } from "../lib/constants"; import { logger } from "../logger"; @@ -154,7 +156,9 @@ export const generateServiceBuildAndUpdatePipelineYaml = ( ringBranches, variableGroups ); - fs.writeFileSync( + + writeVersion(pipelineYamlFullPath); + fs.appendFileSync( pipelineYamlFullPath, yaml.safeDump(buildYaml, { lineWidth: Number.MAX_SAFE_INTEGER }), "utf8" @@ -478,7 +482,8 @@ export const generateHldAzurePipelinesYaml = ( `Generated ${RENDER_HLD_PIPELINE_FILENAME}. Commit and push this file to master before attempting to deploy via the command 'spk hld install-manifest-pipeline'; before running the pipeline ensure the following environment variables are available to your pipeline: ${requiredPipelineVariables}` ); - fs.writeFileSync(azurePipelinesYamlPath, hldYaml, "utf8"); + writeVersion(azurePipelinesYamlPath); + fs.appendFileSync(azurePipelinesYamlPath, hldYaml, "utf8"); }; /** @@ -669,7 +674,9 @@ export const generateHldLifecyclePipelineYaml = async ( logger.info( `Writing ${PROJECT_PIPELINE_FILENAME} file to ${azurePipelinesYamlPath}` ); - fs.writeFileSync(azurePipelinesYamlPath, lifecycleYaml, "utf8"); + + writeVersion(azurePipelinesYamlPath); + fs.appendFileSync(azurePipelinesYamlPath, lifecycleYaml, "utf8"); const requiredPipelineVariables = [ `'HLD_REPO' (Repository for your HLD in AzDo. eg. 'dev.azure.com/bhnook/fabrikam/_git/hld')`, @@ -853,3 +860,19 @@ export const generateDockerfile = (targetDirectory: string): void => { "utf8" ); }; + +/** + * Writes the spk version to the given file + * @param filePath The path to the file + */ +export const writeVersion = (filePath: string): void => { + fs.writeFileSync(filePath, getVersionMessage(), "utf8"); + fs.appendFileSync(filePath, "\n", "utf8"); +}; + +/** + * Gets the spk version message + */ +export const getVersionMessage = (): string => { + return VERSION_MESSAGE + require("../../package.json").version; +}; From 974cde0b727dcea469b8eb54793c6ee506d1dc85 Mon Sep 17 00:00:00 2001 From: Edaena Salinas Date: Tue, 17 Mar 2020 15:26:51 -0700 Subject: [PATCH 2/5] Add spk version to yaml file --- src/commands/project/create-variable-group.test.ts | 4 ++++ src/config.ts | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/commands/project/create-variable-group.test.ts b/src/commands/project/create-variable-group.test.ts index 34b0ce249..e44b69f16 100644 --- a/src/commands/project/create-variable-group.test.ts +++ b/src/commands/project/create-variable-group.test.ts @@ -43,6 +43,10 @@ beforeEach(() => { jest.clearAllMocks(); }); +jest.mock("../../../package.json", () => { + return { version: "0.5" }; +}); + const registryName = uuid(); const variableGroupName = uuid(); const hldRepoUrl = uuid(); diff --git a/src/config.ts b/src/config.ts index 9a4794079..8d2ee17af 100644 --- a/src/config.ts +++ b/src/config.ts @@ -5,6 +5,7 @@ import fs from "fs"; import yaml from "js-yaml"; import * as os from "os"; import path from "path"; +import { writeVersion } from "./lib/fileutils"; import { getSecret } from "./lib/azure/keyvault"; import { logger } from "./logger"; import { @@ -252,7 +253,8 @@ export const write = ( throw new Error(`Pipeline yaml file name is undefined`); } - return fs.writeFileSync(path.join(targetDirectory, fileName), asYaml); + writeVersion(path.join(targetDirectory, fileName)); + return fs.appendFileSync(path.join(targetDirectory, fileName), asYaml); } }; From 28059bd136aa7d79e4c5e33c5eb257e747c9413d Mon Sep 17 00:00:00 2001 From: Edaena Salinas Date: Tue, 17 Mar 2020 17:46:37 -0700 Subject: [PATCH 3/5] Updates based on feedback --- src/lib/fileutils.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/lib/fileutils.ts b/src/lib/fileutils.ts index 8e6bdec50..4683d09dd 100644 --- a/src/lib/fileutils.ts +++ b/src/lib/fileutils.ts @@ -1,5 +1,4 @@ /* eslint-disable @typescript-eslint/no-use-before-define */ -import EOL from "os"; import fs from "fs"; import yaml from "js-yaml"; import path from "path"; @@ -866,8 +865,7 @@ export const generateDockerfile = (targetDirectory: string): void => { * @param filePath The path to the file */ export const writeVersion = (filePath: string): void => { - fs.writeFileSync(filePath, getVersionMessage(), "utf8"); - fs.appendFileSync(filePath, "\n", "utf8"); + fs.writeFileSync(filePath, `${getVersionMessage()}\n`, "utf8"); }; /** From 2a7b069404a08eaeb3f11524731a5764729dd67c Mon Sep 17 00:00:00 2001 From: Edaena Salinas Date: Tue, 17 Mar 2020 18:10:54 -0700 Subject: [PATCH 4/5] Fix unit test --- src/commands/project/create-variable-group.test.ts | 12 ++++++++---- src/lib/fileutils.test.ts | 6 +++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/commands/project/create-variable-group.test.ts b/src/commands/project/create-variable-group.test.ts index e44b69f16..3e5c16d5b 100644 --- a/src/commands/project/create-variable-group.test.ts +++ b/src/commands/project/create-variable-group.test.ts @@ -10,7 +10,10 @@ import { isExists as isBedrockFileExists, read as readBedrockFile } from "../../lib/bedrockYaml"; -import { PROJECT_PIPELINE_FILENAME } from "../../lib/constants"; +import { + PROJECT_PIPELINE_FILENAME, + VERSION_MESSAGE +} from "../../lib/constants"; import { AzureDevOpsOpts } from "../../lib/git"; import { createTempDir } from "../../lib/ioUtil"; import * as pipelineVariableGroup from "../../lib/pipelines/variableGroup"; @@ -30,6 +33,7 @@ import { setVariableGroupInBedrockFile, updateLifeCyclePipeline } from "./create-variable-group"; +import * as fileutils from "../../lib/fileutils"; beforeAll(() => { enableVerboseLogging(); @@ -43,9 +47,9 @@ beforeEach(() => { jest.clearAllMocks(); }); -jest.mock("../../../package.json", () => { - return { version: "0.5" }; -}); +jest + .spyOn(fileutils, "getVersionMessage") + .mockReturnValue(VERSION_MESSAGE + "0.5"); const registryName = uuid(); const variableGroupName = uuid(); diff --git a/src/lib/fileutils.test.ts b/src/lib/fileutils.test.ts index 6e504ff4e..f41e818b7 100644 --- a/src/lib/fileutils.test.ts +++ b/src/lib/fileutils.test.ts @@ -207,7 +207,7 @@ describe("generateServiceBuildAndUpdatePipelineYaml", () => { expect(writeSpy).toBeCalledWith( expectedFilePath, - getVersionMessage(), + `${getVersionMessage()}\n`, "utf8" ); expect(appendSpy).toBeCalledWith( @@ -362,7 +362,7 @@ describe("generateHldLifecyclePipelineYaml", () => { generateHldLifecyclePipelineYaml(targetDirectory); expect(writeSpy).toBeCalledWith( expectedFilePath, - getVersionMessage(), + `${getVersionMessage()}\n`, "utf8" ); expect(appendSpy).toBeCalledWith( @@ -405,7 +405,7 @@ describe("generateHldAzurePipelinesYaml", () => { generateHldAzurePipelinesYaml(targetDirectory); expect(writeSpy).toBeCalledWith( expectedFilePath, - getVersionMessage(), + `${getVersionMessage()}\n`, "utf8" ); expect(appendSpy).toBeCalledWith( From 451900e8de1d18d9bbb366ec876f1d51e8f274ec Mon Sep 17 00:00:00 2001 From: Dennis Seah Date: Wed, 18 Mar 2020 18:22:55 -0700 Subject: [PATCH 5/5] fix lint error --- src/lib/fileutils.ts | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/lib/fileutils.ts b/src/lib/fileutils.ts index 265a58c87..2ef1436b9 100644 --- a/src/lib/fileutils.ts +++ b/src/lib/fileutils.ts @@ -348,6 +348,21 @@ export const serviceBuildAndUpdatePipeline = ( return pipelineYaml; }; +/** + * Gets the spk version message + */ +export const getVersionMessage = (): string => { + return VERSION_MESSAGE + require("../../package.json").version; +}; + +/** + * Writes the spk version to the given file + * @param filePath The path to the file + */ +export const writeVersion = (filePath: string): void => { + fs.writeFileSync(filePath, `${getVersionMessage()}\n`, "utf8"); +}; + /** * Creates the service multistage build and update image tag pipeline. * One pipeline should exist for each service. @@ -872,18 +887,3 @@ export const generateDockerfile = (targetDirectory: string): void => { "utf8" ); }; - -/** - * Writes the spk version to the given file - * @param filePath The path to the file - */ -export const writeVersion = (filePath: string): void => { - fs.writeFileSync(filePath, `${getVersionMessage()}\n`, "utf8"); -}; - -/** - * Gets the spk version message - */ -export const getVersionMessage = (): string => { - return VERSION_MESSAGE + require("../../package.json").version; -};