Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(logging): removed json parsing and capturing incoming payload for… #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Javascript Node CircleCI 2.0 configuration file
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
#
version: 2
jobs:
build:
docker:
- image: circleci/node:12.6

# working_directory: ~/
steps:
- checkout

# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-
- run: yarn install
- save_cache:
paths:
- node_modules
key: v1-dependencies-{{ checksum "package.json" }}

# run tests!
- run: yarn test
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@

[![forthebadge](https://forthebadge.com/images/badges/built-with-love.svg)](https://forthebadge.com)

[![CircleCI](https://circleci.com/gh/darkfadr/welcome-bot.svg?style=svg)](https://circleci.com/gh/darkfadr/welcome-bot)
[![Maintainability](https://api.codeclimate.com/v1/badges/da7a4ed2522a94560fee/maintainability)](https://codeclimate.com/github/darkfadr/welcome-bot/maintainability)
[![David-DM](https://david-dm.org/darkfadr/welcome-bot.svg)](https://david-dm.org/darkfadr/welcome-bot)


## Example
![screenshot](./docs/screenshot.png)



## Authors
- @darkfadr
50 changes: 18 additions & 32 deletions __test__/messenger.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,28 @@ describe('mesenger', () => {
const name = msg[0].text.text.match(/(<@)\w+>/g).pop();
const testmsg = msg[1].message.includes('test');

console.log
expect(name).toBeTruthy();
expect(testmsg).toBe(true);
});
})

describe('should ommit greeting', () => {
test('when a user id is missing or falsy', () => {
const msg = greeting(0, []);
const msg1 = greeting('', []);
const msg2 = greeting(null, []);
const msg3 = greeting(undefined, []);

expect(msg.length).toBe(0);
expect(msg1.length).toBe(0);
expect(msg2.length).toBe(0);
expect(msg3.length).toBe(0);
});
});

describe('should format', () => {
test('should properly format @user mention', () => {
const id = `1234EF`;
const msg = greeting(id, []);
const name = msg[0].text.text.match(/(<@)\w+>/g).pop();

expect(name).toBe('<@1234EF>');
});

test('should correct user ID and format @user mention', () => {
const id = `1234ef`;
const msg = greeting(id, []);
const name = msg[0].text.text.match(/(<@)\w+>/g).pop();

expect(name).toBe('<@1234EF>');
});
})
test('should ommit greeting when a user id is missing or falsy', () => {
const msg = greeting(0, []);
const msg1 = greeting('', []);
const msg2 = greeting(null, []);
const msg3 = greeting(undefined, []);

expect(msg.length).toBe(0);
expect(msg1.length).toBe(0);
expect(msg2.length).toBe(0);
expect(msg3.length).toBe(0);
});

test('should correct user ID and format @user mention', () => {
const id = `1234ef`;
const msg = greeting(id, []);
const name = msg[0].text.text.match(/(<@)\w+>/g).pop();
expect(name).toBe('<@1234EF>');
});
})
});
1 change: 1 addition & 0 deletions now.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"version": 2,
"name": "welcome-bot",
"scope": "tichaiti",
"alias": ["tichaiti-welcome-bot.now.sh"],
"builds": [
{ "src": "src/index.js", "use": "@now/node" }
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
"main": "src/index.js",
"scripts": {
"start": "micro-dev src/index.js",
"test": "jest --watch"
"test": "jest --ci"
},
"author": "",
"author": "Ashley Narcisse <@darkfadr>",
"license": "ISC",
"dependencies": {
"@slack/web-api": "^5.0.1",
"micro": "^9.3.4",
"micro-dev": "^3.0.0"
},
"devDependencies": {
"jest": "^24.8.0"
}
}
13 changes: 7 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const { send, json } = require('micro');
const { send } = require('micro');
const { greeting, sendMessage} = require('./messenger');
const { welcome } = require('./templates');

const {GENERAL_CHANNEL: channel} = process.env;

module.exports = async (req, res) => {
try {
const body = await json(req);
const { type, user={}, event={} } = body;
const { body } = req;
const { type, user={}, event={} } = body || {};
const { id, channel } = user;

console.log(body)
console.debug(`le incoming payload of type: ${typeof body} `, body);
if (!['team_join'].includes(type))
return send(res, 200, body);

Expand All @@ -24,9 +24,10 @@ module.exports = async (req, res) => {
console.log('returning ', result);
return send(res, 200, result);
} catch(err) {
console.log(err);
console.error(err);
return send(res, 500, {
error: 'something bad happened'
error: 'something bad happened',
data: err.data
});
}
};
2 changes: 1 addition & 1 deletion src/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ exports.welcome = [
"elements": [
{
"type": "mrkdwn",
"text": "*Welcome Bot*"
"text": "*Welcome to tichaiti*"
}
]
}
Expand Down
Loading