Skip to content

Commit 9c7f7e4

Browse files
authored
feat: getTimestamp, add timestamp (#112)
feat: getTimestamp, add timestamp
2 parents 35dd063 + 3f544f9 commit 9c7f7e4

8 files changed

+51
-29
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ commitInfo(folder)
3030
// email
3131
// author
3232
// sha
33+
// timestamp (in seconds since epoch)
3334
// remote
3435
})
3536
```
@@ -52,6 +53,7 @@ message: COMMIT_INFO_MESSAGE
5253
email: COMMIT_INFO_EMAIL
5354
author: COMMIT_INFO_AUTHOR
5455
sha: COMMIT_INFO_SHA
56+
timestamp: COMMIT_INFO_TIMESTAMP
5557
remote: COMMIT_INFO_REMOTE
5658
```
5759

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

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

7678
For example
7779

__snapshots__/commit-info-spec.js

+24-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
exports['commit-info combination with environment variables has certain api 1'] = [
1+
exports['commit-info no environment variables has certain api 1'] = [
22
"commitInfo",
33
"getBranch",
44
"getMessage",
@@ -7,19 +7,31 @@ exports['commit-info combination with environment variables has certain api 1']
77
"getSha",
88
"getRemoteOrigin",
99
"getSubject",
10+
"getTimestamp",
1011
"getBody"
1112
]
1213

13-
exports['commit-info combination with environment variables returns information 1'] = {
14+
exports['commit-info no environment variables returns information 1'] = {
1415
"branch": "test-branch",
15-
"message": "some git message",
16-
"email": "user@company.com",
16+
"message": "important commit",
17+
"email": "me@foo.com",
1718
"author": "John Doe",
1819
"sha": "abc123",
19-
"remote": "[email protected]/repo"
20+
"remote": "[email protected]/repo",
21+
"timestamp": "123"
2022
}
2123

22-
exports['commit-info no environment variables has certain api 1'] = [
24+
exports['commit-info no environment variables returns nulls for missing fields 1'] = {
25+
"branch": "test-branch",
26+
"message": null,
27+
"email": "[email protected]",
28+
"author": null,
29+
"sha": "abc123",
30+
"remote": null,
31+
"timestamp": "123"
32+
}
33+
34+
exports['commit-info combination with environment variables has certain api 1'] = [
2335
"commitInfo",
2436
"getBranch",
2537
"getMessage",
@@ -28,23 +40,16 @@ exports['commit-info no environment variables has certain api 1'] = [
2840
"getSha",
2941
"getRemoteOrigin",
3042
"getSubject",
43+
"getTimestamp",
3144
"getBody"
3245
]
3346

34-
exports['commit-info no environment variables returns information 1'] = {
47+
exports['commit-info combination with environment variables returns information 1'] = {
3548
"branch": "test-branch",
36-
"message": "important commit",
37-
"email": "me@foo.com",
49+
"message": "some git message",
50+
"email": "user@company.com",
3851
"author": "John Doe",
3952
"sha": "abc123",
40-
"remote": "[email protected]/repo"
41-
}
42-
43-
exports['commit-info no environment variables returns nulls for missing fields 1'] = {
44-
"branch": "test-branch",
45-
"message": null,
46-
"email": "[email protected]",
47-
"author": null,
48-
"sha": "abc123",
49-
"remote": null
53+
"remote": "[email protected]/repo",
54+
"timestamp": "123"
5055
}

__snapshots__/git-api-spec.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
exports['git-api subject and body gets subject and body 1'] = {
2+
"subject": "commit does this",
3+
"body": "more details"
4+
}
5+
16
exports['git-api getting commit info works 1'] = {
27
"message": "important commit",
38
"email": "[email protected]",
49
"author": "John Doe",
510
"sha": "abc123",
6-
"remote": "[email protected]/repo"
7-
}
8-
9-
exports['git-api subject and body gets subject and body 1'] = {
10-
"subject": "commit does this",
11-
"body": "more details"
11+
"remote": "[email protected]/repo",
12+
"timestamp": "123"
1213
}

src/commit-info-spec.js

+3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ describe('commit-info', () => {
7373
stubSpawnShellOnce(gitCommands.email, 0, '[email protected]', '')
7474
stubSpawnShellOnce(gitCommands.author, 0, 'John Doe', '')
7575
stubSpawnShellOnce(gitCommands.sha, 0, 'abc123', '')
76+
stubSpawnShellOnce(gitCommands.timestamp, 0, '123', '')
7677
stubSpawnShellOnce(
7778
gitCommands.remoteOriginUrl,
7879
0,
@@ -89,6 +90,7 @@ describe('commit-info', () => {
8990
stubSpawnShellOnce(gitCommands.author, 1, '', 'missing author')
9091
stubSpawnShellOnce(gitCommands.sha, 0, 'abc123', '')
9192
stubSpawnShellOnce(gitCommands.remoteOriginUrl, 1, '', 'no remote origin')
93+
stubSpawnShellOnce(gitCommands.timestamp, 0, '123', '')
9294
return commitInfo()
9395
.tap(info => {
9496
la(info.message === null, 'message should be null', info)
@@ -137,6 +139,7 @@ describe('commit-info', () => {
137139
stubSpawnShellOnce(gitCommands.email, 1, '', 'could not get Git email')
138140
stubSpawnShellOnce(gitCommands.author, 0, 'John Doe', '')
139141
stubSpawnShellOnce(gitCommands.sha, 0, 'abc123', '')
142+
stubSpawnShellOnce(gitCommands.timestamp, 0, '123', '')
140143
stubSpawnShellOnce(
141144
gitCommands.remoteOriginUrl,
142145
0,

src/git-api-spec.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,16 @@ describe('git-api', () => {
8585
getEmail,
8686
getAuthor,
8787
getSha,
88-
getRemoteOrigin
88+
getRemoteOrigin,
89+
getTimestamp
8990
} = require('./git-api')
9091

9192
it('works', () => {
9293
stubSpawnShellOnce(gitCommands.message, 0, 'important commit', '')
9394
stubSpawnShellOnce(gitCommands.email, 0, '[email protected]', '')
9495
stubSpawnShellOnce(gitCommands.author, 0, 'John Doe', '')
9596
stubSpawnShellOnce(gitCommands.sha, 0, 'abc123', '')
97+
stubSpawnShellOnce(gitCommands.timestamp, 0, '123', '')
9698
stubSpawnShellOnce(
9799
gitCommands.remoteOriginUrl,
98100
0,
@@ -105,7 +107,8 @@ describe('git-api', () => {
105107
email: getEmail(),
106108
author: getAuthor(),
107109
sha: getSha(),
108-
remote: getRemoteOrigin()
110+
remote: getRemoteOrigin(),
111+
timestamp: getTimestamp()
109112
}).then(snapshot)
110113
})
111114
})

src/git-api.js

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const gitCommands = {
1414
email: 'git show -s --pretty=%ae',
1515
author: 'git show -s --pretty=%an',
1616
sha: 'git show -s --pretty=%H',
17+
timestamp: 'git show -s --pretty=%ct',
1718
remoteOriginUrl: 'git config --get remote.origin.url'
1819
}
1920

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

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

79+
const getTimestamp = runGitCommand.bind(null, gitCommands.timestamp)
80+
7881
const getRemoteOrigin = runGitCommand.bind(null, gitCommands.remoteOriginUrl)
7982

8083
module.exports = {
@@ -86,6 +89,7 @@ module.exports = {
8689
getEmail,
8790
getAuthor,
8891
getSha,
92+
getTimestamp,
8993
getRemoteOrigin,
9094
gitCommands
9195
}

src/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
getEmail,
99
getAuthor,
1010
getSha,
11+
getTimestamp,
1112
getRemoteOrigin
1213
} = require('./git-api')
1314
const { getBranch, getCommitInfoFromEnvironment } = require('./utils')
@@ -24,6 +25,7 @@ function commitInfo (folder) {
2425
email: getEmail(folder),
2526
author: getAuthor(folder),
2627
sha: getSha(folder),
28+
timestamp: getTimestamp(folder),
2729
remote: getRemoteOrigin(folder)
2830
}).then(info => {
2931
const envVariables = getCommitInfoFromEnvironment()
@@ -42,5 +44,6 @@ module.exports = {
4244
getSha,
4345
getRemoteOrigin,
4446
getSubject,
47+
getTimestamp,
4548
getBody
4649
}

src/utils.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ function getCommitInfoFromEnvironment (env = process.env) {
3737
email: getValue('COMMIT_INFO_EMAIL')(env),
3838
author: getValue('COMMIT_INFO_AUTHOR')(env),
3939
sha: getValue('COMMIT_INFO_SHA')(env),
40+
timestamp: getValue('COMMIT_INFO_TIMESTAMP')(env),
4041
remote: getValue('COMMIT_INFO_REMOTE')(env)
4142
}
4243
}
@@ -45,7 +46,7 @@ function getCommitInfoFromEnvironment (env = process.env) {
4546
* Returns list of Git properties that this module searches for
4647
*/
4748
function getFields () {
48-
return ['branch', 'message', 'email', 'author', 'sha', 'remote']
49+
return ['branch', 'message', 'email', 'author', 'sha', 'remote', 'timestamp']
4950
}
5051

5152
module.exports = {

0 commit comments

Comments
 (0)