Skip to content

Commit 962486f

Browse files
feat: create branch after transform
1 parent 6401691 commit 962486f

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

src/content-from-filename.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const {defaults, pick} = require('lodash')
1+
const {defaults} = require('lodash')
22
const {promisify} = require('bluebird')
33

44
module.exports = async function contentFromFilename (github, config) {
@@ -10,7 +10,7 @@ module.exports = async function contentFromFilename (github, config) {
1010
const blob = await promisify(github.repos.getContent)(defaults({
1111
path: filename,
1212
ref: branch
13-
}, pick(config, ['user', 'repo'])))
13+
}, config))
1414

1515
if (blob.type !== 'file') throw new Error('Type is not a file')
1616

src/index.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const updateFileWithContent = require('./update-file-with-content')
88
module.exports = async function (config) {
99
const {
1010
branch = 'master',
11-
newBranch,
1211
token,
1312
transform,
1413
force
@@ -21,6 +20,14 @@ module.exports = async function (config) {
2120
github.authenticate({type: 'oauth', token})
2221
}
2322

23+
const content = await contentFromFilename(github, config)
24+
const newContent = transform(content.content)
25+
26+
var transformedConfig = {}
27+
if (typeof newContent === 'string') transformedConfig.content = newContent
28+
else transformedConfig = newContent
29+
30+
const newBranch = transformedConfig.newBranch || config.newBranch
2431
if (newBranch) {
2532
const reference = await promisify(github.gitdata.getReference)(defaults({
2633
ref: `heads/${branch}`
@@ -33,12 +40,5 @@ module.exports = async function (config) {
3340
}, config))
3441
}
3542

36-
const content = await contentFromFilename(github, config)
37-
const newContent = transform(content.content)
38-
39-
var transformedConfig = {}
40-
if (typeof newContent === 'string') transformedConfig.content = newContent
41-
else transformedConfig = newContent
42-
43-
return await updateFileWithContent(github, defaults(transformedConfig, {sha: content.commitSha, newBranch: newBranch || branch}, config))
43+
return await updateFileWithContent(github, defaults({sha: content.commitSha, branch: newBranch || branch}, transformedConfig, config))
4444
}

src/update-file-with-content.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
const {defaults, pick} = require('lodash')
1+
const {defaults} = require('lodash')
22
const {promisify} = require('bluebird')
33

44
module.exports = async function (github, config) {
5-
const {newBranch, content, filename, sha, committer, author} = config
6-
const message = config.message || `chore: updated ${filename}`
5+
const {
6+
message = `chore: updated ${config.filename}`,
7+
content,
8+
filename,
9+
committer,
10+
author
11+
} = config
712

813
const response = await promisify(github.repos.updateFile)(defaults({
914
path: filename,
1015
message,
1116
content: Buffer.from(content).toString('base64'),
12-
sha,
13-
branch: newBranch,
1417
committer: committer || author
15-
}, pick(config, ['user', 'repo'])))
18+
}, config))
1619

1720
return response.commit
1821
}

0 commit comments

Comments
 (0)