Skip to content

Commit

Permalink
[github-app] change findInstallation semantics
Browse files Browse the repository at this point in the history
to return any non-uninstalled record which may now be used to identify the installer of an GitHub App installation.
  • Loading branch information
AlexTugarev authored and roboquat committed Feb 11, 2022
1 parent 6f11cbb commit ebf351e
Showing 1 changed file with 7 additions and 29 deletions.
36 changes: 7 additions & 29 deletions components/gitpod-db/src/typeorm/app-installation-db-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,37 +39,15 @@ export class TypeORMAppInstallationDBImpl implements AppInstallationDB {
await repo.insert(obj);
}

protected async findAndFinishInstallation(platform: AppInstallationPlatform, installationID: string): Promise<AppInstallation | undefined> {
// check if we find the complementary installation entries. If so, finish the installation
public async findInstallation(platform: AppInstallationPlatform, installationID: string): Promise<AppInstallation | undefined> {
const repo = await this.getRepo();
const installationRecords = await repo.find({ where: { platform, installationID } });

// maybe we're already done and have a finished installation
const finishedInstallation = installationRecords.find(r => r.state == 'installed');
if (!!finishedInstallation) {
return finishedInstallation;
}

// maybe we need to finish an existing/ongoing installation
const platformClaim = installationRecords.find(r => r.state == 'claimed.platform');
const userClaim = installationRecords.find(r => r.state == 'claimed.user');
if (!!platformClaim && !!userClaim) {
const obj = new DBAppInstallation();
obj.platform = platform;
obj.installationID = installationID;
obj.state = 'installed';
obj.ownerUserID = userClaim.ownerUserID || platformClaim.ownerUserID || undefined;
obj.platformUserID = platformClaim.platformUserID;
obj.creationTime = new Date().toISOString();
return await repo.save(obj);
}

// we do not have a finished installation here
return undefined;
}
const qb = repo.createQueryBuilder('installation')
.where("installation.installationID = :installationID", { installationID })
.andWhere('installation.state != "uninstalled"')
.orderBy("installation.lastUpdateTime", "DESC")
.limit(1);

public async findInstallation(platform: AppInstallationPlatform, installationID: string): Promise<AppInstallation | undefined> {
return this.findAndFinishInstallation(platform, installationID);
return (await qb.getMany())[0];
}

public async recordUninstallation(platform: AppInstallationPlatform, source: 'user' | 'platform', installationID: string) {
Expand Down

0 comments on commit ebf351e

Please sign in to comment.