From 3d9e84ab7ca80bffe9eb18247a992a86b1a79309 Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Mon, 19 Jul 2021 13:31:43 -0700 Subject: [PATCH 1/4] fix: Add default Jenkins slug --- src/ci_providers/provider_jenkinsci.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ci_providers/provider_jenkinsci.js b/src/ci_providers/provider_jenkinsci.js index beb7321b3..44722f3f0 100644 --- a/src/ci_providers/provider_jenkinsci.js +++ b/src/ci_providers/provider_jenkinsci.js @@ -47,8 +47,8 @@ function _getSHA(inputs) { } function _getSlug(inputs) { - const { args } = inputs - return args.slug || '' + const { args, envs } = inputs + return args.slug || envs.VCS_SLUG || '' } function getServiceParams(inputs) { From 54a2666476cdd78c441bbf77331c74a5f7da0a9e Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Mon, 19 Jul 2021 15:52:58 -0700 Subject: [PATCH 2/4] fix: Use remote address not VCS --- src/ci_providers/provider_jenkinsci.js | 6 +++-- test/providers/provider_jenkinsci.test.js | 29 +++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/ci_providers/provider_jenkinsci.js b/src/ci_providers/provider_jenkinsci.js index 44722f3f0..515d85595 100644 --- a/src/ci_providers/provider_jenkinsci.js +++ b/src/ci_providers/provider_jenkinsci.js @@ -1,3 +1,5 @@ +const { parseSlugFromRemoteAddr } = require('../helpers/git') + function detect(envs) { return envs.JENKINS_URL } @@ -47,8 +49,8 @@ function _getSHA(inputs) { } function _getSlug(inputs) { - const { args, envs } = inputs - return args.slug || envs.VCS_SLUG || '' + const { args } = inputs + return args.slug || parseSlugFromRemoteAddr('') || '' } function getServiceParams(inputs) { diff --git a/test/providers/provider_jenkinsci.test.js b/test/providers/provider_jenkinsci.test.js index f19a4ec2d..f48e0432f 100644 --- a/test/providers/provider_jenkinsci.test.js +++ b/test/providers/provider_jenkinsci.test.js @@ -1,4 +1,5 @@ const td = require('testdouble') +const childProcess = require('child_process') const providerJenkinsci = require('../../src/ci_providers//provider_jenkinsci') @@ -80,6 +81,34 @@ describe('Jenkins CI Params', () => { expect(params).toMatchObject(expected) }) + it('can get the slug from git config', () => { + const inputs = { + args: {}, + envs: { + BUILD_NUMBER: 1, + BUILD_URL: 'https://example.jenkins.com', + CHANGE_ID: 2, + GIT_BRANCH: 'main', + GIT_COMMIT: 'testingsha', + JENKINS_URL: 'https://example.com', + }, + } + const spawnSync = td.replace(childProcess, 'spawnSync') + td.when( + spawnSync('git', [ + 'config', + '--get', + 'remote.origin.url', + '||', + 'echo', + "''", + ]), + ).thenReturn({ stdout: 'https://github.com/testOrg/testRepo.git' }) + + const params = providerJenkinsci.getServiceParams(inputs) + expect(params.slug).toBe('testOrg/testRepo') + }) + it('gets correct params for overrides', () => { const inputs = { args: { From 07c7c3ba0274924a1ac730a26b60a94d26b7772e Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Tue, 20 Jul 2021 07:56:04 -0700 Subject: [PATCH 3/4] fix: Remove shell commands --- test/providers/provider_jenkinsci.test.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/providers/provider_jenkinsci.test.js b/test/providers/provider_jenkinsci.test.js index f48e0432f..c48a6cecc 100644 --- a/test/providers/provider_jenkinsci.test.js +++ b/test/providers/provider_jenkinsci.test.js @@ -99,9 +99,6 @@ describe('Jenkins CI Params', () => { 'config', '--get', 'remote.origin.url', - '||', - 'echo', - "''", ]), ).thenReturn({ stdout: 'https://github.com/testOrg/testRepo.git' }) From f45dc7feddfd40afe45ab41ffcb8ec60d5c4ed6f Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Tue, 20 Jul 2021 10:37:55 -0700 Subject: [PATCH 4/4] fix: Remove hg command from remoteAddr --- src/helpers/git.js | 9 +++------ test/providers/provider_gitlabci.test.js | 17 ++++++++--------- test/providers/provider_jenkinsci.test.js | 8 ++++++++ 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/helpers/git.js b/src/helpers/git.js index 941a58d49..9c9bd9184 100644 --- a/src/helpers/git.js +++ b/src/helpers/git.js @@ -24,16 +24,13 @@ function parseSlug(slug) { function parseSlugFromRemoteAddr(remoteAddr) { let slug = '' if (!remoteAddr) { - remoteAddr = childProcess + remoteAddr = (childProcess .spawnSync('git', [ 'config', '--get', 'remote.origin.url', - '||', - 'echo', - "''", - ]) - .stdout.toString() + ]).stdout || '') + .toString() .trimRight() } if (remoteAddr) { diff --git a/test/providers/provider_gitlabci.test.js b/test/providers/provider_gitlabci.test.js index 3597fedef..3f6590a9e 100644 --- a/test/providers/provider_gitlabci.test.js +++ b/test/providers/provider_gitlabci.test.js @@ -47,6 +47,14 @@ describe('GitLabCI Params', () => { service: 'gitlab', slug: '', } + const spawnSync = td.replace(childProcess, 'spawnSync') + td.when( + spawnSync('git', [ + 'config', + '--get', + 'remote.origin.url', + ]), + ).thenReturn({ stdout: '' }) const params = providerGitLabci.getServiceParams(inputs) expect(params).toMatchObject(expected) }) @@ -132,9 +140,6 @@ describe('GitLabCI Params', () => { 'config', '--get', 'remote.origin.url', - '||', - 'echo', - "''", ]), ).thenReturn({ stdout: 'https://gitlab.com/testOrg/testRepo.git' }) @@ -150,9 +155,6 @@ describe('GitLabCI Params', () => { 'config', '--get', 'remote.origin.url', - '||', - 'echo', - "''", ]), ).thenReturn({ stdout: 'git@gitlab.com:/' }) @@ -168,9 +170,6 @@ describe('GitLabCI Params', () => { 'config', '--get', 'remote.origin.url', - '||', - 'echo', - "''", ]), ).thenReturn({ stdout: '' }) diff --git a/test/providers/provider_jenkinsci.test.js b/test/providers/provider_jenkinsci.test.js index c48a6cecc..0682a8665 100644 --- a/test/providers/provider_jenkinsci.test.js +++ b/test/providers/provider_jenkinsci.test.js @@ -77,6 +77,14 @@ describe('Jenkins CI Params', () => { service: 'jenkins', slug: '', } + const spawnSync = td.replace(childProcess, 'spawnSync') + td.when( + spawnSync('git', [ + 'config', + '--get', + 'remote.origin.url', + ]), + ).thenReturn({ stdout: '' }) const params = providerJenkinsci.getServiceParams(inputs) expect(params).toMatchObject(expected) })