hookshot/tests/connections/GithubRepoTest.ts
Justin Carlson 9a7839ce42
Implement separate bot users per service (#573)
* Add service bots config

* Add joined rooms manager and keep track of joined rooms

* Add bot users manager and ensure registration and profiles

* Improve joined rooms manager and set up already joined rooms

* Handle invites with service bots

* Handle messages with service bots

* Use service bots for connections

* Use service bots in widget and provisioning APIs

* Use service bots in setup connections

* Use service bots for feed connections

* Handle admin rooms for service bots

* Fix confused event type and service type in provisioning and widget APIs

* Fix generic webhooks service name

* Fix enabled services config

* Handle power level change

* Create widgets with service scope

* Use service bots for gitlab repo connections

* Use service bots for gitlab issue connections

* Use service bots for generic webhook connections

* Use service bots for figma file connections

* Use service bots when verifying state events

* Use service bots for github repo connections

* Use service bots for github discussion connections

* Use service bots for github discussion space connections

* Use service bots for github project connections

* Use service bots for github issue connections

* Use service bots for github user space connections

* Use service bots for jira connections

* Make sure ghost users are invited for gitlab issue comments

* Configure one service per service bot

* Add changelog

* Update tests

* Fix up following rebase

* Fix comment

* Use getter for enabled services

* Ensure homeserver can be reached before registering bots

* Add intent getter on bot user

* Update config comment

* Merge joined rooms manager with bot users manager

* Remove unused localpart from bot user class

* Refactor to pass in bot users manager

* Improve priority sort function

Co-authored-by: Christian Paul <christianp@matrix.org>

* Fix priority sort

Higher priority should come first

* Add debug log when invites are rejected

* Use different state key for scoped setup widgets

* Use different subtitles to differentiate service bots setup widgets

* Refactor bot user setup into bot users manager

* Refactor to reduce duplication in widget API

* Consistent room ID and intent args order

* Add docs and update changelog

* Add overrideUserId deprecation warning

* Add service bots link

Co-authored-by: Christian Paul <christianp@matrix.org>
Co-authored-by: Will Hunt <will@half-shot.uk>
2023-01-13 15:32:09 +00:00

188 lines
5.4 KiB
TypeScript

import { GitHubRepoConnection, GitHubRepoConnectionState } from "../../src/Connections/GithubRepo"
import { GithubInstance } from "../../src/Github/GithubInstance";
import { createMessageQueue } from "../../src/MessageQueue";
import { UserTokenStore } from "../../src/UserTokenStore";
import { DefaultConfig } from "../../src/Config/Defaults";
import { AppserviceMock } from "../utils/AppserviceMock";
import { ApiError, ErrCode, ValidatorApiError } from "../../src/api";
import { expect } from "chai";
const ROOM_ID = "!foo:bar";
const GITHUB_ORG_REPO = {
org: "a-fake-org",
repo: "a-fake-repo",
};
const GITHUB_ISSUE = {
id: 1234,
number: 1234,
user: {
login: "alice"
},
html_url: `https://github.com/${GITHUB_ORG_REPO.org}/${GITHUB_ORG_REPO.repo}/issues/1234`,
title: "My issue",
};
const GITHUB_ISSUE_CREATED_PAYLOAD = {
action: "opened",
issue: GITHUB_ISSUE,
repository: {
full_name: `${GITHUB_ORG_REPO.org}/${GITHUB_ORG_REPO.repo}`,
id: 1234,
html_url: `https://github.com/${GITHUB_ORG_REPO.org}/${GITHUB_ORG_REPO.repo}`,
}
};
function createConnection(state: Record<string, unknown> = {}, isExistingState=false) {
const mq = createMessageQueue({
monolithic: true
});
mq.subscribe('*');
const as = AppserviceMock.create();
const intent = as.getIntentForUserId('@github:example.test');
const githubInstance = new GithubInstance("foo", "bar", new URL("https://github.com"));
const connection = new GitHubRepoConnection(
ROOM_ID,
as,
intent,
GitHubRepoConnection.validateState({
org: "a-fake-org",
repo: "a-fake-repo",
...state,
}, isExistingState),
{} as UserTokenStore,
"state_key",
githubInstance,
// Default config always contains GitHub
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
DefaultConfig.github!
);
return {connection, intent};
}
describe("GitHubRepoConnection", () => {
describe("validateState", () => {
it("can validate a completes state config", () => {
GitHubRepoConnection.validateState({
org: "foo",
repo: "bar",
enableHooks: ["issue", "pull_request", "release"],
commandPrefix: "!foo",
showIssueRoomLink: true,
prDiff: {
enabled: true,
maxLines: 55
},
includingLabels: ["this", "and", "that"],
excludingLabels: ["not", "those"],
hotlinkIssues: {
prefix: "foo"
},
newIssue: {
labels: ["this", "and", "that"]
}
} as GitHubRepoConnectionState as unknown as Record<string, unknown>);
});
it("will convert ignoredHooks for existing state", () => {
const state = GitHubRepoConnection.validateState({
org: "foo",
repo: "bar",
ignoreHooks: ["issue"],
enableHooks: ["issue", "pull_request", "release"],
commandPrefix: "!foo",
} as GitHubRepoConnectionState as unknown as Record<string, unknown>, true);
expect(state.enableHooks).to.not.contain('issue');
});
it("will disallow invalid state", () => {
try {
GitHubRepoConnection.validateState({
org: "foo",
repo: false,
});
} catch (ex) {
if (ex instanceof ValidatorApiError === false || ex.errcode !== ErrCode.BadValue) {
throw ex;
}
}
});
it("will disallow enabledHooks to contains invalid enums if this is new state", () => {
try {
GitHubRepoConnection.validateState({
org: "foo",
repo: "bar",
enabledHooks: ["not-real"],
}, false);
} catch (ex) {
if (ex instanceof ApiError === false || ex.errcode !== ErrCode.BadValue) {
throw ex;
}
}
});
it("will allow enabledHooks to contains invalid enums if this is old state", () => {
GitHubRepoConnection.validateState({
org: "foo",
repo: "bar",
enabledHooks: ["not-real"],
}, true);
});
});
describe("onIssueCreated", () => {
it("will handle a simple issue", async () => {
const { connection, intent } = createConnection();
await connection.onIssueCreated(GITHUB_ISSUE_CREATED_PAYLOAD as never);
// Statement text.
intent.expectEventBodyContains('**alice** created new issue', 0);
intent.expectEventBodyContains(GITHUB_ISSUE_CREATED_PAYLOAD.issue.html_url, 0);
intent.expectEventBodyContains(GITHUB_ISSUE_CREATED_PAYLOAD.issue.title, 0);
});
it("will filter out issues not matching includingLabels.", async () => {
const { connection, intent } = createConnection({
includingLabels: ["include-me"]
});
await connection.onIssueCreated({
...GITHUB_ISSUE_CREATED_PAYLOAD,
issue: {
...GITHUB_ISSUE,
labels: [{
name: "foo",
}],
}
} as never);
// ..or issues with no labels
await connection.onIssueCreated(GITHUB_ISSUE_CREATED_PAYLOAD as never);
intent.expectNoEvent();
});
it("will filter out issues matching excludingLabels.", async () => {
const { connection, intent } = createConnection({
excludingLabels: ["exclude-me"]
});
await connection.onIssueCreated({
...GITHUB_ISSUE_CREATED_PAYLOAD,
issue: {
...GITHUB_ISSUE,
labels: [{
name: "exclude-me",
}],
}
} as never);
intent.expectNoEvent();
});
it("will include issues matching includingLabels.", async () => {
const { connection, intent } = createConnection({
includingIssues: ["include-me"]
});
await connection.onIssueCreated({
...GITHUB_ISSUE_CREATED_PAYLOAD,
issue: {
...GITHUB_ISSUE,
labels: [{
name: "include-me",
}],
}
} as never);
intent.expectEventBodyContains('**alice** created new issue', 0);
});
});
});