From 0530519682486ff764d79af28da1dad9742d7f30 Mon Sep 17 00:00:00 2001 From: rochdev Date: Fri, 17 Mar 2017 14:53:53 -0400 Subject: [PATCH] Add support for Jenkins Blue Ocean --- lib/git.js | 13 +++++++++++++ lib/services/drone.js | 4 ++-- lib/services/jenkins.js | 8 +++++--- lib/services/localGit.js | 6 +++--- test/detect.js | 10 ++-------- test/git.js | 14 ++++++++++++++ test/services/drone.js | 10 ++-------- test/services/jenkins.js | 19 +++++++++++++++++++ test/upload.js | 7 ------- 9 files changed, 60 insertions(+), 31 deletions(-) create mode 100644 lib/git.js create mode 100644 test/git.js diff --git a/lib/git.js b/lib/git.js new file mode 100644 index 00000000..39db8064 --- /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 rev-parse HEAD || 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 f2142816..01286741 100644 --- a/test/detect.js +++ b/test/detect.js @@ -1,12 +1,6 @@ var detect = require("../lib/detect"); -var execSync = require('child_process').execSync; -if (!execSync) { - var exec = require('execSync').exec; - var execSync = function(cmd){ - return exec(cmd).stdout; - }; -} +var git = require("../lib/git"); describe("Codecov", function(){ @@ -20,7 +14,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 2b9e167f..47c062e4 100644 --- a/test/services/drone.js +++ b/test/services/drone.js @@ -1,11 +1,5 @@ var drone = require("../../lib/services/drone"); -var execSync = require('child_process').execSync; -if (!execSync) { - var exec = require('execSync').exec; - var execSync = function(cmd){ - return exec(cmd).stdout; - }; -} +var git = require("../../lib/git"); describe("Drone.io CI Provider", function(){ @@ -21,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 1b96eaa0..51dda3d2 100644 --- a/test/upload.js +++ b/test/upload.js @@ -1,13 +1,6 @@ var fs = require('fs'); var codecov = require('../lib/codecov'); var offlineErrors = require('../lib/offline'); -var execSync = require('child_process').execSync; -if (!execSync) { - var exec = require('execSync').exec; - var execSync = function(cmd){ - return exec(cmd).stdout; - }; -} describe("Codecov", function(){