Skip to content

Commit

Permalink
Merge branch 'release/2.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-meacham committed Jul 8, 2017
2 parents 290d765 + 10663c8 commit 6b55aaa
Show file tree
Hide file tree
Showing 6 changed files with 869 additions and 500 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ coverage
.nyc_output

.vscode
.idea
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ plugins:
- serverless-plugin-git-variables
```

# Serverless Version Support
* If you're using serverless 1.12.x or below, use the 1.x.x version of this plugin.
* This plugin is currently broken for serverless versions between 1.13 and 1.15 (inclusive).
* If you're using serverless 1.16.x or above, use the 2.x.x version of this plugin.

# Version History
* 2.0.0
- support Serverless >= 1.16.0
* 1.0.1
- list babel-runtime as a dependency
* 1.0.0
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-plugin-git-variables",
"version": "1.0.1",
"version": "2.0.0",
"engines": {
"node": ">=7.0"
},
Expand Down Expand Up @@ -32,18 +32,18 @@
],
"scripts": {
"lint": "eslint .",
"eslint": "eslint",
"test": "nyc ava",
"test:watch": "ava --watch",
"build:node": "cross-env BABEL_ENV=production babel src --out-dir lib",
"build": "npm run lint && npm run test && npm run build:node",
"ci:coverage": "nyc report --reporter=text-lcov | coveralls"
},
"dependencies": {
"deasync-promise": "1.0.1",
"babel-runtime": "6.23.0"
},
"devDependencies": {
"ava": "0.17.0",
"ava": "0.20.0",
"babel-cli": "6.18.0",
"babel-core": "6.21.0",
"babel-eslint": "7.1.1",
Expand All @@ -62,11 +62,11 @@
"fs-extra": "2.1.2",
"nyc": "10.0.0",
"rimraf": "2.5.4",
"serverless": "1.11.0",
"serverless": "1.17.0",
"tmp": "0.0.31"
},
"peerDependencies": {
"serverless": ">=1.6.0"
"serverless": ">=1.16.0"
},
"ava": {
"require": [
Expand Down
9 changes: 4 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// TODO: Consider using nodegit instead
import deasyncPromise from 'deasync-promise'
import childProcess from 'child_process'

const GIT_PREFIX = 'git'
Expand All @@ -24,19 +23,19 @@ export default class ServerlessGitVariables {
serverless.variables.getValueFromSource = (variableString) => {
if (variableString.startsWith(`${GIT_PREFIX}:`)) {
const variable = variableString.split(`${GIT_PREFIX}:`)[1]
return this._getValueFromGitSync(variable)
return this._getValue(variable)
}

return delegate(variableString)
}
}

_getValueFromGitSync(variable) {
async _getValue(variable) {
if (this.resolvedValues[variable]) {
return this.resolvedValues[variable]
return Promise.resolve(this.resolvedValues[variable])
}

return deasyncPromise(this._getValueFromGit(variable))
return this._getValueFromGit(variable)
}

async _getValueFromGit(variable) {
Expand Down
32 changes: 19 additions & 13 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,38 @@ test.beforeEach(t => {
})

test.beforeEach(t => {
t.context.tmpDir = tmp.dirSync({ unsafeCleanup: true }).name
t.context.tmpDir = tmp.dirSync({unsafeCleanup: true}).name
})

test.afterEach.always(t => {
process.chdir(t.context.initalDir)
})

test('Variables are passed through', t => {
test('Variables are passed through', async t => {
const sls = buildSls()
sls.service.custom.myVar = 'myVar'
sls.service.custom.myResoledVar = '${self:custom.myVar}' // eslint-disable-line

sls.variables.populateService()
await sls.variables.populateService()
t.is(sls.service.custom.myResoledVar, 'myVar')
})

test('Throws on bad key', t => {
test('Rejects on bad key', async t => {
const sls = buildSls()
sls.service.custom.myVar = '${git:badKey}' // eslint-disable-line
t.throws(() => {
sls.variables.populateService()
}, /Error: Git variable badKey is unknown.*/)
await t.throws(sls.variables.populateService(), /Error: Git variable badKey is unknown.*/)
})

test('Throws on bad git command', t => {
test.serial('Rejects on bad git command', async t => {
fs.copySync('test/resources/simple_repo/git', `${t.context.tmpDir}/.git`)
process.chdir(t.context.tmpDir)

const sls = buildSls()
sls.service.custom.describe = '${git:describe}' // eslint-disable-line
t.throws(() => {
sls.variables.populateService()
}, /Error: Command failed: git describe/)
await t.throws(sls.variables.populateService(), /Error: Command failed: git describe/)
})

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

Expand All @@ -63,10 +59,20 @@ test('Inserts variables', t => {
sls.service.custom.sha1 = '${git:sha1}' // eslint-disable-line
sls.service.custom.branch = '${git:branch}' // eslint-disable-line
sls.service.custom.describe2 = '${git:describe}' // eslint-disable-line
sls.variables.populateService()
await sls.variables.populateService()

t.is(sls.service.custom.sha1, '90440bd')
t.is(sls.service.custom.branch, 'another_branch')
t.is(sls.service.custom.describe, 'my_tag-1-g90440bd')
t.is(sls.service.custom.describe2, 'my_tag-1-g90440bd')
})

test('Returns cached value as promise', async t => {
let serverless = new Serverless()
let vars = new ServerlessGitVariables(serverless, {})
let fakeTag = 'my_tag-2-c1023gh'
vars.resolvedValues['describe'] = fakeTag
await serverless.variables.getValueFromSource('git:describe').then(value => {
t.is(value, fakeTag)
})
})

0 comments on commit 6b55aaa

Please sign in to comment.