Skip to content

Commit

Permalink
feat: allow transform function to transform config
Browse files Browse the repository at this point in the history
  • Loading branch information
christophwitzko committed Aug 14, 2015
1 parent 8f55a5a commit 6747e20
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
18 changes: 12 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ const gitdata = mapValues(github.gitdata, promisify)
module.exports = async function (config, callback) {
const {
branch = 'master',
push,
pr,
token,
transform
} = config
Expand All @@ -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)

Expand Down
34 changes: 28 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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: [{
Expand All @@ -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',
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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: {
Expand Down Expand Up @@ -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({
Expand All @@ -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)
})
})

0 comments on commit 6747e20

Please sign in to comment.