Skip to content

Commit

Permalink
Port everything to ESM and Thesis hubot fork
Browse files Browse the repository at this point in the history
This fork moves TypeScript and CoffeeScript support directly into Hubot
itself, as well as moving the whole system to ES Modules. Some
experimental flags are required for this, but the hubot package takes
care of that, while valkyrie can focus on leveraging the advantages.

The changes are mostly around using `export default` instead of
`module.exports`, using file extensions in non-package-name imports, and
importing certain modules as defaults instead of using `* as`.
  • Loading branch information
Shadowfiend committed Mar 6, 2023
1 parent 7bad383 commit 3ad08ca
Show file tree
Hide file tree
Showing 27 changed files with 708 additions and 466 deletions.
10 changes: 5 additions & 5 deletions lib/Job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import * as scheduler from "node-schedule"
import * as hubot from "hubot"
import { MatrixMessage } from "hubot-matrix"
import util from "util"
import processTemplateString from "./template-strings"
import CONFIG, { RECURRING_JOB_STORAGE_KEY } from "./schedule-config"
import { isMatrixAdapter } from "./adapter-util"
import { JobUser, MessageMetadata, ScheduledJob } from "./scheduled-jobs"
import processTemplateString from "./template-strings.ts"
import CONFIG, { RECURRING_JOB_STORAGE_KEY } from "./schedule-config.ts"
import { isMatrixAdapter } from "./adapter-util.ts"
import { JobUser, MessageMetadata, ScheduledJob } from "./scheduled-jobs.ts"
import {
isCronPattern,
logSerializedJobDetails,
updateJobInBrain,
urlFor,
} from "./schedule-util"
} from "./schedule-util.ts"

