From 3f544f900137df3dcb3fe828fcd56d7101bacab4 Mon Sep 17 00:00:00 2001 From: Zach Bloomquist Date: Wed, 21 Aug 2019 10:06:53 -0400 Subject: [PATCH] feat: getTimestamp, add timestamp --- README.md | 4 ++- __snapshots__/commit-info-spec.js | 43 +++++++++++++++++-------------- __snapshots__/git-api-spec.js | 13 +++++----- src/commit-info-spec.js | 3 +++ src/git-api-spec.js | 7 +++-- src/git-api.js | 4 +++ src/index.js | 3 +++ src/utils.js | 3 ++- 8 files changed, 51 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index b564a64..02b5bda 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ commitInfo(folder) // email // author // sha + // timestamp (in seconds since epoch) // remote }) ``` @@ -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 ``` @@ -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 diff --git a/__snapshots__/commit-info-spec.js b/__snapshots__/commit-info-spec.js index de592d1..84decac 100644 --- a/__snapshots__/commit-info-spec.js +++ b/__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", @@ -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", @@ -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" } diff --git a/__snapshots__/git-api-spec.js b/__snapshots__/git-api-spec.js index 37d91aa..6b18d30 100644 --- a/__snapshots__/git-api-spec.js +++ b/__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" } diff --git a/src/commit-info-spec.js b/src/commit-info-spec.js index 77c3755..75fd3dc 100644 --- a/src/commit-info-spec.js +++ b/src/commit-info-spec.js @@ -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, @@ -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) @@ -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, diff --git a/src/git-api-spec.js b/src/git-api-spec.js index c1e40b7..53c8ce0 100644 --- a/src/git-api-spec.js +++ b/src/git-api-spec.js @@ -85,7 +85,8 @@ describe('git-api', () => { getEmail, getAuthor, getSha, - getRemoteOrigin + getRemoteOrigin, + getTimestamp } = require('./git-api') it('works', () => { @@ -93,6 +94,7 @@ describe('git-api', () => { 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, @@ -105,7 +107,8 @@ describe('git-api', () => { email: getEmail(), author: getAuthor(), sha: getSha(), - remote: getRemoteOrigin() + remote: getRemoteOrigin(), + timestamp: getTimestamp() }).then(snapshot) }) }) diff --git a/src/git-api.js b/src/git-api.js index 850026e..c63c9b9 100644 --- a/src/git-api.js +++ b/src/git-api.js @@ -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' } @@ -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 = { @@ -86,6 +89,7 @@ module.exports = { getEmail, getAuthor, getSha, + getTimestamp, getRemoteOrigin, gitCommands } diff --git a/src/index.js b/src/index.js index a287f27..a04a802 100644 --- a/src/index.js +++ b/src/index.js @@ -8,6 +8,7 @@ const { getEmail, getAuthor, getSha, + getTimestamp, getRemoteOrigin } = require('./git-api') const { getBranch, getCommitInfoFromEnvironment } = require('./utils') @@ -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() @@ -42,5 +44,6 @@ module.exports = { getSha, getRemoteOrigin, getSubject, + getTimestamp, getBody } diff --git a/src/utils.js b/src/utils.js index 6e7ff6d..a387d52 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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) } } @@ -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 = {