diff --git a/lib/git.js b/lib/git.js new file mode 100644 index 00000000..8d9309a9 --- /dev/null +++ b/lib/git.js @@ -0,0 +1,13 @@ +var execSync = require("child_process").execSync; + +module.exports = { + + branch: function(){ + return execSync("git rev-parse --abbrev-ref HEAD || hg branch").toString().trim(); + }, + + head: function(){ + return execSync("git log -1 --pretty=%H || hg id -i --debug | tr -d '+'").toString().trim(); + } + +}; diff --git a/lib/services/drone.js b/lib/services/drone.js index 6f59154a..38ab4681 100644 --- a/lib/services/drone.js +++ b/lib/services/drone.js @@ -1,4 +1,4 @@ -var execSync = require('child_process').execSync; +var git = require('../git'); module.exports = { @@ -11,7 +11,7 @@ module.exports = { return { service : 'drone.io', build : process.env.DRONE_BUILD_NUMBER, - commit : execSync("git rev-parse HEAD || hg id -i --debug | tr -d '+'").toString().trim(), + commit : git.head(), build_url : process.env.DRONE_BUILD_URL, branch : process.env.DRONE_BRANCH, root : process.env.DRONE_BUILD_DIR diff --git a/lib/services/jenkins.js b/lib/services/jenkins.js index f280093d..527b8cf2 100644 --- a/lib/services/jenkins.js +++ b/lib/services/jenkins.js @@ -1,3 +1,5 @@ +var git = require('../git'); + module.exports = { detect : function(){ @@ -8,12 +10,12 @@ module.exports = { console.log(' Jenkins CI Detected'); return { service : 'jenkins', - commit : process.env.ghprbActualCommit || process.env.GIT_COMMIT, - branch : process.env.ghprbSourceBranch || process.env.GIT_BRANCH, + commit : process.env.ghprbActualCommit || process.env.GIT_COMMIT || git.head(), + branch : process.env.ghprbSourceBranch || process.env.GIT_BRANCH || process.env.BRANCH_NAME, build : process.env.BUILD_NUMBER, build_url : process.env.BUILD_URL, root : process.env.WORKSPACE, - pr : process.env.ghprbPullId + pr : process.env.ghprbPullId || process.env.CHANGE_ID }; } diff --git a/lib/services/localGit.js b/lib/services/localGit.js index a3171d63..479df577 100644 --- a/lib/services/localGit.js +++ b/lib/services/localGit.js @@ -1,14 +1,14 @@ -var execSync = require('child_process').execSync; +var git = require('../git') module.exports = { configuration : function(){ console.log(' No CI Detected. Using git/mercurial'); - var branch = execSync("git rev-parse --abbrev-ref HEAD || hg branch").toString().trim(); + var branch = git.branch(); if (branch === 'HEAD') { branch = 'master'; } - var head = execSync("git rev-parse HEAD || hg id -i --debug | tr -d '+'").toString().trim(); + var head = git.head(); return { commit : head, branch : branch diff --git a/test/detect.js b/test/detect.js index a384ca9d..305e2f34 100644 --- a/test/detect.js +++ b/test/detect.js @@ -1,5 +1,5 @@ var detect = require("../lib/detect"); -var execSync = require('child_process').execSync; +var git = require("../lib/git"); describe("Codecov", function(){ @@ -13,7 +13,7 @@ describe("Codecov", function(){ it("can select local git service if no service is found", function(){ expect(detect().commit).to.match(/^\w{40}$/); - expect(detect().commit).to.eql(execSync("git rev-parse HEAD || hg id -i --debug | tr -d '+'").toString().trim()); + expect(detect().commit).to.eql(git.head()); }); }); diff --git a/test/git.js b/test/git.js new file mode 100644 index 00000000..f31684e9 --- /dev/null +++ b/test/git.js @@ -0,0 +1,14 @@ +var git = require("../lib/git"); +var execSync = require("child_process").execSync; + +describe("Git", function(){ + + it("can get the branch", function(){ + expect(git.branch()).to.eql(execSync("git rev-parse --abbrev-ref HEAD || hg branch").toString().trim()); + }); + + it("can get the head", function(){ + expect(git.head()).to.eql(execSync("git log -1 --pretty=%H || hg id -i --debug | tr -d '+'").toString().trim()); + }); + +}); diff --git a/test/services/drone.js b/test/services/drone.js index ef28b8ec..47c062e4 100644 --- a/test/services/drone.js +++ b/test/services/drone.js @@ -1,5 +1,5 @@ var drone = require("../../lib/services/drone"); -var execSync = require('child_process').execSync; +var git = require("../../lib/git"); describe("Drone.io CI Provider", function(){ @@ -15,7 +15,7 @@ describe("Drone.io CI Provider", function(){ process.env.DRONE_BUILD_DIR = '/'; expect(drone.configuration()).to.eql({ service : 'drone.io', - commit : execSync("git rev-parse HEAD || hg id -i --debug | tr -d '+'").toString().trim(), + commit : git.head(), build : '1234', root : '/', branch : 'master', diff --git a/test/services/jenkins.js b/test/services/jenkins.js index d0eb0c72..b2600a89 100644 --- a/test/services/jenkins.js +++ b/test/services/jenkins.js @@ -1,4 +1,5 @@ var jenkins = require("../../lib/services/jenkins"); +var git = require("../../lib/git"); describe("Jenkins CI Provider", function(){ @@ -24,6 +25,24 @@ describe("Jenkins CI Provider", function(){ }); }); + it ("can get service env info when using Blue Ocean", function(){ + delete process.env.GIT_COMMIT; + delete process.env.GIT_BRANCH; + process.env.BUILD_NUMBER = '1234'; + process.env.BUILD_URL = 'http://asdf/'; + process.env.BRANCH_NAME = 'master'; + process.env.WORKSPACE = '/'; + expect(jenkins.configuration()).to.eql({ + service : 'jenkins', + build_url : 'http://asdf/', + build : '1234', + root : '/', + commit : git.head(), + pr : undefined, + branch : 'master' + }); + }) + it ("github pull request env variables win out over jenkins variables", function(){ process.env.BUILD_NUMBER = '1234'; process.env.BUILD_URL = 'http://asdf/'; diff --git a/test/upload.js b/test/upload.js index d697313b..4eb099de 100644 --- a/test/upload.js +++ b/test/upload.js @@ -1,8 +1,6 @@ var fs = require('fs'); var codecov = require('../lib/codecov'); var offlineErrors = require('../lib/offline'); -var execSync = require('child_process').execSync; - describe("Codecov", function(){ it("can get upload to v2", function(done){