Fetch the app name on startup

This commit is contained in:
Will Hunt 2021-12-01 17:31:12 +00:00
parent 4604268df1
commit 49ef4cf970
4 changed files with 11 additions and 6 deletions

View File

@ -6,7 +6,6 @@ import { configKey } from "./Decorators";
interface BridgeConfigGitHubYAML { interface BridgeConfigGitHubYAML {
auth: { auth: {
name: string;
id: number|string; id: number|string;
privateKeyFile: string; privateKeyFile: string;
}; };
@ -29,7 +28,6 @@ interface BridgeConfigGitHubYAML {
export class BridgeConfigGitHub { export class BridgeConfigGitHub {
@configKey("Authentication for the GitHub App.", false) @configKey("Authentication for the GitHub App.", false)
auth: { auth: {
name: string;
id: number|string; id: number|string;
privateKeyFile: string; privateKeyFile: string;
}; };

View File

@ -36,7 +36,6 @@ export const DefaultConfig = new BridgeConfig({
}, },
github: { github: {
auth: { auth: {
name: 'my-app-name',
id: 123, id: 123,
privateKeyFile: "github-key.pem", privateKeyFile: "github-key.pem",
}, },

View File

@ -139,8 +139,6 @@ export class GitHubRepoConnection extends CommandConnection implements IConnecti
throw new ApiError("Could not determine if the user has access to this repository, does the repository exist?", ErrCode.ForbiddenUser); throw new ApiError("Could not determine if the user has access to this repository, does the repository exist?", ErrCode.ForbiddenUser);
} }
octokit.apps.addRepoToInstallationForAuthenticatedUser()
if (permissionLevel !== "admin" && permissionLevel !== "write") { if (permissionLevel !== "admin" && permissionLevel !== "write") {
throw new ApiError("You must at least have write permissions to bridge this repository", ErrCode.ForbiddenUser); throw new ApiError("You must at least have write permissions to bridge this repository", ErrCode.ForbiddenUser);
} }
@ -152,7 +150,7 @@ export class GitHubRepoConnection extends CommandConnection implements IConnecti
-1, -1,
{ {
// E.g. https://github.com/apps/matrix-bridge/installations/new // E.g. https://github.com/apps/matrix-bridge/installations/new
installUrl: `https://github.com/apps/${config.auth.name}/installations/new`, installUrl: `https://github.com/apps/${githubInstance.appName}/installations/new`,
} }
); );
} }

View File

@ -23,11 +23,16 @@ export class GithubInstance {
private internalOctokit!: Octokit; private internalOctokit!: Octokit;
private readonly installationsCache = new Map<number, Installation>(); private readonly installationsCache = new Map<number, Installation>();
private internalAppName?: string;
constructor (private readonly appId: number|string, private readonly privateKey: string) { constructor (private readonly appId: number|string, private readonly privateKey: string) {
this.appId = parseInt(appId as string, 10); this.appId = parseInt(appId as string, 10);
} }
public get appName() {
return this.internalAppName;
}
public static createUserOctokit(token: string) { public static createUserOctokit(token: string) {
return new Octokit({ return new Octokit({
// XXX: A recent release of octokit (rest/auth-token?) broke passing in the token // XXX: A recent release of octokit (rest/auth-token?) broke passing in the token
@ -76,12 +81,17 @@ export class GithubInstance {
privateKey: this.privateKey, privateKey: this.privateKey,
}; };
this.internalOctokit = new Octokit({ this.internalOctokit = new Octokit({
authStrategy: createAppAuth, authStrategy: createAppAuth,
auth, auth,
userAgent: USER_AGENT, userAgent: USER_AGENT,
}); });
const appDetails = await this.internalOctokit.apps.getAuthenticated();
this.internalAppName = appDetails.data.name;
let installPageSize = 100; let installPageSize = 100;
let page = 1; let page = 1;
do { do {