Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions create-or-update-files.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
const plugin = require("./create-or-update-files");
import CreateOrUpdateMultipleFiles from ".";
import CreateOrUpdateMultipleFilesHandler from "./create-or-update-files";
const { Octokit } = require("@octokit/rest");

const nock = require("nock");
nock.disableNetConnect();
const octokit = new Octokit();

function run(body: any) {
return plugin(octokit, body);
return CreateOrUpdateMultipleFilesHandler(octokit, body);
}

const validRequest = {
type RequestStructure = {
owner: string;
repo: string;
branch: string;
createBranch: boolean;
base?: string; // Marked as optional
changes?: Array<{
message: string;
files: Record<string, string | { contents: string }>;
}>;
};

const validRequest: RequestStructure = {
owner: "mheap",
repo: "test-repo",
branch: "new-branch-name",
Expand Down Expand Up @@ -43,8 +56,8 @@ const mockSubmoduleCommitList = {
};

for (let req of ["owner", "repo", "branch"]) {
const body = { ...validRequest };
delete body[req];
const body: RequestStructure = { ...validRequest };
delete body[req as keyof RequestStructure];
test(`missing parameter (${req})`, () => {
expect(run(body)).rejects.toEqual(`'${req}' is a required parameter`);
});
Expand Down Expand Up @@ -499,18 +512,18 @@ test("failure (fileToDelete is missing)", async () => {
});

test("Loads plugin", () => {
const TestOctokit = Octokit.plugin(require("."));
const TestOctokit = Octokit.plugin(CreateOrUpdateMultipleFiles);
const testOctokit = new TestOctokit();
expect(testOctokit).toHaveProperty("createOrUpdateFiles");
});

test("Does not overwrite other methods", () => {
const TestOctokit = Octokit.plugin(require("."));
const TestOctokit = Octokit.plugin(CreateOrUpdateMultipleFiles);
const testOctokit = new TestOctokit();
expect(testOctokit).toHaveProperty("rest.repos.acceptInvitation");
});

function mockGetRef(branch: string, sha: string, success: boolean) {
function mockGetRef(branch: string | undefined, sha: string, success: boolean) {
const m = nock("https://api.github.com").get(
`/repos/${owner}/${repo}/git/ref/heads%2F${branch}`,
);
Expand Down
4 changes: 2 additions & 2 deletions create-or-update-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function isBase64(str: string | Buffer): boolean {
);
}

module.exports = function (
export default function (
octokit: Octokit,
opts: Options,
): Promise<CommitResult> {
Expand Down Expand Up @@ -292,7 +292,7 @@ module.exports = function (
return reject(e);
}
});
};
}

async function fileExistsInRepo(
octokit: Octokit,
Expand Down
6 changes: 3 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Octokit } from "@octokit/rest";

const createOrUpdateFilesPlugin = require("./create-or-update-files");
import createOrUpdateFilesPlugin from "./create-or-update-files";

module.exports = function (octokit: Octokit) {
export default function (octokit: Octokit) {
return {
createOrUpdateFiles: createOrUpdateFilesPlugin.bind(null, octokit),
};
};
}