Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ci-visibility] Use correct repository URL for git metadata upload #3253

Merged
merged 1 commit into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ class CiVisibilityExporter extends AgentInfoExporter {
* CI Visibility Protocol, hence the this._canUseCiVisProtocol promise.
*/
getItrConfiguration (testConfiguration, callback) {
this.sendGitMetadata()
const { repositoryUrl } = testConfiguration
this.sendGitMetadata(repositoryUrl)
if (!this.shouldRequestItrConfiguration()) {
return callback(null, {})
}
Expand All @@ -147,15 +148,15 @@ class CiVisibilityExporter extends AgentInfoExporter {
})
}

sendGitMetadata () {
sendGitMetadata (repositoryUrl) {
if (!this._config.isGitUploadEnabled) {
return
}
this._canUseCiVisProtocolPromise.then((canUseCiVisProtocol) => {
if (!canUseCiVisProtocol) {
return
}
sendGitMetadataRequest(this._getApiUrl(), !!this._isUsingEvpProxy, (err) => {
sendGitMetadataRequest(this._getApiUrl(), !!this._isUsingEvpProxy, repositoryUrl, (err) => {
if (err) {
log.error(`Error uploading git metadata: ${err.message}`)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,11 @@ function uploadPackFile ({ url, isEvpProxy, packFileToUpload, repositoryUrl, hea
/**
* This function uploads git metadata to CI Visibility's backend.
*/
function sendGitMetadata (url, isEvpProxy, callback) {
const repositoryUrl = getRepositoryUrl()
function sendGitMetadata (url, isEvpProxy, configRepositoryUrl, callback) {
let repositoryUrl = configRepositoryUrl
if (!repositoryUrl) {
repositoryUrl = getRepositoryUrl()
}

if (!repositoryUrl) {
return callback(new Error('Repository URL is empty'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ describe('CI Visibility Exporter', () => {
ciVisibilityExporter._resolveCanUseCiVisProtocol(true)
ciVisibilityExporter.sendGitMetadata()
})
it('should use the input repository URL', (done) => {
nock(`http://localhost:${port}`)
.post('/api/v2/git/repository/search_commits')
.reply(200, function () {
const { meta: { repository_url: repositoryUrl } } = JSON.parse(this.req.requestBodyBuffers.toString())
expect(repositoryUrl).to.equal('https://custom-git@datadog.com')
done()
})
.post('/api/v2/git/repository/packfile')
.reply(202, '')

const url = new URL(`http://localhost:${port}`)
const ciVisibilityExporter = new CiVisibilityExporter({ url, isGitUploadEnabled: true })

ciVisibilityExporter._resolveCanUseCiVisProtocol(true)
ciVisibilityExporter.sendGitMetadata('https://custom-git@datadog.com')
})
})

describe('getItrConfiguration', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe('git_metadata', () => {
.reply(204)

isShallowRepositoryStub.returns(true)
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, (err) => {
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, '', (err) => {
uurien marked this conversation as resolved.
Show resolved Hide resolved
expect(unshallowRepositoryStub).to.have.been.called
expect(err).to.be.null
expect(scope.isDone()).to.be.true
Expand All @@ -88,7 +88,7 @@ describe('git_metadata', () => {
.post('/api/v2/git/repository/packfile')
.reply(204)

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

getCommitsToUploadStub.returns([])

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

gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, (err) => {
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, '', (err) => {
// eslint-disable-next-line
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"')
// to check that it is not called
Expand All @@ -137,7 +137,7 @@ describe('git_metadata', () => {
.post('/api/v2/git/repository/packfile')
.reply(204)

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

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

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

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

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

generatePackFilesForCommitsStub.returns([])

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

getRepositoryUrlStub.returns('')

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

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

gitMetadata.sendGitMetadata(new URL('https://api.test.com'), true, (err) => {
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), true, '', (err) => {
expect(err).to.be.null
expect(scope.isDone()).to.be.true
})
})

it('should use the input repository url and not call getRepositoryUrl', (done) => {
let resolvePromise
const requestPromise = new Promise(resolve => {
resolvePromise = resolve
})
const scope = nock('https://api.test.com')
.post('/evp_proxy/v2/api/v2/git/repository/search_commits')
.reply(200, function () {
const { meta: { repository_url: repositoryUrl } } = JSON.parse(this.req.requestBodyBuffers.toString())
resolvePromise(repositoryUrl)
return JSON.stringify({ data: [] })
})
.post('/evp_proxy/v2/api/v2/git/repository/packfile')
.reply(204)

gitMetadata.sendGitMetadata(new URL('https://api.test.com'), true, 'https://custom-git@datadog.com', (err) => {
expect(err).to.be.null
expect(scope.isDone()).to.be.true
requestPromise.then((repositoryUrl) => {
expect(getRepositoryUrlStub).not.to.have.been.called
expect(repositoryUrl).to.equal('https://custom-git@datadog.com')
done()
})
})
})
})