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

Commit

Permalink
Check that repository exits for installing pipeline (#440)
Browse files Browse the repository at this point in the history
* Check that repository exits for installing pipeline

* Add repository validation to other commands

* Remove repositoryExists function

* Update unit tests

Co-authored-by: Edaena Salinas <edaena@Edaenas-MBP.hsd1.ca.comcast.net>
Co-authored-by: Edaena Salinas <edaena@Edaenas-MacBook-Pro.local>
  • Loading branch information
3 people authored Mar 25, 2020
1 parent 1cc3263 commit ea8a656
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/commands/hld/pipeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ afterAll(() => {
disableVerboseLogging();
});

jest.spyOn(azdo, "repositoryHasFile").mockReturnValue(Promise.resolve());
jest.spyOn(azdo, "validateRepository").mockReturnValue(Promise.resolve());

describe("test emptyStringIfUndefined function", () => {
it("pass in undefined", () => {
Expand Down
5 changes: 3 additions & 2 deletions src/commands/hld/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from "azure-devops-node-api/interfaces/BuildInterfaces";
import commander from "commander";
import { Config } from "../../config";
import { repositoryHasFile } from "../../lib/azdoClient";
import { validateRepository } from "../../lib/azdoClient";
import { build as buildCmd, exit as exitCmd } from "../../lib/commandBuilder";
import {
BUILD_SCRIPT_URL,
Expand Down Expand Up @@ -200,7 +200,8 @@ export const execute = async (
};

// By default the version descriptor is for the master branch
await repositoryHasFile(
await validateRepository(
opts.devopsProject,
RENDER_HLD_PIPELINE_FILENAME,
opts.yamlFileBranch ? opts.yamlFileBranch : "master",
opts.hldName,
Expand Down
10 changes: 2 additions & 8 deletions src/commands/project/pipeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ const mockValues: CommandOptions = {
personalAccessToken: "PAT",
pipelineName: "pipelineName",
repoName: "repoName",
repoUrl: "repoUrl",
repoUrl: "https://dev.azure.com/myOrg/myProject/_git/myRepo",
yamlFileBranch: "master",
};

jest.spyOn(azdo, "repositoryHasFile").mockReturnValue(Promise.resolve());
jest.spyOn(azdo, "validateRepository").mockReturnValue(Promise.resolve());

const mockMissingValues: CommandOptions = {
buildScriptUrl: undefined,
Expand Down Expand Up @@ -123,12 +123,6 @@ describe("installLifecyclePipeline and execute tests", () => {
expect(exitFn).toBeCalledTimes(1);
expect(exitFn.mock.calls).toEqual([[0]]);
});
it("test execute function: missing repo url and pipeline name", async () => {
const exitFn = jest.fn();
await execute(mockMissingValues, "", exitFn);
expect(exitFn).toBeCalledTimes(1);
expect(exitFn.mock.calls).toEqual([[1]]);
});
it("test execute function: github repos not supported", async () => {
const exitFn = jest.fn();
await execute(nullValues, "", exitFn);
Expand Down
11 changes: 7 additions & 4 deletions src/commands/project/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from "azure-devops-node-api/interfaces/BuildInterfaces";
import commander from "commander";
import { Config } from "../../config";
import { repositoryHasFile } from "../../lib/azdoClient";
import { validateRepository } from "../../lib/azdoClient";
import { fileInfo as bedrockFileInfo } from "../../lib/bedrockYaml";
import {
build as buildCmd,
Expand Down Expand Up @@ -77,14 +77,16 @@ export const fetchValidateValues = (
if (!opts.repoUrl) {
throw Error(`Repo url not defined`);
}

const values: CommandOptions = {
buildScriptUrl: opts.buildScriptUrl || BUILD_SCRIPT_URL,
devopsProject: opts.devopsProject || azureDevops?.project,
orgName: opts.orgName || azureDevops?.org,
personalAccessToken: opts.personalAccessToken || azureDevops?.access_token,
pipelineName:
opts.pipelineName || getRepositoryName(gitOriginUrl) + "-lifecycle",
repoName: getRepositoryName(gitOriginUrl),
repoName:
getRepositoryName(opts.repoUrl) || getRepositoryName(gitOriginUrl),
repoUrl: opts.repoUrl || getRepositoryUrl(gitOriginUrl),
yamlFileBranch: opts.yamlFileBranch,
};
Expand Down Expand Up @@ -233,10 +235,11 @@ export const execute = async (
personalAccessToken: values.personalAccessToken,
project: values.devopsProject,
};
await repositoryHasFile(
await validateRepository(
values.devopsProject!,
PROJECT_PIPELINE_FILENAME,
values.yamlFileBranch ? opts.yamlFileBranch : "master",
values.repoName!,
values.repoName,
accessOpts
);
await installLifecyclePipeline(values);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/service/pipeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const getMockedValues = (): CommandOptions => {
return deepClone(MOCKED_VALUES);
};

jest.spyOn(azdo, "repositoryHasFile").mockReturnValue(Promise.resolve());
jest.spyOn(azdo, "validateRepository").mockReturnValue(Promise.resolve());

describe("test fetchValues function", () => {
it("with all values set", async () => {
Expand Down
5 changes: 3 additions & 2 deletions src/commands/service/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import commander from "commander";
import path from "path";
import { Config } from "../../config";
import { repositoryHasFile } from "../../lib/azdoClient";
import { validateRepository } from "../../lib/azdoClient";
import { build as buildCmd, exit as exitCmd } from "../../lib/commandBuilder";
import {
BUILD_SCRIPT_URL,
Expand Down Expand Up @@ -90,7 +90,8 @@ export const execute = async (
path.join(serviceName, SERVICE_PIPELINE_FILENAME);

// By default the version descriptor is for the master branch
await repositoryHasFile(
await validateRepository(
opts.devopsProject,
pipelinesYamlPath,
opts.yamlFileBranch ? opts.yamlFileBranch : "master",
opts.repoName,
Expand Down
52 changes: 52 additions & 0 deletions src/lib/azdoClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getWebApi,
invalidateWebApi,
repositoryHasFile,
validateRepository,
} from "./azdoClient";
import * as azdoClient from "./azdoClient";
import { AzureDevOpsOpts } from "./git";
Expand Down Expand Up @@ -144,6 +145,57 @@ describe("test getBuildApi function", () => {
});
});

describe("validateRepository", () => {
test("repository exists", async () => {
const getRepositoryFunc = jest.spyOn(azure, "GitAPI");
getRepositoryFunc.mockReturnValueOnce(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Promise.resolve({ getRepository: () => ({ id: "3839fjfkj" }) } as any)
);
const getItemFunc = jest.spyOn(azure, "GitAPI");
getItemFunc.mockReturnValueOnce(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Promise.resolve({ getItem: () => ({ commitId: "3839fjfkj" }) } as any)
);
const accessOpts: AzureDevOpsOpts = {
orgName: "testOrg",
personalAccessToken: "mytoken",
project: "testProject",
};

await expect(
validateRepository(
"my-project",
"myFile",
"master",
"my-repo",
accessOpts
)
).resolves.not.toThrow();
});
test("repository does not exist", async () => {
const createPullRequestFunc = jest.spyOn(azure, "GitAPI");
createPullRequestFunc.mockReturnValueOnce(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Promise.resolve({ getRepository: () => null } as any)
);
const accessOpts: AzureDevOpsOpts = {
orgName: "testOrg",
personalAccessToken: "mytoken",
project: "testProject",
};
await expect(
validateRepository(
"my-project",
"myFile",
"master",
"my-repo",
accessOpts
)
).rejects.toThrow();
});
});

describe("repositoryHasFile", () => {
test("repository contains the given file", async () => {
const createPullRequestFunc = jest.spyOn(azure, "GitAPI");
Expand Down
27 changes: 27 additions & 0 deletions src/lib/azdoClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,30 @@ export const repositoryHasFile = async (
);
}
};

/**
* Validates if a repository exists and if it contains the given file
* @param project The Azure DevOps project name
* @param fileName The name of the file
* @param branch The branch name
* @param repoName The name of the repository
* @param accessOpts The Azure DevOps access options to the repository
*/
export const validateRepository = async (
project: string,
fileName: string,
branch: string,
repoName: string,
accessOpts: AzureDevOpsOpts
): Promise<void> => {
const gitApi = await GitAPI(accessOpts);
const repo = await gitApi.getRepository(repoName, project);

if (!repo) {
throw Error(
`Project '${project}' does not contain repository '${repoName}'.`
);
}

await repositoryHasFile(fileName, branch, repoName, accessOpts);
};

0 comments on commit ea8a656

Please sign in to comment.