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

Commit e1e0008

Browse files
evanlouiemtarng
andauthored
Remove ./ From Path Triggers (#404)
- Path triggers for service build update pipelines are now sanitized to not include any leading `./` in the path. - Removed old `tslint` flags. fixes microsoft/bedrock#1168 Co-authored-by: Michael Tarng <[email protected]>
1 parent ef615d5 commit e1e0008

File tree

11 files changed

+68
-49
lines changed

11 files changed

+68
-49
lines changed

src/commands/deployment/get.ts

-3
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ export const getDeployments = (
188188
const deployments: IDeployment[] | undefined = tuple[0];
189189
const syncStatuses: ITag[] | undefined = tuple[1];
190190
if (values.outputFormat === OUTPUT_FORMAT.JSON) {
191-
// tslint:disable-next-line: no-console
192191
console.log(JSON.stringify(deployments, null, 2));
193192
resolve(deployments);
194193
} else {
@@ -378,7 +377,6 @@ export const printDeployments = (
378377
header = header.concat(["Cluster Sync"]);
379378
}
380379

381-
// tslint:disable: object-literal-sort-keys
382380
const table = new Table({
383381
head: header,
384382
chars: {
@@ -481,7 +479,6 @@ export const printDeployments = (
481479
table.push(row);
482480
});
483481

484-
// tslint:disable-next-line: no-console
485482
console.log(table.toString());
486483
return table;
487484
} else {

src/commands/hld/reconcile.test.ts

-3
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,6 @@ describe("addChartToRing", () => {
286286
k8sBackendPort: 1337
287287
};
288288

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

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

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

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

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

src/commands/service/create.ts

-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ export const validateGitUrl = async (
194194
let isHelmConfigHttp = true;
195195

196196
try {
197-
// tslint:disable-next-line: no-unused-expression
198197
new URL(gitUrl);
199198
} catch (err) {
200199
logger.warn(

src/commands/setup.ts

-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ export const execute = async (
9494
opts: CommandOptions,
9595
exitFn: (status: number) => Promise<void>
9696
): Promise<void> => {
97-
// tslint:disable-next-line: no-unnecessary-initializer
9897
let requestContext: RequestContext | undefined = undefined;
9998

10099
try {

src/lib/fileutils.test.ts

+40-4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343
generateHldLifecyclePipelineYaml,
4444
generateServiceBuildAndUpdatePipelineYaml,
4545
generateYamlScript,
46+
sanitizeTriggerPath,
4647
serviceBuildAndUpdatePipeline,
4748
updateTriggerBranchesForServiceBuildAndUpdatePipeline
4849
} from "./fileutils";
@@ -222,7 +223,7 @@ describe("generateServiceBuildAndUpdatePipelineYaml", () => {
222223
});
223224

224225
test("path trigger is injected when the path is not the root of the project (not: ./)", () => {
225-
for (const p of ["./my-service", "./foo/bar/baz"]) {
226+
for (const p of ["my-service", "foo/bar/baz"]) {
226227
const serviceYaml = serviceBuildAndUpdatePipeline("my-service", p, [
227228
"master"
228229
]);
@@ -236,7 +237,7 @@ describe("generateServiceBuildAndUpdatePipelineYaml", () => {
236237
["master"]
237238
);
238239
expect(yamlWithNoDot?.trigger?.paths).toStrictEqual({
239-
include: ["./another-service"]
240+
include: ["another-service"]
240241
});
241242
});
242243
});
@@ -633,7 +634,7 @@ describe("serviceBuildUpdatePipeline", () => {
633634
const variableGroups = [uuid(), uuid()];
634635

635636
for (const serviceReference of serviceReferences) {
636-
await generateServiceBuildAndUpdatePipelineYaml(
637+
generateServiceBuildAndUpdatePipelineYaml(
637638
randomDirPath,
638639
ringBranches,
639640
serviceReference.serviceName,
@@ -652,7 +653,7 @@ describe("serviceBuildUpdatePipeline", () => {
652653
)
653654
);
654655
const hasCorrectIncludes = azureYaml.trigger!.paths!.include!.includes(
655-
"./" + path.relative(randomDirPath, serviceReference.servicePath)
656+
path.relative(randomDirPath, serviceReference.servicePath)
656657
);
657658
expect(hasCorrectIncludes).toBe(true);
658659

@@ -683,3 +684,38 @@ describe("generateYamlScript", () => {
683684
expect(generated.startsWith("set -e\n")).toBe(true);
684685
});
685686
});
687+
688+
describe("sanitizeTriggerPath", () => {
689+
const tests: {
690+
name: string;
691+
expected: ReturnType<typeof sanitizeTriggerPath>;
692+
actual: ReturnType<typeof sanitizeTriggerPath>;
693+
}[] = [
694+
{
695+
name: "removes ./ when present",
696+
expected: "foo/bar",
697+
actual: sanitizeTriggerPath("./foo/bar")
698+
},
699+
{
700+
name: "should only remove one slash",
701+
expected: "/foo/bar",
702+
actual: sanitizeTriggerPath(".//foo/bar")
703+
},
704+
{
705+
name: "does nothing if not starting with ./",
706+
expected: "foo/bar",
707+
actual: sanitizeTriggerPath("foo/bar")
708+
},
709+
{
710+
name: "dot is escaped",
711+
expected: "a/foo/bar",
712+
actual: sanitizeTriggerPath("a/foo/bar")
713+
}
714+
];
715+
716+
for (const { name, expected, actual } of tests) {
717+
test(name, () => {
718+
expect(actual).toBe(expected);
719+
});
720+
}
721+
});

src/lib/fileutils.ts

+15-16
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,18 @@ export const generateServiceBuildAndUpdatePipelineYaml = (
161161
);
162162
};
163163

164+
/**
165+
* Sanitize the given path to format Azure DevOps can properly utilize
166+
*
167+
* Transforms:
168+
* - If present, removes leading dot-slash (`./`) prefix from the path
169+
*
170+
* @param pathLike a path-like string to sanitize
171+
*/
172+
export const sanitizeTriggerPath = (pathLike: string): string => {
173+
return pathLike.replace(/^\.\//, "");
174+
};
175+
164176
/**
165177
* Returns a build-update-hld-pipeline.yaml string
166178
* based on: https://github.com/andrebriggs/monorepo-example/blob/master/service-A/azure-pipelines.yml
@@ -176,15 +188,12 @@ export const serviceBuildAndUpdatePipeline = (
176188
ringBranches: string[],
177189
variableGroups?: string[]
178190
): AzurePipelinesYaml => {
179-
const relativeServicePathFormatted = relServicePath.startsWith("./")
180-
? relServicePath
181-
: "./" + relServicePath;
191+
const relativeServicePathFormatted = sanitizeTriggerPath(relServicePath);
182192

183-
// tslint:disable: object-literal-sort-keys
184193
const pipelineYaml: AzurePipelinesYaml = {
185194
trigger: {
186-
branches: { include: ringBranches },
187-
...(relativeServicePathFormatted === "./"
195+
branches: { include: [...new Set(ringBranches)] },
196+
...(relativeServicePathFormatted === ""
188197
? {}
189198
: { paths: { include: [relativeServicePathFormatted] } })
190199
},
@@ -358,7 +367,6 @@ export const serviceBuildAndUpdatePipeline = (
358367
}
359368
]
360369
};
361-
// tslint:enable: object-literal-sort-keys
362370

363371
const requiredPipelineVariables = [
364372
`'ACR_NAME' (name of your ACR)`,
@@ -513,7 +521,6 @@ const defaultComponentYaml = (
513521
subcomponents: [
514522
{
515523
name: componentName,
516-
// tslint:disable-next-line:object-literal-sort-keys
517524
method: "git",
518525
source: componentGit,
519526
path: componentPath
@@ -529,8 +536,6 @@ const defaultComponentYaml = (
529536
*/
530537
const manifestGenerationPipelineYaml = (): string => {
531538
// based on https://github.com/microsoft/bedrock/blob/master/gitops/azure-devops/ManifestGeneration.md#add-azure-pipelines-build-yaml
532-
// tslint:disable: object-literal-sort-keys
533-
// tslint:disable: no-empty
534539
const pipelineYaml: AzurePipelinesYaml = {
535540
trigger: {
536541
branches: {
@@ -618,8 +623,6 @@ const manifestGenerationPipelineYaml = (): string => {
618623
}
619624
]
620625
};
621-
// tslint:enable: object-literal-sort-keys
622-
// tslint:enable: no-empty
623626

624627
return yaml.safeDump(pipelineYaml, { lineWidth: Number.MAX_SAFE_INTEGER });
625628
};
@@ -667,8 +670,6 @@ export const generateHldLifecyclePipelineYaml = async (
667670
};
668671

669672
const hldLifecyclePipelineYaml = (): string => {
670-
// tslint:disable: object-literal-sort-keys
671-
// tslint:disable: no-empty
672673
const pipelineyaml: AzurePipelinesYaml = {
673674
trigger: {
674675
branches: {
@@ -750,8 +751,6 @@ const hldLifecyclePipelineYaml = (): string => {
750751
}
751752
]
752753
};
753-
// tslint:enable: object-literal-sort-keys
754-
// tslint:enable: no-empty
755754

756755
return yaml.safeDump(pipelineyaml, { lineWidth: Number.MAX_SAFE_INTEGER });
757756
};

src/lib/pipelines/pipelines.test.ts

-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ describe("It builds an azure repo pipeline definition", () => {
6363
expect(process.yamlFilename).toBe(sampleAzureConfig.yamlFilePath);
6464

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

116115
const variables = definition.variables!;
117-
// tslint:disable-next-line
118116
expect(variables["foo"].value).toBe("bar");
119117
});
120118
});

src/lib/pipelines/pipelines.ts

-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ interface Pipeline {
5959
* Interface that describes a Pipeline Configuration for an Azure DevOps
6060
* backed git repository.
6161
*/
62-
// tslint:disable-next-line: no-empty-interface
6362
export type IAzureRepoPipelineConfig = Pipeline;
6463

6564
/**

src/logger/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { createLogger, format, transports } from "winston";
22

3-
// tslint:disable:object-literal-sort-keys
43
// visit https://github.com/winstonjs/logform for format options
54
export const logger = createLogger({
65
level: "info",

src/test/mockFactory.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { VM_IMAGE } from "../lib/constants";
33
import {
44
BUILD_REPO_NAME,
55
generateYamlScript,
6+
sanitizeTriggerPath,
67
IMAGE_REPO,
78
IMAGE_TAG,
89
SAFE_SOURCE_BRANCH
@@ -22,11 +23,10 @@ export const createTestServiceBuildAndUpdatePipelineYaml = (
2223
ringBranches: string[] = ["master", "qa", "test"],
2324
variableGroups: string[] = []
2425
): AzurePipelinesYaml | string => {
25-
// tslint:disable: object-literal-sort-keys
2626
const data: AzurePipelinesYaml = {
2727
trigger: {
2828
branches: { include: ringBranches },
29-
paths: { include: [relativeServicePathFormatted] } // Only building for a single service's path.
29+
paths: { include: [sanitizeTriggerPath(relativeServicePathFormatted)] } // Only building for a single service's path.
3030
},
3131
variables: [...(variableGroups ?? []).map(group => ({ group }))],
3232
stages: [
@@ -76,7 +76,7 @@ export const createTestServiceBuildAndUpdatePipelineYaml = (
7676
`export IMAGE_TAG=${IMAGE_TAG}`,
7777
`export IMAGE_NAME=$BUILD_REPO_NAME:$IMAGE_TAG`,
7878
`echo "Image Name: $IMAGE_NAME"`,
79-
`cd ${relativeServicePathFormatted}`,
79+
`cd ${sanitizeTriggerPath(relativeServicePathFormatted)}`,
8080
`echo "az acr build -r $(ACR_NAME) --image $IMAGE_NAME ."`,
8181
`az acr build -r $(ACR_NAME) --image $IMAGE_NAME .`
8282
]),
@@ -198,7 +198,6 @@ export const createTestServiceBuildAndUpdatePipelineYaml = (
198198
}
199199
]
200200
};
201-
// tslint:enable: object-literal-sort-keys
202201

203202
return asString
204203
? yaml.safeDump(data, { lineWidth: Number.MAX_SAFE_INTEGER })
@@ -290,7 +289,6 @@ export const createTestBedrockYaml = (
290289
export const createTestHldLifecyclePipelineYaml = (
291290
asString = true
292291
): AzurePipelinesYaml | string => {
293-
// tslint:disable: object-literal-sort-keys
294292
const data: AzurePipelinesYaml = {
295293
trigger: {
296294
branches: {
@@ -373,7 +371,6 @@ export const createTestHldLifecyclePipelineYaml = (
373371
}
374372
]
375373
};
376-
// tslint:enable: object-literal-sort-keys
377374

378375
return asString
379376
? yaml.safeDump(data, { lineWidth: Number.MAX_SAFE_INTEGER })
@@ -383,7 +380,6 @@ export const createTestHldLifecyclePipelineYaml = (
383380
export const createTestHldAzurePipelinesYaml = (
384381
asString = true
385382
): AzurePipelinesYaml | string => {
386-
// tslint:disable: object-literal-sort-keys
387383
const data: AzurePipelinesYaml = {
388384
trigger: {
389385
branches: {
@@ -471,7 +467,6 @@ export const createTestHldAzurePipelinesYaml = (
471467
}
472468
]
473469
};
474-
// tslint:enable: object-literal-sort-keys
475470

476471
return asString
477472
? yaml.safeDump(data, { lineWidth: Number.MAX_SAFE_INTEGER })
@@ -486,7 +481,6 @@ export const createTestComponentYaml = (
486481
subcomponents: [
487482
{
488483
name: "traefik2",
489-
// tslint:disable-next-line:object-literal-sort-keys
490484
method: "git",
491485
source: "https://github.com/microsoft/fabrikate-definitions.git",
492486
path: "definitions/traefik2"

src/types.d.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,22 @@ export interface BedrockServiceConfig {
7171
k8sBackend?: string; // k8s service backend name for ingress routing
7272
}
7373

74+
/**@see https://docs.microsoft.com/en-us/azure/devops/pipelines/yaml-schema?view=azure-devops&tabs=schema%2Cparameter-schema#triggers */
75+
type Triggerable = {
76+
include?: string[];
77+
exclude?: string[];
78+
};
79+
7480
/**
7581
* Basic AzurePipelines Interface
7682
* @see https://github.com/andrebriggs/monorepo-example/blob/master/service-A/azure-pipelines.yml
7783
*/
7884
export interface AzurePipelinesYaml {
7985
trigger?: {
80-
branches?: {
81-
include?: string[];
82-
exclude?: string[];
83-
};
84-
paths?: {
85-
include?: string[];
86-
exclude?: string[];
87-
};
86+
batch?: boolean;
87+
branches?: Triggerable;
88+
tags?: Triggerable;
89+
paths?: Triggerable;
8890
};
8991
variables?: Array<{ group: string } | { name: string; value: string }>;
9092
pool?: {

0 commit comments

Comments
 (0)