Skip to content

Commit

Permalink
Merge branch 'master' into ya-optimise-build
Browse files Browse the repository at this point in the history
  • Loading branch information
allardy committed Nov 13, 2018
2 parents bc3e84e + 50dfe69 commit a3b0be1
Show file tree
Hide file tree
Showing 30 changed files with 266 additions and 194 deletions.
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "src/bp/professional"]
path = src/bp/professional
url = git@github.com:botpress/botpress-professional.git
[submodule "src/bp/pro"]
path = src/bp/pro
url = git@github.com:botpress/botpress-pro.git
1 change: 1 addition & 0 deletions build/codebuild-extras.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ echo "export REPOSITORY_URI=$REPOSITORY_URI"
echo "export COMMIT_HASH=$COMMIT_HASH"
echo "export IMAGE_TAG=$IMAGE_TAG"
echo "export EDITION=$EDITION"
echo "export CODEBUILD_GIT_TAG=$CODEBUILD_GIT_TAG"
echo "export DOCKER_TAG_COMMIT=$DOCKER_TAG_COMMIT"
8 changes: 3 additions & 5 deletions build/jsonschemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ module.exports = () => {
const definition = TJS.generateSchema(program, typeName, settings)
const json = JSON.stringify(definition, null, 2) + os.EOL + os.EOL

const filesToWrite = ['./out/bp/data', './src/bp/vanilla'].map(p => path.resolve(p, jsonFile))
filesToWrite.forEach(f => {
mkdirp.sync(path.dirname(f))
fs.writeFileSync(f, json)
})
const fileToWrite = path.resolve('./out/bp/data', jsonFile)
mkdirp.sync(path.dirname(fileToWrite))
fs.writeFileSync(fileToWrite, json)
}

writeSchema('BotpressConfig', 'botpress.config.schema.json')
Expand Down
2 changes: 1 addition & 1 deletion build/package.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"bin": "index.js",
"private": true,
"pkg": {
"scripts": ["./core/**/*.js", "./*.js", "./professional/**/*.js", "./common/**/*.js"],
"scripts": ["./core/**/*.js", "./*.js", "./pro/**/*.js", "./common/**/*.js"],
"assets": ["../../node_modules/vm2/lib/*.js", "./ui-admin/**", "./ui-studio/**"]
}
}
2 changes: 1 addition & 1 deletion buildspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ phases:
- echo "Edition = '$EDITION'"
- chmod +x ./build/codebuild-extras.sh
- $(./build/codebuild-extras.sh)
- echo "BUILDING FOR '$ARTIFACT_NAME' edition '$EDITION'"
- echo "BUILDING FOR '$ARTIFACT_NAME' edition '$EDITION'. GIT TAG = '$CODEBUILD_GIT_TAG'"
- yarn
- yarn build:modules-builder
- yarn build:studio-prod
Expand Down
44 changes: 44 additions & 0 deletions release-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash

DOCS_VERSION_COMMAND="run version"

echo "Select an option for release:"
echo

select VERSION in patch minor major "Specific Version"
do
echo
if [[ $REPLY =~ ^[1-4]$ ]]; then
if [[ $REPLY == 4 ]]; then
read -p "Enter a specific version: " -r VERSION
echo
if [[ -z $REPLY ]]; then
VERSION=$REPLY
fi
fi

read -p "Create $VERSION commit - Are you sure ... (y/n) " -n 1 -r
echo

if [[ $REPLY =~ ^[Yy]$ || -z $REPLY ]]; then
# Bump version
yarn version --new-version $VERSION --no-git-tag-version
NEW_VERSION=$(node -p "require('./package.json').version")
cd docs/guide/website && yarn $DOCS_VERSION_COMMAND $NEW_VERSION && cd ../../..

# Create commit
git add -A
git commit -m "v$NEW_VERSION"
git tag -a "v$NEW_VERSION" -m "created tag v$NEW_VERSION"
echo "-----"
echo "Finished, commit created, need to push to origin/master directly"
else
echo Cancelled
fi
break
else
echo Invalid \"${REPLY}\"
echo "To continue, please enter one of the options (1-4):"
echo
fi
done
5 changes: 2 additions & 3 deletions src/bp/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import './common/polyfills'

