diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/fetchGitData.js | 90 | ||||
| -rw-r--r-- | lib/getOptions.js | 35 | ||||
| -rw-r--r-- | lib/handleInput.js | 29 |
3 files changed, 93 insertions, 61 deletions
diff --git a/lib/fetchGitData.js b/lib/fetchGitData.js index 629d650..a77b4a8 100644 --- a/lib/fetchGitData.js +++ b/lib/fetchGitData.js @@ -1,41 +1,39 @@ -var exec = require("exec-sync"); +var exec = require('child_process').exec; var logger = require('./logger')(); -var fetchGitData = function(git) { +var fetchGitData = function(git, cb) { + if (!cb){ + throw new Error("fetchGitData requires a callback"); + } var i; var execGit = true; var head = { "author_name": { "format": "'%aN'", - "default": "Unknown Author" }, "author_email": { "format": "'%ae'", - "default": "" }, "committer_name": { "format": "'%cN'", - "default": "Unknown Committer" }, "committer_email": { "format": "'%ce'", - "default" :"" }, "message": { "format": "'%s'", - "default": "Unknown Commit Message" } }; var remotes = {}; //-- Malformed/undefined git object if ('undefined' === typeof git) { - throw new Error('No options passed'); + return cb(new Error('No options passed')); } else if (!git.hasOwnProperty('head')) { - throw new Error('You must provide the head'); + return cb(new Error('You must provide the head')); } else if (!git.head.hasOwnProperty('id')) { - throw new Error('You must provide the head.id'); + return cb(new Error('You must provide the head.id')); } function saveRemote(name, url, push) { @@ -71,37 +69,53 @@ var fetchGitData = function(git) { } //-- Use git? - try { - exec("git log -1 " + git.head.id + " --pretty=format:'%H'"); - } catch (e) { - execGit = false; - } - - //-- Head - for (i in head) { - if (!git.head.hasOwnProperty(i)) { - if (execGit) { - git.head[i] = exec("git log -1 " + git.head.id + " --pretty=format:" + head[i].format); - } else { - git.head[i] = head[i].default; - } + exec("git log -1 " + git.head.id + " --pretty=format:'%H'", function(err, response){ + if (err){ + // git is not available... + git.head.author_name = git.head.author_name || "Unknown Author"; + git.head.author_email = git.head.author_email || ""; + git.head.committer_name = git.head.committer_name || "Unknown Committer"; + git.head.committer_email = git.head.committer_email || ""; + git.head.message = git.head.message || "Unknown Commit Message"; + return cb(null, git); } - } - - if (execGit) { - - //-- Branch - git.branch = exec("git branch").split("\n")[0].replace(/^\*\ /, "").trim(); - exec("git remote -v").split("\n").forEach(function(remote) { - remote = remote.split(/\s/); - saveRemote(remote[0], remote[1]); + //-- Head + var commands = []; + var fields = []; + for (var field in head) { + fields.push(field); + var command = "git log -1 " + git.head.id + " --pretty=format:" + head[field].format; + commands.push(command); + } + var i = 0; + var remaining = commands.length; + commands.forEach(function(command){ + var field = fields[i]; + i++; + exec(command, function(err, response){ + if (err) return cb(err); + git.head[field] = response; + remaining--; + if (remaining === 0){ + //-- Branch + exec("git branch", function(err, branches){ + if (err) return cb(err); + git.branch = branches.split("\n")[0].replace(/^\*\ /, "").trim(); + exec("git remote -v", function(err, remotes){ + if (err) return cb(err); + remotes.split("\n").forEach(function(remote) { + remote = remote.split(/\s/); + saveRemote(remote[0], remote[1]); + }); + return cb(null, git); + }); + }); + } + }); }); - - } - - return git; - + }); }; + module.exports = fetchGitData; diff --git a/lib/getOptions.js b/lib/getOptions.js index deb1072..97c79d5 100644 --- a/lib/getOptions.js +++ b/lib/getOptions.js @@ -2,9 +2,12 @@ var fs = require('fs'); var path = require('path'); var yaml = require('yaml'); var logger = require('./logger')(); -var git = require('./fetchGitData'); +var fetchGitData = require('./fetchGitData'); -var getOptions = function(){ +var getOptions = function(cb){ + if (!cb){ + throw new Error('getOptions requires a callback'); + } var options = {}; // try to get filepath from the command-line @@ -41,14 +44,6 @@ var getOptions = function(){ git_branch = process.env.CIRCLE_BRANCH; } - if (git_commit){ - options.git = git({ - head: { - id: git_commit - }, - branch: git_branch - }); - } options.run_at = process.env.COVERALLS_RUN_AT || JSON.stringify(new Date()).slice(1, -1); if (process.env.COVERALLS_SERVICE_NAME){ @@ -73,7 +68,25 @@ var getOptions = function(){ logger.warn("Repo token could not be determined. Continuing without it."); } } - return options; + + if (git_commit){ + fetchGitData({ + head: { + id: git_commit + }, + branch: git_branch + }, function(err, git){ + if (err){ + logger.warn('there was an error getting git data: ', err); + } else { + options.git = git; + } + return cb(err, options); + }); + } else { + return cb(null, options); + } + }; module.exports = getOptions; diff --git a/lib/handleInput.js b/lib/handleInput.js index 8dee588..1435c1c 100644 --- a/lib/handleInput.js +++ b/lib/handleInput.js @@ -3,24 +3,29 @@ var logger = require('./logger')(); var handleInput = function(input){ logger.debug(input); - var options = index.getOptions(); - logger.debug(options); - - index.convertLcovToCoveralls(input, options, function(err, postData){ + var options = index.getOptions(function(err, options){ if (err){ - logger.error("error from convertLcovToCoveralls"); + logger.error("error from getOptions"); throw err; } - logger.info("sending this to coveralls.io: ", JSON.stringify(postData)); - index.sendToCoveralls(postData, function(err, response, body){ + logger.debug(options); + + index.convertLcovToCoveralls(input, options, function(err, postData){ if (err){ + logger.error("error from convertLcovToCoveralls"); throw err; } - if (response.statusCode >= 400){ - throw "Bad response: " + response.statusCode + " " + body; - } - logger.debug(response.statusCode); - logger.debug(body); + logger.info("sending this to coveralls.io: ", JSON.stringify(postData)); + index.sendToCoveralls(postData, function(err, response, body){ + if (err){ + throw err; + } + if (response.statusCode >= 400){ + throw "Bad response: " + response.statusCode + " " + body; + } + logger.debug(response.statusCode); + logger.debug(body); + }); }); }); |
