From 5222259427d39cfbde180cdb839eb9cd23dfbe42 Mon Sep 17 00:00:00 2001 From: Daan Klarenbeek Date: Wed, 21 Jun 2023 15:28:14 +0200 Subject: [PATCH] feat(InstallationManager): add account filtering --- packages/octocat/src/lib/Octocat.ts | 5 +++- .../src/lib/managers/InstallationManager.ts | 28 +++++++++++++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/packages/octocat/src/lib/Octocat.ts b/packages/octocat/src/lib/Octocat.ts index e2e920bb..103be76e 100644 --- a/packages/octocat/src/lib/Octocat.ts +++ b/packages/octocat/src/lib/Octocat.ts @@ -21,7 +21,7 @@ export class Octocat { clientSecret: options.clientSecret }); - this.installations = new InstallationManager(this.octokit); + this.installations = new InstallationManager(this, options.allowedInstallations); } /** @@ -52,4 +52,7 @@ export interface OctocatOptions { /** The Redis database url */ redisUrl: string; + + /** A list of account names which are allowed to be loaded */ + allowedInstallations?: string[]; } diff --git a/packages/octocat/src/lib/managers/InstallationManager.ts b/packages/octocat/src/lib/managers/InstallationManager.ts index 26f9242c..9ec56a10 100644 --- a/packages/octocat/src/lib/managers/InstallationManager.ts +++ b/packages/octocat/src/lib/managers/InstallationManager.ts @@ -7,21 +7,43 @@ import { LABEL_CONFIG_LOCATION, README_TEMPLATE_LOCATION } from "@ijsblokje/util import type { Endpoints } from "@octokit/types"; import type { Octokit } from "@ijsblokje/octokit"; import { request } from "@octokit/request"; +import type { Octocat } from "../Octocat.js"; export class InstallationManager { /** The octokit instance that handles all GitHub requests */ public readonly octokit: Octokit; + /** The Octocat instance that initialised this manager */ + public readonly octocat: Octocat; + /** Collection containing cached github installations */ public readonly cache = new Collection(); - public constructor(octokit: Octokit) { - this.octokit = octokit; + /** An array of accounts which are allowed to be loaded */ + public readonly allowedInstallations?: string[]; + + public constructor(octocat: Octocat, allowedInstallations?: string[]) { + this.octocat = octocat; + this.octokit = octocat.octokit; + this.allowedInstallations = allowedInstallations; } + /** + * Loads all the GitHub app installations + */ public async loadAll() { - const installations = await this.getInstallations(); + let installations = await this.getInstallations(); if (!installations) return; + if (this.allowedInstallations) { + const filterFn = (installation: ListInstallationsItem) => { + if (!installation.account) return false; + if (!("login" in installation.account)) return false; + + return this.allowedInstallations!.includes(installation.account.login); + }; + + installations = installations.filter(filterFn.bind(this)); + } await Promise.all(installations.map(this.loadInstallation.bind(this))); }