Skip to content

Commit

Permalink
feat: create branch after transform
Browse files Browse the repository at this point in the history
  • Loading branch information
christophwitzko committed Aug 10, 2016
1 parent 6401691 commit 962486f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/content-from-filename.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {defaults, pick} = require('lodash')
const {defaults} = require('lodash')
const {promisify} = require('bluebird')

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

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

Expand Down
18 changes: 9 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const updateFileWithContent = require('./update-file-with-content')
module.exports = async function (config) {
const {
branch = 'master',
newBranch,
token,
transform,
force
Expand All @@ -21,6 +20,14 @@ module.exports = async function (config) {
github.authenticate({type: 'oauth', token})
}

const content = await contentFromFilename(github, config)
const newContent = transform(content.content)

var transformedConfig = {}
if (typeof newContent === 'string') transformedConfig.content = newContent
else transformedConfig = newContent

const newBranch = transformedConfig.newBranch || config.newBranch
if (newBranch) {
const reference = await promisify(github.gitdata.getReference)(defaults({
ref: `heads/${branch}`
Expand All @@ -33,12 +40,5 @@ module.exports = async function (config) {
}, config))
}

const content = await contentFromFilename(github, config)
const newContent = transform(content.content)

var transformedConfig = {}
if (typeof newContent === 'string') transformedConfig.content = newContent
else transformedConfig = newContent

return await updateFileWithContent(github, defaults(transformedConfig, {sha: content.commitSha, newBranch: newBranch || branch}, config))
return await updateFileWithContent(github, defaults({sha: content.commitSha, branch: newBranch || branch}, transformedConfig, config))
}
15 changes: 9 additions & 6 deletions src/update-file-with-content.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
const {defaults, pick} = require('lodash')
const {defaults} = require('lodash')
const {promisify} = require('bluebird')

module.exports = async function (github, config) {
const {newBranch, content, filename, sha, committer, author} = config
const message = config.message || `chore: updated ${filename}`
const {
message = `chore: updated ${config.filename}`,
content,
filename,
committer,
author
} = config

const response = await promisify(github.repos.updateFile)(defaults({
path: filename,
message,
content: Buffer.from(content).toString('base64'),
sha,
branch: newBranch,
committer: committer || author
}, pick(config, ['user', 'repo'])))
}, config))

return response.commit
}

0 comments on commit 962486f

Please sign in to comment.