Skip to content

Instantly share code, notes, and snippets.

@ForbesLindesay
Created August 21, 2013 20:05
Show Gist options
  • Save ForbesLindesay/6299487 to your computer and use it in GitHub Desktop.
Save ForbesLindesay/6299487 to your computer and use it in GitHub Desktop.
var org = '<organization to fork to (or undefined>'
var token = '<your token here>'
function options(host) {
return {
host: host || undefined,
auth: {
type: 'oauth',
token: token
}
}
}
var color = require('bash-color')
var Promise = require('promise')
var barrage = require('barrage')
var github = require('github-basic')
var pr = require('pull-request')
github.stream('/users/:user/repos', {user: process.argv[2]}, options())
.pipe(fixupStream())
function fixupStream() {
var stream = new barrage.Writable({objectMode: true})
stream._write = function (repo, _, cb) {
if (repo.fork) return cb()
var name = repo.full_name.split('/')
fixup(name[0], name[1])
.then(function (added) {
if (added) {
console.log('fixed ' + color.green(name[1]))
} else {
console.log(color.cyan(name[1]) + ' is already correct.')
}
}, function (err) {
console.log('failed to add repo url to ' + color.red(name[1]))
console.warn(err.message.substring(0, 100))
})
.nodeify(cb)
}
return stream
}
function fixup(user, repo) {
return github.buffer('GET', '/' + user + '/' + repo + '/master/package.json', {}, options('raw.github.com'))
.then(function (pkg) {
pkg = pkg.body
var data
try {
data = JSON.parse(pkg)
if (data.repository) {
return false
}
data.repository = {
type: 'git',
url: 'https://github.com/' + user + '/' + repo + '.git'
}
var newpkg = JSON.stringify(data, null, /\{\n( *|\t)/.exec(pkg)[1])
if (pkg[pkg.length - 1] === '\n') {
newpkg += '\n'
}
return submit(user, repo, newpkg)
} catch (ex) {
throw ex
}
}, function () {
return false
})
}
function submit(user, repo, pkg) {
return pr.exists(org, repo, options())
.then(function (exists) {
if (exists) return false
var opts = options()
opts.organization = org
return pr.fork(user, repo, opts)
.then(function () {
return pr.branch(org, repo, 'master', 'add/repository', options())
})
.then(function () {
return pr.commit(org, repo, {
branch: 'add/repository',
message: 'add repository field to readme',
updates: [{path: 'package.json', content: pkg}]
}, options())
})
.then(function () {
return pr.pull({
user: org,
repo: repo,
branch: 'add/repository'
}, {
user: user,
repo: repo
}, {
title: 'Add repository field to readme',
body: 'I am a bot. I add repository fields to package.json files because they\'re super useful for npm.'
}, options())
})
.then(function () {
return true
})
})
}

Usage

Populate the org and token fields with the appropriate values then run:

node repoify <github-username>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment