Skip to content

Commit

Permalink
Issue 191: Fix Issue with Pun Command and Add Version Hyperlink (#192)
Browse files Browse the repository at this point in the history
* Update pun.ts

* fixed a bug in pun command and added version hyperlink to help card

* remove unnecessary file and fix makefile

* run docker container build test in parallel

* fix linter bugs and docker build test workflow bug
  • Loading branch information
amthorn authored Oct 1, 2021
1 parent 9b5efd1 commit e88f41f
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 30 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ jobs:
- uses: CultureHQ/actions-yarn@master
with:
args: --cwd services/bot lint
build-test:
needs: setup
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: Test Docker Container Builds
run: docker build -t testing:latest services/bot
test:
needs: setup
runs-on: ubuntu-latest
Expand Down
19 changes: 8 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,23 @@
build:
docker compose -f docker-compose.yml -f docker-compose.dev.yml build

.PHONY: build-prod
build-prod:
docker compose build

.PHONY: up
up:
docker compose -f docker-compose.yml -f docker-compose.dev.yml up --build -d
yarn --cwd services/bot start:dev-bot


.PHONY: deploy $(VERSION)
deploy:
${MAKE} build
./bin/deploy $(VERSION)

.PHONY: logs
logs:
docker-compose logs -f bot

.PHONY: up-prod
up-prod:
docker-compose up -d
docker compose logs -f bot

.PHONY: down
down:
docker compose -f docker-compose.yml -f docker-compose.dev.yml down
docker compose down

.PHONY: test
test:
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '3.3'
version: '3.9'
services:
bot:
build:
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '3.3'
version: '3.9'
services:
bot:
image: ${QUTEX_IMAGE:-qutex_bot:latest}
Expand Down
2 changes: 1 addition & 1 deletion services/bot/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:16.9.0-alpine3.13
FROM node:16.10.0-alpine3.13

# Set the working directory
WORKDIR /app
Expand Down
8 changes: 4 additions & 4 deletions services/bot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"@types/jest": "^26.0.22",
"@types/mock-fs": "^4.13.0",
"@types/mongoose": "^5.11.97",
"@types/node": "^16.9.1",
"@types/node": "^15.14.9",
"@types/uuid": "^8.3.0",
"@typescript-eslint/eslint-plugin": "^4.31.2",
"@typescript-eslint/parser": "^4.29.0",
Expand All @@ -60,17 +60,17 @@
"mock-fs": "^5.1.1",
"mockdate": "^3.0.5",
"mrm-task-npm-docker": "^1.3.0",
"ngrok": "^4.1.0",
"ngrok": "^4.2.2",
"nodemon": "^2.0.7",
"rimraf": "^3.0.2",
"ts-jest": "^26.5.5",
"ts-node": "^10.2.1",
"typescript": "^4.3.4"
"typescript": "^4.4.3"
},
"dependencies": {
"axios": "^0.21.2",
"chartjs-to-image": "^1.0.4",
"express": "^4.16.4",
"express": "^4.17.1",
"migrate-mongo": "^8.2.2",
"moment": "^2.29.1",
"mongoose": "^5.13.0",
Expand Down
10 changes: 7 additions & 3 deletions services/bot/src/commands/eggs/pun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ export class Pun extends CommandBase implements ICommand {
*/
public async relax (initiative: IInitiative): Promise<string> {
LOGGER.verbose(`Getting pun for user: ${initiative.user.id}`);
// Only return a list of projects that the user is an admin for.
const response = await axios({ url: 'https://icanhazdadjoke.com/', headers: { 'Accept': 'text/plain' } });
// There's an issue perhaps with the website or the axios library that basically doesn't work properly
// when the axios is used as the User-Agent. Thus, pretend that we are a curl request.
const response = await axios({
url: 'https://icanhazdadjoke.com/',
headers: { 'Accept': 'text/plain', 'User-Agent': 'curl/7.64.1' }
});
LOGGER.info(`Pun: ${JSON.stringify(response.data, null, 2)}`);
return response.data;
}
}
}
2 changes: 1 addition & 1 deletion services/bot/src/commands/help/card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class Card extends CommandBase implements ICommand {
card.attachments[0].content.body[2].actions[2].card.body = superAdminCommands;

// Set "about" information
card.attachments[0].content.body[1].columns[1].items[0].text = process.env.VERSION;
card.attachments[0].content.body[1].columns[1].items[0].text = `[${process.env.VERSION}](https://github.com/amthorn/qutex/releases/tag/${process.env.VERSION})`;
card.attachments[0].content.body[1].columns[1].items[1].text = process.env.RELEASE_DATE;
card.attachments[0].content.body[1].columns[1].items[2].text = `${process.env.AUTHOR_NAME} (${process.env.AUTHOR_EMAIL})`;

Expand Down
20 changes: 16 additions & 4 deletions services/bot/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,31 @@ export class Handler {
const now = new Date();
LOGGER.error(`TRACE ID: ${traceId}`);
LOGGER.error(`OCCURRED AT: ${now}`);
LOGGER.error(e.stack);
// Typescript has a quirk where it doesn't know the type of the error object
// leading to: Object is of type 'unknown'
let errorMessage = 'An unknown error has occurred in Qutex';
let errorStack: string | undefined = 'Stack Trace not found.';
if (e instanceof Error) {
errorMessage = e.message;
errorStack = e.stack;
}
LOGGER.error(errorMessage);
try {
await BOT.messages.create({
markdown: `An unexpected error occurred. Please open an issue by using the "help" command:\n${e}`,
markdown: `An unexpected error occurred. Please open an issue by using the "help" command:\nError: ${errorMessage}`,
roomId: request.body.data.roomId
});

await BOT.messages.create({
markdown: `An unexpected error occurred at ${now}.\n\`\`\`\nTRACE ID: ${traceId}\n${e.stack}\n\`\`\``,
markdown: `An unexpected error occurred at ${now}.\n\`\`\`\nTRACE ID: ${traceId}\n${errorStack}\n\`\`\``,
toPersonEmail: process.env.DEBUG_EMAIL
});
} catch (exc) {
LOGGER.error(exc);
let innerError = 'A very unknown error has occurred in Qutex!!';
if (exc instanceof Error) {
innerError = exc.message;
}
LOGGER.error(innerError);
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions services/bot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
/// <reference path="../types/index.d.ts" />

import express from 'express';
import express, { RequestHandler } from 'express';
import { Handler } from './handler';
import mongoose from 'mongoose';
import { GET } from './secrets';
Expand All @@ -19,7 +19,9 @@ mongoUri += '?authSource=admin';
mongoose.connect(mongoUri, { useCreateIndex: true, useNewUrlParser: true, useUnifiedTopology: true });

const APP = express();
APP.use(express.json());
// without the "as" expression here, there's a bug which yields the error: error TS2769: No overload matches this call
// https://letscodepare.com/blog/argument-of-type-nexthandlefunction-is-not-assignable-nodejs
APP.use(express.json() as RequestHandler);

const HOST = '0.0.0.0';
const PORT = 3000;
Expand Down
2 changes: 1 addition & 1 deletion services/bot/tests/commands/eggs/pun.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ describe('Pun works appropriately', () => {
test('Pun is returned appropriately', async () => {
expect(await PROJECT_MODEL.find({}).exec()).toHaveLength(0);
expect(await new Pun().relax(TEST_INITIATIVE)).toEqual('mock response');
expect(axios).toHaveBeenCalledWith({ url: 'https://icanhazdadjoke.com/', headers: { 'Accept': 'text/plain' } });
expect(axios).toHaveBeenCalledWith({ url: 'https://icanhazdadjoke.com/', headers: { 'Accept': 'text/plain', 'User-Agent': 'curl/7.64.1' } });
});
});
2 changes: 1 addition & 1 deletion services/bot/tests/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ describe('Handler errors as it should', () => {
});
expect(BOT.messages.create).toHaveBeenCalledWith({
toPersonEmail: process.env.DEBUG_EMAIL,
markdown: expect.stringMatching(`An unexpected error occurred at Thu May 06 2021 01:43:08 GMT-0400 \\(Eastern Daylight Time\\).
markdown: expect.stringMatching(`An unexpected error occurred at Thu May 06 2021 01:43:08 GMT-0400 \\(Eastern Daylight Time\\)\\.
\`\`\`
TRACE ID: [\\w\\-]+
Error: THIS IS AN EXPECTED ERROR
Expand Down

0 comments on commit e88f41f

Please sign in to comment.