Skip to content

Commit

Permalink
Include environment authority in the username of slack system messages
Browse files Browse the repository at this point in the history
  • Loading branch information
hakatashi committed Oct 9, 2024
1 parent 9867e7d commit 5914e7d
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 25 deletions.
5 changes: 3 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"run-script",
"test",
"--",
"--colors",
"${input:testFilterArgument}"
],
"internalConsoleOptions": "openOnSessionStart",
Expand All @@ -48,8 +49,8 @@
{
"id": "testFilterArgument",
"type": "promptString",
"description": "実行するユニットテストの正規表現フィルタ",
"default": "**/*.{js,ts}"
"description": "実行するユニットテストの正規表現フィルタ (全部実行する場合は空文字列を入力)",
"default": ""
}
]
}
6 changes: 3 additions & 3 deletions helloworld/HelloWorld.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('helloworld', () => {
it('responds to "Hello"', async () => {
const response = await slack.getResponseTo('Hello');
expect(response).toEqual({
username: 'helloworld [test-hostname]',
username: 'helloworld [TEST_AUTHORITY]',
channel: slack.fakeChannel,
text: 'World!',
});
Expand All @@ -59,7 +59,7 @@ describe('helloworld', () => {

const mockedPostMessage = slack.webClient.chat.postMessage as jest.MockedFunction<typeof slack.webClient.chat.postMessage>;
expect(mockedPostMessage).toBeCalledWith({
username: 'helloworld [test-hostname]',
username: 'helloworld [TEST_AUTHORITY]',
channel: slack.fakeChannel,
text: 'Hello, World!',
blocks: [
Expand Down Expand Up @@ -113,7 +113,7 @@ describe('helloworld', () => {
elements: [
{
type: 'plain_text',
text: '⚠この値は再起動後も保存されます。前回このメッセージを投稿してから60分以上経っている場合はメッセージが再投稿されますが、以前のメッセージの数字は更新されなくなります。ボタンを押すとエラーが出る場合は、「Slackbotを作ろう」ページの「WebSocketトンネルをセットアップする」などを参考に Event API のセットアップが正常にできているかもう一度確認してください。',
text: '⚠この値は再起動後も保存されます。前回このメッセージを投稿してから60分以上経っている場合は、以前のメッセージが削除され再投稿されます。ボタンを押すとエラーが出る場合は、「Slackbotを作ろう」ページの「WebSocketトンネルをセットアップする」などを参考に Event API のセットアップが正常にできているかもう一度確認してください。',
emoji: true,
},
],
Expand Down
7 changes: 4 additions & 3 deletions helloworld/HelloWorld.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import {randomUUID} from 'crypto';
import type EventEmitter from 'events';
import os from 'os';
import type {BlockAction, ViewSubmitAction} from '@slack/bolt';
import type {SlackMessageAdapter} from '@slack/interactive-messages';
import type {MessageEvent, WebClient} from '@slack/web-api';
import {Mutex} from 'async-mutex';
import logger from '../lib/logger';
import type {SlackInterface} from '../lib/slack';
import {extractMessage} from '../lib/slackUtils';
import {extractMessage, getAuthorityLabel} from '../lib/slackUtils';
import State from '../lib/state';
import counterEditDialog from './views/counterEditDialog';
import helloWorldMessage from './views/helloWorldMessage';
Expand All @@ -33,6 +32,8 @@ export class HelloWorld {

#SANDBOX_ID = process.env.CHANNEL_SANDBOX ?? '';

#AUTHORITY = getAuthorityLabel();

// インスタンスを生成するためのファクトリメソッド
static async create(slack: SlackInterface) {
log.info('Creating helloworld bot instance');
Expand Down Expand Up @@ -105,7 +106,7 @@ export class HelloWorld {
}

private get username() {
return `helloworld [${os.hostname()}]`;
return `helloworld [${this.#AUTHORITY}]`;
}

// 「Hello, World!」メッセージを#sandboxに送信する
Expand Down
7 changes: 4 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import dotenv from 'dotenv';
dotenv.config();

import Fastify from 'fastify';
import os from 'os';
import qs from 'querystring';
import { eventClient, messageClient, tsgEventClient, webClient } from './lib/slack';

Expand All @@ -23,6 +22,7 @@ import { throttle, uniq } from 'lodash';
import { RequestHandler } from 'express-serve-static-core';
import { inspect } from 'util';
import concat from 'concat-stream';
import { getAuthorityLabel } from './lib/slackUtils';

const log = logger.child({ bot: 'index' });

Expand Down Expand Up @@ -187,9 +187,10 @@ eventClient.on('error', (error) => {
fastify.use('/slack-message', messageClient.requestListener());

const loadedPlugins = new Set<string>();
const authority = getAuthorityLabel();

const initializationMessage = await webClient.chat.postMessage({
username: `tsgbot [${os.hostname()}]`,
username: `tsgbot [${authority}]`,
channel: process.env.CHANNEL_SANDBOX,
text: `起動中⋯⋯ (${loadedPlugins.size}/${plugins.length})`,
attachments: plugins.map((name) => ({
Expand Down Expand Up @@ -246,7 +247,7 @@ eventClient.on('error', (error) => {

log.info('Launched');
webClient.chat.postMessage({
username: `tsgbot [${os.hostname()}]`,
username: `tsgbot [${authority}]`,
channel: process.env.CHANNEL_SANDBOX,
text: argv.startup,
});
Expand Down
8 changes: 6 additions & 2 deletions lib/__mocks__/slackUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env node, jest */
import {MrkdwnElement, PlainTextElement} from '@slack/web-api';
import type {MessageEvent} from '@slack/bolt';
import { MrkdwnElement, PlainTextElement } from '@slack/web-api';
import type { MessageEvent } from '@slack/bolt';

export const getMemberName = jest.fn(async () => 'Dummy User');
export const getMemberIcon = jest.fn(async () => 'https://example.com/dummy.png');
Expand All @@ -18,4 +18,8 @@ export const mrkdwn = (text: string): MrkdwnElement => ({

export const extractMessage = (message: MessageEvent) => {
return message;
};

export const getAuthorityLabel = () => {
return 'TEST_AUTHORITY';
};
38 changes: 27 additions & 11 deletions lib/slackUtils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type {MrkdwnElement, PlainTextElement} from '@slack/web-api';
import type {Member} from '@slack/web-api/dist/types/response/UsersListResponse';
import {WebClient} from '@slack/web-api';
import {eventClient, getTokens} from './slack';
import {Deferred} from './utils';
import type { MrkdwnElement, PlainTextElement } from '@slack/web-api';
import type { Member } from '@slack/web-api/dist/types/response/UsersListResponse';
import { WebClient } from '@slack/web-api';
import { eventClient, getTokens } from './slack';
import { Deferred } from './utils';
import SlackCache from './slackCache';
import type {GenericMessageEvent, MessageEvent} from '@slack/bolt';
import type { GenericMessageEvent, MessageEvent } from '@slack/bolt';
import os from 'os';

const slackCaches = new Map<string, SlackCache>();
const initializedSlackCachesDeferred = new Deferred<void>();
Expand Down Expand Up @@ -34,11 +35,11 @@ export const getAllTSGMembers = async (): Promise<Array<Member>> => {
return await slackCaches.get(process.env.TEAM_ID!)!.getUsers();
};

export const getMemberName = async (user: string): Promise<string|undefined> => {
export const getMemberName = async (user: string): Promise<string | undefined> => {
await initializedSlackCachesDeferred.promise;

// TODO: receive team_id and use it to choose slackCache
let member: Member|null = null;
let member: Member | null = null;
for (const caches of slackCaches.values()) {
const found = await caches.getUser(user);
if (found) {
Expand All @@ -51,11 +52,11 @@ export const getMemberName = async (user: string): Promise<string|undefined> =>
};

type IconResolution = 24 | 32 | 48 | 72 | 192 | 512;
export const getMemberIcon = async (user: string, res: IconResolution = 24): Promise<string|undefined> => {
export const getMemberIcon = async (user: string, res: IconResolution = 24): Promise<string | undefined> => {
await initializedSlackCachesDeferred.promise;

// TODO: receive team_id and use it to choose slackCache
let member: Member|null = null;
let member: Member | null = null;
for (const caches of slackCaches.values()) {
const found = await caches.getUser(user);
if (found) {
Expand All @@ -82,7 +83,7 @@ export const getMemberIcon = async (user: string, res: IconResolution = 24): Pro
}
};

export const getEmoji = async (name: string, team: string): Promise<string|undefined> => {
export const getEmoji = async (name: string, team: string): Promise<string | undefined> => {
await initializedSlackCachesDeferred.promise;
return slackCaches.get(team)?.getEmoji(name);
};
Expand Down Expand Up @@ -113,4 +114,19 @@ export const extractMessage = (message: MessageEvent) => {
return message.root;
}
return null;
};

export const getAuthorityLabel = () => {
if (process.env.NODE_ENV === 'production') {
return 'production';
}

if (process.env.GITHUB_USER && process.env.CODESPACE_NAME) {
const abbreviatedCodespaceName = process.env.CODESPACE_NAME.split('-')[0] + '-…';
return `Codespaces (@${process.env.GITHUB_USER}): ${abbreviatedCodespaceName}`;
}

const username = process.env.GITHUB_USER || process.env.USER || process.env.USERNAME || os.userInfo()?.username || 'unknown';
const hostname = process.env.CODESPACE_NAME || os.hostname() || 'unknown';
return `${username}@${hostname}`;
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,4 @@
"ts-jest": "^29.0.0",
"ts-node-dev": "^2.0.0"
}
}
}

0 comments on commit 5914e7d

Please sign in to comment.