Support GitHub push events in repo connections (#696)

* Support pushed commits on GitHub

* Add a nice way to see changes in the event content

* changelog

* Fix typing

* Fixing git freaking out
This commit is contained in:
Will Hunt 2023-03-31 09:20:06 +01:00 committed by GitHub
parent 2b5d4640c8
commit 76e2b53cf0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 51 additions and 2 deletions

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

@ -0,0 +1 @@
Add support for push events on Github repo connections.

View File

@ -65,6 +65,7 @@ the events marked as default below will be enabled. Otherwise, this is ignored.
- pull_request.opened *
- pull_request.ready_for_review *
- pull_request.reviewed *
- push
- release *
- release.created *
- release.drafted

View File

@ -293,6 +293,12 @@ export class Bridge {
(c, data) => c.onPROpened(data),
);
this.bindHandlerToQueue<GitHubWebhookTypes.PushEvent, GitHubRepoConnection>(
"github.push",
(data) => connManager.getConnectionsForGithubRepo(data.repository.owner.login, data.repository.name),
(c, data) => c.onPush(data),
);
this.bindHandlerToQueue<GitHubWebhookTypes.PullRequestClosedEvent, GitHubRepoConnection>(
"github.pull_request.closed",
(data) => connManager.getConnectionsForGithubRepo(data.repository.owner.login, data.repository.name),

View File

@ -8,7 +8,7 @@ import { Connection, IConnection, IConnectionState, InstantiateConnectionOpts, P
import { GetConnectionsResponseItem } from "../provisioning/api";
import { IssuesOpenedEvent, IssuesReopenedEvent, IssuesEditedEvent, PullRequestOpenedEvent, IssuesClosedEvent, PullRequestClosedEvent,
PullRequestReadyForReviewEvent, PullRequestReviewSubmittedEvent, ReleasePublishedEvent, ReleaseCreatedEvent,
IssuesLabeledEvent, IssuesUnlabeledEvent, WorkflowRunCompletedEvent,
IssuesLabeledEvent, IssuesUnlabeledEvent, WorkflowRunCompletedEvent, PushEvent,
} from "@octokit/webhooks-types";
import { MatrixMessageContent, MatrixEvent, MatrixReactionContent } from "../MatrixEvent";
import { MessageSenderClient } from "../MatrixSender";
@ -25,7 +25,7 @@ import { GitHubIssueConnection } from "./GithubIssue";
import { BridgeConfigGitHub } from "../Config/Config";
import { ApiError, ErrCode, ValidatorApiError } from "../api";
import { PermissionCheckFn } from ".";
import { MinimalGitHubIssue, MinimalGitHubRepo } from "../libRs";
import { GitHubRepoMessageBody, MinimalGitHubIssue, MinimalGitHubRepo } from "../libRs";
import Ajv, { JSONSchemaType } from "ajv";
import { HookFilter } from "../HookFilter";
import { GitHubGrantChecker } from "../Github/GrantChecker";
@ -107,6 +107,7 @@ export type AllowedEventsNames =
"pull_request.ready_for_review" |
"pull_request.reviewed" |
"pull_request" |
"push" |
"release.created" |
"release.drafted" |
"release" |
@ -132,6 +133,7 @@ export const AllowedEvents: AllowedEventsNames[] = [
"pull_request.ready_for_review" ,
"pull_request.reviewed" ,
"pull_request" ,
"push",
"release.created" ,
"release.drafted" ,
"release",
@ -324,6 +326,21 @@ const CREATED_GRACE_PERIOD_MS = 6000;
const DEFAULT_HOTLINK_PREFIX = "#";
const MAX_RETURNED_TARGETS = 10;
interface IPushEventContent {
body: string,
formatted_body: string,
msgtype: "m.notice",
format: "org.matrix.custom.html",
external_url: string,
"uk.half-shot.matrix-hookshot.github.push": {
commits: string[],
ref: string,
base_ref: string|null,
pusher: string,
},
"uk.half-shot.matrix-hookshot.github.repo": GitHubRepoMessageBody["uk.half-shot.matrix-hookshot.github.repo"],
}
function compareEmojiStrings(e0: string, e1: string, e0Index = 0) {
return e0.codePointAt(e0Index) === e1.codePointAt(0);
}
@ -1259,6 +1276,29 @@ export class GitHubRepoConnection extends CommandConnection<GitHubRepoConnection
}
}
public async onPush(event: PushEvent) {
if (this.hookFilter.shouldSkip('push')) {
return;
}
const content = `**${event.sender.login}** pushed [${event.commits.length} commit${event.commits.length === 1 ? '' : 's'}](${event.compare}) to \`${event.ref}\` for ${event.repository.full_name}`;
const eventContent: IPushEventContent = {
...FormatUtil.getPartialBodyForGithubRepo(event.repository),
external_url: event.compare,
"uk.half-shot.matrix-hookshot.github.push": {
commits: event.commits.map(c => c.id),
pusher: `${event.pusher.name} <${event.pusher.email}>`,
ref: event.ref,
base_ref: event.base_ref,
},
msgtype: "m.notice",
body: content,
formatted_body: md.render(content),
format: "org.matrix.custom.html",
};
await this.intent.sendEvent(this.roomId, eventContent);
}
public toString() {
return `GitHubRepo ${this.org}/${this.repo}`;
}

View File

@ -136,6 +136,7 @@ const ConnectionConfiguration: FunctionComponent<ConnectionConfigurationProps<ne
<EventHookCheckbox enabledHooks={enabledHooks} parentEvent="pull_request" hookEventName="pull_request.ready_for_review" onChange={toggleEnabledHook}>Ready for review</EventHookCheckbox>
<EventHookCheckbox enabledHooks={enabledHooks} parentEvent="pull_request" hookEventName="pull_request.reviewed" onChange={toggleEnabledHook}>Reviewed</EventHookCheckbox>
</ul>
<EventHookCheckbox enabledHooks={enabledHooks} hookEventName="push" onChange={toggleEnabledHook}>Pushed commits</EventHookCheckbox>
<EventHookCheckbox enabledHooks={enabledHooks} hookEventName="workflow.run" onChange={toggleEnabledHook}>Workflow Runs</EventHookCheckbox>
<ul>
<EventHookCheckbox enabledHooks={enabledHooks} parentEvent="workflow.run" hookEventName="workflow.run.success" onChange={toggleEnabledHook}>Success</EventHookCheckbox>