Skip to content

Commit 55044c8

Browse files
committed
feat: big code refactor for clarity
1 parent 99b2036 commit 55044c8

File tree

7 files changed

+182
-108
lines changed

7 files changed

+182
-108
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
exports['utils getting commit info works 1'] = {
1+
exports['git-api getting commit info works 1'] = {
22
"message": "important commit",
33
"email": "[email protected]",
44
"author": "John Doe",

src/commit-info-spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
const { commitInfo } = require('.')
55
const { stubSpawnShellOnce } = require('stub-spawn-once')
66
const snapshot = require('snap-shot-it')
7-
const { gitCommands } = require('./utils')
7+
const { gitCommands } = require('./git-api')
88
const la = require('lazy-ass')
99
const is = require('check-more-types')
1010

src/git-api-spec.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
'use strict'
2+
3+
const la = require('lazy-ass')
4+
const R = require('ramda')
5+
const { stubSpawnShellOnce } = require('stub-spawn-once')
6+
const Promise = require('bluebird')
7+
const snapshot = require('snap-shot-it')
8+
9+
/* eslint-env mocha */
10+
describe('git-api', () => {
11+
const { gitCommands } = require('./git-api')
12+
13+
describe('getting commit info', () => {
14+
const {
15+
getMessage,
16+
getEmail,
17+
getAuthor,
18+
getSha,
19+
getRemoteOrigin
20+
} = require('./git-api')
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+
44+
describe('getBranch', () => {
45+
const { getBranch } = require('./utils')
46+
47+
context('CI environment', () => {
48+
const initialEnv = R.clone(process.env)
49+
50+
beforeEach(() => {
51+
process.env.TRAVIS_BRANCH = 'travis-branch'
52+
process.env.CI_BRANCH = 'ci-branch'
53+
})
54+
55+
afterEach(() => {
56+
process.env = R.clone(initialEnv)
57+
})
58+
59+
it('finds travis branch', () =>
60+
getBranch().then(branch =>
61+
la(branch === 'travis-branch', 'wrong branch', branch)
62+
))
63+
})
64+
65+
context('local environment', () => {
66+
const initialEnv = R.clone(process.env)
67+
68+
beforeEach(() => {
69+
// remove all possible CI branch variables
70+
delete process.env.CIRCLE_BRANCH
71+
delete process.env.TRAVIS_BRANCH
72+
delete process.env.BUILDKITE_BRANCH
73+
delete process.env.CI_BRANCH
74+
})
75+
76+
afterEach(() => {
77+
process.env = R.clone(initialEnv)
78+
})
79+
80+
it('uses git to determine branch', () => {
81+
stubSpawnShellOnce(gitCommands.branch, 0, 'mock-test-branch', '')
82+
return getBranch().then(branch =>
83+
la(branch === 'mock-test-branch', 'wrong branch from git', branch)
84+
)
85+
})
86+
87+
it('returns empty string on failure', () => {
88+
stubSpawnShellOnce(gitCommands.branch, 1, '', 'nope')
89+
return getBranch().then(branch =>
90+
la(branch === '', 'wrong empty branch from git', branch)
91+
)
92+
})
93+
94+
it('returns empty string on HEAD', () => {
95+
stubSpawnShellOnce(gitCommands.branch, 0, 'HEAD', '')
96+
return getBranch().then(branch =>
97+
la(branch === '', 'wrong HEAD branch from git', branch)
98+
)
99+
})
100+
})
101+
})
102+
})

src/git-api.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const chdir = require('chdir-promise')
2+
const execa = require('execa')
3+
const debug = require('debug')('commit-info')
4+
const la = require('lazy-ass')
5+
const is = require('check-more-types')
6+
7+
// common git commands for getting basic info
8+
const gitCommands = {
9+
branch: 'git rev-parse --abbrev-ref HEAD',
10+
message: 'git show -s --pretty=%B',
11+
email: 'git show -s --pretty=%ae',
12+
author: 'git show -s --pretty=%an',
13+
sha: 'git show -s --pretty=%H',
14+
remoteOriginUrl: 'git config --get remote.origin.url'
15+
}
16+
17+
const prop = name => object => object[name]
18+
const emptyString = () => ''
19+
20+
const runGitCommand = (gitCommand, pathToRepo) => {
21+
la(is.unemptyString(gitCommand), 'missing git command', gitCommand)
22+
la(gitCommand.startsWith('git'), 'invalid git command', gitCommand)
23+
24+
pathToRepo = pathToRepo || process.cwd()
25+
la(is.unemptyString(pathToRepo), 'missing repo path', pathToRepo)
26+
27+
const runGit = () => execa.shell(gitCommand).then(prop('stdout'))
28+
29+
debug('running git command: %s', gitCommand)
30+
return chdir
31+
.to(pathToRepo)
32+
.then(runGit)
33+
.tap(chdir.back)
34+
.catch(emptyString)
35+
}
36+
37+
/*
38+
"gift" module returns "" for detached checkouts
39+
and our current command returns "HEAD"
40+
so we must imitate gift's behavior
41+
42+
example:
43+
git checkout <commit sha>
44+
get git branch returns "HEAD"
45+
*/
46+
const checkIfDetached = branch => (branch === 'HEAD' ? '' : branch)
47+
48+
function getGitBranch (pathToRepo) {
49+
return runGitCommand(gitCommands.branch, pathToRepo)
50+
.then(checkIfDetached)
51+
.catch(emptyString)
52+
}
53+
54+
const getMessage = runGitCommand.bind(null, gitCommands.message)
55+
56+
const getEmail = runGitCommand.bind(null, gitCommands.email)
57+
58+
const getAuthor = runGitCommand.bind(null, gitCommands.author)
59+
60+
const getSha = runGitCommand.bind(null, gitCommands.sha)
61+
62+
const getRemoteOrigin = runGitCommand.bind(null, gitCommands.remoteOriginUrl)
63+
64+
module.exports = {
65+
runGitCommand,
66+
getGitBranch,
67+
getMessage,
68+
getEmail,
69+
getAuthor,
70+
getSha,
71+
getRemoteOrigin,
72+
gitCommands
73+
}

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
const debug = require('debug')('commit-info')
44
const {
5-
getBranch,
65
getMessage,
76
getEmail,
87
getAuthor,
98
getSha,
109
getRemoteOrigin
11-
} = require('./utils')
10+
} = require('./git-api')
11+
const { getBranch } = require('./utils')
1212

1313
const Promise = require('bluebird')
1414

src/utils-spec.js

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,10 @@
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')
86

97
/* eslint-env mocha */
108
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-
})
9+
const { gitCommands } = require('./git-api')
4310

4411
describe('getBranch', () => {
4512
const { getBranch } = require('./utils')

src/utils.js

Lines changed: 2 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,5 @@
1-
const chdir = require('chdir-promise')
2-
const execa = require('execa')
3-
const debug = require('debug')('commit-info')
4-
const la = require('lazy-ass')
5-
const is = require('check-more-types')
61
const Promise = require('bluebird')
7-
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-
18-
const prop = name => object => object[name]
19-
const emptyString = () => ''
20-
21-
const runGitCommand = (pathToRepo, gitCommand) => {
22-
pathToRepo = pathToRepo || process.cwd()
23-
la(is.unemptyString(pathToRepo), 'missing repo path', pathToRepo)
24-
la(is.unemptyString(gitCommand), 'missing git command', gitCommand)
25-
la(gitCommand.startsWith('git'), 'invalid git command', gitCommand)
26-
27-
const runGit = () => execa.shell(gitCommand).then(prop('stdout'))
28-
29-
debug('running git command: %s', gitCommand)
30-
return chdir
31-
.to(pathToRepo)
32-
.then(runGit)
33-
.tap(chdir.back)
34-
.catch(emptyString)
35-
}
36-
37-
/*
38-
"gift" module returns "" for detached checkouts
39-
and our current command returns "HEAD"
40-
so we must imitate gift's behavior
41-
42-
example:
43-
git checkout <commit sha>
44-
get git branch returns "HEAD"
45-
*/
46-
const checkIfDetached = branch => (branch === 'HEAD' ? '' : branch)
47-
48-
function getGitBranch (pathToRepo) {
49-
return runGitCommand(pathToRepo, gitCommands.branch)
50-
.then(checkIfDetached)
51-
.catch(emptyString)
52-
}
2+
const { getGitBranch } = require('./git-api')
533

544
function firstFoundValue (keys, object = process.env) {
555
const found = keys.find(key => {
@@ -75,25 +25,7 @@ function getBranch (pathToRepo) {
7525
return getGitBranch(pathToRepo)
7626
}
7727

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-
8928
module.exports = {
90-
runGitCommand,
9129
firstFoundValue,
92-
getBranch,
93-
getMessage,
94-
getEmail,
95-
getAuthor,
96-
getSha,
97-
getRemoteOrigin,
98-
gitCommands
30+
getBranch
9931
}

0 commit comments

Comments
 (0)