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
8 changes: 4 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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=
- SLACK_VERIFICATION_TOKEN=xxxxxxxxxxxxxxxxxxxxxxx1
- SLACK_BOT_USER_OAUTH_ACCESS_TOKEN=
- YARN_PRODUCTION=true
- DATABASE_USE_SSL=false
Expand All @@ -17,7 +17,7 @@ services:
- db

db:
image: postgres:10
image: postgres:16
environment:
- POSTGRES_PASSWORD=toor
- POSTGRES_DB=plus
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;
5 changes: 4 additions & 1 deletion 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 Down Expand Up @@ -32,9 +33,11 @@
"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
32 changes: 16 additions & 16 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

'use strict';

const slack = require( './slack' );
import slack from './slack.js';

const fs = require( 'fs' ),
crypto = require( 'crypto' ),
handlebars = require( 'handlebars' );
import fs from 'fs';
import crypto from 'crypto';
import handlebars from 'handlebars';

const templates = {};

Expand All @@ -33,7 +33,7 @@ const ONE_DAY = 60 * 60 * 24, // eslint-disable-line no-magic-numbers
* @param {array} commands The commands to look for.
* @return {string|Boolean} Either the first command found, or false if no commands were found.
*/
const extractCommand = ( message, commands ) => {
export const extractCommand = ( message, commands ) => {

let firstLocation = Number.MAX_SAFE_INTEGER,
firstCommand;
Expand All @@ -60,7 +60,7 @@ const extractCommand = ( message, commands ) => {
* 'operation' being done on it - expressed as a valid mathematical operation
* (i.e. + or -).
*/
const extractPlusMinusEventData = ( text ) => {
export const extractPlusMinusEventData = ( text ) => {
const data = text.match( /@([A-Za-z0-9]+?)>?\s*(-{2}\+{2}|\+{2}-{2}|\+{2}|-{2}|—{1})/ );

if ( ! data ) {
Expand Down Expand Up @@ -97,7 +97,7 @@ const extractPlusMinusEventData = ( text ) => {
* could not be found.
* @see ::isUser
*/
const extractUserID = ( text ) => {
export const extractUserID = ( text ) => {
const match = text.match( /U[A-Z0-9]{8}/ );
return match ? match[0] : '';
};
Expand All @@ -108,7 +108,7 @@ const extractUserID = ( text ) => {
* @param {string} ts A timestamp to hash into the token.
* @returns {string} A token, that can be re-checked later using the same timestamp.
*/
const getTimeBasedToken = ( ts ) => {
export const getTimeBasedToken = ( ts ) => {

if ( ! ts ) {
throw Error( 'Timestamp not provided when getting time-based token.' );
Expand All @@ -125,7 +125,7 @@ const getTimeBasedToken = ( ts ) => {
*
* @returns {integer} The current Unix timestamp.
*/
const getTimestamp = () => {
export const getTimestamp = () => {
return Math.floor( Date.now() / MILLISECONDS_TO_SECONDS );
};

Expand All @@ -135,7 +135,7 @@ const getTimestamp = () => {
* @param {integer} number The number in question.
* @returns {Boolean} Whether or not the number is a plural.
*/
const isPlural = ( number ) => {
export const isPlural = ( number ) => {
return 1 !== Math.abs( number );
};

Expand All @@ -147,7 +147,7 @@ const isPlural = ( number ) => {
* @param {integer} ts The timestamp the token was supplied with.
* @returns {boolean} Whether or not the token is valid.
*/
const isTimeBasedTokenStillValid = ( token, ts ) => {
export const isTimeBasedTokenStillValid = ( token, ts ) => {
const now = getTimestamp();

// Don't support tokens too far from the past.
Expand Down Expand Up @@ -176,7 +176,7 @@ const isTimeBasedTokenStillValid = ( token, ts ) => {
* @returns {Boolean} Whether or not the string is a Slack user ID.
* @see ::extractUserID()
*/
const isUser = ( item ) => {
export const isUser = ( item ) => {
return item.match( /U[A-Z0-9]{8}/ ) ? true : false;
};

Expand All @@ -188,7 +188,7 @@ const isUser = ( item ) => {
* @return {string} The item linked with Slack mrkdwn
* @see https://api.slack.com/docs/message-formatting#linking_to_channels_and_users
*/
const maybeLinkItem = ( item ) => {
export const maybeLinkItem = ( item ) => {
return isUser( item ) ? '<@' + item + '>' : item;
};

Expand All @@ -200,7 +200,7 @@ const maybeLinkItem = ( item ) => {
* @return {int} item
* The id.
*/
const returnAsId = ( item ) => {
export const returnAsId = ( item ) => {
return item;
};

Expand All @@ -222,7 +222,7 @@ const returnAsId = ( item ) => {
* @returns {string} HTML ready to be rendered in the browser.
* @see https://handlebarsjs.com/
*/
const render = async( templatePath, context = {}, request = {}) => {
export const render = async( templatePath, context = {}, request = {}) => {

// Retrieve the header and footer HTML, if we don't already have it in memory.
if ( ! templates.header ) templates.header = fs.readFileSync( 'src/html/header.html', 'utf8' );
Expand All @@ -249,7 +249,7 @@ const render = async( templatePath, context = {}, request = {}) => {

}; // Render.

module.exports = {
export default {
extractCommand,
extractPlusMinusEventData,
extractUserID,
Expand Down
Loading