From 962486fc5567e2fae2602d26976f293f9180a9ca Mon Sep 17 00:00:00 2001 From: Christoph Witzko Date: Wed, 10 Aug 2016 16:51:53 +0200 Subject: [PATCH] feat: create branch after transform --- src/content-from-filename.js | 4 ++-- src/index.js | 18 +++++++++--------- src/update-file-with-content.js | 15 +++++++++------ 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/content-from-filename.js b/src/content-from-filename.js index 6537c02..cc4b2d2 100644 --- a/src/content-from-filename.js +++ b/src/content-from-filename.js @@ -1,4 +1,4 @@ -const {defaults, pick} = require('lodash') +const {defaults} = require('lodash') const {promisify} = require('bluebird') module.exports = async function contentFromFilename (github, config) { @@ -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') diff --git a/src/index.js b/src/index.js index b9ea15b..7d4e92a 100644 --- a/src/index.js +++ b/src/index.js @@ -8,7 +8,6 @@ const updateFileWithContent = require('./update-file-with-content') module.exports = async function (config) { const { branch = 'master', - newBranch, token, transform, force @@ -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}` @@ -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)) } diff --git a/src/update-file-with-content.js b/src/update-file-with-content.js index 91776af..ced5724 100644 --- a/src/update-file-with-content.js +++ b/src/update-file-with-content.js @@ -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 }