Skip to content

Add forkFromBaseBranch support #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2023
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
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,27 @@ const commits = await octokit.rest.repos.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({
owner,
repo,
branch,
createBranch: true,
base: "main",
forkFromBaseBranch: true
changes: [
{
message: "Your commit message",
files: {
"test.md": `This example wiped out any previous commits on 'branch' before adding this change`,
},
},
],
});
```

In addition, you can set the `mode` of a file change. For example, if you wanted to update a submodule pointer:

```javascript
Expand Down
9 changes: 6 additions & 3 deletions create-or-update-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,24 @@ module.exports = function (octokit, opts) {
author,
changes,
batchSize,
forkFromBaseBranch,
} = opts;

let branchAlreadyExists = true;
let baseTree;

// Does the target branch already exist?
baseTree = await loadRef(octokit, owner, repo, branchName);
if (!baseTree) {
if (!createBranch) {
if (!baseTree || forkFromBaseBranch) {
if (!createBranch && !baseTree) {
return reject(
`The branch '${branchName}' doesn't exist and createBranch is 'false'`
);
}

branchAlreadyExists = false;
if (!baseTree) {
branchAlreadyExists = false;
}

// If not we use the base branch. If not provided, use the
// default from the repo
Expand Down
16 changes: 16 additions & 0 deletions create-or-update-files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,22 @@ test(`success (branch exists)`, async () => {
await expect(run(body)).resolves.toEqual(mockCommitList);
});

test(`success (branch exists, forkFromBaseBranch: true)`, async () => {
const body = {
...validRequest,
forkFromBaseBranch: true,
};
mockGetRef(base, `sha-${base}`, true);
mockGetRef(branch, `sha-${branch}`, true);
mockCreateBlobFileOne();
mockCreateBlobFileTwo();
mockCreateTree(`sha-${base}`);
mockCommit(`sha-${base}`);
mockUpdateRef(branch);

await expect(run(body)).resolves.toEqual(mockCommitList);
});

test(`success (base64 encoded body)`, async () => {
const body = {
...validRequest,
Expand Down