diff --git a/src/ci_providers/provider_jenkinsci.js b/src/ci_providers/provider_jenkinsci.js index beb7321b3..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 } @@ -48,7 +50,7 @@ function _getSHA(inputs) { function _getSlug(inputs) { const { args } = inputs - return args.slug || '' + return args.slug || parseSlugFromRemoteAddr('') || '' } function getServiceParams(inputs) { 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 f19a4ec2d..0682a8665 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') @@ -76,10 +77,43 @@ 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) }) + 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', + ]), + ).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: {