Skip to content

Commit 0299f74

Browse files
committed
Add forkFromBaseBranch support
1 parent 0ff4ad9 commit 0299f74

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,27 @@ const commits = await octokit.rest.repos.createOrUpdateFiles({
6868
});
6969
```
7070

71+
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`:
72+
73+
```javascript
74+
const commits = await octokit.rest.repos.createOrUpdateFiles({
75+
owner,
76+
repo,
77+
branch,
78+
createBranch: true,
79+
base: "main",
80+
forkFromBaseBranch: true
81+
changes: [
82+
{
83+
message: "Your commit message",
84+
files: {
85+
"test.md": `This example wiped out any previous commits on 'branch' before adding this change`,
86+
},
87+
},
88+
],
89+
});
90+
```
91+
7192
In addition, you can set the `mode` of a file change. For example, if you wanted to update a submodule pointer:
7293

7394
```javascript

create-or-update-files.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,24 @@ module.exports = function (octokit, opts) {
3232
author,
3333
changes,
3434
batchSize,
35+
forkFromBaseBranch,
3536
} = opts;
3637

3738
let branchAlreadyExists = true;
3839
let baseTree;
3940

4041
// Does the target branch already exist?
4142
baseTree = await loadRef(octokit, owner, repo, branchName);
42-
if (!baseTree) {
43-
if (!createBranch) {
43+
if (!baseTree || forkFromBaseBranch) {
44+
if (!createBranch && !baseTree) {
4445
return reject(
4546
`The branch '${branchName}' doesn't exist and createBranch is 'false'`
4647
);
4748
}
4849

49-
branchAlreadyExists = false;
50+
if (!baseTree) {
51+
branchAlreadyExists = false;
52+
}
5053

5154
// If not we use the base branch. If not provided, use the
5255
// default from the repo

create-or-update-files.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,22 @@ test(`success (branch exists)`, async () => {
194194
await expect(run(body)).resolves.toEqual(mockCommitList);
195195
});
196196

197+
test(`success (branch exists, forkFromBaseBranch: true)`, async () => {
198+
const body = {
199+
...validRequest,
200+
forkFromBaseBranch: true,
201+
};
202+
mockGetRef(base, `sha-${base}`, true);
203+
mockGetRef(branch, `sha-${branch}`, true);
204+
mockCreateBlobFileOne();
205+
mockCreateBlobFileTwo();
206+
mockCreateTree(`sha-${base}`);
207+
mockCommit(`sha-${base}`);
208+
mockUpdateRef(branch);
209+
210+
await expect(run(body)).resolves.toEqual(mockCommitList);
211+
});
212+
197213
test(`success (base64 encoded body)`, async () => {
198214
const body = {
199215
...validRequest,

0 commit comments

Comments
 (0)