Skip to content

Commit 3e1beda

Browse files
authored
Merge pull request #220 from codecov/fix-jenkins-slug
fix: Add default Jenkins slug
2 parents 2e3228a + f45dc7f commit 3e1beda

File tree

4 files changed

+48
-16
lines changed

4 files changed

+48
-16
lines changed

src/ci_providers/provider_jenkinsci.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { parseSlugFromRemoteAddr } = require('../helpers/git')
2+
13
function detect(envs) {
24
return envs.JENKINS_URL
35
}
@@ -48,7 +50,7 @@ function _getSHA(inputs) {
4850

4951
function _getSlug(inputs) {
5052
const { args } = inputs
51-
return args.slug || ''
53+
return args.slug || parseSlugFromRemoteAddr('') || ''
5254
}
5355

5456
function getServiceParams(inputs) {

src/helpers/git.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,13 @@ function parseSlug(slug) {
2424
function parseSlugFromRemoteAddr(remoteAddr) {
2525
let slug = ''
2626
if (!remoteAddr) {
27-
remoteAddr = childProcess
27+
remoteAddr = (childProcess
2828
.spawnSync('git', [
2929
'config',
3030
'--get',
3131
'remote.origin.url',
32-
'||',
33-
'echo',
34-
"''",
35-
])
36-
.stdout.toString()
32+
]).stdout || '')
33+
.toString()
3734
.trimRight()
3835
}
3936
if (remoteAddr) {

test/providers/provider_gitlabci.test.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ describe('GitLabCI Params', () => {
4747
service: 'gitlab',
4848
slug: '',
4949
}
50+
const spawnSync = td.replace(childProcess, 'spawnSync')
51+
td.when(
52+
spawnSync('git', [
53+
'config',
54+
'--get',
55+
'remote.origin.url',
56+
]),
57+
).thenReturn({ stdout: '' })
5058
const params = providerGitLabci.getServiceParams(inputs)
5159
expect(params).toMatchObject(expected)
5260
})
@@ -132,9 +140,6 @@ describe('GitLabCI Params', () => {
132140
'config',
133141
'--get',
134142
'remote.origin.url',
135-
'||',
136-
'echo',
137-
"''",
138143
]),
139144
).thenReturn({ stdout: 'https://gitlab.com/testOrg/testRepo.git' })
140145

@@ -150,9 +155,6 @@ describe('GitLabCI Params', () => {
150155
'config',
151156
'--get',
152157
'remote.origin.url',
153-
'||',
154-
'echo',
155-
"''",
156158
]),
157159
).thenReturn({ stdout: '[email protected]:/' })
158160

@@ -168,9 +170,6 @@ describe('GitLabCI Params', () => {
168170
'config',
169171
'--get',
170172
'remote.origin.url',
171-
'||',
172-
'echo',
173-
"''",
174173
]),
175174
).thenReturn({ stdout: '' })
176175

test/providers/provider_jenkinsci.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const td = require('testdouble')
2+
const childProcess = require('child_process')
23

34
const providerJenkinsci = require('../../src/ci_providers//provider_jenkinsci')
45

@@ -76,10 +77,43 @@ describe('Jenkins CI Params', () => {
7677
service: 'jenkins',
7778
slug: '',
7879
}
80+
const spawnSync = td.replace(childProcess, 'spawnSync')
81+
td.when(
82+
spawnSync('git', [
83+
'config',
84+
'--get',
85+
'remote.origin.url',
86+
]),
87+
).thenReturn({ stdout: '' })
7988
const params = providerJenkinsci.getServiceParams(inputs)
8089
expect(params).toMatchObject(expected)
8190
})
8291

92+
it('can get the slug from git config', () => {
93+
const inputs = {
94+
args: {},
95+
envs: {
96+
BUILD_NUMBER: 1,
97+
BUILD_URL: 'https://example.jenkins.com',
98+
CHANGE_ID: 2,
99+
GIT_BRANCH: 'main',
100+
GIT_COMMIT: 'testingsha',
101+
JENKINS_URL: 'https://example.com',
102+
},
103+
}
104+
const spawnSync = td.replace(childProcess, 'spawnSync')
105+
td.when(
106+
spawnSync('git', [
107+
'config',
108+
'--get',
109+
'remote.origin.url',
110+
]),
111+
).thenReturn({ stdout: 'https://github.com/testOrg/testRepo.git' })
112+
113+
const params = providerJenkinsci.getServiceParams(inputs)
114+
expect(params.slug).toBe('testOrg/testRepo')
115+
})
116+
83117
it('gets correct params for overrides', () => {
84118
const inputs = {
85119
args: {

0 commit comments

Comments
 (0)