forked from jeresig/pulley
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpulley.js
executable file
·332 lines (281 loc) · 7.89 KB
/
pulley.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/env node
/*
* Pulley: Easy Github Pull Request Lander
* Copyright 2011 John Resig
* MIT Licensed
*/
(function() {
"use strict";
var child = require("child_process"),
http = require("https"),
fs = require("fs"),
prompt = require("prompt"),
request = require("request"),
colors = require("colors"),
os = require("os"),
pkg = require("./package"),
// Process references
exec = child.exec,
spawn = child.spawn,
// Process arguments
id = process.argv[ 2 ],
done = process.argv[ 3 ],
// Localized application references
user_repo = "",
tracker = "",
token = "",
// Initialize config file
config = JSON.parse( fs.readFileSync( __dirname + "/config.json" ) );
// We don't want the default prompt message
prompt.message = "";
process.stdout.write( "Initializing... ".blue );
exec( "git config --global --get pulley.token", function( error, stdout, stderr ) {
token = stdout.trim();
if ( token ) {
init();
} else {
login();
}
});
function login() {
console.log("\nPlease login with your GitHub credentials.");
console.log("Your credentials are only needed this one time to get a token from GitHub.");
prompt.start();
prompt.get([{
name: "username",
message: "Username",
empty: false
}, {
name: "password",
message: "Password",
empty: false,
hidden: true
}], function( err, result ) {
var auth = encodeURIComponent( result.username ) + ":" + encodeURIComponent( result.password );
request.post("https://" + auth + "@api.github.com/authorizations", {
json: true,
headers: {
"User-Agent": "Pulley " + pkg.version
},
body: {
scopes: ["repo"],
note: "Pulley-"+ os.hostname(),
note_url: "https://github.com/jeresig/pulley"
}
}, function( err, res, body ) {
if ( err ) {
exit( err );
}
token = body.token;
if ( token ) {
exec( "git config --global --add pulley.token " + token, function( error, stdout, stderr ) {
console.log( "Success!".green );
init();
});
} else {
console.log( ( body.message + ". Try again." ).red );
login();
}
});
});
}
function init() {
if ( !id ) {
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\-]+)$/m.exec( stdout ) || [] )[ 1 ];
tracker = config.repos[ user_repo ];
if ( user_repo ) {
getStatus();
} else {
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.");
}
} else if ( /Changes not staged for commit/i.test( stdout ) ) {
if ( done ) {
exit("Please add files that you wish to commit.");
} else {
exit("Please stash files before attempting a pull/merge.");
}
} else {
if ( done ) {
exit("It looks like you've broken your merge attempt.");
} else {
getPullData();
}
}
});
}
function getPullData() {
var path = "/repos/" + user_repo + "/pulls/" + id;
console.log( "done.".green );
process.stdout.write( "Getting pull request details... ".blue );
callAPI( path, function( data ) {
try {
var pull = JSON.parse( data );
console.log( "done.".green );
if ( done ) {
commit( pull );
} else {
mergePull( pull );
}
} catch( e ) {
exit("Error retrieving pull request from Github.");
}
});
}
function mergePull( pull ) {
var repo = pull.head.repo.ssh_url,
head_branch = pull.head.ref,
base_branch = pull.base.ref,
branch = "pull-" + id,
checkout = "git checkout " + base_branch,
checkout_cmds = [
checkout,
"git pull " + config.remote + " " + base_branch,
"git submodule update --init",
"git checkout -b " + branch
];
process.stdout.write( "Pulling and merging results... ".blue );
if ( pull.state === "closed" ) {
exit("Can not merge closed Pull Requests.");
}
if ( pull.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.");
}
exec( checkout_cmds.join( " && " ), function( error, stdout, stderr ) {
if ( /toplevel/i.test( stderr ) ) {
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();
}
});
function doPull( error, stdout, stderr ) {
var pull_cmds = [
"git pull " + repo + " " + head_branch,
checkout,
"git merge --no-commit --squash " + branch
];
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");
} else if ( /error/.test( stderr ) ) {
exit("Unable to merge. Please resolve then retry:\n" + stderr);
} else {
console.log( "done.".green );
commit( pull );
}
});
}
}
function commit( pull ) {
var path = "/repos/" + user_repo + "/pulls/" + id + "/commits";
process.stdout.write( "Getting author and committing changes... ".blue );
callAPI( path, function( data ) {
var match,
msg = "Close " + user_repo + "#" + id + ": " + pull.title + ".",
author = JSON.parse( data )[ 0 ].commit.author.name,
base_branch = pull.base.ref,
issues = [],
urls = [],
findBug = /#(\d+)/g;
// 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 ] );
}
}
// 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
msg += issues.join(",");
if ( urls.length ) {
msg += "\n\nMore Details:" + urls.map(function( url ) {
return "\n - " + url;
}).join("");
}
var commit = [ "commit", "-a", "--message=" + msg ];
if ( config.interactive ) {
commit.push("-e");
}
if ( author ) {
commit.push( "--author=" + author );
}
getHEAD(function( oldCommit ) {
// Thanks to: https://gist.github.com/927052
spawn( "git", commit, { stdio: 'inherit' }).on( "exit", function() {
getHEAD(function( newCommit ) {
if ( oldCommit === newCommit ) {
reset("No commit, aborting push.");
} else {
exec( "git push " + config.remote + " " + base_branch, function( error, stdout, stderr ) {
console.log( "done.".green );
exit();
});
}
});
});
});
});
}
function callAPI( path, callback ) {
request.get( "https://api.github.com" + path, {
headers: {
Authorization: "token " + token,
"User-Agent": "Pulley " + pkg.version
}
}, function( err, res, body ) {
var statusCode = res.socket._httpMessage.res.statusCode;
if ( err ) {
exit( err );
}
if ( statusCode === 404 ) {
exit("Pull request doesn't exist");
}
if ( statusCode === 401 ) {
login();
return;
}
callback( body );
});
}
function getHEAD( fn ) {
exec( "git log | head -1", function( error, stdout, stderr ) {
var commit = ( /commit (.*)/.exec( stdout ) || [] )[ 1 ];
fn( commit );
});
}
function reset( msg ) {
console.error( ( "\n" + msg ).red );
process.stderr.write( "Resetting files... ".red );
exec( "git reset --hard ORIG_HEAD", function() {
console.log( "done.".green );
exit();
});
}
function exit( msg ) {
if ( msg ) {
console.error( ( "\nError: " + msg ).red );
}
process.exit( 1 );
}
})();