Skip to content

Commit

Permalink
feat(InstallationManager): add account filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
ijsKoud committed Jun 21, 2023
1 parent 36cef24 commit 5222259
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
5 changes: 4 additions & 1 deletion packages/octocat/src/lib/Octocat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class Octocat {
clientSecret: options.clientSecret
});

this.installations = new InstallationManager(this.octokit);
this.installations = new InstallationManager(this, options.allowedInstallations);
}

/**
Expand Down Expand Up @@ -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[];
}
28 changes: 25 additions & 3 deletions packages/octocat/src/lib/managers/InstallationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, GitHubInstallation>();

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)));
}
Expand Down

0 comments on commit 5222259

Please sign in to comment.