Skip to content

Commit

Permalink
Merge branch 'release/5.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-meacham committed Nov 25, 2020
2 parents 0bfa6c4 + f317dd4 commit 14241ec
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 5 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ resources:
* git:branch - name of the current branch
* git:isDirty - true if the workspace is currently dirty
* git:describe / git:describeLight - see below
* git:user - The user's name
* git:email - The user's email
* git:tags - The tag pointing to the current commit
* git:message - Full git commit message
* git:messageSubject - Only the suject of the message, as `git log -1 --pretty=%s`
* git:messageBody - Only the body of the message, as `git log -1 --pretty=%b`

## describe and describeLight
The describe (`${git:describe}`) and the describeLight (`${git:describeLight}`) variables are both used to return the most recent tag of the repo. However the difference is that whilst `describe` evaluates to `git describe --always`, the `describeLight` variable evaluates to `git describe --always --tags`.
Expand All @@ -66,6 +72,8 @@ For more information on annotated and lightweight tags go to the [git documentat
The tags (`${git:tags}`) is used to get info about which git tags (separated by ::) are pointing to current commit and if none it will show commit ID as fallback.

# Version History
* 5.1.0
- Add messageSubject/messageBody (Thanks @vhenzl)
* 5.0.1
- Fix module export (Thanks @nason)
* 5.0.0
Expand Down
10 changes: 9 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ class ServerlessGitVariables {
value = await _exec('git log -1 --pretty=%B');
break;

case 'messageSubject':
value = await _exec('git log -1 --pretty=%s');
break;

case 'messageBody':
value = await _exec('git log -1 --pretty=%b');
break;

case 'user':
value = await _exec('git config user.name');
break;
Expand Down Expand Up @@ -121,7 +129,7 @@ class ServerlessGitVariables {
break;

default:
throw new Error(`Git variable ${variable} is unknown. Candidates are 'describe', 'describeLight', 'sha1', 'commit', 'branch', 'message', 'user', 'email', 'isDirty', 'repository', 'tags'`);
throw new Error(`Git variable ${variable} is unknown. Candidates are 'describe', 'describeLight', 'sha1', 'commit', 'branch', 'message', 'messageSubject', 'messageBody', 'user', 'email', 'isDirty', 'repository', 'tags'`);
} // TODO: Figure out why if I don't log, the deasync promise
// never resolves. Catching it in the debugger or logging
// causes it to work fine.
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-plugin-git-variables",
"version": "5.0.1",
"version": "5.1.0",
"engines": {
"node": ">=12.0"
},
Expand Down
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ export default class ServerlessGitVariables {
case 'message':
value = await _exec('git log -1 --pretty=%B')
break
case 'messageSubject':
value = await _exec('git log -1 --pretty=%s')
break
case 'messageBody':
value = await _exec('git log -1 --pretty=%b')
break
case 'user':
value = await _exec('git config user.name')
break
Expand All @@ -91,7 +97,7 @@ export default class ServerlessGitVariables {
}
break
default:
throw new Error(`Git variable ${variable} is unknown. Candidates are 'describe', 'describeLight', 'sha1', 'commit', 'branch', 'message', 'user', 'email', 'isDirty', 'repository', 'tags'`)
throw new Error(`Git variable ${variable} is unknown. Candidates are 'describe', 'describeLight', 'sha1', 'commit', 'branch', 'message', 'messageSubject', 'messageBody', 'user', 'email', 'isDirty', 'repository', 'tags'`)
}

// TODO: Figure out why if I don't log, the deasync promise
Expand Down
23 changes: 23 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,29 @@ test.serial('Multiple tags on HEAD', async t => {
t.is(sls.service.custom.tags, 'tag1::tag2')
})

test.serial('Multiline message on HEAD', async t => {
fs.copySync('test/resources/full_repo/git', `${t.context.tmpDir}/.git`)
process.chdir(t.context.tmpDir)

await childProcess.exec('git checkout branch_with_multiline_msg')

const sls = buildSls()
sls.service.custom.sha1 = '${git:sha1}' // eslint-disable-line
sls.service.custom.commit = '${git:commit}' // eslint-disable-line
sls.service.custom.branch = '${git:branch}' // eslint-disable-line
sls.service.custom.message = '${git:message}' // eslint-disable-line
sls.service.custom.messageSubject = '${git:messageSubject}' // eslint-disable-line
sls.service.custom.messageBody = '${git:messageBody}' // eslint-disable-line
await sls.variables.populateService()

t.is(sls.service.custom.sha1, '4b4971e')
t.is(sls.service.custom.commit, '4b4971e54a1cd05eab6c0ae32cf5cd330c9dcc41')
t.is(sls.service.custom.branch, 'branch_with_multiline_msg')
t.is(sls.service.custom.message, 'Commit with subject and body\n\nThis is the first line of the body.\n\nAnd this is another line.')
t.is(sls.service.custom.messageSubject, 'Commit with subject and body')
t.is(sls.service.custom.messageBody, 'This is the first line of the body.\n\nAnd this is another line.')
})

test('Returns cached value as promise', async t => {
const serverless = new Serverless()
const vars = new ServerlessGitVariables(serverless, {})
Expand Down
6 changes: 5 additions & 1 deletion test/resources/full_repo/git/COMMIT_EDITMSG
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
Commit with tags.
Commit with subject and body

This is the first line of the body.

And this is another line.
3 changes: 3 additions & 0 deletions test/resources/full_repo/git/logs/HEAD
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ ef5f0683654427ff38d43836098f6336d73c4576 90440bdc8eb3b2fa20bc578f411cf4b725ae0a2
90440bdc8eb3b2fa20bc578f411cf4b725ae0a25 ef5f0683654427ff38d43836098f6336d73c4576 David Navrkal <navrkald@gmail.com> 1571233695 +0200 checkout: moving from another_branch to branch_with_tags
ef5f0683654427ff38d43836098f6336d73c4576 1335258f70f45c6243bc674df830cd0ec7c3c714 David Navrkal <navrkald@gmail.com> 1571233707 +0200 commit: Commit with tags.
1335258f70f45c6243bc674df830cd0ec7c3c714 90440bdc8eb3b2fa20bc578f411cf4b725ae0a25 David Navrkal <navrkald@gmail.com> 1571234561 +0200 checkout: moving from branch_with_tags to another_branch
90440bdc8eb3b2fa20bc578f411cf4b725ae0a25 ef5f0683654427ff38d43836098f6336d73c4576 V Henzl <vhenzl@github.com> 1605853944 +1300 checkout: moving from another_branch to branch_with_multiline_msg
ef5f0683654427ff38d43836098f6336d73c4576 4b4971e54a1cd05eab6c0ae32cf5cd330c9dcc41 V Henzl <vhenzl@github.com> 1605853950 +1300 commit: Commit with subject and body
4b4971e54a1cd05eab6c0ae32cf5cd330c9dcc41 90440bdc8eb3b2fa20bc578f411cf4b725ae0a25 V Henzl <vhenzl@github.com> 1605854014 +1300 checkout: moving from branch_with_multiline_msg to another_branch
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
0000000000000000000000000000000000000000 ef5f0683654427ff38d43836098f6336d73c4576 V Henzl <vhenzl@github.com> 1605853944 +1300 branch: Created from master
ef5f0683654427ff38d43836098f6336d73c4576 4b4971e54a1cd05eab6c0ae32cf5cd330c9dcc41 V Henzl <vhenzl@github.com> 1605853950 +1300 commit: Commit with subject and body
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4b4971e54a1cd05eab6c0ae32cf5cd330c9dcc41

0 comments on commit 14241ec

Please sign in to comment.