Skip to content

Commit

Permalink
feat: getTimestamp, add timestamp (#112)
Browse files Browse the repository at this point in the history
feat: getTimestamp, add timestamp
  • Loading branch information
flotwig committed Aug 21, 2019
2 parents 35dd063 + 3f544f9 commit 9c7f7e4
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 29 deletions.
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -30,6 +30,7 @@ commitInfo(folder)
// email
// author
// sha
// timestamp (in seconds since epoch)
// remote
})
```
Expand All @@ -52,6 +53,7 @@ message: COMMIT_INFO_MESSAGE
email: COMMIT_INFO_EMAIL
author: COMMIT_INFO_AUTHOR
sha: COMMIT_INFO_SHA
timestamp: COMMIT_INFO_TIMESTAMP
remote: COMMIT_INFO_REMOTE
```

Expand All @@ -71,7 +73,7 @@ See [docker-example](docker-example) for a full example.
## Individual methods

In addition to `commitInfo` this module also exposes individual promise-returning
methods `getBranch`, `getMessage`, `getEmail`, `getAuthor`, `getSha`, `getRemoteOrigin`. These methods do NOT use fallback environment variables.
methods `getBranch`, `getMessage`, `getEmail`, `getAuthor`, `getSha`, `getTimestamp`, `getRemoteOrigin`. These methods do NOT use fallback environment variables.

For example

