Skip to content

Commit

Permalink
Merge pull request #1 from javierbrea/release-1.0.0-alpha.1
Browse files Browse the repository at this point in the history
Release 1.0.0 alpha.1
  • Loading branch information
javierbrea authored Dec 9, 2018
2 parents da93669 + d3f2427 commit 7f8e9f5
Show file tree
Hide file tree
Showing 25 changed files with 678 additions and 17 deletions.
114 changes: 114 additions & 0 deletions .narval.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,67 @@
schemas:
bind: &bind
- lib
- test
- server.js
- bin
coverage: &disable-coverage
enabled: false
envs:
local-service: &local-service-env
service_port: 3000
service_host_name: localhost
domapic_path: .test
service_extra_options: --url=http://google.com --method=GET
docker-service: &docker-service-env
service_port: 3000
service_host_name: module-container
domapic_path: .shared
service_extra_options: --url=http://google.com --method=GET
clean: &clean
local:
command: test/functional/commands/local-clean.sh
docker:
container: service-container
command: test/functional/commands/clean.sh
down-volumes: true
services:
local: &local-service
command: test/functional/commands/start-module.sh
env: *local-service-env
docker: &docker-service
container: module-container
command: test/functional/commands/start-module.sh
env: *docker-service-env
local-cli: &local-service-cli
<<: *local-service
command: test/functional/commands/start-module-cli.sh
docker-cli: &docker-service-cli
<<: *docker-service
command: test/functional/commands/start-module-cli.sh
test: &functional-test
local:
wait-on: tcp:localhost:3000
env: *local-service-env
docker:
container: test-container
wait-on: tcp:module-container:3000
env: *docker-service-env
docker-images:
- name: node-image
from: node:8.11.1
expose:
- 3000
add:
- package.json
- npm-shrinkwrap.json
install: test/functional/commands/install.sh
docker-containers:
- name: module-container
build: node-image
bind: *bind
- name: test-container
build: node-image
bind: *bind
suites:
unit:
- name: unit
Expand All @@ -6,3 +70,53 @@ suites:
coverage:
config:
dir: .coverage
functional:
- name: webhook
describe: should launch the webhook
before: *clean
services:
- name: module
abort-on-error: true
local: *local-service
docker: *docker-service
test:
<<: *functional-test
specs:
- test/functional/specs/webhook.specs.js
- test/functional/specs/config.specs.js
coverage: *disable-coverage
- name: webhook-fail
describe: should return a bad response when webhook fails
before: *clean
services:
- name: module
abort-on-error: true
local:
<<: *local-service
env:
<<: *local-service-env
service_extra_options: --url=http://google.com --method=POST
docker:
<<: *docker-service
env:
<<: *docker-service-env
service_extra_options: --url=http://google.com --method=POST
test:
<<: *functional-test
specs:
- test/functional/specs/webhook-fail.specs.js
coverage: *disable-coverage
- name: api-cli
describe: should launch the webhook when started using cli
before: *clean
services:
- name: module
abort-on-error: true
local: *local-service-cli
docker: *docker-service-cli
test:
<<: *functional-test
specs:
- test/functional/specs/webhook.specs.js
- test/functional/specs/config.specs.js
coverage: *disable-coverage
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed
### Removed

## [1.0.0] - 2018-12-08
## [1.0.0-alpha.1] - 2018-12-09
### Added
- First release
- First pre-release
3 changes: 3 additions & 0 deletions bin/domapic-webhook
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

require('../lib/cli')
12 changes: 12 additions & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'

const path = require('path')
const domapic = require('domapic-service')

const options = require('./options')

domapic.cli({
packagePath: path.resolve(__dirname, '..'),
script: path.resolve(__dirname, '..', 'server.js'),
customConfig: options
})
17 changes: 17 additions & 0 deletions lib/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict'

const { WEBHOOK_URI, WEBHOOK_METHOD } = require('./statics')

module.exports = {
[WEBHOOK_URI]: {
type: 'string',
alias: ['url'],
describe: 'Define the webhook url'
},
[WEBHOOK_METHOD]: {
type: 'string',
describe: 'GPIO number where the relay is connected',
choices: ['GET', 'POST'],
default: 'POST'
}
}
6 changes: 6 additions & 0 deletions lib/statics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict'

