Skip to content

Commit

Permalink
Add commands for listing & removing webhooks (#864)
Browse files Browse the repository at this point in the history
* Add commands for removing webhooks

* Fix command setting state twice

* changelog
  • Loading branch information
Half-Shot authored Dec 27, 2023
1 parent 066d908 commit 9cc402b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.d/866.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `webhook list` and `webhook remove` commands.
54 changes: 53 additions & 1 deletion src/Connections/SetupConnection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// We need to instantiate some functions which are not directly called, which confuses typescript.
import { BotCommands, botCommand, compileBotCommands, HelpFunction } from "../BotCommands";
import { CommandConnection } from "./CommandConnection";
import { GenericHookConnection, GitHubRepoConnection, JiraProjectConnection, JiraProjectConnectionState } from ".";
import { GenericHookConnection, GenericHookConnectionState, GitHubRepoConnection, JiraProjectConnection, JiraProjectConnectionState } from ".";
import { CommandError } from "../errors";
import { BridgePermissionLevel } from "../config/Config";
import markdown from "markdown-it";
Expand Down Expand Up @@ -231,6 +231,58 @@ export class SetupConnection extends CommandConnection {
return this.client.sendNotice(this.roomId, `Room configured to bridge webhooks. See admin room for secret url.`);
}



@botCommand("webhook list", { help: "Show webhooks currently configured.", category: "generic"})
public async onWebhookList() {
const webhooks: GenericHookConnectionState[] = await this.client.getRoomState(this.roomId).catch((err: any) => {
if (err.body.errcode === 'M_NOT_FOUND') {
return []; // not an error to us
}
throw err;
}).then(events =>
events.filter(
(ev: any) => ev.type === GenericHookConnection.CanonicalEventType && ev.content.name
).map(ev => ev.content)
);

if (webhooks.length === 0) {
return this.client.sendHtmlNotice(this.roomId, md.renderInline('No webhooks configured'));
} else {
const feedDescriptions = webhooks.sort(
(a, b) => a.name.localeCompare(b.name)
).map(feed => {
return feed.name;
});

return this.client.sendHtmlNotice(this.roomId, md.render(
'Webhooks configured:\n\n' +
feedDescriptions.map(desc => ` - ${desc}`).join('\n')
));
}
}

@botCommand("webhook remove", { help: "Remove a webhook from the room.", requiredArgs: ["name"], includeUserId: true, category: "generic"})
public async onWebhookRemove(userId: string, name: string) {
await this.checkUserPermissions(userId, "generic", GenericHookConnection.CanonicalEventType);

const event = await this.client.getRoomStateEvent(this.roomId, GenericHookConnection.CanonicalEventType, name).catch((err: any) => {
if (err.body.errcode === 'M_NOT_FOUND') {
return null; // not an error to us
}
throw err;
});
if (!event || event.disabled === true || Object.keys(event).length === 0) {
throw new CommandError("Invalid webhook name", `No webhook by the name of "${name}" is configured.`);
}

await this.client.sendStateEvent(this.roomId, GenericHookConnection.CanonicalEventType, name, {
disabled: true
});

return this.client.sendHtmlNotice(this.roomId, md.renderInline(`Removed webhook \`${name}\``));
}

@botCommand("figma file", { help: "Bridge a Figma file to the room.", requiredArgs: ["url"], includeUserId: true, category: "figma"})
public async onFigma(userId: string, url: string) {
if (!this.config.figma) {
Expand Down

0 comments on commit 9cc402b

Please sign in to comment.