Expand Down
43 changes: 24 additions & 19 deletions __snapshots__/commit-info-spec.js
@@ -1,4 +1,4 @@
exports['commit-info combination with environment variables has certain api 1'] = [
exports['commit-info no environment variables has certain api 1'] = [
"commitInfo",
"getBranch",
"getMessage",
Expand All @@ -7,19 +7,31 @@ exports['commit-info combination with environment variables has certain api 1']
"getSha",
"getRemoteOrigin",
"getSubject",
"getTimestamp",
"getBody"
]

exports['commit-info combination with environment variables returns information 1'] = {
exports['commit-info no environment variables returns information 1'] = {
"branch": "test-branch",
"message": "some git message",
"email": "user@company.com",
"message": "important commit",
"email": "me@foo.com",
"author": "John Doe",
"sha": "abc123",
"remote": "git@github.com/repo"
"remote": "git@github.com/repo",
"timestamp": "123"
}

exports['commit-info no environment variables has certain api 1'] = [
exports['commit-info no environment variables returns nulls for missing fields 1'] = {
"branch": "test-branch",
"message": null,
"email": "me@foo.com",
"author": null,
"sha": "abc123",
"remote": null,
"timestamp": "123"
}

exports['commit-info combination with environment variables has certain api 1'] = [
"commitInfo",
"getBranch",
"getMessage",
Expand All @@ -28,23 +40,16 @@ exports['commit-info no environment variables has certain api 1'] = [
"getSha",
"getRemoteOrigin",
"getSubject",
"getTimestamp",
"getBody"
]

exports['commit-info no environment variables returns information 1'] = {
exports['commit-info combination with environment variables returns information 1'] = {
"branch": "test-branch",
"message": "important commit",
"email": "me@foo.com",
"message": "some git message",
"email": "user@company.com",
"author": "John Doe",
"sha": "abc123",
"remote": "git@github.com/repo"
}

exports['commit-info no environment variables returns nulls for missing fields 1'] = {
"branch": "test-branch",
"message": null,
"email": "me@foo.com",
"author": null,
"sha": "abc123",
"remote": null
"remote": "git@github.com/repo",
"timestamp": "123"
}
13 changes: 7 additions & 6 deletions __snapshots__/git-api-spec.js
@@ -1,12 +1,13 @@
exports['git-api subject and body gets subject and body 1'] = {
"subject": "commit does this",
"body": "more details"
}

exports['git-api getting commit info works 1'] = {
"message": "important commit",
"email": "me@foo.com",
"author": "John Doe",
"sha": "abc123",
"remote": "git@github.com/repo"
}

exports['git-api subject and body gets subject and body 1'] = {
"subject": "commit does this",
"body": "more details"
"remote": "git@github.com/repo",
"timestamp": "123"
}
3 changes: 3 additions & 0 deletions src/commit-info-spec.js
Expand Up @@ -73,6 +73,7 @@ describe('commit-info', () => {
stubSpawnShellOnce(gitCommands.email, 0, 'me@foo.com', '')
stubSpawnShellOnce(gitCommands.author, 0, 'John Doe', '')
stubSpawnShellOnce(gitCommands.sha, 0, 'abc123', '')
stubSpawnShellOnce(gitCommands.timestamp, 0, '123', '')
stubSpawnShellOnce(
gitCommands.remoteOriginUrl,
0,
Expand All @@ -89,6 +90,7 @@ describe('commit-info', () => {
stubSpawnShellOnce(gitCommands.author, 1, '', 'missing author')
stubSpawnShellOnce(gitCommands.sha, 0, 'abc123', '')
stubSpawnShellOnce(gitCommands.remoteOriginUrl, 1, '', 'no remote origin')
stubSpawnShellOnce(gitCommands.timestamp, 0, '123', '')
return commitInfo()
.tap(info => {
la(info.message === null, 'message should be null', info)
Expand Down Expand Up @@ -137,6 +139,7 @@ describe('commit-info', () => {
stubSpawnShellOnce(gitCommands.email, 1, '', 'could not get Git email')
stubSpawnShellOnce(gitCommands.author, 0, 'John Doe', '')
stubSpawnShellOnce(gitCommands.sha, 0, 'abc123', '')
stubSpawnShellOnce(gitCommands.timestamp, 0, '123', '')
stubSpawnShellOnce(
gitCommands.remoteOriginUrl,
0,
Expand Down
7 changes: 5 additions & 2 deletions src/git-api-spec.js
Expand Up @@ -85,14 +85,16 @@ describe('git-api', () => {
getEmail,
getAuthor,
getSha,
getRemoteOrigin
getRemoteOrigin,
getTimestamp
} = require('./git-api')

it('works', () => {
stubSpawnShellOnce(gitCommands.message, 0, 'important commit', '')
stubSpawnShellOnce(gitCommands.email, 0, 'me@foo.com', '')
stubSpawnShellOnce(gitCommands.author, 0, 'John Doe', '')
stubSpawnShellOnce(gitCommands.sha, 0, 'abc123', '')
stubSpawnShellOnce(gitCommands.timestamp, 0, '123', '')
stubSpawnShellOnce(
gitCommands.remoteOriginUrl,
0,
Expand All @@ -105,7 +107,8 @@ describe('git-api', () => {
email: getEmail(),
author: getAuthor(),
sha: getSha(),
remote: getRemoteOrigin()
remote: getRemoteOrigin(),
timestamp: getTimestamp()
}).then(snapshot)
})
})
Expand Down
4 changes: 4 additions & 0 deletions src/git-api.js
Expand Up @@ -14,6 +14,7 @@ const gitCommands = {
email: 'git show -s --pretty=%ae',
author: 'git show -s --pretty=%an',
sha: 'git show -s --pretty=%H',
timestamp: 'git show -s --pretty=%ct',
remoteOriginUrl: 'git config --get remote.origin.url'
}

Expand Down Expand Up @@ -75,6 +76,8 @@ const getAuthor = runGitCommand.bind(null, gitCommands.author)

const getSha = runGitCommand.bind(null, gitCommands.sha)

const getTimestamp = runGitCommand.bind(null, gitCommands.timestamp)

const getRemoteOrigin = runGitCommand.bind(null, gitCommands.remoteOriginUrl)

module.exports = {
Expand All @@ -86,6 +89,7 @@ module.exports = {
getEmail,
getAuthor,
getSha,
getTimestamp,
getRemoteOrigin,
gitCommands
}
3 changes: 3 additions & 0 deletions src/index.js
Expand Up @@ -8,6 +8,7 @@ const {
getEmail,
getAuthor,
getSha,
getTimestamp,
getRemoteOrigin
} = require('./git-api')
const { getBranch, getCommitInfoFromEnvironment } = require('./utils')
Expand All @@ -24,6 +25,7 @@ function commitInfo (folder) {
email: getEmail(folder),
author: getAuthor(folder),
sha: getSha(folder),
timestamp: getTimestamp(folder),
remote: getRemoteOrigin(folder)
}).then(info => {
const envVariables = getCommitInfoFromEnvironment()
Expand All @@ -42,5 +44,6 @@ module.exports = {
getSha,
getRemoteOrigin,
getSubject,
getTimestamp,
getBody
}
3 changes: 2 additions & 1 deletion src/utils.js
Expand Up @@ -37,6 +37,7 @@ function getCommitInfoFromEnvironment (env = process.env) {
email: getValue('COMMIT_INFO_EMAIL')(env),
author: getValue('COMMIT_INFO_AUTHOR')(env),
sha: getValue('COMMIT_INFO_SHA')(env),
timestamp: getValue('COMMIT_INFO_TIMESTAMP')(env),
remote: getValue('COMMIT_INFO_REMOTE')(env)
}
}
Expand All @@ -45,7 +46,7 @@ function getCommitInfoFromEnvironment (env = process.env) {
* Returns list of Git properties that this module searches for
*/
function getFields () {
return ['branch', 'message', 'email', 'author', 'sha', 'remote']
return ['branch', 'message', 'email', 'author', 'sha', 'remote', 'timestamp']
}

module.exports = {
Expand Down

0 comments on commit 9c7f7e4

Please sign in to comment.