Skip to content
Draft
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 .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Node.js Testing

on:
push:
branches: ["main", "issue-21-b"]
pull_request:
branches: ["main", "issue-21-b"]

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [24.x]

steps:
- uses: actions/checkout@v4

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: "npm"

- run: yarn install
- run: node --test src/test/*.test.js
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
install:
docker-compose run -u node --rm -T --entrypoint yarn web
docker compose run -u node --rm -T --entrypoint yarn web

test:
docker compose run --rm --entrypoint node web --test src/test/*.test.js

up:
docker-compose up -d
docker compose up -d

dev: install up

up_prod:
docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d
docker compose -f docker-compose.yml -f docker-compose.prod.yml up -d

prod: install up_prod
22 changes: 12 additions & 10 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
services:
web:
image: node:lts-slim
expose:
- 80
ports:
- 80:80
volumes:
- ./:/app
working_dir: /app
environment:
- DATABASE_URL=postgresql://postgres:toor@db:5432/plus
- SLACK_VERIFICATION_TOKEN=
- MONGODB_URI=mongodb://db:27017/plus
- MONGODB_DB=plus
- SLACK_VERIFICATION_TOKEN=xxxxxxxxxxxxxxxxxxxxxxx1
- SLACK_BOT_USER_OAUTH_ACCESS_TOKEN=
- YARN_PRODUCTION=true
- DATABASE_USE_SSL=false
entrypoint: node index.js
links:
depends_on:
- db

db:
image: postgres:10
environment:
- POSTGRES_PASSWORD=toor
- POSTGRES_DB=plus
image: mongo:7
volumes:
- mongo-data:/data/db

volumes:
mongo-data:
20 changes: 10 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

'use strict';

const app = require( './src/app' ),
slack = require( './src/slack' );

const fs = require( 'fs' ),
mime = require( 'mime' ),
express = require( 'express' ),
bodyParser = require( 'body-parser' ),
slackClient = require( '@slack/client' );
import app from './src/app.js';
import slack from './src/slack.js';
import express from 'express';
import fs from 'fs';
import mime from 'mime';
import bodyParser from 'body-parser';
import slackClient from '@slack/client';
import { fileURLToPath } from 'url';

/* eslint-disable no-process-env, no-magic-numbers */
const PORT = process.env.PORT || 80; // Let Heroku set the port.
Expand Down Expand Up @@ -64,8 +64,8 @@ const bootstrap = ( options = {}) => {
}; // Bootstrap.

// If module was called directly, bootstrap now.
if ( require.main === module ) {
if (process.argv[1] === fileURLToPath(import.meta.url)) {
bootstrap();
}

module.exports = bootstrap;
export default bootstrap;
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "working-plusplus",
"type": "module",
"version": "0.0.5",
"description": "PHP México PlusPlus bot",
"main": "index.js",
Expand All @@ -24,17 +25,19 @@
"handlebars": "^4.7.8",
"lodash.camelcase": "^4.3.0",
"mime": "^4.1.0",
"pg": "^8.16.3"
"mongodb": "^6.9.0"
},
"devDependencies": {
"coveralls": "^3.1.1",
"eslint": "^9.39.2",
"eslint-config-tdmalone": "^0.1.3",
"eslint-plugin-dollar-sign": "^1.0.1",
"eslint-plugin-jest": "^29.12.1",
"esmock": "^2.7.3",
"jest": "^30.2.0",
"jest-chain": "^1.1.6",
"jest-extended": "^7.0.0",
"object-assign-deep": "^0.4.0"
"object-assign-deep": "^0.4.0",
"supertest": "^7.2.2"
}
}
12 changes: 6 additions & 6 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

'use strict';

const events = require( './events' ),
helpers = require( './helpers' ),
leaderboard = require( './leaderboard' );
import events from './events.js';
import helpers from './helpers.js';
import leaderboard from './leaderboard.js';

