Skip to content

Commit ec764d1

Browse files
more and better tests
1 parent b7b5dfa commit ec764d1

File tree

4 files changed

+109
-5
lines changed

4 files changed

+109
-5
lines changed

packages/dd-trace/src/git_properties.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,30 @@ const commitSHARegex = /git\.commit\.sha=([a-f\d]{40})/
22
const repositoryUrlRegex = /git\.repository_url=([\w\d:@/.-]+)/
33

44
function getGitMetadataFromGitProperties (gitPropertiesString) {
5+
if (!gitPropertiesString) {
6+
return {}
7+
}
58
const commitSHAMatch = gitPropertiesString.match(commitSHARegex)
69
const repositoryUrlMatch = gitPropertiesString.match(repositoryUrlRegex)
710

11+
const repositoryUrl = repositoryUrlMatch ? repositoryUrlMatch[1] : undefined
12+
let parsedUrl = repositoryUrl
13+
14+
if (repositoryUrl) {
15+
try {
16+
// repository URLs can contain username and password, so we want to filter those out
17+
parsedUrl = new URL(repositoryUrl)
18+
if (parsedUrl.password) {
19+
parsedUrl = `${parsedUrl.origin}${parsedUrl.pathname}`
20+
}
21+
} catch (e) {
22+
// if protocol isn't https, no password will be used
23+
}
24+
}
25+
826
return {
9-
commitSHA: commitSHAMatch ? commitSHAMatch[1] : null,
10-
repositoryUrl: repositoryUrlMatch ? repositoryUrlMatch[1] : null
27+
commitSHA: commitSHAMatch ? commitSHAMatch[1] : undefined,
28+
repositoryUrl: parsedUrl
1129
}
1230
}
1331

packages/dd-trace/test/config.spec.js

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,14 +1061,64 @@ describe('Config', () => {
10611061
})
10621062

10631063
context('sci embedding', () => {
1064+
const DUMMY_COMMIT_SHA = 'b7b5dfa992008c77ab3f8a10eb8711e0092445b0'
1065+
const DUMMY_REPOSITORY_URL = '[email protected]:DataDog/dd-trace-js.git'
1066+
let ddTags
1067+
beforeEach(() => {
1068+
ddTags = process.env.DD_TAGS
1069+
})
10641070
afterEach(() => {
10651071
delete process.env.DD_GIT_PROPERTIES_FILE
1072+
delete process.env.DD_GIT_COMMIT_SHA
1073+
delete process.env.DD_GIT_REPOSITORY_URL
1074+
process.env.DD_TAGS = ddTags
10661075
})
1067-
it('reads git.properties', () => {
1076+
it('reads DD_GIT_* env vars', () => {
1077+
process.env.DD_GIT_COMMIT_SHA = DUMMY_COMMIT_SHA
1078+
process.env.DD_GIT_REPOSITORY_URL = DUMMY_REPOSITORY_URL
1079+
const config = new Config({})
1080+
expect(config).to.have.property('commitSHA', DUMMY_COMMIT_SHA)
1081+
expect(config).to.have.property('repositoryUrl', DUMMY_REPOSITORY_URL)
1082+
})
1083+
it('reads DD_TAGS env var', () => {
1084+
process.env.DD_TAGS = `git.commit.sha:${DUMMY_COMMIT_SHA},git.repository_url:${DUMMY_REPOSITORY_URL}`
1085+
process.env.DD_GIT_REPOSITORY_URL = DUMMY_REPOSITORY_URL
1086+
const config = new Config({})
1087+
expect(config).to.have.property('commitSHA', DUMMY_COMMIT_SHA)
1088+
expect(config).to.have.property('repositoryUrl', DUMMY_REPOSITORY_URL)
1089+
})
1090+
it('reads git.properties if it is available', () => {
10681091
process.env.DD_GIT_PROPERTIES_FILE = DD_GIT_PROPERTIES_FILE
10691092
const config = new Config({})
10701093
expect(config).to.have.property('commitSHA', '4e7da8069bcf5ffc8023603b95653e2dc99d1c7d')
1071-
expect(config).to.have.property('repositoryUrl', '[email protected]:DataDog/dd-trace-js.git')
1094+
expect(config).to.have.property('repositoryUrl', DUMMY_REPOSITORY_URL)
1095+
})
1096+
it('does not crash if git.properties is not available', () => {
1097+
process.env.DD_GIT_PROPERTIES_FILE = '/does/not/exist'
1098+
const config = new Config({})
1099+
expect(config).to.have.property('commitSHA', undefined)
1100+
expect(config).to.have.property('repositoryUrl', undefined)
1101+
})
1102+
it('does not read git.properties if env vars are passed', () => {
1103+
process.env.DD_GIT_PROPERTIES_FILE = DD_GIT_PROPERTIES_FILE
1104+
process.env.DD_GIT_COMMIT_SHA = DUMMY_COMMIT_SHA
1105+
process.env.DD_GIT_REPOSITORY_URL = 'https://github.com:env-var/dd-trace-js.git'
1106+
const config = new Config({})
1107+
expect(config).to.have.property('commitSHA', DUMMY_COMMIT_SHA)
1108+
expect(config).to.have.property('repositoryUrl', 'https://github.com:env-var/dd-trace-js.git')
1109+
})
1110+
it('still reads git.properties if one of the env vars is missing', () => {
1111+
process.env.DD_GIT_PROPERTIES_FILE = DD_GIT_PROPERTIES_FILE
1112+
process.env.DD_GIT_COMMIT_SHA = DUMMY_COMMIT_SHA
1113+
const config = new Config({})
1114+
expect(config).to.have.property('commitSHA', DUMMY_COMMIT_SHA)
1115+
expect(config).to.have.property('repositoryUrl', DUMMY_REPOSITORY_URL)
1116+
})
1117+
it('reads git.properties and filters out credentials', () => {
1118+
process.env.DD_GIT_PROPERTIES_FILE = require.resolve('./fixtures/config/git.properties.credentials')
1119+
const config = new Config({})
1120+
expect(config).to.have.property('commitSHA', '4e7da8069bcf5ffc8023603b95653e2dc99d1c7d')
1121+
expect(config).to.have.property('repositoryUrl', 'https://github.com/datadog/dd-trace-js')
10721122
})
10731123
})
10741124
})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
git.commit.sha=4e7da8069bcf5ffc8023603b95653e2dc99d1c7d
2+
git.repository_url=https://username:[email protected]/datadog/dd-trace-js
3+

