Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,10 +7,9 @@ | |
(function() { | ||
"use strict"; | ||
|
||
var // Application requirements | ||
child = require( "child_process" ), | ||
http = require( "https" ), | ||
fs = require( "fs" ), | ||
var child = require("child_process"), | ||
http = require("https"), | ||
fs = require("fs"), | ||
prompt = require("prompt"), | ||
request = require("request"), | ||
|
||
|
@@ -19,8 +18,8 @@ | |
spawn = child.spawn, | ||
|
||
// Process arguments | ||
id = process.argv[2], | ||
done = process.argv[3], | ||
id = process.argv[ 2 ], | ||
done = process.argv[ 3 ], | ||
|
||
// Localized application references | ||
user_repo = "", | ||
|
@@ -33,7 +32,7 @@ | |
// We don't want the default prompt message | ||
prompt.message = ""; | ||
|
||
process.stdout.write( "Initializing... " ); | ||
process.stdout.write("Initializing... "); | ||
|
||
exec( "git config --global --get pulley.token", function( error, stdout, stderr ) { | ||
token = trim( stdout ); | ||
|
@@ -84,100 +83,94 @@ | |
|
||
function init() { | ||
if ( !id ) { | ||
exit( "No pull request ID specified, please provide one." ); | ||
exit("No pull request ID specified, please provide one."); | ||
} | ||
exec( "git remote -v show " + config.remote, function( error, stdout, stderr ) { | ||
user_repo = (/URL:.*?([\w\-]+\/[\w\-]+)/.exec( stdout ) || [])[1]; | ||
user_repo = ( /URL:.*?([\w\-]+\/[\w\-]+)/.exec( stdout ) || [] )[ 1 ]; | ||
tracker = config.repos[ user_repo ]; | ||
|
||
if ( user_repo ) { | ||
getStatus(); | ||
} else { | ||
exit( "External repository not found." ); | ||
exit("External repository not found."); | ||
} | ||
}); | ||
} | ||
|
||
function getStatus() { | ||
|
||
exec( "git status", function( error, stdout, stderr ) { | ||
if ( /Changes to be committed/i.test( stdout ) ) { | ||
if ( done ) { | ||
getPullData(); | ||
|
||
} else { | ||
exit( "Please commit changed files before attemping a pull/merge." ); | ||
exit("Please commit changed files before attemping a pull/merge."); | ||
} | ||
|
||
} else if ( /Changes not staged for commit/i.test( stdout ) ) { | ||
if ( done ) { | ||
exit( "Please add files that you wish to commit." ); | ||
exit("Please add files that you wish to commit."); | ||
|
||
} else { | ||
exit( "Please stash files before attempting a pull/merge." ); | ||
exit("Please stash files before attempting a pull/merge."); | ||
} | ||
} else { | ||
if ( done ) { | ||
exit( "It looks like you've broken your merge attempt." ); | ||
|
||
exit("It looks like you've broken your merge attempt."); | ||
} else { | ||
getPullData(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
|
||
function getPullData() { | ||
process.stdout.write( "done.\n" ); | ||
process.stdout.write( "Getting pull request details... " ); | ||
process.stdout.write("done.\n"); | ||
process.stdout.write("Getting pull request details... "); | ||
|
||
callApi({ | ||
path: "/repos/" + user_repo + "/pulls/" + id | ||
}, function( data ) { | ||
try { | ||
var pull = JSON.parse(data); | ||
var pull = JSON.parse( data ); | ||
|
||
process.stdout.write( "done.\n" ); | ||
process.stdout.write("done.\n"); | ||
|
||
if ( done ) { | ||
commit( pull ); | ||
} else { | ||
mergePull( pull ); | ||
} | ||
} catch( e ) { | ||
exit( "Error retrieving pull request from Github." ); | ||
exit("Error retrieving pull request from Github."); | ||
} | ||
}); | ||
} | ||
|
||
function mergePull( pull ) { | ||
process.stdout.write( "Pulling and merging results... " ); | ||
var repo = pull.head.repo.ssh_url, | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
mikesherov
Contributor
|
||
repo_branch = pull.head.ref, | ||
branch = "pull-" + id, | ||
checkout = "git checkout -b " + branch; | ||
|
||
if( pull.state === "closed" ) { | ||
exit( "Can not merge closed Pull Requests. " ); | ||
process.stdout.write("Pulling and merging results... "); | ||
|
||
if ( pull.state === "closed" ) { | ||
exit("Can not merge closed Pull Requests."); | ||
} | ||
|
||
if ( pull.merged ) { | ||
exit( "This Pull Request has already been merged. " ); | ||
exit("This Pull Request has already been merged."); | ||
} | ||
|
||
// TODO: give user the option to resolve the merge by themselves | ||
if ( !pull.mergeable ) { | ||
exit( "This Pull Request is not automatically mergeable. " ); | ||
exit("This Pull Request is not automatically mergeable."); | ||
} | ||
|
||
var repo = pull.head.repo.ssh_url, | ||
repo_branch = pull.head.ref, | ||
branch = "pull-" + id, | ||
checkout = "git checkout -b " + branch; | ||
|
||
exec( "git checkout master && git pull " + config.remote + " master && git submodule update --init && " + checkout, function( error, stdout, stderr ) { | ||
if ( /toplevel/i.test( stderr ) ) { | ||
exit( "please call pulley from the toplevel directory of this repository" ); | ||
exit("Please call pulley from the toplevel directory of this repo."); | ||
} else if ( /fatal/i.test( stderr ) ) { | ||
exec( "git branch -D " + branch + " && " + checkout, doPull ); | ||
|
||
} else { | ||
doPull(); | ||
} | ||
|
@@ -192,44 +185,42 @@ | |
|
||
exec( pull_cmds.join( " && " ), function( error, stdout, stderr ) { | ||
if ( /Merge conflict/i.test( stdout ) ) { | ||
exit( "Merge conflict. Please resolve then run: " + | ||
process.argv.join( " " ) + " done" ); | ||
|
||
exit("Merge conflict. Please resolve then run: " + | ||
process.argv.join(" ") + " done"); | ||
} else { | ||
process.stdout.write( "done.\n" ); | ||
process.stdout.write("done.\n"); | ||
commit( pull ); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
function commit( pull ) { | ||
process.stdout.write( "Getting author and committing changes... " ); | ||
process.stdout.write("Getting author and committing changes... "); | ||
|
||
callApi({ | ||
path: "/repos/" + user_repo + "/pulls/" + id + "/commits" | ||
}, function( data ) { | ||
var match, | ||
msg = "Closes #" + id + ": " + pull.title + ".", | ||
author = JSON.parse(data)[0].commit.author.name, | ||
author = JSON.parse( data )[ 0 ].commit.author.name, | ||
issues = [], | ||
urls = [], | ||
findBug = /#(\d+)/g; | ||
|
||
// search title and body for issues | ||
// for issues to link to | ||
// Search title and body for issues for issues to link to | ||
if ( tracker ) { | ||
while ( (match = findBug.exec( pull.title + pull.body )) ) { | ||
urls.push( tracker + match[1] ); | ||
while ( ( match = findBug.exec( pull.title + pull.body ) ) ) { | ||
urls.push( tracker + match[ 1 ] ); | ||
} | ||
} | ||
|
||
// search just body for issues to add to the commit message | ||
while ( (match = findBug.exec( pull.body )) ) { | ||
issues.push( " Fixes #" + match[1] ); | ||
// Search just body for issues to add to the commit message | ||
while ( ( match = findBug.exec( pull.body ) ) ) { | ||
issues.push( " Fixes #" + match[ 1 ] ); | ||
} | ||
|
||
// add issues to the commit message | ||
// Add issues to the commit message | ||
msg += issues.join(","); | ||
|
||
if ( urls.length ) { | ||
|
@@ -241,7 +232,7 @@ | |
var commit = [ "commit", "-a", "--message=" + msg ]; | ||
|
||
if ( config.interactive ) { | ||
commit.push( "-e" ); | ||
commit.push("-e"); | ||
} | ||
|
||
if ( author ) { | ||
|
@@ -252,14 +243,13 @@ | |
// Thanks to: https://gist.github.com/927052 | ||
spawn( "git", commit, { | ||
customFds: [ process.stdin, process.stdout, process.stderr ] | ||
}).on("exit", function() { | ||
}).on( "exit", function() { | ||
getHEAD(function( newCommit ) { | ||
|
||
if ( oldCommit === newCommit ) { | ||
reset( "No commit, aborting push." ); | ||
reset("No commit, aborting push."); | ||
} else { | ||
exec( "git push " + config.remote + " master", function( error, stdout, stderr ) { | ||
process.stdout.write( "done.\n" ); | ||
process.stdout.write("done.\n"); | ||
exit(); | ||
}); | ||
} | ||
|
@@ -270,8 +260,8 @@ | |
} | ||
|
||
// TODO: Add check to API call if autorization fails. Show login to reauthorize. | ||
function callApi(options, callback, data) { | ||
setTimeout(function(){ | ||
function callApi( options, callback, data ) { | ||
setTimeout(function() { | ||
var req, datastring; | ||
|
||
options.host = options.host || "api.github.com"; | ||
|
@@ -281,28 +271,28 @@ | |
Host: "api.github.com" | ||
}; | ||
|
||
if(data){ | ||
datastring = JSON.stringify(data); | ||
options.headers['Content-Type'] = 'application/x-www-form-urlencoded'; | ||
options.headers['Content-Length'] = datastring.length; | ||
if ( data ) { | ||
datastring = JSON.stringify( data ); | ||
options.headers["Content-Type"] = "application/x-www-form-urlencoded"; | ||
options.headers["Content-Length"] = datastring.length; | ||
} | ||
|
||
req = http.request(options, function( res ) { | ||
req = http.request( options, function( res ) { | ||
var data = []; | ||
|
||
res.on( "data", function( chunk ) { | ||
data.push( chunk ); | ||
}); | ||
|
||
res.on( "end", function() { | ||
setTimeout(function(){ | ||
callback(data.join("")); | ||
setTimeout(function() { | ||
callback( data.join("") ); | ||
}, 1000); | ||
}); | ||
}); | ||
|
||
if(data){ | ||
req.write(datastring); | ||
if ( data ) { | ||
req.write( datastring ); | ||
} | ||
|
||
req.end(); | ||
|
@@ -311,18 +301,18 @@ | |
|
||
function getHEAD( fn ) { | ||
exec( "git log | head -1", function( error, stdout, stderr ) { | ||
var commit = (/commit (.*)/.exec( stdout ) || [])[1]; | ||
var commit = ( /commit (.*)/.exec( stdout ) || [] )[ 1 ]; | ||
|
||
fn( commit ); | ||
}); | ||
} | ||
|
||
function reset( msg ) { | ||
console.error( "\n" + msg ); | ||
process.stderr.write( "Resetting files... " ); | ||
process.stderr.write("Resetting files... "); | ||
|
||
exec( "git reset --hard ORIG_HEAD", function() { | ||
process.stderr.write( "done.\n" ); | ||
process.stderr.write("done.\n"); | ||
exit(); | ||
}); | ||
} | ||
|
@@ -336,7 +326,7 @@ | |
} | ||
|
||
function trim( string ) { | ||
return string.replace(/^\s*|\s*$/g, ''); | ||
return string.replace( /^\s*|\s*$/g, '' ); | ||
} | ||
|
||
})(); |
I'm not sure we need to declare vars before guard clauses to conform to style.