dd-trace
Version:
Datadog APM tracing client for JavaScript
61 lines (55 loc) • 1.91 kB
JavaScript
const { sanitizedExec } = require('./exec')
const {
GIT_COMMIT_SHA,
GIT_BRANCH,
GIT_REPOSITORY_URL,
GIT_TAG,
GIT_COMMIT_MESSAGE,
GIT_COMMIT_COMMITTER_DATE,
GIT_COMMIT_COMMITTER_EMAIL,
GIT_COMMIT_COMMITTER_NAME,
GIT_COMMIT_AUTHOR_DATE,
GIT_COMMIT_AUTHOR_EMAIL,
GIT_COMMIT_AUTHOR_NAME,
CI_WORKSPACE_PATH
} = require('./tags')
// If there is ciMetadata, it takes precedence.
function getGitMetadata (ciMetadata) {
const {
commitSHA,
branch,
repositoryUrl,
tag,
commitMessage,
authorName: ciAuthorName,
authorEmail: ciAuthorEmail,
ciWorkspacePath
} = ciMetadata
// With stdio: 'pipe', errors in this command will not be output to the parent process,
// so if `git` is not present in the env, we won't show a warning to the user.
const [
authorName,
authorEmail,
authorDate,
committerName,
committerEmail,
committerDate
] = sanitizedExec('git show -s --format=%an,%ae,%ad,%cn,%ce,%cd', { stdio: 'pipe' }).split(',')
return {
[]:
repositoryUrl || sanitizedExec('git ls-remote --get-url', { stdio: 'pipe' }),
[]:
commitMessage || sanitizedExec('git show -s --format=%s', { stdio: 'pipe' }),
[]: authorDate,
[]: ciAuthorName || authorName,
[]: ciAuthorEmail || authorEmail,
[]: committerDate,
[]: committerName,
[]: committerEmail,
[]: branch || sanitizedExec('git rev-parse --abbrev-ref HEAD', { stdio: 'pipe' }),
[]: commitSHA || sanitizedExec('git rev-parse HEAD', { stdio: 'pipe' }),
[]: tag,
[]: ciWorkspacePath || sanitizedExec('git rev-parse --show-toplevel', { stdio: 'pipe' })
}
}
module.exports = { getGitMetadata }