Skip to content

Commit aca63bd

Browse files
juan-fernandeznsavoire
authored andcommitted
[ci-visibility] Use correct repository URL for git metadata upload (#3253)
1 parent 60b6d13 commit aca63bd

File tree

4 files changed

+65
-18
lines changed

4 files changed

+65
-18
lines changed

packages/dd-trace/src/ci-visibility/exporters/ci-visibility-exporter.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ class CiVisibilityExporter extends AgentInfoExporter {
120120
* CI Visibility Protocol, hence the this._canUseCiVisProtocol promise.
121121
*/
122122
getItrConfiguration (testConfiguration, callback) {
123-
this.sendGitMetadata()
123+
const { repositoryUrl } = testConfiguration
124+
this.sendGitMetadata(repositoryUrl)
124125
if (!this.shouldRequestItrConfiguration()) {
125126
return callback(null, {})
126127
}
@@ -147,15 +148,15 @@ class CiVisibilityExporter extends AgentInfoExporter {
147148
})
148149
}
149150

150-
sendGitMetadata () {
151+
sendGitMetadata (repositoryUrl) {
151152
if (!this._config.isGitUploadEnabled) {
152153
return
153154
}
154155
this._canUseCiVisProtocolPromise.then((canUseCiVisProtocol) => {
155156
if (!canUseCiVisProtocol) {
156157
return
157158
}
158-
sendGitMetadataRequest(this._getApiUrl(), !!this._isUsingEvpProxy, (err) => {
159+
sendGitMetadataRequest(this._getApiUrl(), !!this._isUsingEvpProxy, repositoryUrl, (err) => {
159160
if (err) {
160161
log.error(`Error uploading git metadata: ${err.message}`)
161162
} else {

packages/dd-trace/src/ci-visibility/exporters/git/git_metadata.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,11 @@ function uploadPackFile ({ url, isEvpProxy, packFileToUpload, repositoryUrl, hea
152152
/**
153153
* This function uploads git metadata to CI Visibility's backend.
154154
*/
155-
function sendGitMetadata (url, isEvpProxy, callback) {
156-
const repositoryUrl = getRepositoryUrl()
155+
function sendGitMetadata (url, isEvpProxy, configRepositoryUrl, callback) {
156+
let repositoryUrl = configRepositoryUrl
157+
if (!repositoryUrl) {
158+
repositoryUrl = getRepositoryUrl()
159+
}
157160

158161
if (!repositoryUrl) {
159162
return callback(new Error('Repository URL is empty'))

packages/dd-trace/test/ci-visibility/exporters/ci-visibility-exporter.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,23 @@ describe('CI Visibility Exporter', () => {
5959
ciVisibilityExporter._resolveCanUseCiVisProtocol(true)
6060
ciVisibilityExporter.sendGitMetadata()
6161
})
62+
it('should use the input repository URL', (done) => {
63+
nock(`http://localhost:${port}`)
64+
.post('/api/v2/git/repository/search_commits')
65+
.reply(200, function () {
66+
const { meta: { repository_url: repositoryUrl } } = JSON.parse(this.req.requestBodyBuffers.toString())
67+
expect(repositoryUrl).to.equal('https://[email protected]')
68+
done()
69+
})
70+
.post('/api/v2/git/repository/packfile')
71+
.reply(202, '')
72+
73+
const url = new URL(`http://localhost:${port}`)
74+
const ciVisibilityExporter = new CiVisibilityExporter({ url, isGitUploadEnabled: true })
75+
76+
ciVisibilityExporter._resolveCanUseCiVisProtocol(true)
77+
ciVisibilityExporter.sendGitMetadata('https://[email protected]')
78+
})
6279
})
6380

6481
describe('getItrConfiguration', () => {

packages/dd-trace/test/ci-visibility/exporters/git/git_metadata.spec.js

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe('git_metadata', () => {
7373
.reply(204)
7474

7575
isShallowRepositoryStub.returns(true)
76-
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, (err) => {
76+
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, '', (err) => {
7777
expect(unshallowRepositoryStub).to.have.been.called
7878
expect(err).to.be.null
7979
expect(scope.isDone()).to.be.true
@@ -88,7 +88,7 @@ describe('git_metadata', () => {
8888
.post('/api/v2/git/repository/packfile')
8989
.reply(204)
9090

91-
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, (err) => {
91+
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, '', (err) => {
9292
expect(err).to.be.null
9393
expect(scope.isDone()).to.be.true
9494
done()
@@ -104,7 +104,7 @@ describe('git_metadata', () => {
104104

105105
getCommitsToUploadStub.returns([])
106106

107-
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, (err) => {
107+
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, '', (err) => {
108108
expect(err).to.be.null
109109
// to check that it is not called
110110
expect(scope.isDone()).to.be.false
@@ -120,7 +120,7 @@ describe('git_metadata', () => {
120120
.post('/api/v2/git/repository/packfile')
121121
.reply(204)
122122

123-
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, (err) => {
123+
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, '', (err) => {
124124
// eslint-disable-next-line
125125
expect(err.message).to.contain('Error fetching commits to exclude: Error from https://api.test.com/api/v2/git/repository/search_commits: 404 Not Found. Response from the endpoint: "Not found SHA"')
126126
// to check that it is not called
@@ -137,7 +137,7 @@ describe('git_metadata', () => {
137137
.post('/api/v2/git/repository/packfile')
138138
.reply(204)
139139

140-
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, (err) => {
140+
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, '', (err) => {
141141
expect(err.message).to.contain("Can't parse commits to exclude response: Invalid commit type response")
142142
// to check that it is not called
143143
expect(scope.isDone()).to.be.false
@@ -153,7 +153,7 @@ describe('git_metadata', () => {
153153
.post('/api/v2/git/repository/packfile')
154154
.reply(204)
155155

156-
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, (err) => {
156+
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, '', (err) => {
157157
expect(err.message).to.contain("Can't parse commits to exclude response: Invalid commit format")
158158
// to check that it is not called
159159
expect(scope.isDone()).to.be.false
@@ -169,7 +169,7 @@ describe('git_metadata', () => {
169169
.post('/api/v2/git/repository/packfile')
170170
.reply(502)
171171

172-
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, (err) => {
172+
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, '', (err) => {
173173
expect(err.message).to.contain('Could not upload packfiles: status code 502')
174174
expect(scope.isDone()).to.be.true
175175
done()
@@ -196,7 +196,7 @@ describe('git_metadata', () => {
196196
secondTemporaryPackFile
197197
])
198198

199-
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, (err) => {
199+
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, '', (err) => {
200200
expect(err).to.be.null
201201
expect(scope.isDone()).to.be.true
202202
done()
@@ -215,7 +215,7 @@ describe('git_metadata', () => {
215215
'not there either'
216216
])
217217

218-
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, (err) => {
218+
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, '', (err) => {
219219
expect(err.message).to.contain('Could not read "not-there"')
220220
expect(scope.isDone()).to.be.false
221221
done()
@@ -231,7 +231,7 @@ describe('git_metadata', () => {
231231

232232
generatePackFilesForCommitsStub.returns([])
233233

234-
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, (err) => {
234+
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, '', (err) => {
235235
expect(err.message).to.contain('Failed to generate packfiles')
236236
expect(scope.isDone()).to.be.false
237237
done()
@@ -247,7 +247,7 @@ describe('git_metadata', () => {
247247

248248
getRepositoryUrlStub.returns('')
249249

250-
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, (err) => {
250+
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, '', (err) => {
251251
expect(err.message).to.contain('Repository URL is empty')
252252
expect(scope.isDone()).to.be.false
253253
done()
@@ -265,7 +265,7 @@ describe('git_metadata', () => {
265265
.post('/api/v2/git/repository/packfile')
266266
.reply(204)
267267

268-
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, (err) => {
268+
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, '', (err) => {
269269
expect(err).to.be.null
270270
expect(scope.isDone()).to.be.true
271271
done()
@@ -282,9 +282,35 @@ describe('git_metadata', () => {
282282
done()
283283
})
284284

285-
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), true, (err) => {
285+
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), true, '', (err) => {
286286
expect(err).to.be.null
287287
expect(scope.isDone()).to.be.true
288288
})
289289
})
290+
291+
it('should use the input repository url and not call getRepositoryUrl', (done) => {
292+
let resolvePromise
293+
const requestPromise = new Promise(resolve => {
294+
resolvePromise = resolve
295+
})
296+
const scope = nock('https://api.test.com')
297+
.post('/evp_proxy/v2/api/v2/git/repository/search_commits')
298+
.reply(200, function () {
299+
const { meta: { repository_url: repositoryUrl } } = JSON.parse(this.req.requestBodyBuffers.toString())
300+
resolvePromise(repositoryUrl)
301+
return JSON.stringify({ data: [] })
302+
})
303+
.post('/evp_proxy/v2/api/v2/git/repository/packfile')
304+
.reply(204)
305+
306+
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), true, 'https://[email protected]', (err) => {
307+
expect(err).to.be.null
308+
expect(scope.isDone()).to.be.true
309+
requestPromise.then((repositoryUrl) => {
310+
expect(getRepositoryUrlStub).not.to.have.been.called
311+
expect(repositoryUrl).to.equal('https://[email protected]')
312+
done()
313+
})
314+
})
315+
})
290316
})

0 commit comments

Comments
 (0)