generated from nhcarrigan/template
This commit is contained in:
+16
-1
@@ -6,6 +6,7 @@
|
||||
|
||||
import { Client, GatewayIntentBits, Events, Partials } from "discord.js";
|
||||
import { scheduleJob } from "node-schedule";
|
||||
import { App } from "octokit";
|
||||
import { handleMessageCreate } from "./events/handleMessageCreate.js";
|
||||
import { cacheData } from "./modules/cacheData.js";
|
||||
import {
|
||||
@@ -18,6 +19,19 @@ import { instantiateServer } from "./server/serve.js";
|
||||
import { logger } from "./utils/logger.js";
|
||||
import type { Amari } from "./interfaces/amari.js";
|
||||
|
||||
if (process.env.GH_CLIENT_ID === undefined
|
||||
|| process.env.GH_PRIVATE_KEY === undefined) {
|
||||
throw new Error("Cannot initialise GitHub!");
|
||||
}
|
||||
|
||||
const githubApp = new App({
|
||||
appId: process.env.GH_CLIENT_ID,
|
||||
privateKey: process.env.GH_PRIVATE_KEY.replaceAll("\\n", "\n"),
|
||||
});
|
||||
const octokit = await githubApp.getInstallationOctokit(83_119_105);
|
||||
const { data } = await octokit.rest.apps.getAuthenticated();
|
||||
await logger.log("debug", `Authenticated to GitHub as ${data?.name ?? "unknown"}`);
|
||||
|
||||
const amari: Amari = {
|
||||
discord: new Client({ intents: [
|
||||
GatewayIntentBits.Guilds,
|
||||
@@ -27,6 +41,7 @@ const amari: Amari = {
|
||||
GatewayIntentBits.DirectMessages,
|
||||
],
|
||||
partials: [ Partials.Channel ] }),
|
||||
github: octokit,
|
||||
lastRssItems: {
|
||||
freeCodeCamp: null,
|
||||
hackerNews: null,
|
||||
@@ -66,4 +81,4 @@ amari.discord.on(Events.UserUpdate, (_oldUser, updatedUser) => {
|
||||
});
|
||||
|
||||
await amari.discord.login(process.env.BOT_TOKEN);
|
||||
instantiateServer();
|
||||
instantiateServer(amari);
|
||||
|
||||
@@ -5,9 +5,11 @@
|
||||
*/
|
||||
|
||||
import type { Client } from "discord.js";
|
||||
import type { App } from "octokit";
|
||||
|
||||
export interface Amari {
|
||||
discord: Client;
|
||||
github: App["octokit"];
|
||||
lastRssItems: {
|
||||
freeCodeCamp: string | null;
|
||||
hackerNews: string | null;
|
||||
|
||||
+506
-2
@@ -6,6 +6,508 @@
|
||||
|
||||
/* eslint-disable @typescript-eslint/naming-convention, max-lines -- These are API interfaces. */
|
||||
|
||||
interface Repository {
|
||||
id: number;
|
||||
node_id: string;
|
||||
name: string;
|
||||
full_name: string;
|
||||
owner: {
|
||||
login: string;
|
||||
id: number;
|
||||
node_id: string;
|
||||
avatar_url: string;
|
||||
gravatar_id: string;
|
||||
url: string;
|
||||
html_url: string;
|
||||
followers_url: string;
|
||||
following_url: string;
|
||||
gists_url: string;
|
||||
starred_url: string;
|
||||
subscriptions_url: string;
|
||||
organizations_url: string;
|
||||
repos_url: string;
|
||||
events_url: string;
|
||||
received_events_url: string;
|
||||
type: string;
|
||||
site_admin: boolean;
|
||||
};
|
||||
private: boolean;
|
||||
html_url: string;
|
||||
description: string;
|
||||
fork: boolean;
|
||||
url: string;
|
||||
archive_url: string;
|
||||
assignees_url: string;
|
||||
blobs_url: string;
|
||||
branches_url: string;
|
||||
collaborators_url: string;
|
||||
comments_url: string;
|
||||
commits_url: string;
|
||||
compare_url: string;
|
||||
contents_url: string;
|
||||
contributors_url: string;
|
||||
deployments_url: string;
|
||||
downloads_url: string;
|
||||
events_url: string;
|
||||
forks_url: string;
|
||||
git_commits_url: string;
|
||||
git_refs_url: string;
|
||||
git_tags_url: string;
|
||||
git_url: string;
|
||||
issue_comment_url: string;
|
||||
issue_events_url: string;
|
||||
issues_url: string;
|
||||
keys_url: string;
|
||||
labels_url: string;
|
||||
languages_url: string;
|
||||
merges_url: string;
|
||||
milestones_url: string;
|
||||
notifications_url: string;
|
||||
pulls_url: string;
|
||||
releases_url: string;
|
||||
ssh_url: string;
|
||||
stargazers_url: string;
|
||||
statuses_url: string;
|
||||
subscribers_url: string;
|
||||
subscription_url: string;
|
||||
tags_url: string;
|
||||
teams_url: string;
|
||||
trees_url: string;
|
||||
clone_url: string;
|
||||
mirror_url: string;
|
||||
hooks_url: string;
|
||||
svn_url: string;
|
||||
homepage: string;
|
||||
forks_count: number;
|
||||
forks: number;
|
||||
stargazers_count: number;
|
||||
watchers_count: number;
|
||||
watchers: number;
|
||||
size: number;
|
||||
default_branch: string;
|
||||
open_issues_count: number;
|
||||
open_issues: number;
|
||||
is_template: boolean;
|
||||
topics: Array<string>;
|
||||
has_issues: boolean;
|
||||
has_projects: boolean;
|
||||
has_wiki: boolean;
|
||||
has_pages: boolean;
|
||||
has_downloads: boolean;
|
||||
has_discussions: boolean;
|
||||
archived: boolean;
|
||||
disabled: boolean;
|
||||
visibility: string;
|
||||
pushed_at: string;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
permissions: {
|
||||
pull: boolean;
|
||||
push: boolean;
|
||||
admin: boolean;
|
||||
};
|
||||
allow_rebase_merge: boolean;
|
||||
template_repository: {
|
||||
id: number;
|
||||
node_id: string;
|
||||
name: string;
|
||||
full_name: string;
|
||||
owner: {
|
||||
login: string;
|
||||
id: number;
|
||||
node_id: string;
|
||||
avatar_url: string;
|
||||
gravatar_id: string;
|
||||
url: string;
|
||||
html_url: string;
|
||||
followers_url: string;
|
||||
following_url: string;
|
||||
gists_url: string;
|
||||
starred_url: string;
|
||||
subscriptions_url: string;
|
||||
organizations_url: string;
|
||||
repos_url: string;
|
||||
events_url: string;
|
||||
received_events_url: string;
|
||||
type: string;
|
||||
site_admin: boolean;
|
||||
};
|
||||
private: boolean;
|
||||
html_url: string;
|
||||
description: string;
|
||||
fork: boolean;
|
||||
url: string;
|
||||
archive_url: string;
|
||||
assignees_url: string;
|
||||
blobs_url: string;
|
||||
branches_url: string;
|
||||
collaborators_url: string;
|
||||
comments_url: string;
|
||||
commits_url: string;
|
||||
compare_url: string;
|
||||
contents_url: string;
|
||||
contributors_url: string;
|
||||
deployments_url: string;
|
||||
downloads_url: string;
|
||||
events_url: string;
|
||||
forks_url: string;
|
||||
git_commits_url: string;
|
||||
git_refs_url: string;
|
||||
git_tags_url: string;
|
||||
git_url: string;
|
||||
issue_comment_url: string;
|
||||
issue_events_url: string;
|
||||
issues_url: string;
|
||||
keys_url: string;
|
||||
labels_url: string;
|
||||
languages_url: string;
|
||||
merges_url: string;
|
||||
milestones_url: string;
|
||||
notifications_url: string;
|
||||
pulls_url: string;
|
||||
releases_url: string;
|
||||
ssh_url: string;
|
||||
stargazers_url: string;
|
||||
statuses_url: string;
|
||||
subscribers_url: string;
|
||||
subscription_url: string;
|
||||
tags_url: string;
|
||||
teams_url: string;
|
||||
trees_url: string;
|
||||
clone_url: string;
|
||||
mirror_url: string;
|
||||
hooks_url: string;
|
||||
svn_url: string;
|
||||
homepage: string;
|
||||
language: unknown;
|
||||
forks: number;
|
||||
forks_count: number;
|
||||
stargazers_count: number;
|
||||
watchers_count: number;
|
||||
watchers: number;
|
||||
size: number;
|
||||
default_branch: string;
|
||||
open_issues: number;
|
||||
open_issues_count: number;
|
||||
is_template: boolean;
|
||||
license: {
|
||||
key: string;
|
||||
name: string;
|
||||
url: string;
|
||||
spdx_id: string;
|
||||
node_id: string;
|
||||
html_url: string;
|
||||
};
|
||||
topics: Array<string>;
|
||||
has_issues: boolean;
|
||||
has_projects: boolean;
|
||||
has_wiki: boolean;
|
||||
has_pages: boolean;
|
||||
has_downloads: boolean;
|
||||
archived: boolean;
|
||||
disabled: boolean;
|
||||
visibility: string;
|
||||
pushed_at: string;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
permissions: {
|
||||
admin: boolean;
|
||||
push: boolean;
|
||||
pull: boolean;
|
||||
};
|
||||
allow_rebase_merge: boolean;
|
||||
temp_clone_token: string;
|
||||
allow_squash_merge: boolean;
|
||||
allow_auto_merge: boolean;
|
||||
delete_branch_on_merge: boolean;
|
||||
allow_merge_commit: boolean;
|
||||
subscribers_count: number;
|
||||
network_count: number;
|
||||
};
|
||||
temp_clone_token: string;
|
||||
allow_squash_merge: boolean;
|
||||
allow_auto_merge: boolean;
|
||||
delete_branch_on_merge: boolean;
|
||||
allow_merge_commit: boolean;
|
||||
allow_forking: boolean;
|
||||
subscribers_count: number;
|
||||
network_count: number;
|
||||
license: {
|
||||
key: string;
|
||||
name: string;
|
||||
spdx_id: string;
|
||||
url: string;
|
||||
node_id: string;
|
||||
};
|
||||
organization: {
|
||||
login: string;
|
||||
id: number;
|
||||
node_id: string;
|
||||
avatar_url: string;
|
||||
gravatar_id: string;
|
||||
url: string;
|
||||
html_url: string;
|
||||
followers_url: string;
|
||||
following_url: string;
|
||||
gists_url: string;
|
||||
starred_url: string;
|
||||
subscriptions_url: string;
|
||||
organizations_url: string;
|
||||
repos_url: string;
|
||||
events_url: string;
|
||||
received_events_url: string;
|
||||
type: string;
|
||||
site_admin: boolean;
|
||||
};
|
||||
parent: {
|
||||
id: number;
|
||||
node_id: string;
|
||||
name: string;
|
||||
full_name: string;
|
||||
owner: {
|
||||
login: string;
|
||||
id: number;
|
||||
node_id: string;
|
||||
avatar_url: string;
|
||||
gravatar_id: string;
|
||||
url: string;
|
||||
html_url: string;
|
||||
followers_url: string;
|
||||
following_url: string;
|
||||
gists_url: string;
|
||||
starred_url: string;
|
||||
subscriptions_url: string;
|
||||
organizations_url: string;
|
||||
repos_url: string;
|
||||
events_url: string;
|
||||
received_events_url: string;
|
||||
type: string;
|
||||
site_admin: boolean;
|
||||
};
|
||||
private: boolean;
|
||||
html_url: string;
|
||||
description: string;
|
||||
fork: boolean;
|
||||
url: string;
|
||||
archive_url: string;
|
||||
assignees_url: string;
|
||||
blobs_url: string;
|
||||
branches_url: string;
|
||||
collaborators_url: string;
|
||||
comments_url: string;
|
||||
commits_url: string;
|
||||
compare_url: string;
|
||||
contents_url: string;
|
||||
contributors_url: string;
|
||||
deployments_url: string;
|
||||
downloads_url: string;
|
||||
events_url: string;
|
||||
forks_url: string;
|
||||
git_commits_url: string;
|
||||
git_refs_url: string;
|
||||
git_tags_url: string;
|
||||
git_url: string;
|
||||
issue_comment_url: string;
|
||||
issue_events_url: string;
|
||||
issues_url: string;
|
||||
keys_url: string;
|
||||
labels_url: string;
|
||||
languages_url: string;
|
||||
merges_url: string;
|
||||
milestones_url: string;
|
||||
notifications_url: string;
|
||||
pulls_url: string;
|
||||
releases_url: string;
|
||||
ssh_url: string;
|
||||
stargazers_url: string;
|
||||
statuses_url: string;
|
||||
subscribers_url: string;
|
||||
subscription_url: string;
|
||||
tags_url: string;
|
||||
teams_url: string;
|
||||
trees_url: string;
|
||||
clone_url: string;
|
||||
mirror_url: string;
|
||||
hooks_url: string;
|
||||
svn_url: string;
|
||||
homepage: string;
|
||||
language: unknown;
|
||||
forks_count: number;
|
||||
stargazers_count: number;
|
||||
watchers_count: number;
|
||||
size: number;
|
||||
default_branch: string;
|
||||
open_issues_count: number;
|
||||
is_template: boolean;
|
||||
topics: Array<string>;
|
||||
has_issues: boolean;
|
||||
has_projects: boolean;
|
||||
has_wiki: boolean;
|
||||
has_pages: boolean;
|
||||
has_downloads: boolean;
|
||||
archived: boolean;
|
||||
disabled: boolean;
|
||||
visibility: string;
|
||||
pushed_at: string;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
permissions: {
|
||||
admin: boolean;
|
||||
push: boolean;
|
||||
pull: boolean;
|
||||
};
|
||||
allow_rebase_merge: boolean;
|
||||
temp_clone_token: string;
|
||||
allow_squash_merge: boolean;
|
||||
allow_auto_merge: boolean;
|
||||
delete_branch_on_merge: boolean;
|
||||
allow_merge_commit: boolean;
|
||||
subscribers_count: number;
|
||||
network_count: number;
|
||||
license: {
|
||||
key: string;
|
||||
name: string;
|
||||
url: string;
|
||||
spdx_id: string;
|
||||
node_id: string;
|
||||
html_url: string;
|
||||
};
|
||||
forks: number;
|
||||
open_issues: number;
|
||||
watchers: number;
|
||||
};
|
||||
source: {
|
||||
id: number;
|
||||
node_id: string;
|
||||
name: string;
|
||||
full_name: string;
|
||||
owner: {
|
||||
login: string;
|
||||
id: number;
|
||||
node_id: string;
|
||||
avatar_url: string;
|
||||
gravatar_id: string;
|
||||
url: string;
|
||||
html_url: string;
|
||||
followers_url: string;
|
||||
following_url: string;
|
||||
gists_url: string;
|
||||
starred_url: string;
|
||||
subscriptions_url: string;
|
||||
organizations_url: string;
|
||||
repos_url: string;
|
||||
events_url: string;
|
||||
received_events_url: string;
|
||||
type: string;
|
||||
site_admin: boolean;
|
||||
};
|
||||
private: boolean;
|
||||
html_url: string;
|
||||
description: string;
|
||||
fork: boolean;
|
||||
url: string;
|
||||
archive_url: string;
|
||||
assignees_url: string;
|
||||
blobs_url: string;
|
||||
branches_url: string;
|
||||
collaborators_url: string;
|
||||
comments_url: string;
|
||||
commits_url: string;
|
||||
compare_url: string;
|
||||
contents_url: string;
|
||||
contributors_url: string;
|
||||
deployments_url: string;
|
||||
downloads_url: string;
|
||||
events_url: string;
|
||||
forks_url: string;
|
||||
git_commits_url: string;
|
||||
git_refs_url: string;
|
||||
git_tags_url: string;
|
||||
git_url: string;
|
||||
issue_comment_url: string;
|
||||
issue_events_url: string;
|
||||
issues_url: string;
|
||||
keys_url: string;
|
||||
labels_url: string;
|
||||
languages_url: string;
|
||||
merges_url: string;
|
||||
milestones_url: string;
|
||||
notifications_url: string;
|
||||
pulls_url: string;
|
||||
releases_url: string;
|
||||
ssh_url: string;
|
||||
stargazers_url: string;
|
||||
statuses_url: string;
|
||||
subscribers_url: string;
|
||||
subscription_url: string;
|
||||
tags_url: string;
|
||||
teams_url: string;
|
||||
trees_url: string;
|
||||
clone_url: string;
|
||||
mirror_url: string;
|
||||
hooks_url: string;
|
||||
svn_url: string;
|
||||
homepage: string;
|
||||
forks_count: number;
|
||||
stargazers_count: number;
|
||||
watchers_count: number;
|
||||
size: number;
|
||||
default_branch: string;
|
||||
open_issues_count: number;
|
||||
is_template: boolean;
|
||||
topics: Array<string>;
|
||||
has_issues: boolean;
|
||||
has_projects: boolean;
|
||||
has_wiki: boolean;
|
||||
has_pages: boolean;
|
||||
has_downloads: boolean;
|
||||
archived: boolean;
|
||||
disabled: boolean;
|
||||
visibility: string;
|
||||
pushed_at: string;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
permissions: {
|
||||
admin: boolean;
|
||||
push: boolean;
|
||||
pull: boolean;
|
||||
};
|
||||
allow_rebase_merge: boolean;
|
||||
temp_clone_token: string;
|
||||
allow_squash_merge: boolean;
|
||||
allow_auto_merge: boolean;
|
||||
delete_branch_on_merge: boolean;
|
||||
allow_merge_commit: boolean;
|
||||
subscribers_count: number;
|
||||
network_count: number;
|
||||
license: {
|
||||
key: string;
|
||||
name: string;
|
||||
url: string;
|
||||
spdx_id: string;
|
||||
node_id: string;
|
||||
html_url: string;
|
||||
};
|
||||
forks: number;
|
||||
open_issues: number;
|
||||
watchers: number;
|
||||
security_and_analysis: {
|
||||
advanced_security: {
|
||||
status: string;
|
||||
};
|
||||
secret_scanning: {
|
||||
status: string;
|
||||
};
|
||||
secret_scanning_push_protection: {
|
||||
status: string;
|
||||
};
|
||||
secret_scanning_non_provider_patterns: {
|
||||
status: string;
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
interface Issue {
|
||||
id: number;
|
||||
node_id: string;
|
||||
@@ -162,8 +664,9 @@ interface Issue {
|
||||
}
|
||||
|
||||
interface IssueCreated {
|
||||
action: "opened";
|
||||
issue: Issue;
|
||||
action: "opened";
|
||||
issue: Issue;
|
||||
repository: Repository;
|
||||
}
|
||||
|
||||
interface PullRequest {
|
||||
@@ -669,6 +1172,7 @@ interface PullRequestCreated {
|
||||
action: "opened";
|
||||
number: number;
|
||||
pull_request: PullRequest;
|
||||
repository: Repository;
|
||||
}
|
||||
|
||||
type Ping = Record<string, unknown>;
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import type { Amari } from "../interfaces/amari.js";
|
||||
import type { IssueCreated,
|
||||
PullRequestCreated,
|
||||
GithubPayload } from "../interfaces/github.js";
|
||||
@@ -19,10 +20,12 @@ const isPull = (body: GithubPayload): body is PullRequestCreated => {
|
||||
|
||||
/**
|
||||
* Handles a payload from a GitHub webhook.
|
||||
* @param amari - Amari's instance.
|
||||
* @param request - The Fastify request payload.
|
||||
* @param response - The Fastify reply class.
|
||||
*/
|
||||
export const processGithubEvent = async(
|
||||
amari: Amari,
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Fastify standard.
|
||||
request: FastifyRequest<{ Body: GithubPayload }>,
|
||||
response: FastifyReply,
|
||||
@@ -37,7 +40,30 @@ export const processGithubEvent = async(
|
||||
await response.status(200).send({ message: "Pong!" });
|
||||
return;
|
||||
}
|
||||
const { action: _action } = request.body;
|
||||
isIssue(request.body);
|
||||
isPull(request.body);
|
||||
const { action } = request.body;
|
||||
await response.status(200).send({ message: "Payload received!" });
|
||||
if (action === "opened" && event === "issue" && isIssue(request.body)) {
|
||||
const { issue, repository } = request.body;
|
||||
const { number } = issue;
|
||||
const { owner, name } = repository;
|
||||
await amari.github.rest.issues.addAssignees({
|
||||
assignees: [ "naomi-lgbt" ],
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Github SDK requirement.
|
||||
issue_number: number,
|
||||
owner: owner.login,
|
||||
repo: name,
|
||||
});
|
||||
}
|
||||
if (action === "opened" && event === "pull_request" && isPull(request.body)) {
|
||||
const { pull_request: pr, repository } = request.body;
|
||||
const { number } = pr;
|
||||
const { owner, name } = repository;
|
||||
await amari.github.rest.pulls.requestReviewers({
|
||||
owner: owner.login,
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Github SDK requirement.
|
||||
pull_number: number,
|
||||
repo: name,
|
||||
reviewers: [ "naomi-lgbt" ],
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
+11
-2
@@ -7,6 +7,7 @@
|
||||
import fastify from "fastify";
|
||||
import { processGithubEvent } from "../modules/processGitHubEvent.js";
|
||||
import { logger } from "../utils/logger.js";
|
||||
import type { Amari } from "../interfaces/amari.js";
|
||||
import type { GithubPayload } from "../interfaces/github.js";
|
||||
|
||||
const html = `<!DOCTYPE html>
|
||||
@@ -52,8 +53,9 @@ const html = `<!DOCTYPE html>
|
||||
|
||||
/**
|
||||
* Starts up a web server for health monitoring.
|
||||
* @param amari - Amari's instance.
|
||||
*/
|
||||
export const instantiateServer = (): void => {
|
||||
export const instantiateServer = (amari: Amari): void => {
|
||||
try {
|
||||
const server = fastify({
|
||||
logger: false,
|
||||
@@ -67,7 +69,14 @@ export const instantiateServer = (): void => {
|
||||
server.
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Fastify standard.
|
||||
post<{ Body: GithubPayload }>("/github", async(request, response) => {
|
||||
await processGithubEvent(request, response);
|
||||
try {
|
||||
await processGithubEvent(amari, request, response);
|
||||
} catch (error) {
|
||||
if (!(error instanceof Error)) {
|
||||
return;
|
||||
}
|
||||
await logger.error("/github route", error);
|
||||
}
|
||||
});
|
||||
|
||||
server.listen({ port: 7044 }, (error) => {
|
||||
|
||||
Reference in New Issue
Block a user