From 6747e20a2f36c4c499a8887d7ba71d96145ca4a4 Mon Sep 17 00:00:00 2001 From: Christoph Witzko Date: Fri, 14 Aug 2015 22:56:33 +0200 Subject: [PATCH] feat: allow transform function to transform config --- src/index.js | 18 ++++++++++++------ test/index.js | 34 ++++++++++++++++++++++++++++------ 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/index.js b/src/index.js index c252e1c..fa8ca76 100644 --- a/src/index.js +++ b/src/index.js @@ -15,8 +15,6 @@ const gitdata = mapValues(github.gitdata, promisify) module.exports = async function (config, callback) { const { branch = 'master', - push, - pr, token, transform } = config @@ -27,10 +25,18 @@ module.exports = async function (config, callback) { const content = await contentFromFilename(gitdata, config) const newContent = transform(content.content) - const commit = await updateFileWithContent(gitdata, defaults({ - content: newContent, - sha: content.commit - }, config)) + var transformedConfig = {} + if (typeof newContent === 'string') transformedConfig.content = newContent + else transformedConfig = newContent + + config = defaults(transformedConfig, {sha: content.commit}, config) + + const commit = await updateFileWithContent(gitdata, config) + + const { + push, + pr + } = config if (!(pr || push)) return callback(null, commit) diff --git a/test/index.js b/test/index.js index a5511a3..742f2fe 100644 --- a/test/index.js +++ b/test/index.js @@ -19,7 +19,7 @@ const new_tree_sha = 'jkl' nock('https://api.github.com') .get(`/repos/${user}/${repo}/git/refs/heads%2F${branch}`) -.times(3) +.times(4) .query({access_token}) .reply(200, { object: { @@ -28,7 +28,7 @@ nock('https://api.github.com') }) .get(`/repos/${user}/${repo}/git/trees/${heads_master_sha}`) -.times(3) +.times(4) .query({access_token}) .reply(200, { tree: [{ @@ -38,7 +38,7 @@ nock('https://api.github.com') }) .get(`/repos/${user}/${repo}/git/blobs/${file_sha}`) -.times(3) +.times(4) .query({access_token}) .reply(200, { content: 'YWJj', @@ -54,7 +54,7 @@ nock('https://api.github.com') }], base_tree: heads_master_sha }) -.times(3) +.times(4) .query({access_token}) .reply(201, { sha: new_tree_sha @@ -65,7 +65,7 @@ nock('https://api.github.com') tree: new_tree_sha, parents: [heads_master_sha] }) -.times(3) +.times(4) .query({access_token}) .reply(201, { sha: tree_sha @@ -84,6 +84,7 @@ nock('https://api.github.com') .patch(`/repos/${user}/${repo}/git/refs/heads%2F${branch}`, { sha: tree_sha }) +.times(2) .query({access_token}) .reply(201, { object: { @@ -124,7 +125,7 @@ test('create commit and send pr', (t) => { }) }) -test('create commit and push to master', (t) => { +test('create commit and push to master (transform: string)', (t) => { t.plan(2) githubChangeRemoteFile({ @@ -139,3 +140,24 @@ test('create commit and push to master', (t) => { t.is(res.object.sha, tree_sha) }) }) + +test('create commit and push to master (transform: object)', (t) => { + t.plan(2) + + githubChangeRemoteFile({ + user, + repo, + filename, + transform: (input) => { + return { + content: input.toUpperCase(), + push: true + } + }, + token: access_token + }, (err, res) => { + t.error(err) + console.log(res) + t.is(res.object.sha, tree_sha) + }) +})