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

Commit f55bf47

Browse files
dennisseahbnookala
authored andcommitted
[HOUSEKEEPING] move functions and remove files
1 parent a319935 commit f55bf47

14 files changed

+243
-252
lines changed

bedrock.yaml

Whitespace-only changes.

src/commands/hld/pipeline.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as config from "../../config";
2-
import * as azdo from "../../lib/azdoClient";
2+
import * as azureGit from "../../lib/git/azure";
33
import { BUILD_SCRIPT_URL } from "../../lib/constants";
44
import { getRepositoryName } from "../../lib/gitutils";
55
import { disableVerboseLogging, enableVerboseLogging } from "../../logger";
@@ -59,7 +59,7 @@ afterAll(() => {
5959
disableVerboseLogging();
6060
});
6161

62-
jest.spyOn(azdo, "validateRepository").mockResolvedValue();
62+
jest.spyOn(azureGit, "validateRepository").mockResolvedValue();
6363

6464
describe("test emptyStringIfUndefined function", () => {
6565
it("pass in undefined", () => {

src/commands/hld/pipeline.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
} from "azure-devops-node-api/interfaces/BuildInterfaces";
66
import commander from "commander";
77
import { Config } from "../../config";
8-
import { validateRepository } from "../../lib/azdoClient";
8+
import { validateRepository } from "../../lib/git/azure";
99
import {
1010
build as buildCmd,
1111
exit as exitCmd,

src/commands/project/pipeline.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as azdo from "../../lib/azdoClient";
1+
import * as azdoGit from "../../lib/git/azure";
22
import { create as createBedrockYaml } from "../../lib/bedrockYaml";
33
import { createTempDir } from "../../lib/ioUtil";
44
import { disableVerboseLogging, enableVerboseLogging } from "../../logger";
@@ -41,7 +41,7 @@ const mockValues: ConfigValues = {
4141
yamlFileBranch: "master",
4242
};
4343

44-
jest.spyOn(azdo, "validateRepository").mockResolvedValue();
44+
jest.spyOn(azdoGit, "validateRepository").mockResolvedValue();
4545

4646
const mockMissingValues: CommandOptions = {
4747
buildScriptUrl: undefined,

src/commands/project/pipeline.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
} from "azure-devops-node-api/interfaces/BuildInterfaces";
66
import commander from "commander";
77
import { Config } from "../../config";
8-
import { validateRepository } from "../../lib/azdoClient";
8+
import { validateRepository } from "../../lib/git/azure";
99
import { fileInfo as bedrockFileInfo } from "../../lib/bedrockYaml";
1010
import {
1111
build as buildCmd,

src/commands/service/pipeline.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as azdo from "../../lib/azdoClient";
1+
import * as azdoGit from "../../lib/git/azure";
22
import { disableVerboseLogging, enableVerboseLogging } from "../../logger";
33

44
jest.mock("../../lib/pipelines/pipelines");
@@ -46,7 +46,7 @@ const getMockedValues = (): CommandOptions => {
4646
return deepClone(MOCKED_VALUES);
4747
};
4848

49-
jest.spyOn(azdo, "validateRepository").mockReturnValue(Promise.resolve());
49+
jest.spyOn(azdoGit, "validateRepository").mockReturnValue(Promise.resolve());
5050

5151
describe("test fetchValues function", () => {
5252
it("with all values set", async () => {

src/commands/service/pipeline.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
import commander from "commander";
77
import path from "path";
88
import { Config } from "../../config";
9-
import { validateRepository } from "../../lib/azdoClient";
9+
import { validateRepository } from "../../lib/git/azure";
1010
import { build as buildCmd, exit as exitCmd } from "../../lib/commandBuilder";
1111
import {
1212
BUILD_SCRIPT_URL,

src/lib/azdoClient.test.ts

-108
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,8 @@ import {
1010
getTaskAgentApi,
1111
getWebApi,
1212
invalidateWebApi,
13-
repositoryHasFile,
14-
validateRepository,
1513
} from "./azdoClient";
1614
import * as azdoClient from "./azdoClient";
17-
import { AzureDevOpsOpts } from "./git";
18-
import * as azure from "./git/azure";
1915

2016
describe("AzDo Pipeline utility functions", () => {
2117
test("azdo url is well formed", () => {
@@ -143,107 +139,3 @@ describe("test getBuildApi function", () => {
143139
expect(again).toBeDefined();
144140
});
145141
});
146-
147-
describe("validateRepository", () => {
148-
test("repository exists", async () => {
149-
const getRepositoryFunc = jest.spyOn(azure, "GitAPI");
150-
getRepositoryFunc.mockReturnValueOnce(
151-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
152-
Promise.resolve({ getRepository: () => ({ id: "3839fjfkj" }) } as any)
153-
);
154-
const getItemFunc = jest.spyOn(azure, "GitAPI");
155-
getItemFunc.mockReturnValueOnce(
156-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
157-
Promise.resolve({ getItem: () => ({ commitId: "3839fjfkj" }) } as any)
158-
);
159-
const accessOpts: AzureDevOpsOpts = {
160-
orgName: "testOrg",
161-
personalAccessToken: "mytoken",
162-
project: "testProject",
163-
};
164-
165-
await expect(
166-
validateRepository(
167-
"my-project",
168-
"myFile",
169-
"master",
170-
"my-repo",
171-
accessOpts
172-
)
173-
).resolves.not.toThrow();
174-
});
175-
test("repository does not exist", async () => {
176-
const createPullRequestFunc = jest.spyOn(azure, "GitAPI");
177-
createPullRequestFunc.mockReturnValueOnce(
178-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
179-
Promise.resolve({ getRepository: () => null } as any)
180-
);
181-
const accessOpts: AzureDevOpsOpts = {
182-
orgName: "testOrg",
183-
personalAccessToken: "mytoken",
184-
project: "testProject",
185-
};
186-
await expect(
187-
validateRepository(
188-
"my-project",
189-
"myFile",
190-
"master",
191-
"my-repo",
192-
accessOpts
193-
)
194-
).rejects.toThrow();
195-
});
196-
});
197-
198-
describe("repositoryHasFile", () => {
199-
test("repository contains the given file", async () => {
200-
const createPullRequestFunc = jest.spyOn(azure, "GitAPI");
201-
createPullRequestFunc.mockReturnValueOnce(
202-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
203-
Promise.resolve({ getItem: () => ({ commitId: "3839fjfkj" }) } as any)
204-
);
205-
const accessOpts: AzureDevOpsOpts = {
206-
orgName: "testOrg",
207-
personalAccessToken: "mytoken",
208-
project: "testProject",
209-
};
210-
let hasError = false;
211-
212-
try {
213-
await repositoryHasFile(
214-
"testFile.txt",
215-
"master",
216-
"test-repo",
217-
accessOpts
218-
);
219-
} catch (err) {
220-
hasError = true;
221-
}
222-
expect(hasError).toBe(false);
223-
});
224-
test("repository does not contain the given file", async () => {
225-
const createPullRequestFunc = jest.spyOn(azure, "GitAPI");
226-
createPullRequestFunc.mockReturnValueOnce(
227-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
228-
Promise.resolve({ getItem: () => null } as any)
229-
);
230-
const accessOpts: AzureDevOpsOpts = {
231-
orgName: "testOrg",
232-
personalAccessToken: "mytoken",
233-
project: "testProject",
234-
};
235-
let hasError = false;
236-
237-
try {
238-
await repositoryHasFile(
239-
"testFile2.txt",
240-
"master",
241-
"test-repo",
242-
accessOpts
243-
);
244-
} catch (err) {
245-
hasError = true;
246-
}
247-
expect(hasError).toBe(true);
248-
});
249-
});

src/lib/azdoClient.ts

-64
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { RestClient } from "typed-rest-client";
66
import { Config } from "../config";
77
import { logger } from "../logger";
88
import { AzureDevOpsOpts } from "./git";
9-
import { GitAPI } from "./git/azure";
109

1110
// Module state Variables
1211
let connection: WebApi | undefined;
@@ -121,66 +120,3 @@ export const getTaskAgentApi = async (
121120
taskAgentApi = await webApi.getTaskAgentApi();
122121
return taskAgentApi;
123122
};
124-
125-
/**
126-
* Checks if the repository has a given file.
127-
* @param fileName The name of the file
128-
* @param branch The branch name
129-
* @param repoName The name of the repository
130-
* @accessOpts The Azure DevOps access options to the repository
131-
*/
132-
export const repositoryHasFile = async (
133-
fileName: string,
134-
branch: string,
135-
repoName: string,
136-
accessOpts: AzureDevOpsOpts
137-
): Promise<void> => {
138-
const gitApi = await GitAPI(accessOpts);
139-
const versionDescriptor = { version: branch }; // change to branch
140-
const gitItem = await gitApi.getItem(
141-
repoName,
142-
fileName, // Add path to service
143-
accessOpts.project,
144-
"",
145-
undefined,
146-
undefined,
147-
undefined,
148-
undefined,
149-
versionDescriptor
150-
);
151-
152-
if (gitItem === null) {
153-
throw Error(
154-
"Error installing build pipeline. Repository does not have a " +
155-
fileName +
156-
" file."
157-
);
158-
}
159-
};
160-
161-
/**
162-
* Validates if a repository exists and if it contains the given file
163-
* @param project The Azure DevOps project name
164-
* @param fileName The name of the file
165-
* @param branch The branch name
166-
* @param repoName The name of the repository
167-
* @param accessOpts The Azure DevOps access options to the repository
168-
*/
169-
export const validateRepository = async (
170-
project: string,
171-
fileName: string,
172-
branch: string,
173-
repoName: string,
174-
accessOpts: AzureDevOpsOpts
175-
): Promise<void> => {
176-
const gitApi = await GitAPI(accessOpts);
177-
const repo = await gitApi.getRepository(repoName, project);
178-
179-
if (!repo) {
180-
throw Error(
181-
`Project '${project}' does not contain repository '${repoName}'.`
182-
);
183-
}
184-
185-
await repositoryHasFile(fileName, branch, repoName, accessOpts);
186-
};

src/lib/git/azure.test.ts

+92-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ import { WebApi } from "azure-devops-node-api";
22
import uuid from "uuid/v4";
33
import { Config } from "../../config";
44
import { disableVerboseLogging, enableVerboseLogging } from "../../logger";
5+
import { AzureDevOpsOpts } from "../git";
56
import * as gitutils from "../gitutils";
67
import {
78
createPullRequest,
89
generatePRUrl,
910
getGitOrigin,
1011
GitAPI,
12+
repositoryHasFile,
13+
validateRepository,
1114
} from "./azure";
12-
15+
import * as azure from "./azure";
1316
jest.mock("azure-devops-node-api");
1417
jest.mock("../../config");
1518

@@ -205,3 +208,91 @@ describe("test generatePRUrl function", async () => {
205208
).rejects.toThrow();
206209
});
207210
});
211+
212+
describe("validateRepository", () => {
213+
test("repository exists", async () => {
214+
const getRepositoryFunc = jest.spyOn(azure, "GitAPI");
215+
getRepositoryFunc.mockResolvedValueOnce(
216+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
217+
{ getRepository: () => ({ id: "3839fjfkj" }) } as any
218+
);
219+
220+
const getItemFunc = jest.spyOn(azure, "GitAPI");
221+
getItemFunc.mockResolvedValueOnce(
222+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
223+
{ getItem: () => ({ commitId: "3839fjfkj" }) } as any
224+
);
225+
226+
const accessOpts: AzureDevOpsOpts = {
227+
orgName: "testOrg",
228+
personalAccessToken: "mytoken",
229+
project: "testProject",
230+
};
231+
232+
await expect(
233+
validateRepository(
234+
"my-project",
235+
"myFile",
236+
"master",
237+
"my-repo",
238+
accessOpts
239+
)
240+
).resolves.not.toThrow();
241+
});
242+
test("repository does not exist", async () => {
243+
const createPullRequestFunc = jest.spyOn(azure, "GitAPI");
244+
createPullRequestFunc.mockResolvedValueOnce(
245+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
246+
{ getRepository: () => null } as any
247+
);
248+
249+
const accessOpts: AzureDevOpsOpts = {
250+
orgName: "testOrg",
251+
personalAccessToken: "mytoken",
252+
project: "testProject",
253+
};
254+
await expect(
255+
validateRepository(
256+
"my-project",
257+
"myFile",
258+
"master",
259+
"my-repo",
260+
accessOpts
261+
)
262+
).rejects.toThrow();
263+
});
264+
});
265+
266+
describe("repositoryHasFile", () => {
267+
test("repository contains the given file", async () => {
268+
const createPullRequestFunc = jest.spyOn(azure, "GitAPI");
269+
createPullRequestFunc.mockResolvedValueOnce(
270+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
271+
{ getItem: () => ({ commitId: "3839fjfkj" }) } as any
272+
);
273+
274+
const accessOpts: AzureDevOpsOpts = {
275+
orgName: "testOrg",
276+
personalAccessToken: "mytoken",
277+
project: "testProject",
278+
};
279+
await expect(
280+
repositoryHasFile("testFile.txt", "master", "test-repo", accessOpts)
281+
).resolves.not.toThrow();
282+
});
283+
test("repository does not contain the given file", async () => {
284+
jest.spyOn(azure, "GitAPI").mockResolvedValueOnce(
285+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
286+
{ getItem: () => null } as any
287+
);
288+
const accessOpts: AzureDevOpsOpts = {
289+
orgName: "testOrg",
290+
personalAccessToken: "mytoken",
291+
project: "testProject",
292+
};
293+
294+
await expect(
295+
repositoryHasFile("testFile2.txt", "master", "test-repo", accessOpts)
296+
).rejects.toThrow();
297+
});
298+
});

0 commit comments

Comments
 (0)