Skip to content

Commit 8fc437e

Browse files
[ci-app][CIAPP-936] Add More Git Metadata to Tests (#1254)
1 parent 9baa26d commit 8fc437e

File tree

7 files changed

+516
-21
lines changed

7 files changed

+516
-21
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ node_modules
66
protobuf
77
versions
88
acmeair-nodejs
9+
vendor

LICENSE-3rdparty.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,4 @@ dev,tape,MIT,Copyright James Halliday
6060
dev,wait-on,MIT,Copyright 2015 Jeff Barczewski
6161
file,profile.proto,Apache license 2.0,Copyright 2016 Google Inc.
6262
file,TDigest.h,Apache license 2.0,Copyright Derrick R. Burns
63+
file,git-repo-info.js,MIT,Copyright Robert Jackson and Contributors

packages/dd-trace/src/plugins/util/ci-app-spec.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"test.framework",
55
"test.suite",
66
"test.name",
7-
"resource.name",
87
"ci.pipeline.id",
98
"ci.pipeline.name",
109
"ci.pipeline.number",
@@ -18,6 +17,13 @@
1817
"git.commit.sha",
1918
"git.branch",
2019
"git.tag",
20+
"git.commit.message",
21+
"git.commit.committer.date",
22+
"git.commit.committer.email",
23+
"git.commit.committer.name",
24+
"git.commit.author.date",
25+
"git.commit.author.email",
26+
"git.commit.author.name",
2127
"os.platform",
2228
"os.version",
2329
"os.architecture",
Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,80 @@
1+
const coalesce = require('koalas')
2+
const getRepoInfo = require('../../../../../vendor/git-repo-info')
3+
14
const { sanitizedExec } = require('./exec')
25

36
const GIT_COMMIT_SHA = 'git.commit.sha'
47
const GIT_BRANCH = 'git.branch'
58
const GIT_REPOSITORY_URL = 'git.repository_url'
69
const GIT_TAG = 'git.tag'
10+
const GIT_COMMIT_MESSAGE = 'git.commit.message'
11+
const GIT_COMMIT_COMMITTER_DATE = 'git.commit.committer.date'
12+
const GIT_COMMIT_COMMITTER_EMAIL = 'git.commit.committer.email'
13+
const GIT_COMMIT_COMMITTER_NAME = 'git.commit.committer.name'
14+
const GIT_COMMIT_AUTHOR_DATE = 'git.commit.author.date'
15+
const GIT_COMMIT_AUTHOR_EMAIL = 'git.commit.author.email'
16+
const GIT_COMMIT_AUTHOR_NAME = 'git.commit.author.name'
17+
18+
// Receives a string with the form 'John Doe <[email protected]>'
19+
// and returns { name: 'John Doe', email: 'john.doe@gmail.com' }
20+
function parseUser (user) {
21+
if (!user) {
22+
return { name: '', email: '' }
23+
}
24+
let email = ''
25+
const matchEmail = user.match(/[^@<\s]+@[^@\s>]+/g)
26+
if (matchEmail) {
27+
email = matchEmail[0]
28+
}
29+
return { name: user.replace(`<${email}>`, '').trim(), email }
30+
}
731

832
// If there is ciMetadata, it takes precedence.
933
function getGitMetadata (ciMetadata) {
10-
const { commitSHA, branch, repositoryUrl } = ciMetadata
11-
// With stdio: 'pipe', errors in this command will not be output to the parent process,
12-
// so if `git` is not present in the env, we'll just fallback to the default
13-
// and not show a warning to the user.
14-
const execOptions = { stdio: 'pipe' }
34+
const { commitSHA, branch, repositoryUrl, tag } = ciMetadata
35+
36+
const {
37+
author,
38+
committer,
39+
authorDate,
40+
committerDate,
41+
commitMessage,
42+
branch: gitBranch,
43+
tag: gitTag,
44+
sha: gitCommitSHA
45+
} = getRepoInfo(process.cwd())
46+
47+
const { name: authorName, email: authorEmail } = parseUser(author)
48+
const { name: committerName, email: committerEmail } = parseUser(committer)
49+
1550
return {
16-
[GIT_REPOSITORY_URL]: repositoryUrl || sanitizedExec('git ls-remote --get-url', execOptions),
17-
[GIT_BRANCH]: branch || sanitizedExec('git rev-parse --abbrev-ref HEAD', execOptions),
18-
[GIT_COMMIT_SHA]: commitSHA || sanitizedExec('git rev-parse HEAD', execOptions)
51+
// With stdio: 'pipe', errors in this command will not be output to the parent process,
52+
// so if `git` is not present in the env, we won't show a warning to the user.
53+
[GIT_REPOSITORY_URL]: repositoryUrl || sanitizedExec('git ls-remote --get-url', { stdio: 'pipe' }),
54+
[GIT_BRANCH]: coalesce(branch, gitBranch),
55+
[GIT_COMMIT_SHA]: coalesce(commitSHA, gitCommitSHA),
56+
[GIT_TAG]: coalesce(tag, gitTag),
57+
[GIT_COMMIT_MESSAGE]: commitMessage,
58+
[GIT_COMMIT_AUTHOR_DATE]: authorDate,
59+
[GIT_COMMIT_AUTHOR_NAME]: authorName,
60+
[GIT_COMMIT_AUTHOR_EMAIL]: authorEmail,
61+
[GIT_COMMIT_COMMITTER_DATE]: committerDate,
62+
[GIT_COMMIT_COMMITTER_NAME]: committerName,
63+
[GIT_COMMIT_COMMITTER_EMAIL]: committerEmail
1964
}
2065
}
2166

22-
module.exports = { getGitMetadata, GIT_COMMIT_SHA, GIT_BRANCH, GIT_REPOSITORY_URL, GIT_TAG }
67+
module.exports = {
68+
getGitMetadata,
69+
GIT_COMMIT_SHA,
70+
GIT_BRANCH,
71+
GIT_REPOSITORY_URL,
72+
GIT_TAG,
73+
GIT_COMMIT_MESSAGE,
74+
GIT_COMMIT_COMMITTER_DATE,
75+
GIT_COMMIT_COMMITTER_EMAIL,
76+
GIT_COMMIT_COMMITTER_NAME,
77+
GIT_COMMIT_AUTHOR_DATE,
78+
GIT_COMMIT_AUTHOR_EMAIL,
79+
GIT_COMMIT_AUTHOR_NAME
80+
}

packages/dd-trace/src/plugins/util/test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { getGitMetadata, GIT_BRANCH, GIT_COMMIT_SHA, GIT_REPOSITORY_URL } = require('./git')
1+
const { getGitMetadata, GIT_BRANCH, GIT_COMMIT_SHA, GIT_REPOSITORY_URL, GIT_TAG } = require('./git')
22
const { getCIMetadata } = require('./ci')
33
const { getRuntimeAndOSMetadata } = require('./env')
44

@@ -23,10 +23,11 @@ function getTestEnvironmentMetadata (testFramework) {
2323
const {
2424
[GIT_COMMIT_SHA]: commitSHA,
2525
[GIT_BRANCH]: branch,
26-
[GIT_REPOSITORY_URL]: repositoryUrl
26+
[GIT_REPOSITORY_URL]: repositoryUrl,
27+
[GIT_TAG]: tag
2728
} = ciMetadata
2829

29-
const gitMetadata = getGitMetadata({ commitSHA, branch, repositoryUrl })
30+
const gitMetadata = getGitMetadata({ commitSHA, branch, repositoryUrl, tag })
3031

3132
const runtimeAndOSMetadata = getRuntimeAndOSMetadata()
3233

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,111 @@
11
const proxyquire = require('proxyquire')
22

33
const sanitizedExecStub = sinon.stub()
4+
const gitRepoInfoStub = sinon.stub().returns({
5+
author: 'author <[email protected]>',
6+
committer: 'committer <[email protected]>',
7+
authorDate: '1970',
8+
committerDate: '1971',
9+
commitMessage: 'commit message',
10+
branch: 'gitBranch',
11+
tag: 'gitTag',
12+
sha: 'gitSha'
13+
})
14+
415
const {
516
getGitMetadata,
617
GIT_COMMIT_SHA,
718
GIT_BRANCH,
8-
GIT_REPOSITORY_URL
9-
} = proxyquire('../../../src/plugins/util/git', { './exec': {
10-
'sanitizedExec': sanitizedExecStub
11-
} })
19+
GIT_TAG,
20+
GIT_REPOSITORY_URL,
21+
GIT_COMMIT_MESSAGE,
22+
GIT_COMMIT_COMMITTER_DATE,
23+
GIT_COMMIT_COMMITTER_EMAIL,
24+
GIT_COMMIT_COMMITTER_NAME,
25+
GIT_COMMIT_AUTHOR_DATE,
26+
GIT_COMMIT_AUTHOR_EMAIL,
27+
GIT_COMMIT_AUTHOR_NAME
28+
} = proxyquire('../../../src/plugins/util/git',
29+
{
30+
'./exec': {
31+
'sanitizedExec': sanitizedExecStub
32+
},
33+
'../../../../../vendor/git-repo-info': gitRepoInfoStub
34+
}
35+
)
1236

1337
describe('git', () => {
1438
afterEach(() => {
1539
sanitizedExecStub.reset()
1640
})
41+
const commonGitMetadata = {
42+
[GIT_COMMIT_MESSAGE]: 'commit message',
43+
[GIT_COMMIT_COMMITTER_DATE]: '1971',
44+
[GIT_COMMIT_COMMITTER_EMAIL]: '[email protected]',
45+
[GIT_COMMIT_COMMITTER_NAME]: 'committer',
46+
[GIT_COMMIT_AUTHOR_DATE]: '1970',
47+
[GIT_COMMIT_AUTHOR_EMAIL]: '[email protected]',
48+
[GIT_COMMIT_AUTHOR_NAME]: 'author',
49+
[GIT_TAG]: 'gitTag',
50+
[GIT_BRANCH]: 'gitBranch'
51+
}
1752
it('calls git when some ci metadata is not present', () => {
18-
const ciMetadata = { commitSHA: 'ciSHA', branch: 'ciBranch' }
53+
const ciMetadata = { commitSHA: 'ciSHA' }
1954
const metadata = getGitMetadata(ciMetadata)
2055

2156
expect(metadata).to.include(
2257
{
2358
[GIT_COMMIT_SHA]: 'ciSHA',
24-
[GIT_BRANCH]: 'ciBranch'
59+
...commonGitMetadata
2560
}
2661
)
2762
expect(metadata[GIT_REPOSITORY_URL]).not.to.equal('ciRepositoryUrl')
2863
expect(sanitizedExecStub).to.have.been.calledWith('git ls-remote --get-url', { stdio: 'pipe' })
64+
expect(gitRepoInfoStub).to.have.been.called
2965
})
3066
it('returns ci metadata if present and does not call git', () => {
31-
const ciMetadata = { commitSHA: 'ciSHA', branch: 'ciBranch', repositoryUrl: 'ciRepositoryUrl' }
67+
const ciMetadata = { commitSHA: 'ciSHA', branch: 'ciBranch', repositoryUrl: 'ciRepositoryUrl', tag: 'tag' }
3268
const metadata = getGitMetadata(ciMetadata)
3369

3470
expect(metadata).to.eql(
35-
{ [GIT_COMMIT_SHA]: 'ciSHA', [GIT_BRANCH]: 'ciBranch', [GIT_REPOSITORY_URL]: 'ciRepositoryUrl' }
71+
{
72+
...commonGitMetadata,
73+
[GIT_COMMIT_SHA]: 'ciSHA',
74+
[GIT_BRANCH]: 'ciBranch',
75+
[GIT_REPOSITORY_URL]: 'ciRepositoryUrl',
76+
[GIT_TAG]: 'tag'
77+
}
3678
)
3779
expect(sanitizedExecStub).not.to.have.been.called
3880
})
81+
it('does not crash with empty or badly shapen author or committer', () => {
82+
gitRepoInfoStub.returns({
83+
author: 'author <>',
84+
committer: undefined,
85+
authorDate: '1970',
86+
committerDate: '1971',
87+
commitMessage: 'commit message',
88+
branch: 'gitBranch',
89+
tag: 'gitTag',
90+
sha: 'gitSha'
91+
})
92+
const ciMetadata = { repositoryUrl: 'ciRepositoryUrl' }
93+
const metadata = getGitMetadata(ciMetadata)
94+
95+
expect(metadata).to.eql(
96+
{
97+
[GIT_COMMIT_MESSAGE]: 'commit message',
98+
[GIT_COMMIT_COMMITTER_DATE]: '1971',
99+
[GIT_COMMIT_COMMITTER_EMAIL]: '',
100+
[GIT_COMMIT_COMMITTER_NAME]: '',
101+
[GIT_COMMIT_AUTHOR_DATE]: '1970',
102+
[GIT_COMMIT_AUTHOR_EMAIL]: '',
103+
[GIT_COMMIT_AUTHOR_NAME]: 'author',
104+
[GIT_TAG]: 'gitTag',
105+
[GIT_BRANCH]: 'gitBranch',
106+
[GIT_COMMIT_SHA]: 'gitSha',
107+
[GIT_REPOSITORY_URL]: 'ciRepositoryUrl'
108+
}
109+
)
110+
})
39111
})

0 commit comments

Comments
 (0)