@@ -9,18 +9,42 @@ var flatiron = require('flatiron'),
9
9
util = require ( 'util' ) ,
10
10
rimraf = require ( 'rimraf' ) ,
11
11
exec = require ( 'child_process' ) . exec ,
12
- username = 'XXXXXXXXXXXXX' ,
13
- password = 'XXXXXXXXXXXXX' ,
12
+ redis = require ( "redis" ) ,
14
13
app = flatiron . app ;
15
14
15
+ app . config . file ( { file : path . join ( __dirname , 'config' , 'config.json' ) } ) ;
16
+
17
+ var username = app . config . get ( 'username' ) ,
18
+ password = app . config . get ( 'password' ) ,
19
+ port = app . config . get ( 'database:port' ) ,
20
+ host = app . config . get ( 'database:host' ) ,
21
+ pass = app . config . get ( 'database:password' ) ,
22
+ npm_hash = app . config . get ( 'database:npm_hash' ) ;
23
+
24
+ var redisClient = redis . createClient ( port , host ) ;
25
+
26
+ redisClient . auth ( pass , function ( err ) {
27
+ if ( err ) {
28
+ throw err ;
29
+ }
30
+ app . log . info ( "REDIS Authed!" ) ;
31
+ } ) ;
32
+
16
33
17
34
var gitQue = async . queue ( function ( task , callback ) {
18
35
app . log . debug ( 'GITQUE:' . cyan . bold + ' Running ' . green . bold + task [ "info" ] . toString ( ) . magenta ) ;
19
36
callback ( null , task ) ;
20
37
} , 1 ) ;
21
38
39
+ redisClient . on ( "error" , function ( err ) {
40
+ app . log . error ( "REDIS Error " + err ) ;
41
+ } ) ;
42
+
43
+ redisClient . on ( "quit" , function ( err ) {
44
+ app . log . error ( "REDIS Shutting down" ) ;
45
+ } ) ;
46
+
22
47
23
- app . config . file ( { file : path . join ( __dirname , 'config' , 'config.json' ) } ) ;
24
48
25
49
app . use ( flatiron . plugins . cli , {
26
50
source : path . join ( __dirname , 'lib' , 'commands' ) ,
@@ -46,6 +70,12 @@ app.commands.repo = function file(link, cb) {
46
70
doRepoUpdate ( link , cb ) ;
47
71
} ;
48
72
73
+ app . commands . db = function file ( cb ) {
74
+ redisClient . hgetall ( npm_hash , function ( err , obj ) {
75
+ console . dir ( obj ) ;
76
+ } ) ;
77
+ } ;
78
+
49
79
app . commands . npm = function file ( link , cb ) {
50
80
this . log . warn ( 'Running on all available npm repositories that are hosted on github!!!' . red . bold ) ;
51
81
doNPMUpdate ( cb ) ;
@@ -62,17 +92,21 @@ app.commands.file = function file(filename, cb) {
62
92
doFileUpdate ( filename , cb ) ;
63
93
} ;
64
94
65
- function npmShortCircuit ( cb ) {
66
- var ex = [ 'git://github.com/eller86/surrogate-pair.js.git' , 'git://github.com/vesln/surround.git' , 'git@github.com:sebv/sv-cake-utils.git' ] ;
67
- async . forEachSeries ( ex , doRepoUpdate , function ( err ) {
68
- if ( err ) {
69
- app . log . warn ( 'Error processing npm repositories that are hosted on github!!!' . red . bold ) ;
70
- return cb ( err ) ;
71
- } else {
72
- return cb ( null , 'OK' ) ;
73
- }
74
- } ) ;
75
- }
95
+ app . start ( function ( err ) {
96
+ if ( err ) {
97
+ app . log . error ( err . message || 'You didn\'t call any commands!' ) ;
98
+ app . log . warn ( 'node-migrator-bot' . grey + ' NOT OK.' ) ;
99
+ redisClient . quit ( ) ;
100
+ return process . exit ( 1 ) ;
101
+ }
102
+ redisClient . quit ( ) ;
103
+ app . log . info ( 'node-migrator-bot' . grey + ' ok' . green . bold ) ;
104
+ } ) ;
105
+
106
+
107
+
108
+
109
+
76
110
function doNPMUpdate ( cb ) {
77
111
//app.log.debug("doNPMUpdate");
78
112
getNPMRepos ( function ( err , results ) {
@@ -143,13 +177,25 @@ function doRepoUpdate(link, cb) {
143
177
var re = / ( h t t p | f t p | h t t p s | g i t | f i l e ) : \/ \/ ( \/ ) ? [ \w \- ] + ( \. [ \w \- ] + ) + ( [ \w . , @ ? \^ = % & a m p ; : \/ ~ + # \- ] * [ \w @ ? \^ = % & a m p ; \/ ~ + # \- ] ) ? / gi,
144
178
reSSH = / g i t @ g i t h u b \. c o m : .* \/ .* ( \. g i t $ | $ ) / g;
145
179
146
- if ( XRegExp . test ( link , re ) || XRegExp . test ( link , reSSH ) ) {
147
- app . log . info ( link . blue . bold + ' is a url' ) ;
148
- forkAndFix ( link , cb ) ;
149
- } else {
150
- app . log . info ( link . blue . bold + ' is a folder' ) ;
151
- walkAndFix ( link , 'OK' , cb ) ;
152
- }
180
+ redisClient . hget ( npm_hash , link , function ( err , hashk_value ) {
181
+ if ( err ) {
182
+ app . log . error ( 'There was a problem with finding the value of ' + npm_hash + ':' + link ) ;
183
+ return cb ( err ) ;
184
+ }
185
+
186
+ if ( hashk_value === null ) {
187
+ if ( XRegExp . test ( link , re ) || XRegExp . test ( link , reSSH ) ) {
188
+ app . log . info ( link . blue . bold + ' is a url' ) ;
189
+ forkAndFix ( link , cb ) ;
190
+ } else {
191
+ app . log . info ( link . blue . bold + ' is a folder' ) ;
192
+ walkAndFix ( null , link , 'OK' , cb ) ; //NOTE: LINKE HERE IS the path!!!
193
+ }
194
+ } else {
195
+ app . log . debug ( link . yellow . bold + ' Has already been processed!' ) ;
196
+ return cb ( null , "DONE" ) ;
197
+ }
198
+ } ) ;
153
199
}
154
200
155
201
function forkAndFix ( link , cb ) {
@@ -163,7 +209,7 @@ function forkAndFix(link, cb) {
163
209
app . log . info ( 'Forking ' + user . magenta . bold + '/' + repo . yellow . bold ) ;
164
210
async . waterfall ( [
165
211
function ( callback ) {
166
- forkRepo ( forkedRepo , username , user , repo , repoLocation , callback ) ;
212
+ forkRepo ( link , forkedRepo , username , user , repo , repoLocation , callback ) ;
167
213
} , //fork
168
214
function ( status , callback ) {
169
215
notifyAvailability ( forkedRepo , username , repo , repoLocation , status , callback ) ;
@@ -175,16 +221,16 @@ function forkAndFix(link, cb) {
175
221
gitQue . push ( { task : switchBranch ( forkedRepo , repoLocation , status , callback ) , info : 'git:switchBranch :: ' + forkedRepo } ) ;
176
222
} , // switch branch
177
223
function ( status , callback ) {
178
- walkAndFix ( repoLocation , status , callback ) ; //? lose all variables?
224
+ walkAndFix ( link , repoLocation , status , callback ) ; //? lose all variables?
179
225
} , // walkAndFix
180
226
function ( status , callback ) {
181
- gitQue . push ( { task : commitRepo ( forkedRepo , repoLocation , status , callback ) , info : ' git:commitRepo :: ' + forkedRepo } ) ;
227
+ gitQue . push ( { task : commitRepo ( link , forkedRepo , repoLocation , status , callback ) , info : ' git:commitRepo :: ' + forkedRepo } ) ;
182
228
} , // commit
183
229
function ( status , callback ) {
184
230
gitQue . push ( { task : pushCommit ( forkedRepo , repoLocation , status , callback ) , info : ' git:pushCommit :: ' + forkedRepo } ) ;
185
231
} , // push
186
232
function ( status , callback ) {
187
- submitPullRequest ( username , user , repo , status , callback ) ;
233
+ submitPullRequest ( link , username , user , repo , status , callback ) ;
188
234
} , // submit pull request
189
235
function ( status , callback ) {
190
236
cleanUpFileSystem ( repoLocation , callback ) ;
@@ -217,24 +263,24 @@ function cleanUpFileSystem(repoLocation, cb) {
217
263
} ) ;
218
264
}
219
265
220
- function forkRepo ( forkedRepo , username , user , repo , repoLocation , cb ) {
266
+ function forkRepo ( link , forkedRepo , username , user , repo , repoLocation , cb ) {
221
267
var client = github . client ( {
222
268
username : username ,
223
269
password : password
224
270
} ) ;
225
271
226
272
client . me ( ) . fork ( user + '/' + repo , function ( err , data ) {
227
273
if ( err ) {
228
- app . log . error ( 'error: ' + err ) ;
229
- app . log . error ( 'data: ' + data ) ;
274
+ app . log . error ( err + ' ' + link . yellow . bold ) ;
275
+ app . log . debug ( 'data: ' + data ) ;
230
276
return cb ( err ) ;
231
277
} else {
232
278
return cb ( null , 'OK' ) ;
233
279
}
234
280
} ) ;
235
281
}
236
282
237
- function submitPullRequest ( username , user , repo , status , cb ) {
283
+ function submitPullRequest ( link , username , user , repo , status , cb ) {
238
284
if ( status === 'DONE' ) {
239
285
return cb ( null , 'DONE' ) ;
240
286
}
@@ -288,17 +334,16 @@ function submitPullRequest(username, user, repo, status, cb) {
288
334
request . post ( { url : url , body : payload } , function ( error , response , body ) {
289
335
if ( ! error && response . statusCode === 201 ) { //Status: 201 Created
290
336
app . log . info ( 'Pull Request to ' + user + '/' + repo + ' from ' + username + '/' + repo + ' Succesfull!' ) ;
337
+ redisClient . hset ( npm_hash , link , 'processed' ) ;
338
+
291
339
return cb ( null , 'DONE' ) ;
292
340
} else {
293
- //app.log.debug('response:');
294
- //app.log.debug(response.statusCode);
295
- //app.log.debug(response.body);
296
- app . log . error ( 'error: ' + error ) ;
297
341
if ( error === null ) {
298
342
try {
299
343
throw new Error ( response . statusCode + ' ' + response . body . toString ( ) ) ;
300
344
} catch ( err ) {
301
- cb ( err ) ;
345
+ app . log . error ( 'submitPullRequest::error : ' + err ) ;
346
+ return cb ( null , 'DONE' ) ;
302
347
}
303
348
} else {
304
349
return cb ( error ) ;
@@ -383,7 +428,7 @@ function switchBranch(forkedRepo, repoLocation, status, cb) {
383
428
} ) ;
384
429
}
385
430
386
- function commitRepo ( forkedRepo , repoLocation , status , cb ) {
431
+ function commitRepo ( link , forkedRepo , repoLocation , status , cb ) {
387
432
388
433
if ( status === 'DONE' ) {
389
434
return cb ( null , 'DONE' ) ;
@@ -403,6 +448,7 @@ function commitRepo(forkedRepo, repoLocation, status, cb) {
403
448
console . dir ( stderr ) ;
404
449
if ( stdout === '# On branch clean\nnothing to commit (working directory clean)\n' ) {
405
450
app . log . info ( forkedRepo . blue . bold + '@' + repoLocation . yellow . bold + ':clean branch ' + 'NOTHING TO COMMIT' . red . bold ) ;
451
+ redisClient . hset ( npm_hash , link , 'processed' ) ;
406
452
return cb ( null , 'DONE' ) ;
407
453
} else {
408
454
return cb ( error ) ;
@@ -433,7 +479,7 @@ function pushCommit(forkedRepo, repoLocation, status, cb) {
433
479
434
480
if ( stdout === 'To prevent you from losing history, non-fast-forward updates were rejected\nMerge the remote changes before pushing again. See the \'Note about\nfast-forwards\' section of \'git push --help\' for details.\n' ) {
435
481
app . log . warn ( forkedRepo . blue . bold + '@' + repoLocation . yellow . bold + ':clean branch ' + 'COMMIT NOT PUSHED' . red . bold + ' : We may have already pushed to this fork!' . magenta . bold ) ;
436
- return cb ( null , 'DONE ' ) ;
482
+ return cb ( null , 'OK ' ) ;
437
483
} else {
438
484
return cb ( error ) ;
439
485
}
@@ -453,6 +499,7 @@ function notifyAvailability(forkedRepo, username, repo, repoLocation, status, cb
453
499
app . log . info ( 'Waiting for ' + username . magenta . bold + '/' + repo . yellow . bold + ' to become available...' ) ;
454
500
}
455
501
request . head ( forkedRepo , function ( error , response , body ) {
502
+ app . log . debug ( 'notifyAvailability :: forkedRepo = ' + forkedRepo + ' | ' + 'response.statusCode = ' + response . statusCode ) ;
456
503
if ( ! error && response . statusCode === 200 ) {
457
504
Available = true ;
458
505
}
@@ -480,11 +527,11 @@ function notifyAvailability(forkedRepo, username, repo, repoLocation, status, cb
480
527
) ;
481
528
}
482
529
483
- function walkAndFix ( link , status , cb ) {
530
+ function walkAndFix ( link , repoLocation , status , cb ) {
484
531
if ( status === 'DONE' ) {
485
532
return cb ( null , 'DONE' ) ;
486
533
}
487
- walk ( link , function ( err , results ) {
534
+ walk ( repoLocation , function ( err , results ) {
488
535
if ( err ) {
489
536
return cb ( err ) ;
490
537
}
@@ -496,7 +543,8 @@ function walkAndFix(link, status, cb) {
496
543
//app.log.debug(results);
497
544
//app.log.debug(results.indexOf('OK'));
498
545
if ( results . indexOf ( 'OK' ) === - 1 ) {
499
- app . log . warn ( 'No changes to make for ' . bold . red + link . yellow ) ;
546
+ app . log . warn ( 'No changes to make for ' . bold . red + repoLocation . yellow ) ;
547
+ redisClient . hset ( npm_hash , link , 'processed' ) ;
500
548
return cb ( null , 'DONE' ) ;
501
549
} else {
502
550
return cb ( null , 'OK' ) ;
@@ -587,12 +635,5 @@ function doFileUpdate(filename, cb) {
587
635
} ) ;
588
636
}
589
637
590
- app . start ( function ( err ) {
591
- if ( err ) {
592
- app . log . error ( err . message || 'You didn"t call any commands!' ) ;
593
- app . log . warn ( 'node-migrator-bot' . grey + ' NOT OK.' ) ;
594
- return process . exit ( 1 ) ;
595
- }
596
- app . log . info ( 'node-migrator-bot' . grey + ' ok' . green . bold ) ;
597
- } ) ;
638
+
598
639
0 commit comments