Skip to content

Commit 424ca92

Browse files
committed
feat: returns commit information
1 parent 1df7285 commit 424ca92

File tree

8 files changed

+168
-16
lines changed

8 files changed

+168
-16
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,21 @@ const {commitInfo} = require('@cypress/commit-info')
2424
commitInfo(folder)
2525
.then(info => {
2626
// info object will have properties
27-
// sha
2827
// branch
28+
// message
2929
// email
3030
// author
31+
// sha
3132
// remote
32-
// subject
33-
// body
3433
})
3534
```
3635

37-
Resolves with [Bluebird](https://github.com/petkaantonov/bluebird) promise.
36+
Notes:
37+
38+
- Resolves with [Bluebird](https://github.com/petkaantonov/bluebird) promise.
39+
- Tries to read branch from CI variables first, otherwise uses Git command.
40+
- If a command fails, returns empty string for each property
41+
- If you need to debug, run with `DEBUG=commit-info` environment variable.
3842

3943
### Small print
4044

__snapshots__/commit-info-spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
exports['commit-info returns information 1'] = {
2+
"branch": "test-branch",
3+
"message": "important commit",
4+
"email": "[email protected]",
5+
"author": "John Doe",
6+
"sha": "abc123",
7+
"remote": "[email protected]/repo"
8+
}
9+
10+
exports['commit-info returns empty strings for missing info 1'] = {
11+
"branch": "test-branch",
12+
"message": "",
13+
"email": "[email protected]",
14+
"author": "",
15+
"sha": "abc123",
16+
"remote": ""
17+
}

__snapshots__/utils-spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
exports['utils getting commit info works 1'] = {
2+
"message": "important commit",
3+
"email": "[email protected]",
4+
"author": "John Doe",
5+
"sha": "abc123",
6+
"remote": "[email protected]/repo"
7+
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"size": "t=\"$(npm pack .)\"; wc -c \"${t}\"; tar tvf \"${t}\"; rm \"${t}\";",
6262
"test": "npm run unit",
6363
"unit": "mocha src/*-spec.js",
64-
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
64+
"semantic-release": "semantic-release pre && npm publish --access public && semantic-release post"
6565
},
6666
"release": {
6767
"analyzeCommits": "simple-commit-message",
@@ -86,10 +86,12 @@
8686
"ramda": "0.25.0",
8787
"semantic-release": "8.2.0",
8888
"simple-commit-message": "3.3.2",
89+
"snap-shot-it": "4.0.1",
8990
"standard": "10.0.3",
9091
"stub-spawn-once": "2.3.0"
9192
},
9293
"dependencies": {
94+
"bluebird": "3.5.1",
9395
"chdir-promise": "0.6.2",
9496
"check-more-types": "2.24.0",
9597
"debug": "3.1.0",

src/commit-info-spec.js

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,44 @@
11
'use strict'
22

33
/* eslint-env mocha */
4-
const commitInfo = require('.')
4+
const { commitInfo } = require('.')
5+
const { stubSpawnShellOnce } = require('stub-spawn-once')
6+
const snapshot = require('snap-shot-it')
7+
const { gitCommands } = require('./utils')
58

69
describe('commit-info', () => {
7-
it('write this test', () => {
8-
console.assert(commitInfo, 'should export something')
10+
const env = process.env
11+
12+
beforeEach(() => {
13+
process.env = {}
14+
})
15+
16+
afterEach(() => {
17+
process.env = env
18+
})
19+
20+
it('returns information', () => {
21+
stubSpawnShellOnce(gitCommands.branch, 0, 'test-branch', '')
22+
stubSpawnShellOnce(gitCommands.message, 0, 'important commit', '')
23+
stubSpawnShellOnce(gitCommands.email, 0, '[email protected]', '')
24+
stubSpawnShellOnce(gitCommands.author, 0, 'John Doe', '')
25+
stubSpawnShellOnce(gitCommands.sha, 0, 'abc123', '')
26+
stubSpawnShellOnce(
27+
gitCommands.remoteOriginUrl,
28+
0,
29+
30+
''
31+
)
32+
return commitInfo().then(snapshot)
33+
})
34+
35+
it('returns empty strings for missing info', () => {
36+
stubSpawnShellOnce(gitCommands.branch, 0, 'test-branch', '')
37+
stubSpawnShellOnce(gitCommands.message, 1, '', 'no message')
38+
stubSpawnShellOnce(gitCommands.email, 0, '[email protected]', '')
39+
stubSpawnShellOnce(gitCommands.author, 1, '', 'missing author')
40+
stubSpawnShellOnce(gitCommands.sha, 0, 'abc123', '')
41+
stubSpawnShellOnce(gitCommands.remoteOriginUrl, 1, '', 'no remote origin')
42+
return commitInfo().then(snapshot)
943
})
1044
})

src/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
11
'use strict'
22

3+
const debug = require('debug')('commit-info')
4+
const {
5+
getBranch,
6+
getMessage,
7+
getEmail,
8+
getAuthor,
9+
getSha,
10+
getRemoteOrigin
11+
} = require('./utils')
12+
13+
const Promise = require('bluebird')
14+
315
function commitInfo (folder) {
416
folder = folder || process.cwd()
17+
debug('commit-info in folder', folder)
18+
19+
return Promise.props({
20+
branch: getBranch(folder),
21+
message: getMessage(folder),
22+
email: getEmail(folder),
23+
author: getAuthor(folder),
24+
sha: getSha(folder),
25+
remote: getRemoteOrigin(folder)
26+
})
527
}
628

729
module.exports = { commitInfo }

src/utils-spec.js

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,44 @@
33
const la = require('lazy-ass')
44
const R = require('ramda')
55
const { stubSpawnShellOnce } = require('stub-spawn-once')
6+
const Promise = require('bluebird')
7+
const snapshot = require('snap-shot-it')
68

79
/* eslint-env mocha */
810
describe('utils', () => {
11+
const { gitCommands } = require('./utils')
12+
13+
describe('getting commit info', () => {
14+
const {
15+
getMessage,
16+
getEmail,
17+
getAuthor,
18+
getSha,
19+
getRemoteOrigin
20+
} = require('./utils')
21+
22+
it('works', () => {
23+
stubSpawnShellOnce(gitCommands.message, 0, 'important commit', '')
24+
stubSpawnShellOnce(gitCommands.email, 0, '[email protected]', '')
25+
stubSpawnShellOnce(gitCommands.author, 0, 'John Doe', '')
26+
stubSpawnShellOnce(gitCommands.sha, 0, 'abc123', '')
27+
stubSpawnShellOnce(
28+
gitCommands.remoteOriginUrl,
29+
0,
30+
31+
''
32+
)
33+
34+
return Promise.props({
35+
message: getMessage(),
36+
email: getEmail(),
37+
author: getAuthor(),
38+
sha: getSha(),
39+
remote: getRemoteOrigin()
40+
}).then(snapshot)
41+
})
42+
})
43+
944
describe('getBranch', () => {
1045
const { getBranch } = require('./utils')
1146

@@ -43,24 +78,21 @@ describe('utils', () => {
4378
})
4479

4580
it('uses git to determine branch', () => {
46-
const cmd = 'git rev-parse --abbrev-ref HEAD'
47-
stubSpawnShellOnce(cmd, 0, 'mock-test-branch', '')
81+
stubSpawnShellOnce(gitCommands.branch, 0, 'mock-test-branch', '')
4882
return getBranch().then(branch =>
4983
la(branch === 'mock-test-branch', 'wrong branch from git', branch)
5084
)
5185
})
5286

5387
it('returns empty string on failure', () => {
54-
const cmd = 'git rev-parse --abbrev-ref HEAD'
55-
stubSpawnShellOnce(cmd, 1, '', 'nope')
88+
stubSpawnShellOnce(gitCommands.branch, 1, '', 'nope')
5689
return getBranch().then(branch =>
5790
la(branch === '', 'wrong empty branch from git', branch)
5891
)
5992
})
6093

6194
it('returns empty string on HEAD', () => {
62-
const cmd = 'git rev-parse --abbrev-ref HEAD'
63-
stubSpawnShellOnce(cmd, 0, 'HEAD', '')
95+
stubSpawnShellOnce(gitCommands.branch, 0, 'HEAD', '')
6496
return getBranch().then(branch =>
6597
la(branch === '', 'wrong HEAD branch from git', branch)
6698
)

src/utils.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,21 @@ const la = require('lazy-ass')
55
const is = require('check-more-types')
66
const Promise = require('bluebird')
77

8+
// common git commands for getting basic info
9+
const gitCommands = {
10+
branch: 'git rev-parse --abbrev-ref HEAD',
11+
message: 'git show -s --pretty=%B',
12+
email: 'git show -s --pretty=%ae',
13+
author: 'git show -s --pretty=%an',
14+
sha: 'git show -s --pretty=%H',
15+
remoteOriginUrl: 'git config --get remote.origin.url'
16+
}
17+
818
const prop = name => object => object[name]
919
const emptyString = () => ''
1020

1121
const runGitCommand = (pathToRepo, gitCommand) => {
22+
pathToRepo = pathToRepo || process.cwd()
1223
la(is.unemptyString(pathToRepo), 'missing repo path', pathToRepo)
1324
la(is.unemptyString(gitCommand), 'missing git command', gitCommand)
1425
la(gitCommand.startsWith('git'), 'invalid git command', gitCommand)
@@ -35,7 +46,7 @@ const runGitCommand = (pathToRepo, gitCommand) => {
3546
const checkIfDetached = branch => (branch === 'HEAD' ? '' : branch)
3647

3748
function getGitBranch (pathToRepo) {
38-
return runGitCommand(pathToRepo, 'git rev-parse --abbrev-ref HEAD')
49+
return runGitCommand(pathToRepo, gitCommands.branch)
3950
.then(checkIfDetached)
4051
.catch(emptyString)
4152
}
@@ -47,6 +58,8 @@ function firstFoundValue (keys, object = process.env) {
4758
return found ? object[found] : null
4859
}
4960

61+
// first try finding branch from CI environment variables
62+
// if fails, use "git" command
5063
function getBranch (pathToRepo) {
5164
pathToRepo = pathToRepo || process.cwd()
5265
const ciNames = [
@@ -62,4 +75,25 @@ function getBranch (pathToRepo) {
6275
return getGitBranch(pathToRepo)
6376
}
6477

65-
module.exports = { runGitCommand, firstFoundValue, getBranch }
78+
const getMessage = pathToRepo => runGitCommand(pathToRepo, gitCommands.message)
79+
80+
const getEmail = pathToRepo => runGitCommand(pathToRepo, gitCommands.email)
81+
82+
const getAuthor = pathToRepo => runGitCommand(pathToRepo, gitCommands.author)
83+
84+
const getSha = pathToRepo => runGitCommand(pathToRepo, gitCommands.sha)
85+
86+
const getRemoteOrigin = pathToRepo =>
87+
runGitCommand(pathToRepo, gitCommands.remoteOriginUrl)
88+
89+
module.exports = {
90+
runGitCommand,
91+
firstFoundValue,
92+
getBranch,
93+
getMessage,
94+
getEmail,
95+
getAuthor,
96+
getSha,
97+
getRemoteOrigin,
98+
gitCommands
99+
}

0 commit comments

Comments
 (0)