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

Commit 3ae45c0

Browse files
authored
Adding config to service create-pipeline (#97)
* Working service create-pipelines via config * adding gitutil tests
1 parent 46eb0d8 commit 3ae45c0

File tree

4 files changed

+172
-15
lines changed

4 files changed

+172
-15
lines changed

src/commands/service/pipeline.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ describe("create pipeline tests", () => {
3131
"wao",
3232
"amazing",
3333
"meow",
34+
".",
3435
exitFn
3536
);
3637

@@ -49,6 +50,7 @@ describe("create pipeline tests", () => {
4950
"wao",
5051
"amazing",
5152
"meow",
53+
".",
5254
exitFn
5355
);
5456

@@ -70,6 +72,7 @@ describe("create pipeline tests", () => {
7072
"wao",
7173
"amazing",
7274
"meow",
75+
".",
7376
exitFn
7477
);
7578

@@ -90,6 +93,7 @@ describe("create pipeline tests", () => {
9093
"wao",
9194
"amazing",
9295
"meow",
96+
".",
9397
exitFn
9498
);
9599

src/commands/service/pipeline.ts

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
import commander = require("commander");
2-
import { logger } from "../../logger";
3-
41
import { IBuildApi } from "azure-devops-node-api/BuildApi";
2+
import commander = require("commander");
3+
import path from "path";
4+
import { Config } from "../../config";
5+
import {
6+
getOriginUrl,
7+
getRepositoryName,
8+
getRepositoryUrl
9+
} from "../../lib/gitutils";
510
import {
611
createPipelineForDefinition,
712
definitionForAzureRepoPipeline,
813
getBuildApiClient,
914
queueBuild
1015
} from "../../lib/pipelines/pipelines";
16+
import { logger } from "../../logger";
1117

1218
import { BuildDefinition } from "azure-devops-node-api/interfaces/BuildInterfaces";
1319

@@ -30,18 +36,32 @@ export const createPipelineCommandDecorator = (
3036
.option("-r, --repo-name <repo-name>", "Repository Name in Azure DevOps")
3137
.option("-u, --repo-url <repo-url>", "Repository URL")
3238
.option("-d, --devops-project <devops-project>", "Azure DevOps Project")
33-
.option("-l, --project-path <project-path>", "Path to Bedrock Project")
39+
.option(
40+
"-l, --packages-dir <packages-dir>",
41+
"The monorepository directory containing this service definition. ie. '--packages-dir packages' if my-service is located under ./packages/my-service."
42+
)
3443
.action(async (serviceName, opts) => {
44+
const gitOriginUrl = await getOriginUrl();
45+
46+
const { azure_devops } = Config();
3547
const {
36-
pipelineName,
37-
personalAccessToken,
38-
orgName,
39-
repoName,
40-
repoUrl,
41-
devopsProject,
42-
projectPath
48+
orgName = azure_devops && azure_devops.org,
49+
personalAccessToken = azure_devops && azure_devops.access_token,
50+
devopsProject = azure_devops && azure_devops.project,
51+
pipelineName = serviceName + "-pipeline",
52+
packagesDir = "./",
53+
repoName = getRepositoryName(gitOriginUrl),
54+
repoUrl = getRepositoryUrl(gitOriginUrl)
4355
} = opts;
4456

57+
logger.debug(`orgName: ${orgName}`);
58+
logger.debug(`personalAccessToken: ${personalAccessToken}`);
59+
logger.debug(`devopsProject: ${devopsProject}`);
60+
logger.debug(`pipelineName: ${pipelineName}`);
61+
logger.debug(`packagesDir: ${packagesDir}`);
62+
logger.debug(`repoName: ${repoName}`);
63+
logger.debug(`repoUrl: ${repoUrl}`);
64+
4565
try {
4666
if (typeof pipelineName !== "string") {
4767
throw new Error(
@@ -79,9 +99,9 @@ export const createPipelineCommandDecorator = (
7999
);
80100
}
81101

82-
if (typeof projectPath !== "string") {
102+
if (typeof packagesDir !== "string") {
83103
throw new Error(
84-
`--project-path projectPath must be of type 'string', ${typeof projectPath} given.`
104+
`--packages-dir must be of type 'string', ${typeof packagesDir} given.`
85105
);
86106
}
87107
} catch (err) {
@@ -99,6 +119,7 @@ export const createPipelineCommandDecorator = (
99119
repoName,
100120
repoUrl,
101121
devopsProject,
122+
packagesDir,
102123
process.exit
103124
);
104125
} catch (err) {
@@ -118,7 +139,6 @@ export const createPipelineCommandDecorator = (
118139
* @param repoName
119140
* @param repoUrl
120141
* @param project
121-
* @param projectPath
122142
*/
123143
export const installPipeline = async (
124144
serviceName: string,
@@ -128,6 +148,7 @@ export const installPipeline = async (
128148
repoName: string,
129149
repoUrl: string,
130150
project: string,
151+
packagesDir: string,
131152
exitFn: (status: number) => void
132153
) => {
133154
let devopsClient;
@@ -149,7 +170,7 @@ export const installPipeline = async (
149170
repositoryName: repoName,
150171
repositoryUrl: repoUrl,
151172
yamlFileBranch: "master",
152-
yamlFilePath: `packages/${serviceName}/azure-pipelines.yaml`
173+
yamlFilePath: path.join(packagesDir, serviceName, "azure-pipelines.yaml") // This may not work if we're using a non-mono repository and azure-pipelines.yaml is in the root directory.
153174
});
154175

155176
try {

src/lib/gitutils.test.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
getCurrentBranch,
77
getOriginUrl,
88
getPullRequestLink,
9+
getRepositoryName,
10+
getRepositoryUrl,
911
pushBranch
1012
} from "../lib/gitutils";
1113
import { disableVerboseLogging, enableVerboseLogging, logger } from "../logger";
@@ -222,6 +224,96 @@ describe("getOriginUrl", () => {
222224
});
223225
});
224226

227+
describe("getRepositoryName", () => {
228+
it("returns the repository name for an AzDo HTTP origin url.", async () => {
229+
const originUrl =
230+
"https://[email protected]/myorg/spk-test-project/_git/new-repo";
231+
const repositoryName = getRepositoryName(originUrl);
232+
233+
expect(repositoryName).toEqual(`new-repo`);
234+
});
235+
236+
it("returns the repository name for an AzDo SSH origin url.", async () => {
237+
const originUrl =
238+
"[email protected]:v3/mitarng/spk-test-project/new-repo";
239+
const repositoryName = getRepositoryName(originUrl);
240+
241+
expect(repositoryName).toEqual(`new-repo`);
242+
});
243+
244+
it("returns the repository name for a GitHub HTTP origin url.", async () => {
245+
const originUrl = "https://github.com/CatalystCode/spk.git";
246+
const repositoryName = getRepositoryName(originUrl);
247+
248+
expect(repositoryName).toEqual(`spk`);
249+
});
250+
251+
it("returns the repository name for a GitHub SSH origin url.", async () => {
252+
const originUrl = "[email protected]:CatalystCode/spk.git";
253+
const repositoryName = getRepositoryName(originUrl);
254+
255+
expect(repositoryName).toEqual(`spk`);
256+
});
257+
258+
it("Returns a help message for unknown or unsupported git providers.", async () => {
259+
const originUrl = "[email protected]:org/spk.git";
260+
let threwError = false;
261+
try {
262+
getRepositoryName(originUrl);
263+
} catch (e) {
264+
threwError = true;
265+
}
266+
expect(threwError).toEqual(true);
267+
});
268+
});
269+
270+
describe("getRepositoryUrl", () => {
271+
it("return a proper repo url for an AzDo HTTP origin url.", async () => {
272+
const originUrl =
273+
"https://[email protected]/myorg/spk-test-project/_git/new-repo";
274+
const repositoryUrl = getRepositoryUrl(originUrl);
275+
276+
expect(repositoryUrl).toEqual(
277+
`https://dev.azure.com/myorg/spk-test-project/_git/new-repo`
278+
);
279+
});
280+
281+
it("return a proper repo url for an AzDo SSH origin url.", async () => {
282+
const originUrl =
283+
"[email protected]:v3/mitarng/spk-test-project/new-repo";
284+
const repositoryUrl = getRepositoryUrl(originUrl);
285+
286+
expect(repositoryUrl).toEqual(
287+
`https://dev.azure.com/mitarng/spk-test-project/_git/new-repo`
288+
);
289+
});
290+
291+
it("return a proper repo url for a GitHub HTTP origin url.", async () => {
292+
const originUrl = "https://github.com/CatalystCode/spk.git";
293+
const repositoryUrl = getRepositoryUrl(originUrl);
294+
295+
expect(repositoryUrl).toEqual(`https://github.com/CatalystCode/spk`);
296+
});
297+
298+
it("return a proper repo url for a GitHub SSH origin url.", async () => {
299+
const originUrl = "[email protected]:CatalystCode/spk.git";
300+
const repositoryUrl = getRepositoryUrl(originUrl);
301+
302+
expect(repositoryUrl).toEqual(`https://github.com/CatalystCode/spk`);
303+
});
304+
305+
it("Returns a help message for unknown or unsupported git providers.", async () => {
306+
const originUrl = "[email protected]:org/spk.git";
307+
let threwError = false;
308+
try {
309+
getRepositoryUrl(originUrl);
310+
} catch (e) {
311+
threwError = true;
312+
}
313+
expect(threwError).toEqual(true);
314+
});
315+
});
316+
225317
describe("getPullRequestLink", () => {
226318
it("return a proper PR url for an AzDo HTTP origin url.", async () => {
227319
const originUrl =

src/lib/gitutils.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,46 @@ export const getOriginUrl = async (): Promise<string> => {
9999
}
100100
};
101101

102+
/**
103+
* Will return the name of the repository
104+
* Currently only AzDo and Github are supported.
105+
* @param originUrl
106+
*/
107+
export const getRepositoryName = (originUrl: string) => {
108+
const gitComponents = GitUrlParse(originUrl);
109+
if (gitComponents.resource.includes("dev.azure.com")) {
110+
logger.debug("azure devops repo found.");
111+
return gitComponents.name;
112+
} else if (gitComponents.resource === "github.com") {
113+
logger.debug("github repo found.");
114+
return gitComponents.name;
115+
} else {
116+
throw Error(
117+
"Could not determine origin repository, or it is not a supported type."
118+
);
119+
}
120+
};
121+
122+
/**
123+
* Will return the URL of the repository
124+
* Currently only AzDo and Github are supported.
125+
* @param originUrl
126+
*/
127+
export const getRepositoryUrl = (originUrl: string) => {
128+
const gitComponents = GitUrlParse(originUrl);
129+
if (gitComponents.resource.includes("dev.azure.com")) {
130+
logger.debug("azure devops repo found.");
131+
return `https://dev.azure.com/${gitComponents.organization}/${gitComponents.owner}/_git/${gitComponents.name}`;
132+
} else if (gitComponents.resource === "github.com") {
133+
logger.debug("github repo found.");
134+
return `https://github.com/${gitComponents.organization}/${gitComponents.name}`;
135+
} else {
136+
throw Error(
137+
"Could not determine origin repository, or it is not a supported type."
138+
);
139+
}
140+
};
141+
102142
/**
103143
* Will create a link to create a PR for a given origin, base branch, and new branch.
104144
* Currently only AzDo and Github are supported.

0 commit comments

Comments
 (0)