Skip to content

Commit 9aeb34d

Browse files
authored
Updates to support 0.7.1-element.14 (#1081)
* Updates to support 0.7.1-element.14 * Cleanup * fix bot spec * never * Update express to match bot-sdk
1 parent 092a13d commit 9aeb34d

File tree

10 files changed

+151
-83
lines changed

10 files changed

+151
-83
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@
5656
"clsx": "^2.1.1",
5757
"cors": "^2.8.5",
5858
"date-fns": "^4.1.0",
59-
"express": "^4.20.0",
59+
"express": "^4.21.2",
6060
"figma-js": "^1.16.1-0",
6161
"helmet": "^7.1.0",
6262
"http-status-codes": "^2.2.0",
6363
"ioredis": "^5.6.1",
6464
"jira-client": "^8.2.2",
6565
"markdown-it": "^14.0.0",
6666
"matrix-appservice-bridge": "^10.3.3",
67-
"matrix-bot-sdk": "npm:@vector-im/[email protected].11",
67+
"matrix-bot-sdk": "npm:@vector-im/[email protected].14",
6868
"matrix-widget-api": "^1.10.0",
6969
"micromatch": "^4.0.8",
7070
"mime": "^4.0.4",
@@ -99,7 +99,7 @@
9999
"@types/busboy": "^1.5.4",
100100
"@types/chai": "^4.2.22",
101101
"@types/cors": "^2.8.12",
102-
"@types/express": "^4.17.14",
102+
"@types/express": "^4.17.23",
103103
"@types/jira-client": "^7.1.0",
104104
"@types/markdown-it": "^13.0.7",
105105
"@types/micromatch": "^4.0.1",

src/BotCommands.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { CommandError } from "./Errors";
44
import { MatrixEvent, MatrixMessageContent } from "./MatrixEvent";
55
import { BridgePermissionLevel } from "./config/Config";
66
import { PermissionCheckFn } from "./Connections";
7+
import { RoomEvent } from "matrix-bot-sdk";
8+
import { IJsonType } from "matrix-bot-sdk/lib/helpers/Types";
79

810
const stringArgv = import("string-argv");
911
const md = new markdown();
@@ -76,12 +78,12 @@ type BotCommandFunctionWithUserId = (
7678
...args: string[]
7779
) => Promise<BotCommandResult>;
7880
type BotCommandFunctionWithReply = (
79-
reply?: MatrixEvent<unknown>,
81+
reply?: RoomEvent<IJsonType>,
8082
...args: string[]
8183
) => Promise<BotCommandResult>;
8284
type BotCommandFunctionWithUserIdAndReply = (
8385
userId: string,
84-
reply?: MatrixEvent<unknown>,
86+
reply?: RoomEvent<IJsonType>,
8587
...args: string[]
8688
) => Promise<BotCommandResult>;
8789
type BotCommandFunctionStandard = (
@@ -195,7 +197,7 @@ interface CommandResultErrorHuman {
195197
export async function handleCommand(
196198
userId: string,
197199
command: string,
198-
parentEvent: MatrixEvent<unknown> | undefined,
200+
parentEvent: RoomEvent<IJsonType> | undefined,
199201
botCommands: BotCommands,
200202
obj: unknown,
201203
permissionCheckFn: PermissionCheckFn,

src/Bridge.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
PowerLevelsEvent,
1313
Intent,
1414
MatrixError,
15+
RoomEvent,
1516
} from "matrix-bot-sdk";
1617
import BotUsersManager from "./managers/BotUsersManager";
1718
import {
@@ -108,6 +109,7 @@ import { HoundReader } from "./hound/HoundReader";
108109
import { OpenProjectWebhookPayloadWorkPackage } from "./openproject/Types";
109110
import { OpenProjectConnection } from "./Connections/OpenProjectConnection";
110111
import { OAuthRequest, OAuthRequestResult } from "./tokens/Oauth";
112+
import { IJsonType } from "matrix-bot-sdk/lib/helpers/Types";
111113

112114
const log = new Logger("Bridge");
113115

@@ -1352,7 +1354,7 @@ export class Bridge {
13521354
);
13531355
log.debug("Content:", JSON.stringify(event));
13541356

1355-
let replyEvent: MatrixEvent<unknown> | undefined;
1357+
let replyEvent: RoomEvent<IJsonType> | undefined;
13561358
if (event.content["m.relates_to"]?.["m.in_reply_to"]) {
13571359
if (event.content.formatted_body?.includes("<mx-reply>")) {
13581360
// This is a legacy fallback reply:
@@ -1402,7 +1404,7 @@ export class Bridge {
14021404
handled = await connection.onMessageEvent(
14031405
event,
14041406
checkPermission,
1405-
replyEvent,
1407+
replyEvent?.raw,
14061408
);
14071409
}
14081410
} catch (ex) {
@@ -1475,7 +1477,7 @@ export class Bridge {
14751477

14761478
if (replyEvent) {
14771479
log.info(
1478-
`Handling reply to ${replyEvent.event_id} for ${adminRoom.userId}`,
1480+
`Handling reply to ${replyEvent.eventId} for ${adminRoom.userId}`,
14791481
);
14801482
// This might be a reply to a notification
14811483
try {
@@ -1497,7 +1499,7 @@ export class Bridge {
14971499
await Promise.all(
14981500
connections.map(async (c) => {
14991501
if (c instanceof GitHubIssueConnection) {
1500-
return c.onMatrixIssueComment(replyEvent as any);
1502+
return c.onMatrixIssueComment(replyEvent.raw);
15011503
}
15021504
}),
15031505
);

src/Connections/CommandConnection.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@ import {
55
HelpFunction,
66
} from "../BotCommands";
77
import { Logger } from "matrix-appservice-bridge";
8-
import { IRichReplyMetadata, MatrixClient, MessageEvent } from "matrix-bot-sdk";
8+
import {
9+
IRichReplyMetadata,
10+
MatrixClient,
11+
MessageEvent,
12+
RoomEvent,
13+
} from "matrix-bot-sdk";
914
import { MatrixMessageContent, MatrixEvent } from "../MatrixEvent";
1015
import { BaseConnection } from "./BaseConnection";
1116
import { IConnectionState, PermissionCheckFn } from ".";
17+
import { IJsonType } from "matrix-bot-sdk/lib/helpers/Types";
1218
const log = new Logger("CommandConnection");
1319

1420
/**
@@ -54,7 +60,7 @@ export abstract class CommandConnection<
5460
public async onMessageEvent(
5561
ev: MatrixEvent<MatrixMessageContent>,
5662
checkPermission: PermissionCheckFn,
57-
parentEvent?: MatrixEvent<unknown>,
63+
parentEvent?: RoomEvent<IJsonType>,
5864
) {
5965
const commandResult = await handleCommand(
6066
ev.sender,

src/Connections/GithubRepo.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
Appservice,
33
Intent,
44
IRichReplyMetadata,
5+
RoomEvent,
56
StateEvent,
67
} from "matrix-bot-sdk";
78
import {
@@ -60,10 +61,16 @@ import { GitHubIssueConnection } from "./GithubIssue";
6061
import { BridgeConfigGitHub } from "../config/Config";
6162
import { ApiError, ErrCode, ValidatorApiError } from "../api";
6263
import { PermissionCheckFn } from ".";
63-
import { GitHubRepoMessageBody, MinimalGitHubIssue } from "../libRs";
64+
import {
65+
GitHubIssueMessageBodyIssue,
66+
GitHubIssueMessageBodyRepo,
67+
GitHubRepoMessageBody,
68+
MinimalGitHubIssue,
69+
} from "../libRs";
6470
import Ajv, { JSONSchemaType } from "ajv";
6571
import { HookFilter } from "../HookFilter";
6672
import { GitHubGrantChecker } from "../github/GrantChecker";
73+
import { IJsonType } from "matrix-bot-sdk/lib/helpers/Types";
6774

6875
const log = new Logger("GitHubRepoConnection");
6976
const md = new markdown();
@@ -849,18 +856,21 @@ export class GitHubRepoConnection
849856
public async onMessageEvent(
850857
ev: MatrixEvent<MatrixMessageContent>,
851858
checkPermission: PermissionCheckFn,
852-
reply?: MatrixEvent<any>,
859+
reply?: RoomEvent<IJsonType>,
853860
): Promise<boolean> {
854861
if (await super.onMessageEvent(ev, checkPermission)) {
855862
return true;
856863
}
857864
const body = ev.content.body?.trim();
858865
if (reply) {
859-
const repoInfo =
860-
reply.content["uk.half-shot.matrix-hookshot.github.repo"];
861-
const pullRequestId =
862-
reply.content["uk.half-shot.matrix-hookshot.github.pull_request"]
863-
?.number;
866+
const repoInfo = reply.content[
867+
"uk.half-shot.matrix-hookshot.github.repo"
868+
] as unknown as GitHubIssueMessageBodyRepo;
869+
const pullRequestId = (
870+
reply.content[
871+
"uk.half-shot.matrix-hookshot.github.pull_request"
872+
] as unknown as GitHubIssueMessageBodyIssue
873+
)?.number;
864874
// Emojis can be multi-byte, so make sure we split properly
865875
const reviewKey = Object.keys(EMOJI_TO_REVIEW_STATE).find((k) =>
866876
k.includes(body.split(" ")[0]),
@@ -1699,8 +1709,9 @@ export class GitHubRepoConnection
16991709
this.roomId,
17001710
event_id,
17011711
);
1702-
const issueContent =
1703-
ev.content["uk.half-shot.matrix-hookshot.github.issue"];
1712+
const issueContent = ev.content[
1713+
"uk.half-shot.matrix-hookshot.github.issue"
1714+
] as GitHubIssueMessageBodyIssue | undefined;
17041715
if (!issueContent) {
17051716
log.debug("Reaction to event did not pertain to a issue");
17061717
return; // Not our event.

src/Connections/IConnection.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
Appservice,
66
Intent,
77
IRichReplyMetadata,
8+
RoomEvent,
89
StateEvent,
910
} from "matrix-bot-sdk";
1011
import { BridgeConfig, BridgePermissionLevel } from "../config/Config";
@@ -14,6 +15,7 @@ import { MessageSenderClient } from "../MatrixSender";
1415
import { IBridgeStorageProvider } from "../stores/StorageProvider";
1516
import { GithubInstance } from "../github/GithubInstance";
1617
import "reflect-metadata";
18+
import { IJsonType } from "matrix-bot-sdk/lib/helpers/Types";
1719

1820
export type PermissionCheckFn = (
1921
service: string,
@@ -66,7 +68,7 @@ export interface IConnection {
6668
onMessageEvent?: (
6769
ev: MatrixEvent<MatrixMessageContent>,
6870
checkPermission: PermissionCheckFn,
69-
parentEvent?: MatrixEvent<unknown>,
71+
parentEvent?: RoomEvent<IJsonType>,
7072
) => Promise<boolean>;
7173

7274
onIssueCreated?: (ev: IssuesOpenedEvent) => Promise<void>;

src/Connections/JiraProject.ts

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -76,44 +76,6 @@ export interface JiraTargetFilter {
7676
export type JiraProjectResponseItem =
7777
GetConnectionsResponseItem<JiraProjectConnectionState>;
7878

79-
function validateJiraConnectionState(
80-
state: unknown,
81-
): JiraProjectConnectionState {
82-
const { id, url, commandPrefix, priority } =
83-
state as Partial<JiraProjectConnectionState>;
84-
if (id !== undefined && typeof id !== "string") {
85-
throw new ApiError("Expected 'id' to be a string", ErrCode.BadValue);
86-
}
87-
if (url === undefined) {
88-
throw new ApiError("Expected a 'url' property", ErrCode.BadValue);
89-
}
90-
if (commandPrefix) {
91-
if (typeof commandPrefix !== "string") {
92-
throw new ApiError(
93-
"Expected 'commandPrefix' to be a string",
94-
ErrCode.BadValue,
95-
);
96-
}
97-
if (commandPrefix.length < 2 || commandPrefix.length > 24) {
98-
throw new ApiError(
99-
"Expected 'commandPrefix' to be between 2-24 characters",
100-
ErrCode.BadValue,
101-
);
102-
}
103-
}
104-
let { events } = state as Partial<JiraProjectConnectionState>;
105-
if (!events || (events[0] as string) == "issue.created") {
106-
// migration
107-
events = ["issue_created"];
108-
} else if (events.find((ev) => !JiraAllowedEvents.includes(ev))?.length) {
109-
throw new ApiError(
110-
`'events' can only contain ${JiraAllowedEvents.join(", ")}`,
111-
ErrCode.BadValue,
112-
);
113-
}
114-
return { id, url, commandPrefix, events, priority };
115-
}
116-
11779
const log = new Logger("JiraProjectConnection");
11880
const md = new markdownit();
11981

@@ -138,6 +100,42 @@ export class JiraProjectConnection
138100
static botCommands: BotCommands;
139101
static helpMessage: (cmdPrefix?: string) => MatrixMessageContent;
140102

103+
public static validateState(state: unknown): JiraProjectConnectionState {
104+
const { id, url, commandPrefix, priority } =
105+
state as Partial<JiraProjectConnectionState>;
106+
if (id !== undefined && typeof id !== "string") {
107+
throw new ApiError("Expected 'id' to be a string", ErrCode.BadValue);
108+
}
109+
if (url === undefined) {
110+
throw new ApiError("Expected a 'url' property", ErrCode.BadValue);
111+
}
112+
if (commandPrefix) {
113+
if (typeof commandPrefix !== "string") {
114+
throw new ApiError(
115+
"Expected 'commandPrefix' to be a string",
116+
ErrCode.BadValue,
117+
);
118+
}
119+
if (commandPrefix.length < 2 || commandPrefix.length > 24) {
120+
throw new ApiError(
121+
"Expected 'commandPrefix' to be between 2-24 characters",
122+
ErrCode.BadValue,
123+
);
124+
}
125+
}
126+
let { events } = state as Partial<JiraProjectConnectionState>;
127+
if (!events || (events[0] as string) == "issue.created") {
128+
// migration
129+
events = ["issue_created"];
130+
} else if (events.find((ev) => !JiraAllowedEvents.includes(ev))?.length) {
131+
throw new ApiError(
132+
`'events' can only contain ${JiraAllowedEvents.join(", ")}`,
133+
ErrCode.BadValue,
134+
);
135+
}
136+
return { id, url, commandPrefix, events, priority };
137+
}
138+
141139
static async assertUserHasAccessToProject(
142140
tokenStore: UserTokenStore,
143141
userId: string,
@@ -189,7 +187,7 @@ export class JiraProjectConnection
189187
ErrCode.DisabledFeature,
190188
);
191189
}
192-
const validData = validateJiraConnectionState(data);
190+
const validData = JiraProjectConnection.validateState(data);
193191
log.info(
194192
`Attempting to provisionConnection for ${roomId} ${validData.url} on behalf of ${userId}`,
195193
);
@@ -236,7 +234,7 @@ export class JiraProjectConnection
236234
if (!config.jira) {
237235
throw Error("JIRA is not configured");
238236
}
239-
const connectionConfig = validateJiraConnectionState(state.content);
237+
const connectionConfig = JiraProjectConnection.validateState(state.content);
240238
return new JiraProjectConnection(
241239
roomId,
242240
as,
@@ -347,7 +345,7 @@ export class JiraProjectConnection
347345
}
348346

349347
protected validateConnectionState(content: unknown) {
350-
return validateJiraConnectionState(content);
348+
return JiraProjectConnection.validateState(content);
351349
}
352350

353351
public ensureGrant(sender?: string) {
@@ -737,7 +735,7 @@ export class JiraProjectConnection
737735
) {
738736
// Apply previous state to the current config, as provisioners might not return "unknown" keys.
739737
config = { ...this.state, ...config };
740-
const validatedConfig = validateJiraConnectionState(config);
738+
const validatedConfig = JiraProjectConnection.validateState(config);
741739
if (!validatedConfig.id) {
742740
await this.updateProjectId(validatedConfig, userId);
743741
}

0 commit comments

Comments
 (0)