module.exports = {
WEBHOOK_URI: 'url',
WEBHOOK_METHOD: 'method'
}
31 changes: 23 additions & 8 deletions npm-shrinkwrap.json

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

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webhook-domapic-module",
"version": "1.0.0",
"version": "1.0.0-alpha.1",
"description": "Domapic module for triggering a webhook",
"main": "server.js",
"bin": {
Expand All @@ -9,10 +9,11 @@
"scripts": {
"test": "narval",
"coveralls": "cat ./.coverage/lcov.info | coveralls",
"relay": "./bin/domapic-webhook"
"domapic-webhook": "./bin/domapic-webhook"
},
"dependencies": {
"domapic-service": "1.0.0-alpha.3",
"domapic-service": "1.0.0-alpha.4",
"request": "2.88.0",
"request-promise": "4.2.2"
},
"devDependencies": {
Expand Down
45 changes: 45 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -1 +1,46 @@
'use strict'

const path = require('path')

const domapic = require('domapic-service')
const requestPromise = require('request-promise')

const options = require('./lib/options')
const { WEBHOOK_URI, WEBHOOK_METHOD } = require('./lib/statics')

const ABILITY = 'webhook'

domapic.createModule({
packagePath: path.resolve(__dirname),
customConfig: options
}).then(async dmpcModule => {
const config = await dmpcModule.config.get()

await dmpcModule.register({
[ABILITY]: {
description: 'Launch a request to a webhook',
event: {
description: 'The webhook has been triggered'
},
action: {
description: 'Trigger the webhook',
handler: async () => {
await dmpcModule.tracer.info(`Sending ${config[WEBHOOK_METHOD]} request to ${config[WEBHOOK_URI]}`)
return requestPromise({
uri: config[WEBHOOK_URI],
method: config[WEBHOOK_METHOD]
}).then(() => {
dmpcModule.events.emit(ABILITY)
return Promise.resolve()
}).catch(async error => {
const errorMessage = `Error ${error.response.statusCode} received from ${config[WEBHOOK_METHOD]} to ${config[WEBHOOK_URI]}`
await dmpcModule.tracer.error(errorMessage)
return Promise.reject(new dmpcModule.errors.BadGateway(errorMessage))
})
}
}
}
})

return dmpcModule.start()
})
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
sonar.organization=javierbrea
sonar.projectKey=webhook-domapic-module
sonar.projectVersion=1.0.0
sonar.projectVersion=1.0.0-alpha.1

sonar.sources=.
sonar.exclusions=node_modules/**
Expand Down
4 changes: 4 additions & 0 deletions test/functional/commands/clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

rm -rf .test
mkdir .test
3 changes: 3 additions & 0 deletions test/functional/commands/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

npm i
4 changes: 4 additions & 0 deletions test/functional/commands/local-clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

./test/functional/commands/pm2-clean.sh
./test/functional/commands/clean.sh
4 changes: 4 additions & 0 deletions test/functional/commands/pm2-clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

pm2 flush
pm2 delete webhook-domapic-module
4 changes: 4 additions & 0 deletions test/functional/commands/start-module-cli.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

npm run domapic-webhook start -- --path=${domapic_path} ${service_extra_options}
npm run domapic-webhook logs
3 changes: 3 additions & 0 deletions test/functional/commands/start-module.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

node server.js --path=${domapic_path} ${service_extra_options}
24 changes: 24 additions & 0 deletions test/functional/specs/config.specs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const test = require('narval')

const utils = require('./utils')

test.describe('module configuration', function () {
this.timeout(10000)
let connection

test.before(() => {
connection = new utils.Connection()
})

test.it('should be exposed in config api', () => {
return connection.request('/config', {
method: 'GET'
}).then(response => {
return Promise.all([
test.expect(response.statusCode).to.equal(200),
test.expect(response.body.url).to.equal('http://google.com'),
test.expect(response.body.method).to.equal('GET')
])
})
})
})
Loading

0 comments on commit 7f8e9f5

Please sign in to comment.