Skip to content

Commit

Permalink
Merge pull request #12 from boennemann/next
Browse files Browse the repository at this point in the history
Merge next into master
  • Loading branch information
christophwitzko committed Aug 10, 2016
2 parents 391b4c9 + bfc2059 commit 9fc64de
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 262 deletions.
4 changes: 0 additions & 4 deletions .babelrc

This file was deleted.

9 changes: 1 addition & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,7 @@ cache:
notifications:
email: false
node_js:
- 4
- iojs-v3
- iojs-v2
- iojs-v1
- '0.12'
- '0.10'
before_install:
- npm i -g npm@^2.0.0
- 6
before_script:
- npm prune
- 'curl -Lo travis_after_all.py https://git.io/vLSON'
Expand Down
55 changes: 16 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,23 @@ This module allows you to change a single file in a repository on GitHub and cre

## Examples

Create a new commit:
Create a new branch and commit on top of it:

```js
githubChangeRemoteFile({
user: 'boennemann',
repo: 'animals',
filename: 'package.json'
transform: function (pkg) {
pkg = JSON.parse(pkg)
pkg.devDependencies.standard = semver.inc(pkg.devDependencies.standard, 'major')
return JSON.stringify(pkg)
filename: 'package.json',
newBranch: 'upgrade-standard',
transform: pkg => {
const parsedPkg = JSON.parse(pkg)
pkg.devDependencies.standard = semver.inc(parsedPkg.devDependencies.standard, 'major')
return JSON.stringify(parsedPkg, null, 2)
},
token: '<github access token with sufficent rights>'
}, function (err, res) {
console.log(res.sha) // sha of the new commit
})
```

Create a new commit and send a PR:

```js
githubChangeRemoteFile({
user: 'boennemann',
repo: 'animals',
filename: 'package.json'
transform: function (pkg) {
pkg = JSON.parse(pkg)
pkg.devDependencies.standard = semver.inc(pkg.devDependencies.standard, 'major')
return JSON.stringify(pkg)
},
token: '<github access token with sufficent rights>',
pr: {
title: 'Updated standard to latest version',
body: 'whatever'
}
}, function (err, res) {
console.log(res.html_url) // url of the pr
})
.then(res => console.log(res))
.catch(console.log)
```

Create a new commit and push it on top of the (master) branch:
Expand All @@ -54,15 +33,13 @@ githubChangeRemoteFile({
user: 'boennemann',
repo: 'animals',
filename: 'package.json',
branch: 'master', // default
transform: function (pkg) {
pkg = JSON.parse(pkg)
pkg.devDependencies.standard = semver.inc(pkg.devDependencies.standard, 'major')
return JSON.stringify(pkg)
transform: pkg => {
const parsedPkg = JSON.parse(pkg)
pkg.devDependencies.standard = semver.inc(parsedPkg.devDependencies.standard, 'major')
return JSON.stringify(parsedPkg, null, 2)
},
token: '<github access token with sufficent rights>',
push: true
}, function (err, res) {
console.log(res.object.url) // url of the new commit
token: '<github access token with sufficent rights>'
})
.then(res => console.log(res))
.catch(console.log)
```
39 changes: 23 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@
"coverage": "nyc report",
"coverage:upload": "npm run -s coverage -- --reporter=text-lcov | coveralls",
"prepublish": "npm run build",
"pretest:unit": "npm run build && npm run test:build",
"pretest:unit": "npm run build",
"test": "npm run test:style && npm run test:unit",
"test:build": "rimraf .test && mkdirp .test && babel test --out-dir .test",
"test:style": "standard",
"test:unit": "nyc tap --no-cov .test/*.js",
"test:unit": "nyc tap --no-cov test/*.js",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"engines": {
"node": "6"
},
"babel": {
"plugins": [
"transform-async-to-generator"
]
},
"standard": {
"parser": "babel-eslint"
},
Expand All @@ -32,21 +39,21 @@
},
"homepage": "https://github.com/boennemann/github-change-remote-file#readme",
"devDependencies": {
"babel": "^5.8.20",
"babel-eslint": "^4.0.5",
"coveralls": "^2.11.3",
"babel-cli": "^6.11.4",
"babel-eslint": "^6.1.2",
"babel-plugin-transform-async-to-generator": "^6.8.0",
"coveralls": "^2.11.12",
"mkdirp": "^0.5.1",
"nock": "^2.9.1",
"nyc": "^3.1.0",
"rimraf": "^2.4.2",
"semantic-release": "^4.0.2",
"standard": "^5.3.1",
"tap": "^1.3.2"
"nock": "^8.0.0",
"nyc": "^7.1.0",
"rimraf": "^2.5.4",
"semantic-release": "^4.3.5",
"standard": "^7.1.2",
"tap": "^6.3.0"
},
"dependencies": {
"babel-core": "^5.8.20",
"es6-promisify": "^3.0.0",
"github": "^0.2.4",
"lodash": "^3.10.0"
"bluebird": "^3.4.1",
"github": "^2.4.0",
"lodash": "^4.14.2"
}
}
40 changes: 15 additions & 25 deletions src/content-from-filename.js
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
}
}
6 changes: 0 additions & 6 deletions src/default-default.js

This file was deleted.

70 changes: 27 additions & 43 deletions src/index.js
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))
}
40 changes: 13 additions & 27 deletions src/update-file-with-content.js
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
}

0 comments on commit 9fc64de

Please sign in to comment.