Skip to content
Open
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
8 changes: 8 additions & 0 deletions .changeset/humble-weeks-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@changesets/ghcommit": major
---

Removed the `commitFilesFromBuffers` and `commitFilesFromDirectory` APIs. These APIs were simple wrappers over the core `commitChangesFromBase64` API, which should be used instead. Read the file and pass the base64-encoded content to `commitChangesFromBase64` `fileChanges` directly. For example:

- `Buffer.from("hello world").toString("base64")`
- `await fs.readFile("path/to/file", "base64")`
5 changes: 5 additions & 0 deletions .changeset/slick-bottles-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@changesets/ghcommit": major
---

Removed all subpath exports. All APIs should be imported from root, e.g. `import { commitChangesFromRepo } from "@changesets/ghcommit"`.
190 changes: 60 additions & 130 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,14 @@ pnpm install @changesets/ghcommit

### Usage in github actions

All functions in this library that interact with the GitHub API require an octokit client that can execute GraphQL. If you are writing code that is designed to be run from within a GitHub Action, this can be done using the `@actions.github` library:
All functions in this library that interact with the GitHub API require an octokit client that can execute GraphQL. If you are writing code that is designed to be run from within a GitHub Action, this can be done using the `@actions/github` library:

```ts
import { getOctokit } from "@actions/github";

const octokit = getOctokit(process.env.GITHUB_TOKEN);
```

### Importing specific modules

To allow for you to produce smaller bundle sizes, the functionality exposed in this package is grouped into specific modules that only import the packages required for their use. We recommend that you import from the specific modules rather than the root of the package.

## API

All the functions below accept a single object as its argument, and share the following base arguments:
Expand All @@ -77,8 +73,66 @@ All the functions below accept a single object as its argument, and share the fo
}
```

### `commitFilesFromBase64`

> Works in Node.js and browsers

This function will add or delete specific files from a repository's branch based on the given `fileChanges` argument.

In addition to `CommitFilesBasedArgs`, this function has the following arguments:

```ts
{
/**
* The current branch, tag or commit that the new branch should be based on.
*/
base: GitBase;
/**
* The file paths, relative to git root, to add or delete from the branch on GitHub.
*/
fileChanges: {
/**
* File paths, relative to git root, to add to the repo. Content is a base64-encoded string.
*/
additions?: { path: string; content: string }[];
/**
* File paths, relative to git root, to remove from the repo.
*/
deletions?: { path: string }[];
};
}
```

Example:

```ts
import fs from "node:fs/promises";
import { context, getOctokit } from "@actions/github";
import { commitFilesFromBase64 } from "@changesets/ghcommit";

const octokit = getOctokit(process.env.GITHUB_TOKEN);

// Commit the changes to README.md based on the main branch
await commitFilesFromBase64({
octokit,
...context.repo,
branch: "new-branch-to-create",
message: "[chore] do something",
base: {
branch: "main",
},
fileChanges: {
additions: [
{ path: "README.md", content: await fs.readFile("README.md", "base64") },
],
},
});
```

### `commitChangesFromRepo`

> Works in Node.js only

This function will take an existing repository on your filesystem (defaulting to the current working directory). This function is good to use if you're usually working within the context of a git repository, such as after running `@actions/checkout` in github actions.

In addition to `CommitFilesBasedArgs`, this function has the following arguments:
Expand Down Expand Up @@ -122,7 +176,7 @@ Example:

```ts
import { context, getOctokit } from "@actions/github";
import { commitChangesFromRepo } from "@changesets/ghcommit/git";
import { commitChangesFromRepo } from "@changesets/ghcommit";

const octokit = getOctokit(process.env.GITHUB_TOKEN);

Expand Down Expand Up @@ -166,130 +220,6 @@ await commitChangesFromRepo({
});
```

### `commitFilesFromDirectory`

This function will add or delete specific files from a repository's branch based on files found on the local filesystem. This is good to use when there are specific files that need to be updated on a branch, or if many changes may have been made locally, but only some files need to be pushed.

In addition to `CommitFilesBasedArgs`, this function has the following arguments:

```ts
{
/**
* The current branch, tag or commit that the new branch should be based on.
*/
base: GitBase;
/**
* The directory to consider the root of the repository when calculating
* file paths
*/
cwd: string;
/**
* The file paths, relative to {@link workingDirectory},
* to add or delete from the branch on GitHub.
*/
fileChanges: {
/** File paths, relative to {@link workingDirectory}, to remove from the repo. */
additions?: string[];
/** File paths, relative to the repository root, to remove from the repo. */
deletions?: string[];
};
}
```

Example:

```ts
import { context, getOctokit } from "@actions/github";
import { commitFilesFromDirectory } from "@changesets/ghcommit/fs";

