1 | const { getGitBranch } = require('./git-api')
|
2 | const debug = require('debug')('commit-info')
|
3 |
|
4 | function firstFoundValue (keys, object = process.env) {
|
5 | const found = keys.find(key => {
|
6 | return key in object
|
7 | })
|
8 | return found ? object[found] : null
|
9 | }
|
10 |
|
11 | const getValue = name => o => {
|
12 | if (name in o) {
|
13 | return o[name]
|
14 | }
|
15 | return null
|
16 | }
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | function getBranch (pathToRepo) {
|
25 | pathToRepo = pathToRepo || process.cwd()
|
26 | debug('using Git tool to find branch')
|
27 | return getGitBranch(pathToRepo)
|
28 | }
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | function getCommitInfoFromEnvironment (env = process.env) {
|
34 | return {
|
35 | branch: getValue('COMMIT_INFO_BRANCH')(env),
|
36 | message: getValue('COMMIT_INFO_MESSAGE')(env),
|
37 | email: getValue('COMMIT_INFO_EMAIL')(env),
|
38 | author: getValue('COMMIT_INFO_AUTHOR')(env),
|
39 | sha: getValue('COMMIT_INFO_SHA')(env),
|
40 | timestamp: getValue('COMMIT_INFO_TIMESTAMP')(env),
|
41 | remote: getValue('COMMIT_INFO_REMOTE')(env)
|
42 | }
|
43 | }
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | function getFields () {
|
49 | return ['branch', 'message', 'email', 'author', 'sha', 'remote', 'timestamp']
|
50 | }
|
51 |
|
52 | module.exports = {
|
53 | firstFoundValue,
|
54 | getBranch,
|
55 | getCommitInfoFromEnvironment,
|
56 | getFields
|
57 | }
|