// eslint-disable-next-line no-process-env
const SLACK_VERIFICATION_TOKEN = process.env.SLACK_VERIFICATION_TOKEN;
Expand Down Expand Up @@ -77,7 +77,7 @@ const validateToken = ( suppliedToken, serverToken ) => {
* @param {express.res} response An Express response. See https://expressjs.com/en/4x/api.html#res.
* @return {void}
*/
const handleGet = async( request, response ) => {
export const handleGet = async( request, response ) => {
logRequest( request );

switch ( request.path.replace( /\/$/, '' ) ) {
Expand Down Expand Up @@ -115,7 +115,7 @@ const handleGet = async( request, response ) => {
* @return {bool|Promise} Either `false` if the event cannot be handled, or a Promise as returned
* by `events.handleEvent()`.
*/
const handlePost = ( request, response ) => {
export const handlePost = ( request, response ) => {
logRequest( request );

// Respond to challenge sent by Slack during event subscription set up.
Expand Down Expand Up @@ -150,7 +150,7 @@ const handlePost = ( request, response ) => {

}; // HandlePost.

module.exports = {
export default {
logRequest,
validateToken,
handleGet,
Expand Down
29 changes: 15 additions & 14 deletions src/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

'use strict';

const slack = require( './slack' ),
points = require( './points' ),
helpers = require( './helpers' ),
messages = require( './messages' ),
operations = require( './operations' ),
leaderboard = require( './leaderboard' );
import slack from './slack.js';
import points from './points.js';
import helpers from './helpers.js';
import messages from './messages.js';
import operationsPkg from './operations.js';
const { operations } = operationsPkg;
import leaderboard from './leaderboard.js';

const camelCase = require( 'lodash.camelcase' );
import camelCase from 'lodash.camelcase';

/**
* Handles an attempt by a user to 'self plus' themselves, which includes both logging the attempt
Expand All @@ -25,7 +26,7 @@ const camelCase = require( 'lodash.camelcase' );
* private channels - aka groups) that the message was sent from.
* @return {Promise} A Promise to send a Slack message back to the requesting channel.
*/
const handleSelfPlus = ( user, channel ) => {
export const handleSelfPlus = ( user, channel ) => {
console.log( user + ' tried to alter their own score.' );
const message = messages.getRandomMessage( operations.operations.SELF, user );
return slack.sendMessage( message, channel );
Expand All @@ -42,7 +43,7 @@ const handleSelfPlus = ( user, channel ) => {
* @return {Promise} A Promise to send a Slack message back to the requesting channel after the
* points have been updated.
*/
const handlePlusMinus = async( item, operation, channel ) => {
export const handlePlusMinus = async( item, operation, channel ) => {
const score = await points.updateScore( item, operation ),
operationName = operations.getOperationName( operation ),
message = messages.getRandomMessage( operationName, item, score );
Expand Down Expand Up @@ -70,7 +71,7 @@ const handlePlusPlusMinusMinus = async( item, channel ) => {
* https://api.slack.com/events/app_mention for details.
* @returns {Promise} A Promise to send the Slack message.
*/
const sayThankyou = ( event ) => {
export const sayThankyou = ( event ) => {

const thankyouMessages = [
'¡Ni lo menciones!',
Expand Down Expand Up @@ -98,7 +99,7 @@ const sayThankyou = ( event ) => {
* https://api.slack.com/events/app_mention for details.
* @returns {Promise} A Promise to send the Slack message.
*/
const sendHelp = ( event ) => {
export const sendHelp = ( event ) => {

const botUserID = helpers.extractUserID( event.text );

Expand All @@ -118,7 +119,7 @@ const sendHelp = ( event ) => {

}; // SendHelp.

const handlers = {
export const handlers = {

/**
* Handles standard incoming 'message' events sent from Slack.
Expand Down Expand Up @@ -206,7 +207,7 @@ const handlers = {
* @return {bool|Promise} Either `false` if the event cannot be handled, or a Promise as returned
* by the event's handler function.
*/
const handleEvent = ( event, request ) => {
export const handleEvent = ( event, request ) => {

// If the event has no type, something has gone wrong.
if ( 'undefined' === typeof event.type ) {
Expand Down Expand Up @@ -241,7 +242,7 @@ const handleEvent = ( event, request ) => {

}; // HandleEvent.

module.exports = {
export default {
handleSelfPlus,
handlePlusMinus,
sayThankyou,
Expand Down
Loading