const octokit = getOctokit(process.env.GITHUB_TOKEN);

// Commit the changes to package.json and package-lock.json
// based on the main branch
await commitFilesFromDirectory({
octokit,
...context.repo,
branch: "new-branch-to-create",
message: "[chore] do something",
base: {
branch: "main",
},
cwd: "foo/bar",
fileChanges: {
additions: ["package-lock.json", "package.json"],
},
});

// Push just the index.html file to a new branch called docs, based off the tag v1.0.0
await commitFilesFromDirectory({
octokit,
...context.repo,
branch: "docs",
message: "[chore] do something",
force: true, // Overwrite any existing branch
base: {
tag: "v1.0.0",
},
cwd: "some-dir",
fileChanges: {
additions: ["index.html"],
},
});
```

### `commitFilesFromBuffers`

This function will add or delete specific files from a repository's branch based on Node.js `Buffers` that can be any binary data in memory. This is useful for when you want to make changes to a repository / branch without cloning a repo or interacting with a filesystem.

In addition to `CommitFilesBasedArgs`, this function has the following arguments:

```ts
{
/**
* The current branch, tag or commit that the new branch should be based on.
*/
base: GitBase;
/**
* The file changes, relative to the repository root, to make to the specified branch.
*/
fileChanges: {
additions?: Array<{
path: string;
contents: Buffer;
}>;
deletions?: string[];
};
}
```

Example:

```ts
import { context, getOctokit } from "@actions/github";
import { commitFilesFromBuffers } from "@changesets/ghcommit/node";

const octokit = getOctokit(process.env.GITHUB_TOKEN);

// Add a file called hello-world
await commitFilesFromBuffers({
octokit,
...context.repo,
branch: "new-branch-to-create",
message: "[chore] do something",
base: {
branch: "main",
},
fileChanges: {
additions: [
{
path: "hello/world.txt",
contents: Buffer.alloc(1024, "Hello, world!"),
},
],
},
});
```

## Known Limitations

Due to using the GitHub API to make changes to repository contents,
Expand Down
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
"type": "module",
"sideEffects": false,
"exports": {
".": "./dist/index.mjs",
"./core": "./dist/core.mjs",
"./fs": "./dist/fs.mjs",
"./git": "./dist/git.mjs",
"./node": "./dist/node.mjs",
".": {
"types": "./dist/index-node.d.mts",
"workerd": "./dist/index-node.mjs",
Comment thread
bluwy marked this conversation as resolved.
"edge-light": "./dist/index-node.mjs",
"browser": "./dist/index-browser.mjs",
"default": "./dist/index-node.mjs"
},
"./package.json": "./package.json"
},
"publishConfig": {
Expand Down
37 changes: 0 additions & 37 deletions src/fs.ts

This file was deleted.

5 changes: 5 additions & 0 deletions src/index-browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export { commitFilesFromBase64 } from "./core.ts";

export function commitChangesFromRepo() {
throw new Error("commitChangesFromRepo is not supported in the browser");
}
1 change: 0 additions & 1 deletion src/index.ts → src/index-node.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { commitFilesFromBase64 } from "./core.ts";
export { commitChangesFromRepo } from "./git.ts";
export { commitFilesFromDirectory } from "./fs.ts";
31 changes: 0 additions & 31 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,37 +48,6 @@ export interface CommitFilesFromBase64Args extends CommitFilesSharedArgsWithBase
fileChanges: FileChanges;
}

export interface CommitFilesFromBuffersArgs extends CommitFilesSharedArgsWithBase {
/**
* The file changes, relative to the repository root, to make to the specified branch.
*/
fileChanges: {
additions?: Array<{
path: string;
contents: Buffer;
}>;
deletions?: string[];
};
}

export interface CommitFilesFromDirectoryArgs extends CommitFilesSharedArgsWithBase {
/**
* The directory to consider the root of the repository when calculating
* file paths
*/
cwd: string;
/**
* The file paths, relative to {@link cwd},
* to add or delete from the branch on GitHub.
*/
fileChanges: {
/** File paths, relative to {@link cwd}, to remove from the repo. */
additions?: string[];
/** File paths, relative to the repository root, to remove from the repo. */
deletions?: string[];
};
}

export interface CommitChangesFromRepoArgs extends CommitFilesBasedArgs {
/**
* The directory used to find the repository root,
Expand Down
29 changes: 0 additions & 29 deletions src/node.ts

This file was deleted.

Loading