export async function postMessageAndSaveThreadId(
robot: hubot.Robot,
Expand Down
9 changes: 1 addition & 8 deletions lib/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Provides a collection of configuration validation helpers

import { Adapter } from "hubot"
import { getRoomInfoFromIdOrName, isRoomNameValid } from "./adapter-util"
import { getRoomInfoFromIdOrName, isRoomNameValid } from "./adapter-util.ts"

/**
* Given a config key and an issueReporter:
Expand Down Expand Up @@ -113,10 +113,3 @@ export function issueReporterForRobot(
logOrThrow(robot, errorMessage)
}
}

module.exports = {
withConfigOrReportIssues,
fetchRoomInfoOrReportIssue,
fetchConfigOrReportIssue,
issueReporterForRobot,
}
4 changes: 2 additions & 2 deletions lib/remind/formatting.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DateTime } from "luxon"
import { encodeThreadId, matrixUrlFor } from "../adapter-util"
import { PersistedJob, RecurringDefinition } from "./data"
import { encodeThreadId, matrixUrlFor } from "../adapter-util.ts"
import { PersistedJob, RecurringDefinition } from "./data.ts"

// List of escape regex patterns (search to replace) to use when formatting a
// reminder message for display. Used to show raw input and avoid tagging
Expand Down
6 changes: 3 additions & 3 deletions lib/remind/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Envelope, Message, User } from "hubot"
import { DateTime } from "luxon"
import { sendThreaded } from "../adapter-util"
import { sendThreaded } from "../adapter-util.ts"
import {
Job,
PersistedJob,
RecurringDefinition,
SingleShotDefinition,
} from "./data"
import { parseFromString, parseSpec as parseJobSpec } from "./parsing"
} from "./data.ts"
import { parseFromString, parseSpec as parseJobSpec } from "./parsing.ts"

/**
* Given the previous recurrence as an ISO-8601 date and a recurring or
Expand Down
10 changes: 5 additions & 5 deletions lib/schedule-management.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import * as hubot from "hubot"
import { getRoomNameFromId } from "./adapter-util"
import Job from "./Job"
import { getRoomNameFromId } from "./adapter-util.ts"
import Job from "./Job.ts"
import {
CONFIG,
formatJobForMessage,
isCronPattern,
isRestrictedRoom,
logSerializedJobDetails,
updateJobInBrain,
} from "./schedule-util"
} from "./schedule-util.ts"
import {
JobUser,
MessageMetadata,
ScheduledJob,
ScheduledJobMap,
} from "./scheduled-jobs"
import processTemplateString from "./template-strings"
} from "./scheduled-jobs.ts"
import processTemplateString from "./template-strings.ts"

const JOB_MAX_COUNT = 10000

Expand Down
6 changes: 3 additions & 3 deletions lib/schedule-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ import {
getRoomNameFromId,
encodeThreadId,
matrixUrlFor,
} from "./adapter-util"
import CONFIG, { RECURRING_JOB_STORAGE_KEY } from "./schedule-config"
} from "./adapter-util.ts"
import CONFIG, { RECURRING_JOB_STORAGE_KEY } from "./schedule-config.ts"
import {
MessageMetadata,
ScheduledJob,
ScheduledJobMap,
} from "./scheduled-jobs"
} from "./scheduled-jobs.ts"

function urlFor(roomId: string, serverName: string, eventId: string): string {
return matrixUrlFor(roomId, serverName, eventId)
Expand Down
4 changes: 2 additions & 2 deletions lib/template-strings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Adapter, Brain } from "hubot"
import { RECURRING_JOB_STORAGE_KEY } from "./schedule-config"
import { ScheduledJob } from "./scheduled-jobs"
import { RECURRING_JOB_STORAGE_KEY } from "./schedule-config.ts"
import { ScheduledJob } from "./scheduled-jobs.ts"

// This is a temporary function to test the template string behavior.
// It takes a string, coverts it to an int, and returns its square.
Expand Down
6 changes: 3 additions & 3 deletions lib/zoom/Session.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as moment from "moment"
import Account from "./account"
import { tokenFrom } from "./password"
import { User } from "./user"
import Account from "./account.ts"
import { tokenFrom } from "./password.ts"
import { User } from "./user.ts"

export default class Session {
constructor(
Expand Down
8 changes: 4 additions & 4 deletions lib/zoom/account.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as axios from "axios"
import URLs from "./urls"
import { Meeting, MeetingScheduleCategory } from "./meeting"
import { generateZoomPassword, tokenFrom } from "./password"
import { UserType } from "./user"
import URLs from "./urls.ts"
import { Meeting, MeetingScheduleCategory } from "./meeting.ts"
import { generateZoomPassword, tokenFrom } from "./password.ts"
import { UserType } from "./user.ts"

export default class Account {
constructor(
Expand Down
6 changes: 3 additions & 3 deletions lib/zoom/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as axios from "axios"
import * as util from "util"
import { tokenFrom } from "./password"
import Session from "./Session"
import URLs from "./urls"
import { tokenFrom } from "./password.ts"
import Session from "./Session.ts"
import URLs from "./urls.ts"

async function getSession(
apiKey: string,
Expand Down
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
"dependencies": {
"axios": "^1.1.3",
"canvas": "^2.11.0",
"coffeescript": "^1.12.3",
"coffeescript": "^2",
"cookie-parser": "^1.4.3",
"cron-parser": "^2.7.3",
"cronstrue": "^1.68.0",
"decode-html": "^2.0.0",
"github-api": "^3.4.0",
"hubot": "^3.3.2",
"hubot": "git+https://github.com/thesis/hubot.git#v4.0.0-alpha.0",
"hubot-diagnostics": "^1.0.0",
"hubot-even-better-help": "git+https://github.com/thesis/hubot-even-better-help.git#3192eedf2a0f11dd0b5fa736e0b9033741a59050",
"hubot-gif-locker": "^1.0.7",
"hubot-matrix": "git+https://github.com/thesis/hubot-matrix.git#v3.0.0-beta.4",
"hubot-matrix": "git+https://github.com/thesis/hubot-matrix.git#v3.0.0-beta.5",
"hubot-redis-brain": "^1.0.0",
"hubot-rules": "^0.1.2",
"hubot-scripts": "^2.17.2",
Expand All @@ -31,31 +31,31 @@
"node-schedule": "1.3.2",
"passport": "0.6.0",
"passport-github2": "0.1.12",
"typescript": "~4.7.0",
"typescript": "^4.9.4",
"uuid": "^3.2.1"
},
"engines": {
"node": "<=19 >=16"
},
"devDependencies": {
"@jest/globals": "^29.3.1",
"@thesis-co/eslint-config": "^0.3.0",
"@thesis-co/eslint-config": "^0.4.0",
"@types/cookie": "^0.3.1",
"@types/hubot": "^3.3.1",
"@types/jsonwebtoken": "~7.2.8",
"@types/luxon": "^3.2.0",
"@types/mocha": "^10.0.0",
"@types/node": "^18.11.7",
"@types/node-schedule": "^2.1.0",
"@typescript-eslint/eslint-plugin": "^5.40.1",
"@typescript-eslint/parser": "^5.40.1",
"eslint": "^8.25.0",
"@typescript-eslint/eslint-plugin": "^5.54.0",
"@typescript-eslint/parser": "^5.54.0",
"eslint": "^8.35.0",
"hubot-chat-testing": "git+https://github.com/thesis/hubot-chat-testing.git#v2.0.0-beta.1",
"hubot-test-helper": "^1.9.0",
"jest": "^29.3.1",
"mocha": "10.0.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.7.1",
"prettier": "^2.8.4",
"ts-jest": "^29.0.3",
"ts-node": "^10.9.1"
},
Expand Down
2 changes: 1 addition & 1 deletion scripts/_alias-help-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import { Robot, TextMessage } from "hubot"

module.exports = function aliasHelpCommand(robot: Robot<any>) {
export default function aliasHelpCommand(robot: Robot<any>) {
robot.receiveMiddleware((context, next, done) => {
const robotRespondPatternInText = robot
.respondPattern(new RegExp(""))
Expand Down
29 changes: 0 additions & 29 deletions scripts/_typescript.js

This file was deleted.

4 changes: 2 additions & 2 deletions scripts/access-limit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// kb0rg

import { Robot } from "hubot"
import { getRoomNameFromId } from "../lib/adapter-util"
import { getRoomNameFromId } from "../lib/adapter-util.ts"

const ALLOWED_ROOMS = [
"Bifrost",
Expand All @@ -31,7 +31,7 @@ const ALLOWED_BOTS = ["valkyrie"]
const BOT_RESTICTED_COMMANDS = ["reload-scripts.reload"] // string that matches the listener ID
const ROOM_RESTRICTED_COMMANDS = ["pod-bay-doors", "users", "reconnect"] // string that matches the listener ID

module.exports = function setUpAccessLimit(robot: Robot) {
export default function setUpAccessLimit(robot: Robot) {
robot.listenerMiddleware((context, next, done) => {
if (
BOT_RESTICTED_COMMANDS.indexOf(
Expand Down
14 changes: 10 additions & 4 deletions scripts/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
import { Robot } from "hubot"
import { MatrixEvent, EventType, RoomMemberEvent } from "matrix-js-sdk"
import * as hubot from "hubot"
import { isMatrixAdapter } from "../lib/adapter-util"
import { generateAvatar, roomNameToAlias } from "../lib/matrix-room-utils"
import { isMatrixAdapter } from "../lib/adapter-util.ts"
import { generateAvatar, roomNameToAlias } from "../lib/matrix-room-utils.ts"

const SPACE_BASE_COLORS: { [spaceName: string]: string } = {
Thesis: "#000000",
"Thesis Studio": "#000000",
Keep: "#49DBB4",
"Tally Ho": "#EE9C32",
Fold: "#FFCF30",
Expand All @@ -28,6 +29,7 @@ const SPACE_BASE_COLORS: { [spaceName: string]: string } = {

const SPACE_IDS: { [spaceName: string]: string } = {
Thesis: "!outFXRZStxHJasvWKL:thesis.co",
"Thesis Studio": "!VRGYJeUwuhkMmZPcpX:thesis.co",
Keep: "!YDpOcIsEpQabwiHpdV:thesis.co",
"Tally Ho": "!wCfAwzfZOUHTYIDjRn:thesis.co",
Fold: "!SuBAnawNxcIXoCHfPM:thesis.co",
Expand All @@ -53,6 +55,8 @@ const ADMIN_USERS = [
const SPACE_ADMINS: { [spaceRoomId: string]: string[] } = {
// Thesis* space.
"!outFXRZStxHJasvWKL:thesis.co": [],
// Thesis* Studio space.
"!VRGYJeUwuhkMmZPcpX:thesis.co": [],
// Keep space.
"!YDpOcIsEpQabwiHpdV:thesis.co": ["@piotr.dyraga:thesis.co"],
// Tally Ho space.
Expand All @@ -70,7 +74,7 @@ const SPACE_ADMINS: { [spaceRoomId: string]: string[] } = {
"!XEnwlDoWvSBvrloDVH:thesis.co": ["@anna:thesis.co"],
}

module.exports = (robot: Robot<any>) => {
export default (robot: Robot<any>) => {
robot.respond(/users/i, (response) => {
response.reply(
`\n${Object.values(robot.brain.users())
Expand Down Expand Up @@ -221,7 +225,9 @@ module.exports = (robot: Robot<any>) => {

const nearestParentId = parentRoomIds.at(-1)
const prefixParentSpaceName =
nearestParentId !== undefined && nearestParentId !== SPACE_IDS.Thesis
nearestParentId !== undefined &&
nearestParentId !== SPACE_IDS.Thesis &&
nearestParentId !== SPACE_IDS["Thesis Studio"]
? SPACE_NAMES[nearestParentId]
: undefined

Expand Down
11 changes: 9 additions & 2 deletions scripts/current-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@
// Author:
// shadowfiend

import { dirname } from "path"
import { fileURLToPath } from "url"

import * as fs from "fs"
import { Robot } from "hubot"
import { isRoomNameValid } from "../lib/adapter-util"
import { isRoomNameValid } from "../lib/adapter-util.ts"

let buildNumberBuffer = Buffer.from("")
try {
// Replacement for global __dirname constant in CJS modules.
// eslint-disable-next-line @typescript-eslint/naming-convention, no-underscore-dangle
const __dirname = dirname(fileURLToPath(import.meta.url))

buildNumberBuffer = fs.readFileSync(`${__dirname}/../BUILD`)
} catch (e) {
console.error(`Error reading buildNumber file: ${e}`)
Expand All @@ -32,7 +39,7 @@ function sendReleaseNotification(robot: Robot) {
}
}

module.exports = function setUpCurrentBuild(robot: Robot) {
export default function setUpCurrentBuild(robot: Robot) {
sendReleaseNotification(robot)

robot.respond(/current build/, (response) =>
Expand Down
9 changes: 6 additions & 3 deletions scripts/github-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
// hubot github auth - returns a URL where you can identify your GitHub self to the hubot. Upon identification, if a pending addition request exists from a call to `github add user`, it will be executed.

import * as passport from "passport"
import * as UUIDV4 from "uuid/v4"
import * as UUIDV4 from "uuid/v4.js"
import * as cookieParser from "cookie-parser"
import { Strategy as GitHubStrategy } from "passport-github2"
import { Robot } from "hubot"
import { VerifyCallback } from "jsonwebtoken"
import { withConfigOrReportIssues, issueReporterForRobot } from "../lib/config"
import {
withConfigOrReportIssues,
issueReporterForRobot,
} from "../lib/config.ts"

const HOST = process.env.HUBOT_HOST
const SECOND = 1000
Expand All @@ -27,7 +30,7 @@ const MINUTE = 60 * SECOND
const PENDING_GITHUB_TOKENS_KEY = "pendingGitHubTokens"
const GITHUB_TOKENS_KEY = "gitHubTokens"

export = function setupGitHubAuth(robot: Robot) {
export default function setupGitHubAuth(robot: Robot) {
withConfigOrReportIssues(
issueReporterForRobot(robot),
"GITHUB_CLIENT_ID",
Expand Down
4 changes: 2 additions & 2 deletions scripts/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function apiFor(bot: Robot, user: User) {
return api
}

module.exports = function setupGitHub(robot: Robot) {
export default function setupGitHub(robot: Robot) {
robot.respond(/github who am (i|I)/, (res) => {
const api = apiFor(robot, res.message.user)

Expand Down Expand Up @@ -108,7 +108,7 @@ module.exports = function setupGitHub(robot: Robot) {
return { successes, failures }
},
{
successes: [] as Exclude<typeof teamResponses[0], string>[],
successes: [] as Exclude<(typeof teamResponses)[0], string>[],
failures: [] as typeof teamResponses,
},
)
Expand Down
Loading

0 comments on commit 3ad08ca

Please sign in to comment.