Support GitLab wiki page events (#104)

* Support GitLab wiki events

* changelog

* Fixup formatting
This commit is contained in:
Will Hunt 2021-12-23 15:08:49 +00:00 committed by GitHub
parent 02cae5eb88
commit 16e97d309d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 56 additions and 2 deletions

1
changelog.d/104.feature Normal file
View File

@ -0,0 +1 @@
Support GitLab wiki page change events for GitLabProject connections.

View File

@ -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<IGitLabWebhookWikiPageEvent, GitLabRepoConnection>(
"gitlab.wiki_page",
(data) => connManager.getConnectionsForGitLabRepo(data.project.path_with_namespace),
(c, data) => c.onWikiPageEvent(data),
);
this.queue.on<UserNotificationsEvent>("notifications.user.events", async (msg) => {
const adminRoom = this.adminRooms.get(msg.data.roomId);
if (!adminRoom) {

View File

@ -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}`;

View File

@ -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;

View File

@ -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;
}