From 16e97d309d409effa1e4a4f1c4c311953a0beee3 Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Thu, 23 Dec 2021 15:08:49 +0000 Subject: [PATCH] Support GitLab wiki page events (#104) * Support GitLab wiki events * changelog * Fixup formatting --- changelog.d/104.feature | 1 + src/Bridge.ts | 8 +++++++- src/Connections/GitlabRepo.ts | 30 +++++++++++++++++++++++++++++- src/Gitlab/WebhookTypes.ts | 17 +++++++++++++++++ src/Webhooks.ts | 2 ++ 5 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 changelog.d/104.feature diff --git a/changelog.d/104.feature b/changelog.d/104.feature new file mode 100644 index 00000000..6268574e --- /dev/null +++ b/changelog.d/104.feature @@ -0,0 +1 @@ +Support GitLab wiki page change events for GitLabProject connections. diff --git a/src/Bridge.ts b/src/Bridge.ts index eb502fec..1b925a47 100644 --- a/src/Bridge.ts +++ b/src/Bridge.ts @@ -11,7 +11,7 @@ import { GithubInstance } from "./Github/GithubInstance"; import { IBridgeStorageProvider } from "./Stores/StorageProvider"; import { IConnection, GitHubDiscussionSpace, GitHubDiscussionConnection, GitHubUserSpace, JiraProjectConnection, GitLabRepoConnection, GitHubIssueConnection, GitHubProjectConnection, GitHubRepoConnection, GitLabIssueConnection } from "./Connections"; -import { IGitLabWebhookIssueStateEvent, IGitLabWebhookMREvent, IGitLabWebhookNoteEvent, IGitLabWebhookTagPushEvent } from "./Gitlab/WebhookTypes"; +import { IGitLabWebhookIssueStateEvent, IGitLabWebhookMREvent, IGitLabWebhookNoteEvent, IGitLabWebhookTagPushEvent, IGitLabWebhookWikiPageEvent } from "./Gitlab/WebhookTypes"; import { JiraIssueEvent, JiraIssueUpdatedEvent } from "./Jira/WebhookTypes"; import { JiraOAuthResult } from "./Jira/Types"; import { MatrixEvent, MatrixMemberContent, MatrixMessageContent } from "./MatrixEvent"; @@ -331,6 +331,12 @@ export class Bridge { (c, data) => c.onGitLabTagPush(data), ); + this.bindHandlerToQueue( + "gitlab.wiki_page", + (data) => connManager.getConnectionsForGitLabRepo(data.project.path_with_namespace), + (c, data) => c.onWikiPageEvent(data), + ); + this.queue.on("notifications.user.events", async (msg) => { const adminRoom = this.adminRooms.get(msg.data.roomId); if (!adminRoom) { diff --git a/src/Connections/GitlabRepo.ts b/src/Connections/GitlabRepo.ts index 7705fe51..9bf71886 100644 --- a/src/Connections/GitlabRepo.ts +++ b/src/Connections/GitlabRepo.ts @@ -7,7 +7,7 @@ import { MatrixEvent, MatrixMessageContent } from "../MatrixEvent"; import markdown from "markdown-it"; import LogWrapper from "../LogWrapper"; import { GitLabInstance } from "../Config/Config"; -import { IGitLabWebhookMREvent, IGitLabWebhookTagPushEvent } from "../Gitlab/WebhookTypes"; +import { IGitLabWebhookMREvent, IGitLabWebhookTagPushEvent, IGitLabWebhookWikiPageEvent } from "../Gitlab/WebhookTypes"; import { CommandConnection } from "./CommandConnection"; export interface GitLabRepoConnectionState { @@ -192,6 +192,34 @@ export class GitLabRepoConnection extends CommandConnection { format: "org.matrix.custom.html", }); } + + public async onWikiPageEvent(data: IGitLabWebhookWikiPageEvent) { + const attributes = data.object_attributes; + log.info(`onWikiPageEvent ${this.roomId} ${this.instance}/${this.path}`); + if (this.shouldSkipHook('wiki', `wiki.${attributes.action}`)) { + return; + } + + + let statement: string; + if (attributes.action === "create") { + statement = "created new wiki page"; + } else if (attributes.action === "delete") { + statement = "deleted wiki page"; + } else { + statement = "updated wiki page"; + } + + const message = attributes.message && ` "${attributes.message}"`; + + const content = `**${data.user.username}** ${statement} "[${attributes.title}](${attributes.url})" for ${data.project.path_with_namespace} ${message}`; + await this.as.botIntent.sendEvent(this.roomId, { + msgtype: "m.notice", + body: content, + formatted_body: md.renderInline(content), + format: "org.matrix.custom.html", + }); + } public toString() { return `GitLabRepo ${this.instance}/${this.path}`; diff --git a/src/Gitlab/WebhookTypes.ts b/src/Gitlab/WebhookTypes.ts index 1d23a404..3eea1be9 100644 --- a/src/Gitlab/WebhookTypes.ts +++ b/src/Gitlab/WebhookTypes.ts @@ -86,6 +86,23 @@ export interface IGitLabWebhookTagPushEvent { repository: IGitlabRepository; } +export interface IGitLabWebhookWikiPageEvent { + object_kind: "wiki_page"; + user: IGitlabUser; + project: IGitlabProject; + wiki: { + web_url: string; + path_with_namespace: string; + }; + object_attributes: { + title: string; + url: string; + message: string; + format: "markdown"; + content: string; + action: "create"|"update"|"delete"; + }; +} export interface IGitLabWebhookNoteEvent { user: IGitlabUser; diff --git a/src/Webhooks.ts b/src/Webhooks.ts index b5a31a1f..942d593e 100644 --- a/src/Webhooks.ts +++ b/src/Webhooks.ts @@ -89,6 +89,8 @@ export class Webhooks extends EventEmitter { return `gitlab.note.created`; } else if (body.object_kind === "tag_push") { return "gitlab.tag_push"; + } else if (body.object_kind === "wiki_page") { + return "gitlab.wiki_page"; } else { return null; }