import sdk from 'botpress/sdk'
import chalk from 'chalk'
import { BotpressEditions } from 'common/editions'
import { Botpress, Config, Logger } from 'core/app'
import center from 'core/logger/center'
import { ModuleLoader } from 'core/module-loader'
Expand All @@ -14,10 +15,8 @@ import os from 'os'
import path from 'path'
import { FatalError } from './errors'

const editions = { ce: 'Community', pro: 'Professional', ee: 'Enterprise' }

async function start() {
let currentEdition = editions[process.BOTPRESS_EDITION] + ' Edition'
let currentEdition = BotpressEditions[process.BOTPRESS_EDITION] + ' Edition'

if (process.BOTPRESS_EDITION === 'ce') {
currentEdition = chalk.cyan(currentEdition)
Expand Down
1 change: 1 addition & 0 deletions src/bp/common/editions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const BotpressEditions = { ce: 'Community', pro: 'Professional', ee: 'Enterprise' }
2 changes: 1 addition & 1 deletion src/bp/core/app.inversify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ container.load(...ServicesContainerModules)

if (process.BOTPRESS_EDITION && process.BOTPRESS_EDITION !== 'ce') {
// Otherwise this will fail on compile when the submodule is not available.
const ProContainerModule = require('professional/services/professionnal.inversify')
const ProContainerModule = require('pro/services/pro.inversify')
container.load(ProContainerModule)
}

Expand Down
2 changes: 1 addition & 1 deletion src/bp/core/database/tables/server-wide/auth-roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class AuthRolesTable extends Table {
if (process.BOTPRESS_EDITION === undefined || process.BOTPRESS_EDITION === 'ce') {
await insertRoles(this.knex, this.name, communityRoles)
} else {
const { roles } = require('professional/services/admin/pro-roles')
const { roles } = require('pro/services/admin/pro-roles')
await insertRoles(this.knex, this.name, roles)
}
}
Expand Down
13 changes: 0 additions & 13 deletions src/bp/core/errors.ts

This file was deleted.

28 changes: 21 additions & 7 deletions src/bp/core/routers/admin/teams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import { CustomRouter } from '..'
import { Bot } from '../../misc/interfaces'
import AuthService from '../../services/auth/auth-service'
import { InvalidOperationError } from '../../services/auth/errors'
import { asyncMiddleware, error as sendError, success as sendSuccess, validateBodySchema } from '../util'
import { asyncMiddleware, success as sendSuccess, validateBodySchema } from '../util'

export class TeamsRouter implements CustomRouter {
private asyncMiddleware!: Function
public readonly router: Router

constructor(logger: Logger, private authService: AuthService, private adminService: AdminService) {
constructor(private logger: Logger, private authService: AuthService, private adminService: AdminService) {
this.asyncMiddleware = asyncMiddleware({ logger })
this.router = Router({ mergeParams: true })
this.setupRoutes()
Expand Down Expand Up @@ -233,11 +233,7 @@ export class TeamsRouter implements CustomRouter {
const userId = req.dbUser.id
await svc.assertUserMember(userId, teamId)
await svc.assertUserPermission(userId, teamId, 'admin.team.bots', 'write')
try {
await svc.addBot(teamId, bot)
} catch (err) {
return sendError(res, 400, undefined, err.message)
}
await svc.addBot(teamId, bot)

return sendSuccess(res, 'Added new bot', {
botId: bot.id,
Expand All @@ -246,6 +242,24 @@ export class TeamsRouter implements CustomRouter {
})
)

router.put(
'/:teamId/bots/:botId', // Update bot
this.asyncMiddleware(async (req, res) => {
const { teamId, botId } = req.params
const bot = <Bot>req.body
const userId = req.dbUser.id

await svc.assertUserMember(userId, teamId)
await svc.assertUserPermission(userId, teamId, 'admin.team.bots', 'write')
await svc.updateBot(teamId, botId, bot)

return sendSuccess(res, 'Updated bot', {
botId,
teamId
})
})
)

router.get(
'/:teamId/bots', // List bots
this.asyncMiddleware(async (req, res) => {
Expand Down
2 changes: 1 addition & 1 deletion src/bp/core/routers/admin/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import _ from 'lodash'
import { CustomRouter } from '..'
import AuthService from '../../services/auth/auth-service'
import { InvalidOperationError } from '../../services/auth/errors'
import { asyncMiddleware, error as sendError, success as sendSuccess, validateBodySchema } from '../util'
import { asyncMiddleware, success as sendSuccess, validateBodySchema } from '../util'

export class UsersRouter implements CustomRouter {
private asyncMiddleware!: Function
Expand Down
21 changes: 21 additions & 0 deletions src/bp/core/routers/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* The object that wraps HTTP errors.
*
* @constructor
* @param message - The error message that will be sent to the end-user
* @param statusCode - The HTTP status code
* @param errorCode - Botpress error codes e.g. BP_0001, BP_0002, etc.
*/
export class ResponseError extends Error {
errorCode: string
statusCode: number

skipLogging = false

constructor(message: string, statusCode: number, errorCode: string) {
super(message)
Error.captureStackTrace(this, this.constructor)
this.statusCode = statusCode
this.errorCode = errorCode
}
}
3 changes: 2 additions & 1 deletion src/bp/core/routers/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { ModuleLoader } from '../module-loader'
import { SkillService } from '../services/dialog/skill/service'

import { CustomRouter } from '.'
import { asyncMiddleware, error as sendError, success as sendSuccess } from './util'
import { asyncMiddleware } from './util'

export class ModulesRouter implements CustomRouter {
public readonly router: Router
private asyncMiddleware!: Function
Expand Down
13 changes: 2 additions & 11 deletions src/bp/core/routers/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ export const asyncMiddleware = ({ logger }: { logger: Logger }) => (
fn: (req: Request, res: Response, next?: NextFunction) => Promise<any>
) => (req: Request, res: Response, next: NextFunction) => {
Promise.resolve(fn(req, res, next)).catch(err => {
if (err.logError) {
if (!err.skipLogging) {
logger.attachError(err).debug(`Async request error ${err.message}`)
}

next(err)
})
}
Expand All @@ -39,16 +40,6 @@ export const success = (res: Response, message: string = 'Success', payload = {}
})
}

export const error = (res: Response, status = 400, code?: string, message?: string, docs?: string) => {
res.status(status).json({
status: 'error',
type: 'Error',
code: code || status,
message: message || 'Unknown error',
docs: docs || 'https://botpress.io/docs'
})
}

export const checkTokenHeader = (authService: AuthService, audience?: string) => async (
req: RequestWithUser,
res: Response,
Expand Down
22 changes: 9 additions & 13 deletions src/bp/core/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,23 +119,19 @@ export default class HTTPServer {
this.app.use(`${BASE_API_PATH}/bots/:botId`, this.botsRouter.router)
this.app.use(`/s`, this.shortlinksRouter.router)

this.app.use((err, req, res, next) => {
const statusCode = err.status || 500
const code = err.code || 'BP_000'
const message = (err.code && err.message) || 'Unexpected error'
const devOnly = isProd
? {}
: {
stack: err.stack,
full: err.message
}
this.app.use(function handleUnexpectedError(err, req, res, next) {
const statusCode = err.statusCode || 500
const errorCode = err.errorCode || 'BP_000'
const message = (err.errorCode && err.message) || 'Unexpected error'
const docs = err.docs || 'https://botpress.io/docs'
const devOnly = isProd ? {} : { showStackInDev: true, stack: err.stack, full: err.message }

res.status(statusCode).json({
status: 'error',
code,
statusCode,
errorCode,
type: err.type || Object.getPrototypeOf(err).name || 'Exception',
message,
docs: err.docs || undefined,
docs,
...devOnly
})
})
Expand Down
Loading

0 comments on commit a3b0be1

Please sign in to comment.