Skip to content

Commit 2dcf393

Browse files
committed
[api] added database logging, seperated out vars into config file
1 parent 8575ca7 commit 2dcf393

File tree

3 files changed

+100
-49
lines changed

3 files changed

+100
-49
lines changed

app.js

Lines changed: 88 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,42 @@ var flatiron = require('flatiron'),
99
util = require('util'),
1010
rimraf = require('rimraf'),
1111
exec = require('child_process').exec,
12-
username = 'XXXXXXXXXXXXX',
13-
password = 'XXXXXXXXXXXXX',
12+
redis = require("redis"),
1413
app = flatiron.app;
1514

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+
1633

1734
var gitQue = async.queue(function (task, callback) {
1835
app.log.debug('GITQUE:'.cyan.bold + ' Running '.green.bold + task["info"].toString().magenta);
1936
callback(null, task);
2037
}, 1);
2138

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+
2247

23-
app.config.file({ file: path.join(__dirname, 'config', 'config.json') });
2448

2549
app.use(flatiron.plugins.cli, {
2650
source: path.join(__dirname, 'lib', 'commands'),
@@ -46,6 +70,12 @@ app.commands.repo = function file(link, cb) {
4670
doRepoUpdate(link, cb);
4771
};
4872

73+
app.commands.db = function file(cb) {
74+
redisClient.hgetall(npm_hash, function (err, obj) {
75+
console.dir(obj);
76+
});
77+
};
78+
4979
app.commands.npm = function file(link, cb) {
5080
this.log.warn('Running on all available npm repositories that are hosted on github!!!'.red.bold);
5181
doNPMUpdate(cb);
@@ -62,17 +92,21 @@ app.commands.file = function file(filename, cb) {
6292
doFileUpdate(filename, cb);
6393
};
6494

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+
76110
function doNPMUpdate(cb) {
77111
//app.log.debug("doNPMUpdate");
78112
getNPMRepos(function (err, results) {
@@ -143,13 +177,25 @@ function doRepoUpdate(link, cb) {
143177
var re = /(http|ftp|https|git|file):\/\/(\/)?[\w\-]+(\.[\w\-]+)+([\w.,@?\^=%&:\/~+#\-]*[\w@?\^=%&\/~+#\-])?/gi,
144178
reSSH = /git@github\.com:.*\/.*(\.git$|$)/g;
145179

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+
});
153199
}
154200

155201
function forkAndFix(link, cb) {
@@ -163,7 +209,7 @@ function forkAndFix(link, cb) {
163209
app.log.info('Forking ' + user.magenta.bold + '/' + repo.yellow.bold);
164210
async.waterfall([
165211
function (callback) {
166-
forkRepo(forkedRepo, username, user, repo, repoLocation, callback);
212+
forkRepo(link, forkedRepo, username, user, repo, repoLocation, callback);
167213
},//fork
168214
function (status, callback) {
169215
notifyAvailability(forkedRepo, username, repo, repoLocation, status, callback);
@@ -175,16 +221,16 @@ function forkAndFix(link, cb) {
175221
gitQue.push({task: switchBranch(forkedRepo, repoLocation, status, callback), info: 'git:switchBranch :: ' + forkedRepo});
176222
},// switch branch
177223
function (status, callback) {
178-
walkAndFix(repoLocation, status, callback);//? lose all variables?
224+
walkAndFix(link, repoLocation, status, callback);//? lose all variables?
179225
},// walkAndFix
180226
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});
182228
},// commit
183229
function (status, callback) {
184230
gitQue.push({task: pushCommit(forkedRepo, repoLocation, status, callback), info: ' git:pushCommit :: ' + forkedRepo});
185231
},// push
186232
function (status, callback) {
187-
submitPullRequest(username, user, repo, status, callback);
233+
submitPullRequest(link, username, user, repo, status, callback);
188234
},// submit pull request
189235
function (status, callback) {
190236
cleanUpFileSystem(repoLocation, callback);
@@ -217,24 +263,24 @@ function cleanUpFileSystem(repoLocation, cb) {
217263
});
218264
}
219265

220-
function forkRepo(forkedRepo, username, user, repo, repoLocation, cb) {
266+
function forkRepo(link, forkedRepo, username, user, repo, repoLocation, cb) {
221267
var client = github.client({
222268
username: username,
223269
password: password
224270
});
225271

226272
client.me().fork(user + '/' + repo, function (err, data) {
227273
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);
230276
return cb(err);
231277
} else {
232278
return cb(null, 'OK');
233279
}
234280
});
235281
}
236282

237-
function submitPullRequest(username, user, repo, status, cb) {
283+
function submitPullRequest(link, username, user, repo, status, cb) {
238284
if (status === 'DONE') {
239285
return cb(null, 'DONE');
240286
}
@@ -288,17 +334,16 @@ function submitPullRequest(username, user, repo, status, cb) {
288334
request.post({url: url, body: payload}, function (error, response, body) {
289335
if (!error && response.statusCode === 201) {//Status: 201 Created
290336
app.log.info('Pull Request to ' + user + '/' + repo + ' from ' + username + '/' + repo + ' Succesfull!');
337+
redisClient.hset(npm_hash, link, 'processed');
338+
291339
return cb(null, 'DONE');
292340
} else {
293-
//app.log.debug('response:');
294-
//app.log.debug(response.statusCode);
295-
//app.log.debug(response.body);
296-
app.log.error('error: ' + error);
297341
if (error === null) {
298342
try {
299343
throw new Error(response.statusCode + ' ' + response.body.toString());
300344
}catch (err) {
301-
cb(err);
345+
app.log.error('submitPullRequest::error : ' + err);
346+
return cb(null, 'DONE');
302347
}
303348
} else {
304349
return cb(error);
@@ -383,7 +428,7 @@ function switchBranch(forkedRepo, repoLocation, status, cb) {
383428
});
384429
}
385430

386-
function commitRepo(forkedRepo, repoLocation, status, cb) {
431+
function commitRepo(link, forkedRepo, repoLocation, status, cb) {
387432

388433
if (status === 'DONE') {
389434
return cb(null, 'DONE');
@@ -403,6 +448,7 @@ function commitRepo(forkedRepo, repoLocation, status, cb) {
403448
console.dir(stderr);
404449
if (stdout === '# On branch clean\nnothing to commit (working directory clean)\n') {
405450
app.log.info(forkedRepo.blue.bold + '@' + repoLocation.yellow.bold + ':clean branch ' + 'NOTHING TO COMMIT'.red.bold);
451+
redisClient.hset(npm_hash, link, 'processed');
406452
return cb(null, 'DONE');
407453
} else {
408454
return cb(error);
@@ -433,7 +479,7 @@ function pushCommit(forkedRepo, repoLocation, status, cb) {
433479

434480
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') {
435481
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');
437483
} else {
438484
return cb(error);
439485
}
@@ -453,6 +499,7 @@ function notifyAvailability(forkedRepo, username, repo, repoLocation, status, cb
453499
app.log.info('Waiting for ' + username.magenta.bold + '/' + repo.yellow.bold + ' to become available...');
454500
}
455501
request.head(forkedRepo, function (error, response, body) {
502+
app.log.debug('notifyAvailability :: forkedRepo = ' + forkedRepo + ' | ' + 'response.statusCode = ' + response.statusCode);
456503
if (!error && response.statusCode === 200) {
457504
Available = true;
458505
}
@@ -480,11 +527,11 @@ function notifyAvailability(forkedRepo, username, repo, repoLocation, status, cb
480527
);
481528
}
482529

483-
function walkAndFix(link, status, cb) {
530+
function walkAndFix(link, repoLocation, status, cb) {
484531
if (status === 'DONE') {
485532
return cb(null, 'DONE');
486533
}
487-
walk(link, function (err, results) {
534+
walk(repoLocation, function (err, results) {
488535
if (err) {
489536
return cb(err);
490537
}
@@ -496,7 +543,8 @@ function walkAndFix(link, status, cb) {
496543
//app.log.debug(results);
497544
//app.log.debug(results.indexOf('OK'));
498545
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');
500548
return cb(null, 'DONE');
501549
} else {
502550
return cb(null, 'OK');
@@ -587,12 +635,5 @@ function doFileUpdate(filename, cb) {
587635
});
588636
}
589637

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+
598639

config/config.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
11
{
2-
}
2+
"name": "node-migrator-bot",
3+
"username":"node-migrator-bot",
4+
"password":"****",
5+
"database": {
6+
"port": 9241,
7+
"host": "koi.redistogo.com",
8+
"password": "****",
9+
"npm_hash": "npm_june_2012"
10+
}
11+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"async": "~0.1.22",
1010
"request": "~2.9.202",
1111
"octonode": "~0.2.0",
12-
"rimraf": "~2.0.2"
12+
"rimraf": "~2.0.2",
13+
"redis": "~0.7.2"
1314
},
1415
"devDependencies": {
1516
"cli-easy": "0.1.0",

0 commit comments

Comments
 (0)