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

Commit f6b9097

Browse files
committed
use azdo git uri
1 parent 0579b42 commit f6b9097

File tree

4 files changed

+92
-7
lines changed

4 files changed

+92
-7
lines changed

src/commands/hld/reconcile.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { when } from "jest-when";
12
import { create as createBedrockYaml } from "../../lib/bedrockYaml";
23
import { disableVerboseLogging, enableVerboseLogging } from "../../logger";
34

@@ -15,12 +16,17 @@ import {
1516
IReconcileDependencies,
1617
reconcileHld,
1718
testAndGetAbsPath,
19+
tryGetGitOrigin,
1820
validateInputs
1921
} from "./reconcile";
2022
import * as reconcile from "./reconcile";
2123

24+
import { getAzdoOriginUrl, getOriginUrl } from "../../lib/gitutils";
25+
2226
import { IBedrockFile, IBedrockServiceConfig } from "../../types";
2327

28+
jest.mock("../../lib/gitutils");
29+
2430
beforeAll(() => {
2531
enableVerboseLogging();
2632
});
@@ -98,6 +104,31 @@ describe("checkForFabrikate", () => {
98104
});
99105
});
100106

107+
describe("tryGetGitOrigin", () => {
108+
it("attempts to retrieve azdo git origin", async () => {
109+
const originUrl = "http://github.com/repo/url";
110+
111+
(getAzdoOriginUrl as jest.Mock).mockReturnValue(originUrl);
112+
113+
const originUrlResponse = await tryGetGitOrigin();
114+
expect(originUrlResponse).toEqual(originUrl);
115+
expect(getAzdoOriginUrl).toHaveBeenCalledTimes(1);
116+
});
117+
118+
it("attempts to retrieve git origin from using git cli", async () => {
119+
const originUrl = "http://github.com/repo/url";
120+
(getAzdoOriginUrl as jest.Mock).mockRejectedValue("some reason");
121+
122+
(getOriginUrl as jest.Mock).mockReturnValue(originUrl);
123+
124+
const originUrlResponse = await tryGetGitOrigin();
125+
126+
expect(originUrlResponse).toEqual(originUrl);
127+
expect(getAzdoOriginUrl).toHaveBeenCalledTimes(1);
128+
expect(getOriginUrl).toHaveBeenCalledTimes(1);
129+
});
130+
});
131+
101132
describe("testAndGetAbsPath", () => {
102133
it("fails to test and get an absolute path for a file", () => {
103134
const test = jest.fn();

src/commands/hld/reconcile.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ import yaml from "js-yaml";
55
import path from "path";
66
import process from "process";
77
import shelljs, { TestOptions } from "shelljs";
8-
import url from "url";
98
import { Bedrock } from "../../config";
109
import { assertIsStringWithContent } from "../../lib/assertions";
1110
import { build as buildCmd, exit as exitCmd } from "../../lib/commandBuilder";
1211
import { generateAccessYaml } from "../../lib/fileutils";
13-
import { getOriginUrl } from "../../lib/gitutils";
12+
import { getAzdoOriginUrl, getOriginUrl } from "../../lib/gitutils";
1413
import { TraefikIngressRoute } from "../../lib/traefik/ingress-route";
1514
import {
1615
ITraefikMiddleware,
@@ -170,7 +169,7 @@ export const execute = async (
170169
createStaticComponent,
171170
exec: execAndLog,
172171
generateAccessYaml,
173-
getGitOrigin: getOriginUrl,
172+
getGitOrigin: tryGetGitOrigin,
174173
test: shelljs.test,
175174
writeFile: writeFileSync
176175
};
@@ -211,6 +210,17 @@ export const commandDecorator = (command: commander.Command) => {
211210
);
212211
};
213212

213+
export const tryGetGitOrigin = async (
214+
absRepoPath?: string
215+
): Promise<string> => {
216+
return getAzdoOriginUrl().catch(_ => {
217+
logger.warn(
218+
"Could not get Git Origin for Azure DevOps - are you running 'spk' _not_ in a pipeline?"
219+
);
220+
return getOriginUrl(absRepoPath);
221+
});
222+
};
223+
214224
export const reconcileHld = async (
215225
dependencies: IReconcileDependencies,
216226
bedrockYaml: IBedrockFile,
@@ -231,7 +241,7 @@ export const reconcileHld = async (
231241
// Repository in HLD ie /path/to/hld/repositoryName/
232242
const absRepositoryInHldPath = path.join(absHldPath, repositoryName);
233243

234-
// Create access.yaml containing the bedrock application repo's URL in access.yaml.
244+
// Create access.yaml containing the bedrock application repo's URL in
235245
await dependencies.createAccessYaml(
236246
dependencies.getGitOrigin,
237247
dependencies.generateAccessYaml,

src/lib/gitutils.test.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
checkoutBranch,
44
commitPath,
55
deleteBranch,
6+
getAzdoOriginUrl,
67
getCurrentBranch,
78
getOriginUrl,
89
getPullRequestLink,
@@ -229,7 +230,7 @@ describe("getOriginUrl", () => {
229230
);
230231
});
231232

232-
it("should call exec with the proper git arguments", async () => {
233+
it("should call exec with the proper git arguments with a repo path", async () => {
233234
const originUrl = "foo";
234235
const repoPath = "/repo/path";
235236

@@ -266,6 +267,37 @@ describe("getOriginUrl", () => {
266267
});
267268
});
268269

270+
describe("getAzdoOriginUrl", () => {
271+
it("should call exec with the proper git arguments", async () => {
272+
const originUrl = "foo";
273+
274+
when(exec as jest.Mock)
275+
.calledWith("echo", ["$(Build.Repository.Uri)"])
276+
.mockReturnValue(originUrl);
277+
278+
const originUrlResponse = await getAzdoOriginUrl();
279+
280+
expect(originUrlResponse).toEqual(originUrl);
281+
expect(exec).toHaveBeenCalledTimes(1);
282+
expect(exec).toHaveBeenCalledWith("echo", ["$(Build.Repository.Uri)"]);
283+
});
284+
285+
it("should return an error when exec throws an error", async () => {
286+
(exec as jest.Mock).mockImplementation(() => {
287+
throw new Error("sample error.");
288+
});
289+
290+
let error: Error | undefined;
291+
try {
292+
await getAzdoOriginUrl();
293+
} catch (_) {
294+
error = _;
295+
}
296+
297+
expect(error).not.toBeUndefined();
298+
});
299+
});
300+
269301
describe("getRepositoryName", () => {
270302
it("returns the repository name for a visualstudio.com HTTPS origin url.", async () => {
271303
const originUrl =

src/lib/gitutils.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,20 @@ export const getOriginUrl = async (
127127
const safeLoggingUrl = safeGitUrlForLogging(originUrl);
128128
logger.debug(`Got git origin url ${safeLoggingUrl}`);
129129
return originUrl;
130-
} catch (_) {
131-
throw Error(`Unable to get git origin URL.: ` + _);
130+
} catch (error) {
131+
throw Error(`Unable to get git origin URL: ${error}`);
132+
}
133+
};
134+
135+
export const getAzdoOriginUrl = async (): Promise<string> => {
136+
try {
137+
const originUrl = await exec("echo", ["$(Build.Repository.Uri)"]);
138+
139+
const safeLoggingUrl = safeGitUrlForLogging(originUrl);
140+
logger.debug(`Got azdo git origin url ${safeLoggingUrl}`);
141+
return originUrl;
142+
} catch (error) {
143+
throw Error(`Unable to get azdo origin URL: ${error}`);
132144
}
133145
};
134146

0 commit comments

Comments
 (0)