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
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@ If the `branch` provided does not exist, the plugin will error. To automatically
In addition, it accepts `changes` which is an array of objects containing a `message` and a `files` object

```javascript
let { Octokit } = require("@octokit/rest");
Octokit = Octokit.plugin(require("octokit-commit-multiple-files"));
import { Octokit } from "@octokit/rest";
import { CreateOrUpdateFiles } from "octokit-commit-multiple-files";

const octokit = new Octokit();
const OctokitWithPlugin = Octokit.plugin(CreateOrUpdateFiles);

const octokit = new OctokitWithPlugin();

// Define your repository details
const owner = "your-username";
const repo = "your-repo";
const branch = "main";
const createBranch = false;

const commits = await octokit.createOrUpdateFiles({
owner,
Expand Down Expand Up @@ -71,7 +79,7 @@ const commits = await octokit.createOrUpdateFiles({
By default the plugin will append commits to an existing branch. If you want to reset the branch to the state of `base` before adding any changes, set `forkFromBaseBranch: true`:

```javascript
const commits = await octokit.rest.repos.createOrUpdateFiles({
const commits = await octokit.createOrUpdateFiles({
owner,
repo,
branch,
Expand Down
10 changes: 5 additions & 5 deletions create-or-update-files.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import CreateOrUpdateMultipleFiles from ".";
import CreateOrUpdateMultipleFilesHandler from "./create-or-update-files";
import { CreateOrUpdateFiles } from ".";
import { createOrUpdateFiles } 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 CreateOrUpdateMultipleFilesHandler(octokit, body);
return createOrUpdateFiles(octokit, body);
}

type RequestStructure = {
Expand Down Expand Up @@ -512,13 +512,13 @@ test("failure (fileToDelete is missing)", async () => {
});

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

test("Does not overwrite other methods", () => {
const TestOctokit = Octokit.plugin(CreateOrUpdateMultipleFiles);
const TestOctokit = Octokit.plugin(CreateOrUpdateFiles);
const testOctokit = new TestOctokit();
expect(testOctokit).toHaveProperty("rest.repos.acceptInvitation");
});
Expand Down
14 changes: 8 additions & 6 deletions create-or-update-files.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { Octokit } from "@octokit/rest";
import { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods";

interface FileChange {
export interface FileChange {
contents?: string | Buffer;
mode?: "100644" | "100755" | "040000" | "160000" | "120000";
type?: "blob" | "tree" | "commit";
}

interface Change {
export interface Change {
message: string;
files?: Record<string, string | Buffer | FileChange>;
filesToDelete?: string[];
ignoreDeletionFailures?: boolean;
}

interface Options {
export interface Options {
owner: string;
repo: string;
branch: string;
Expand All @@ -27,7 +27,7 @@ interface Options {
forkFromBaseBranch?: boolean;
}

interface CommitResult {
export interface CommitResult {
commits: Array<
RestEndpointMethodTypes["git"]["createCommit"]["response"]["data"]
>;
Expand Down Expand Up @@ -56,7 +56,7 @@ function isBase64(str: string | Buffer): boolean {
);
}

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

async function fileExistsInRepo(
octokit: Octokit | any,
Expand Down Expand Up @@ -414,3 +414,5 @@ const chunk = <T>(input: T[], size: number): T[][] => {
: [...arr.slice(0, -1), [...arr.slice(-1)[0], item]];
}, []);
};

export { createOrUpdateFiles };
10 changes: 6 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Octokit } from "@octokit/rest";

import createOrUpdateFilesPlugin from "./create-or-update-files";
import { createOrUpdateFiles, Options } from "./create-or-update-files";

export default function (octokit: Octokit | any) {
const CreateOrUpdateFiles = function (octokit: Octokit | any) {
return {
createOrUpdateFiles: createOrUpdateFilesPlugin.bind(null, octokit),
createOrUpdateFiles: (opts: Options) => createOrUpdateFiles(octokit, opts),
};
}
};

export { CreateOrUpdateFiles };
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
"description": "Octokit plugin to create/update multiple files at once",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"require": "./dist/index.js",
"import": "./dist/index.js",
"default": "./dist/index.js"
}
},
"files": [
"dist"
],
Expand Down