-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
157 additions
and
262 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,21 @@ | ||
const { defaults, pick } = require('lodash') | ||
const defaultDefault = require('./default-default') | ||
const {defaults, pick} = require('lodash') | ||
const {promisify} = require('bluebird') | ||
|
||
module.exports = async function contentFromFilename (gitdata, config) { | ||
config = defaults(config, { | ||
branch: 'master' | ||
}) | ||
module.exports = async function contentFromFilename (github, config) { | ||
const { | ||
branch = 'master', | ||
filename | ||
} = config | ||
|
||
const { branch, filename } = config | ||
const blob = await promisify(github.repos.getContent)(defaults({ | ||
path: filename, | ||
ref: branch | ||
}, pick(config, ['user', 'repo']))) | ||
|
||
const addRepo = defaultDefault(pick(config, ['user', 'repo'])) | ||
if (blob.type !== 'file') throw new Error('Type is not a file') | ||
|
||
try { | ||
const head = await gitdata.getReference(addRepo({ref: `heads/${branch}`})) | ||
|
||
const { tree } = await gitdata.getTree(addRepo({sha: head.object.sha})) | ||
|
||
const { sha } = tree.find((object) => object.path === filename) | ||
|
||
if (!sha) return Promise.reject(new Error(`Couldn't find ${filename}.`)) | ||
|
||
const blob = await gitdata.getBlob(addRepo({sha})) | ||
|
||
return Promise.resolve({ | ||
content: (new Buffer(blob.content, 'base64')).toString(), | ||
commit: head.object.sha | ||
}) | ||
} catch (err) { | ||
return Promise.reject(err) | ||
return { | ||
content: (new Buffer(blob.content, 'base64')).toString(), | ||
commit: blob.sha | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,44 @@ | ||
require('babel-core/polyfill') | ||
|
||
const { defaults, mapValues } = require('lodash') | ||
const {defaults} = require('lodash') | ||
const GitHubApi = require('github') | ||
const promisify = require('es6-promisify') | ||
const {promisify} = require('bluebird') | ||
|
||
const contentFromFilename = require('./content-from-filename') | ||
const updateFileWithContent = require('./update-file-with-content') | ||
|
||
var github = new GitHubApi({ | ||
version: '3.0.0' | ||
}) | ||
const gitdata = mapValues(github.gitdata, promisify) | ||
|
||
module.exports = async function (config, callback) { | ||
module.exports = async function (config) { | ||
const { | ||
branch = 'master', | ||
newBranch, | ||
token, | ||
transform | ||
transform, | ||
force | ||
} = config | ||
|
||
try { | ||
github.authenticate({type: 'oauth', token}) | ||
|
||
const content = await contentFromFilename(gitdata, config) | ||
const newContent = transform(content.content) | ||
|
||
var transformedConfig = {} | ||
if (typeof newContent === 'string') transformedConfig.content = newContent | ||
else transformedConfig = newContent | ||
let github = config.github | ||
|
||
config = defaults(transformedConfig, {sha: content.commit}, config) | ||
if (!github) { | ||
github = new GitHubApi() | ||
github.authenticate({type: 'oauth', token}) | ||
} | ||
|
||
const commit = await updateFileWithContent(gitdata, config) | ||
if (newBranch) { | ||
const reference = await promisify(github.gitdata.getReference)(defaults({ | ||
ref: `heads/${branch}` | ||
}, config)) | ||
|
||
const { | ||
push, | ||
pr | ||
} = config | ||
await promisify(github.gitdata[force ? 'updateReference' : 'createReference'])(defaults({ | ||
sha: reference.object.sha, | ||
ref: (force ? '' : 'refs/') + `heads/${newBranch}`, | ||
force | ||
}, config)) | ||
} | ||
|
||
if (!(pr || push)) return callback(null, commit) | ||
const content = await contentFromFilename(github, config) | ||
const newContent = transform(content.content) | ||
|
||
if (push) { | ||
return github.gitdata.updateReference( | ||
defaults({ | ||
ref: `heads/${branch}`, | ||
sha: commit.sha | ||
}, config), | ||
callback | ||
) | ||
} | ||
var transformedConfig = {} | ||
if (typeof newContent === 'string') transformedConfig.content = newContent | ||
else transformedConfig = newContent | ||
|
||
github.pullRequests.create(defaults(pr, config, { | ||
base: branch, | ||
head: commit.sha | ||
}), callback) | ||
} catch (err) { | ||
callback(err) | ||
} | ||
return await updateFileWithContent(github, defaults(transformedConfig, {sha: content.commit, newBranch: newBranch || branch}, config)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,18 @@ | ||
const { pick } = require('lodash') | ||
const {defaults, pick} = require('lodash') | ||
const {promisify} = require('bluebird') | ||
|
||
const defaultDefault = require('./default-default') | ||
|
||
module.exports = async function (gitdata, config) { | ||
const { content, filename, sha } = config | ||
module.exports = async function (github, config) { | ||
const {newBranch, content, filename, sha, committer, author} = config | ||
const message = config.message || `chore: updated ${filename}` | ||
|
||
const addRepo = defaultDefault(pick(config, ['user', 'repo'])) | ||
|
||
try { | ||
const tree = await gitdata.createTree(addRepo({ | ||
base_tree: sha, | ||
tree: [{ | ||
path: filename, | ||
mode: '100644', | ||
type: 'blob', | ||
content | ||
}] | ||
})) | ||
|
||
const commit = await gitdata.createCommit(addRepo({ | ||
message, | ||
tree: tree.sha, | ||
parents: [sha] | ||
})) | ||
const response = await promisify(github.repos.updateFile)(defaults({ | ||
path: filename, | ||
message, | ||
content: (new Buffer(content, 'utf8')).toString('base64'), | ||
sha, | ||
branch: newBranch, | ||
committer: committer || author | ||
}, pick(config, ['user', 'repo']))) | ||
|
||
return Promise.resolve(commit) | ||
} catch (err) { | ||
return Promise.reject(err) | ||
} | ||
return response.commit | ||
} |
Oops, something went wrong.