packages/dd-trace/test/git_properties.spec.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ require('./setup/tap')
33
const { getGitMetadataFromGitProperties } = require('../src/git_properties')
44

55
describe('git_properties', () => {
6-
describe('getGitMetadataFromGitProperties', () => {
6+
context('getGitMetadataFromGitProperties', () => {
77
it('reads commit SHA and repository URL', () => {
88
const { commitSHA, repositoryUrl } = getGitMetadataFromGitProperties(`
99
git.commit.sha=4e7da8069bcf5ffc8023603b95653e2dc99d1c7d
@@ -12,5 +12,38 @@ [email protected]:DataDog/dd-trace-js.git
1212
expect(commitSHA).to.equal('4e7da8069bcf5ffc8023603b95653e2dc99d1c7d')
1313
expect(repositoryUrl).to.equal('[email protected]:DataDog/dd-trace-js.git')
1414
})
15+
it('filters out credentials', () => {
16+
const { commitSHA, repositoryUrl } = getGitMetadataFromGitProperties(`
17+
git.commit.sha=4e7da8069bcf5ffc8023603b95653e2dc99d1c7d
18+
git.repository_url=https://username:[email protected]/datadog/dd-trace-js.git
19+
`)
20+
expect(commitSHA).to.equal('4e7da8069bcf5ffc8023603b95653e2dc99d1c7d')
21+
expect(repositoryUrl).to.equal('https://github.com/datadog/dd-trace-js.git')
22+
})
23+
it('ignores other fields', () => {
24+
const { commitSHA, repositoryUrl } = getGitMetadataFromGitProperties(`
25+
git.commit.sha=4e7da8069bcf5ffc8023603b95653e2dc99d1c7d
26+
[email protected]:DataDog/dd-trace-js.git
27+
28+
`)
29+
expect(commitSHA).to.equal('4e7da8069bcf5ffc8023603b95653e2dc99d1c7d')
30+
expect(repositoryUrl).to.equal('[email protected]:DataDog/dd-trace-js.git')
31+
})
32+
it('ignores badly formatted files', () => {
33+
const { commitSHA, repositoryUrl } = getGitMetadataFromGitProperties(`
34+
git.commit.sha=; rm -rf ;
35+
git.repository_url=; rm -rf ;
36+
`)
37+
expect(commitSHA).to.equal(undefined)
38+
expect(repositoryUrl).to.equal(undefined)
39+
})
40+
it('does not crash with empty files', () => {
41+
const emptyStringResult = getGitMetadataFromGitProperties('')
42+
expect(emptyStringResult.commitSHA).to.equal(undefined)
43+
expect(emptyStringResult.repositoryUrl).to.equal(undefined)
44+
const undefinedResult = getGitMetadataFromGitProperties(undefined)
45+
expect(undefinedResult.commitSHA).to.equal(undefined)
46+
expect(undefinedResult.repositoryUrl).to.equal(undefined)
47+
})
1548
})
1649
})

0 commit comments

Comments
 (0)