Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.

Remove ./ From Path Triggers #404

Merged
merged 2 commits into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/commands/deployment/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ export const getDeployments = (
const deployments: IDeployment[] | undefined = tuple[0];
const syncStatuses: ITag[] | undefined = tuple[1];
if (values.outputFormat === OUTPUT_FORMAT.JSON) {
// tslint:disable-next-line: no-console
console.log(JSON.stringify(deployments, null, 2));
resolve(deployments);
} else {
Expand Down Expand Up @@ -378,7 +377,6 @@ export const printDeployments = (
header = header.concat(["Cluster Sync"]);
}

// tslint:disable: object-literal-sort-keys
const table = new Table({
head: header,
chars: {
Expand Down Expand Up @@ -481,7 +479,6 @@ export const printDeployments = (
table.push(row);
});

// tslint:disable-next-line: no-console
console.log(table.toString());
return table;
} else {
Expand Down
3 changes: 0 additions & 3 deletions src/commands/hld/reconcile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ describe("addChartToRing", () => {
k8sBackendPort: 1337
};

/* tslint:disable-next-line: no-string-literal */
const addHelmChartCommand = `fab add chart --source ${git} --path ${chartPath} --branch ${branch} --type helm`;

const expectedInvocation = `cd ${ringPath} && ${addHelmChartCommand}`;
Expand Down Expand Up @@ -315,7 +314,6 @@ describe("addChartToRing", () => {
k8sBackendPort: 1337
};

/* tslint:disable-next-line: no-string-literal */
const addHelmChartCommand = `fab add chart --source ${git} --path ${chartPath} --version ${sha} --type helm`;

const expectedInvocation = `cd ${ringPath} && ${addHelmChartCommand}`;
Expand All @@ -342,7 +340,6 @@ describe("addChartToRing", () => {
k8sBackendPort: 1337
};

/* tslint:disable-next-line: no-string-literal */
const addHelmChartCommand = `fab add chart --source ${repository} --path ${chart} --type helm`;
const expectedInvocation = `cd ${ringPath} && ${addHelmChartCommand}`;

Expand Down
1 change: 0 additions & 1 deletion src/commands/service/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ export const validateGitUrl = async (
let isHelmConfigHttp = true;

try {
// tslint:disable-next-line: no-unused-expression
new URL(gitUrl);
} catch (err) {
logger.warn(
Expand Down
1 change: 0 additions & 1 deletion src/commands/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ export const execute = async (
opts: CommandOptions,
exitFn: (status: number) => Promise<void>
): Promise<void> => {
// tslint:disable-next-line: no-unnecessary-initializer
let requestContext: RequestContext | undefined = undefined;

try {
Expand Down
44 changes: 40 additions & 4 deletions src/lib/fileutils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
generateHldLifecyclePipelineYaml,
generateServiceBuildAndUpdatePipelineYaml,
generateYamlScript,
sanitizeTriggerPath,
serviceBuildAndUpdatePipeline,
updateTriggerBranchesForServiceBuildAndUpdatePipeline
} from "./fileutils";
Expand Down Expand Up @@ -222,7 +223,7 @@ describe("generateServiceBuildAndUpdatePipelineYaml", () => {
});

test("path trigger is injected when the path is not the root of the project (not: ./)", () => {
for (const p of ["./my-service", "./foo/bar/baz"]) {
for (const p of ["my-service", "foo/bar/baz"]) {
const serviceYaml = serviceBuildAndUpdatePipeline("my-service", p, [
"master"
]);
Expand All @@ -236,7 +237,7 @@ describe("generateServiceBuildAndUpdatePipelineYaml", () => {
["master"]
);
expect(yamlWithNoDot?.trigger?.paths).toStrictEqual({
include: ["./another-service"]
include: ["another-service"]
});
});
});
Expand Down Expand Up @@ -633,7 +634,7 @@ describe("serviceBuildUpdatePipeline", () => {
const variableGroups = [uuid(), uuid()];

for (const serviceReference of serviceReferences) {
await generateServiceBuildAndUpdatePipelineYaml(
generateServiceBuildAndUpdatePipelineYaml(
randomDirPath,
ringBranches,
serviceReference.serviceName,
Expand All @@ -652,7 +653,7 @@ describe("serviceBuildUpdatePipeline", () => {
)
);
const hasCorrectIncludes = azureYaml.trigger!.paths!.include!.includes(
"./" + path.relative(randomDirPath, serviceReference.servicePath)
path.relative(randomDirPath, serviceReference.servicePath)
);
expect(hasCorrectIncludes).toBe(true);

Expand Down Expand Up @@ -683,3 +684,38 @@ describe("generateYamlScript", () => {
expect(generated.startsWith("set -e\n")).toBe(true);
});
});

describe("sanitizeTriggerPath", () => {
const tests: {
name: string;
expected: ReturnType<typeof sanitizeTriggerPath>;
actual: ReturnType<typeof sanitizeTriggerPath>;
}[] = [
{
name: "removes ./ when present",
expected: "foo/bar",
actual: sanitizeTriggerPath("./foo/bar")
},
{
name: "should only remove one slash",
expected: "/foo/bar",
actual: sanitizeTriggerPath(".//foo/bar")
},
{
name: "does nothing if not starting with ./",
expected: "foo/bar",
actual: sanitizeTriggerPath("foo/bar")
},
{
name: "dot is escaped",
expected: "a/foo/bar",
actual: sanitizeTriggerPath("a/foo/bar")
}
];

for (const { name, expected, actual } of tests) {
test(name, () => {
expect(actual).toBe(expected);
});
}
});
31 changes: 15 additions & 16 deletions src/lib/fileutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,18 @@ export const generateServiceBuildAndUpdatePipelineYaml = (
);
};

/**
* Sanitize the given path to format Azure DevOps can properly utilize
*
* Transforms:
* - If present, removes leading dot-slash (`./`) prefix from the path
*
* @param pathLike a path-like string to sanitize
*/
export const sanitizeTriggerPath = (pathLike: string): string => {
return pathLike.replace(/^\.\//, "");
};

/**
* Returns a build-update-hld-pipeline.yaml string
* based on: https://github.com/andrebriggs/monorepo-example/blob/master/service-A/azure-pipelines.yml
Expand All @@ -176,15 +188,12 @@ export const serviceBuildAndUpdatePipeline = (
ringBranches: string[],
variableGroups?: string[]
): AzurePipelinesYaml => {
const relativeServicePathFormatted = relServicePath.startsWith("./")
? relServicePath
: "./" + relServicePath;
const relativeServicePathFormatted = sanitizeTriggerPath(relServicePath);

// tslint:disable: object-literal-sort-keys
const pipelineYaml: AzurePipelinesYaml = {
trigger: {
branches: { include: ringBranches },
...(relativeServicePathFormatted === "./"
branches: { include: [...new Set(ringBranches)] },
...(relativeServicePathFormatted === ""
? {}
: { paths: { include: [relativeServicePathFormatted] } })
},
Expand Down Expand Up @@ -358,7 +367,6 @@ export const serviceBuildAndUpdatePipeline = (
}
]
};
// tslint:enable: object-literal-sort-keys

const requiredPipelineVariables = [
`'ACR_NAME' (name of your ACR)`,
Expand Down Expand Up @@ -513,7 +521,6 @@ const defaultComponentYaml = (
subcomponents: [
{
name: componentName,
// tslint:disable-next-line:object-literal-sort-keys
method: "git",
source: componentGit,
path: componentPath
Expand All @@ -529,8 +536,6 @@ const defaultComponentYaml = (
*/
const manifestGenerationPipelineYaml = (): string => {
// based on https://github.com/microsoft/bedrock/blob/master/gitops/azure-devops/ManifestGeneration.md#add-azure-pipelines-build-yaml
// tslint:disable: object-literal-sort-keys
// tslint:disable: no-empty
const pipelineYaml: AzurePipelinesYaml = {
trigger: {
branches: {
Expand Down Expand Up @@ -612,8 +617,6 @@ const manifestGenerationPipelineYaml = (): string => {
}
]
};
// tslint:enable: object-literal-sort-keys
// tslint:enable: no-empty

return yaml.safeDump(pipelineYaml, { lineWidth: Number.MAX_SAFE_INTEGER });
};
Expand Down Expand Up @@ -661,8 +664,6 @@ export const generateHldLifecyclePipelineYaml = async (
};

const hldLifecyclePipelineYaml = (): string => {
// tslint:disable: object-literal-sort-keys
// tslint:disable: no-empty
const pipelineyaml: AzurePipelinesYaml = {
trigger: {
branches: {
Expand Down Expand Up @@ -744,8 +745,6 @@ const hldLifecyclePipelineYaml = (): string => {
}
]
};
// tslint:enable: object-literal-sort-keys
// tslint:enable: no-empty

return yaml.safeDump(pipelineyaml, { lineWidth: Number.MAX_SAFE_INTEGER });
};
Expand Down
2 changes: 0 additions & 2 deletions src/lib/pipelines/pipelines.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ describe("It builds an azure repo pipeline definition", () => {
expect(process.yamlFilename).toBe(sampleAzureConfig.yamlFilePath);

const variables = definition.variables!;
// tslint:disable-next-line
expect(variables["foo"].value).toBe("bar");
});
});
Expand Down Expand Up @@ -114,7 +113,6 @@ describe("It builds a github repo pipeline definition", () => {
expect(process.yamlFilename).toBe(sampleGithubConfig.yamlFilePath);

const variables = definition.variables!;
// tslint:disable-next-line
expect(variables["foo"].value).toBe("bar");
});
});
1 change: 0 additions & 1 deletion src/lib/pipelines/pipelines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ interface Pipeline {
* Interface that describes a Pipeline Configuration for an Azure DevOps
* backed git repository.
*/
// tslint:disable-next-line: no-empty-interface
export type IAzureRepoPipelineConfig = Pipeline;

/**
Expand Down
1 change: 0 additions & 1 deletion src/logger/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createLogger, format, transports } from "winston";

// tslint:disable:object-literal-sort-keys
// visit https://github.com/winstonjs/logform for format options
export const logger = createLogger({
level: "info",
Expand Down
12 changes: 3 additions & 9 deletions src/test/mockFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { VM_IMAGE } from "../lib/constants";
import {
BUILD_REPO_NAME,
generateYamlScript,
sanitizeTriggerPath,
IMAGE_REPO,
IMAGE_TAG,
SAFE_SOURCE_BRANCH
Expand All @@ -22,11 +23,10 @@ export const createTestServiceBuildAndUpdatePipelineYaml = (
ringBranches: string[] = ["master", "qa", "test"],
variableGroups: string[] = []
): AzurePipelinesYaml | string => {
// tslint:disable: object-literal-sort-keys
const data: AzurePipelinesYaml = {
trigger: {
branches: { include: ringBranches },
paths: { include: [relativeServicePathFormatted] } // Only building for a single service's path.
paths: { include: [sanitizeTriggerPath(relativeServicePathFormatted)] } // Only building for a single service's path.
},
variables: [...(variableGroups ?? []).map(group => ({ group }))],
stages: [
Expand Down Expand Up @@ -76,7 +76,7 @@ export const createTestServiceBuildAndUpdatePipelineYaml = (
`export IMAGE_TAG=${IMAGE_TAG}`,
`export IMAGE_NAME=$BUILD_REPO_NAME:$IMAGE_TAG`,
`echo "Image Name: $IMAGE_NAME"`,
`cd ${relativeServicePathFormatted}`,
`cd ${sanitizeTriggerPath(relativeServicePathFormatted)}`,
`echo "az acr build -r $(ACR_NAME) --image $IMAGE_NAME ."`,
`az acr build -r $(ACR_NAME) --image $IMAGE_NAME .`
]),
Expand Down Expand Up @@ -198,7 +198,6 @@ export const createTestServiceBuildAndUpdatePipelineYaml = (
}
]
};
// tslint:enable: object-literal-sort-keys

return asString
? yaml.safeDump(data, { lineWidth: Number.MAX_SAFE_INTEGER })
Expand Down Expand Up @@ -290,7 +289,6 @@ export const createTestBedrockYaml = (
export const createTestHldLifecyclePipelineYaml = (
asString = true
): AzurePipelinesYaml | string => {
// tslint:disable: object-literal-sort-keys
const data: AzurePipelinesYaml = {
trigger: {
branches: {
Expand Down Expand Up @@ -373,7 +371,6 @@ export const createTestHldLifecyclePipelineYaml = (
}
]
};
// tslint:enable: object-literal-sort-keys

return asString
? yaml.safeDump(data, { lineWidth: Number.MAX_SAFE_INTEGER })
Expand All @@ -383,7 +380,6 @@ export const createTestHldLifecyclePipelineYaml = (
export const createTestHldAzurePipelinesYaml = (
asString = true
): AzurePipelinesYaml | string => {
// tslint:disable: object-literal-sort-keys
const data: AzurePipelinesYaml = {
trigger: {
branches: {
Expand Down Expand Up @@ -465,7 +461,6 @@ export const createTestHldAzurePipelinesYaml = (
}
]
};
// tslint:enable: object-literal-sort-keys

return asString
? yaml.safeDump(data, { lineWidth: Number.MAX_SAFE_INTEGER })
Expand All @@ -480,7 +475,6 @@ export const createTestComponentYaml = (
subcomponents: [
{
name: "traefik2",
// tslint:disable-next-line:object-literal-sort-keys
method: "git",
source: "https://github.com/microsoft/fabrikate-definitions.git",
path: "definitions/traefik2"
Expand Down
18 changes: 10 additions & 8 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,22 @@ export interface BedrockServiceConfig {
k8sBackend?: string; // k8s service backend name for ingress routing
}

/**@see https://docs.microsoft.com/en-us/azure/devops/pipelines/yaml-schema?view=azure-devops&tabs=schema%2Cparameter-schema#triggers */
type Triggerable = {
include?: string[];
exclude?: string[];
};

/**
* Basic AzurePipelines Interface
* @see https://github.com/andrebriggs/monorepo-example/blob/master/service-A/azure-pipelines.yml
*/
export interface AzurePipelinesYaml {
trigger?: {
branches?: {
include?: string[];
exclude?: string[];
};
paths?: {
include?: string[];
exclude?: string[];
};
batch?: boolean;
branches?: Triggerable;
tags?: Triggerable;
paths?: Triggerable;
};
variables?: Array<{ group: string } | { name: string; value: string }>;
pool?: {
Expand Down
Loading