Skip to content

Commit 2e114e5

Browse files
committed
Rework interface to be more consistent with Octokit. Add tests
1 parent 4f7e27a commit 2e114e5

File tree

5 files changed

+478
-66
lines changed

5 files changed

+478
-66
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ npm install octokit-commit-multiple-files --save
1010

1111
## Usage
1212

13-
This plugin accepts `owner`, `repo`, `path`, `branch` and `message` like `createOrUpdateFile`.
13+
This plugin accepts `owner`, `repo`, `path`, `branch` and `message` like `.createOrUpdateFile` ([Octokit Docs](https://octokit.github.io/rest.js/#octokit-routes-repos-create-or-update-file)).
1414

15-
In addition, it accepts `changes` which is an array of objects containing a `path` and the file `contents`.
15+
If the `branch` provided does not exist, the plugin will error. To automatically create it, set `createBranch` to true. You may provide a `base` branch if you choose to do this, or the plugin will use the repo's default branch as the base.
1616

17-
It also accepts a `newBranch` parameter which will commit these changes to a different branch than the one provided. You can set `overwriteBranch` to `true` to use `branch` as the base commit, or `false` to use the latest commit on `newBranch` as the base commit.
17+
In addition, it accepts `changes` which is an array of objects containing a `path` and the file `contents`.
1818

1919
```javascript
2020
const Octokit = require("@octokit/rest").plugin(
@@ -25,8 +25,8 @@ const octokit = new Octokit();
2525
const branchName = await octokit.repos.createOrUpdateFiles({
2626
owner,
2727
repo,
28-
path,
2928
branch,
29+
createBranch,
3030
message,
3131
changes: [
3232
{

create-or-update-files.js

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,43 @@ module.exports = function(octokit, opts) {
1616
let {
1717
owner,
1818
repo,
19-
newBranch,
19+
base,
2020
branch: branchName,
21-
overwriteBranch,
21+
createBranch,
2222
changes,
2323
message
2424
} = opts;
2525

26-
if (overwriteBranch && !newBranch) {
27-
return reject(
28-
"You can only overwrite a branch if you provide a 'newBranch'"
29-
);
30-
}
31-
3226
let branchAlreadyExists = true;
33-
let targetBranch = branchName;
3427
let baseTree;
3528

3629
// Does the target branch already exist?
37-
if (newBranch) {
38-
baseTree = await loadRef(octokit, owner, repo, newBranch);
39-
targetBranch = newBranch;
40-
if (!baseTree) {
41-
branchAlreadyExists = false;
30+
baseTree = await loadRef(octokit, owner, repo, branchName);
31+
if (!baseTree) {
32+
if (!createBranch) {
33+
return reject(
34+
`The branch '${branchName}' doesn't exist and createBranch is 'false'`
35+
);
4236
}
43-
}
4437

45-
// If it doesn't exist, or we want to replace the branch then
46-
// we grab the base sha from our target branch
47-
if (overwriteBranch || !baseTree) {
48-
baseTree = await loadRef(octokit, owner, repo, branchName);
38+
branchAlreadyExists = false;
39+
40+
// If not we use the base branch. If not provided, use the
41+
// default from the repo
42+
if (!base) {
43+
// Work out the default branch
44+
base = (
45+
await octokit.repos.get({
46+
owner,
47+
repo
48+
})
49+
).data.default_branch;
50+
}
51+
52+
baseTree = await loadRef(octokit, owner, repo, base);
53+
4954
if (!baseTree) {
50-
return reject(
51-
`Unable to load branch. '${branchName}' does not exist`
52-
);
55+
return reject(`The branch '${base}' doesn't exist`);
5356
}
5457
}
5558

@@ -124,14 +127,14 @@ module.exports = function(octokit, opts) {
124127
owner,
125128
repo,
126129
force: true,
127-
ref: `${updateRefBase}heads/${targetBranch}`,
130+
ref: `${updateRefBase}heads/${branchName}`,
128131
sha: commit.sha
129132
})
130133
).data;
131134

132135
// Return the new branch name so that we can use it later
133136
// e.g. to create a pull request
134-
return resolve(targetBranch);
137+
return resolve(branchName);
135138
} catch (e) {
136139
return reject(e);
137140
}
@@ -147,5 +150,7 @@ async function loadRef(octokit, owner, repo, ref) {
147150
ref: `heads/${ref}`
148151
})
149152
).data.object.sha;
150-
} catch (e) {}
153+
} catch (e) {
154+
//console.log(e);
155+
}
151156
}

0 commit comments

Comments
 (0)