Skip to content

Commit

Permalink
[test visibility] Add check to determine whether git is available (#4588
Browse files Browse the repository at this point in the history
)
  • Loading branch information
juan-fernandez authored and uurien committed Aug 9, 2024
1 parent d9ae471 commit 16a4604
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const {
generatePackFilesForCommits,
getCommitsRevList,
isShallowRepository,
unshallowRepository
unshallowRepository,
isGitAvailable
} = require('../../../plugins/util/git')

const {
Expand Down Expand Up @@ -242,6 +243,9 @@ function generateAndUploadPackFiles ({
* This function uploads git metadata to CI Visibility's backend.
*/
function sendGitMetadata (url, { isEvpProxy, evpProxyPrefix }, configRepositoryUrl, callback) {
if (!isGitAvailable()) {
return callback(new Error('Git is not available'))
}
let repositoryUrl = configRepositoryUrl
if (!repositoryUrl) {
repositoryUrl = getRepositoryUrl()
Expand Down
15 changes: 14 additions & 1 deletion packages/dd-trace/src/plugins/util/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ function isDirectory (path) {
}
}

function isGitAvailable () {
const isWindows = os.platform() === 'win32'
const command = isWindows ? 'where' : 'which'
try {
cp.execFileSync(command, ['git'], { stdio: 'pipe' })
return true
} catch (e) {
incrementCountMetric(TELEMETRY_GIT_COMMAND_ERRORS, { command: 'check_git', exitCode: 'missing' })
return false
}
}

function isShallowRepository () {
return sanitizedExec(
'git',
Expand Down Expand Up @@ -342,5 +354,6 @@ module.exports = {
getCommitsRevList,
GIT_REV_LIST_MAX_BUFFER,
isShallowRepository,
unshallowRepository
unshallowRepository,
isGitAvailable
}
Original file line number Diff line number Diff line change
Expand Up @@ -325,17 +325,20 @@ describe('git_metadata', () => {
})

it('should not crash if git is missing', (done) => {
const oldPath = process.env.PATH
// git will not be found
process.env.PATH = ''

const scope = nock('https://api.test.com')
.post('/api/v2/git/repository/search_commits')
.reply(200, JSON.stringify({ data: [] }))
.post('/api/v2/git/repository/packfile')
.reply(204)

getRepositoryUrlStub.returns('')

gitMetadata.sendGitMetadata(new URL('https://api.test.com'), { isEvpProxy: false }, '', (err) => {
expect(err.message).to.contain('Repository URL is empty')
expect(err.message).to.contain('Git is not available')
expect(scope.isDone()).to.be.false
process.env.PATH = oldPath
done()
})
})
Expand Down
24 changes: 23 additions & 1 deletion packages/dd-trace/test/plugins/util/git.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const os = require('os')
const fs = require('fs')
const path = require('path')

const { GIT_REV_LIST_MAX_BUFFER } = require('../../../src/plugins/util/git')
const { GIT_REV_LIST_MAX_BUFFER, isGitAvailable } = require('../../../src/plugins/util/git')
const proxyquire = require('proxyquire')
const execFileSyncStub = sinon.stub().returns('')

Expand Down Expand Up @@ -378,3 +378,25 @@ describe('user credentials', () => {
.to.equal('ssh://host.xz:port/path/to/repo.git/')
})
})

describe('isGitAvailable', () => {
let originalPath

beforeEach(() => {
originalPath = process.env.PATH
})

afterEach(() => {
process.env.PATH = originalPath
})

it('returns true if git is available', () => {
expect(isGitAvailable()).to.be.true
})

it('returns false if git is not available', () => {
process.env.PATH = ''

expect(isGitAvailable()).to.be.false
})
})

0 comments on commit 16a4604

Please sign in to comment.