From 8448f30fe21847a8c6a2450cf7323ce992e31f9f Mon Sep 17 00:00:00 2001 From: Alex Tugarev Date: Thu, 5 Aug 2021 15:37:52 +0000 Subject: [PATCH] [projects/prebuilds] get rid of findProjectByInstallationId --- components/gitpod-db/src/project-db.ts | 1 - .../gitpod-db/src/typeorm/project-db-impl.ts | 5 ---- .../server/ee/src/prebuilds/github-app.ts | 27 ++++++++++++------- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/components/gitpod-db/src/project-db.ts b/components/gitpod-db/src/project-db.ts index 2243e58506db04..4aed843c05dd51 100644 --- a/components/gitpod-db/src/project-db.ts +++ b/components/gitpod-db/src/project-db.ts @@ -12,7 +12,6 @@ export interface ProjectDB { findProjectByCloneUrl(cloneUrl: string): Promise; findProjectsByCloneUrls(cloneUrls: string[]): Promise; findProjectByTeamAndName(teamId: string, projectName: string): Promise; - findProjectByInstallationId(installationId: string): Promise; findTeamProjects(teamId: string): Promise; findUserProjects(userId: string): Promise; storeProject(project: Project): Promise; diff --git a/components/gitpod-db/src/typeorm/project-db-impl.ts b/components/gitpod-db/src/typeorm/project-db-impl.ts index 8a34faebf6f652..ef69e89bd9ad82 100644 --- a/components/gitpod-db/src/typeorm/project-db-impl.ts +++ b/components/gitpod-db/src/typeorm/project-db-impl.ts @@ -50,11 +50,6 @@ export class ProjectDBImpl implements ProjectDB { return projects.find(p => p.name === projectName); } - public async findProjectByInstallationId(appInstallationId: string): Promise { - const repo = await this.getRepo(); - return repo.findOne({ appInstallationId, markedDeleted: false }); - } - public async findTeamProjects(teamId: string): Promise { const repo = await this.getRepo(); return repo.find({ teamId, markedDeleted: false }); diff --git a/components/server/ee/src/prebuilds/github-app.ts b/components/server/ee/src/prebuilds/github-app.ts index 56f4279aa3a26c..c28de3fed714c9 100644 --- a/components/server/ee/src/prebuilds/github-app.ts +++ b/components/server/ee/src/prebuilds/github-app.ts @@ -106,10 +106,16 @@ export class GithubApp { return; } if (action === "renamed") { - const project = await this.projectDB.findProjectByInstallationId(String(installation.id)) - if (project) { - project.cloneUrl = repository.clone_url; - await this.projectDB.storeProject(project); + // HINT(AT): This is undocumented, but the event payload contains something like + // "changes": { "repository": { "name": { "from": "test-repo-123" } } } + // To implement this in a more robust way, we'd need to store `repository.id` with the project, next to the cloneUrl. + const oldName = (ctx.payload as any)?.changes?.repository?.name?.from; + if (oldName) { + const project = await this.projectDB.findProjectByCloneUrl(`https://github.com/${repository.owner.login}/${oldName}.git`) + if (project) { + project.cloneUrl = repository.clone_url; + await this.projectDB.storeProject(project); + } } } // TODO(at): handle deleted as well @@ -155,7 +161,8 @@ export class GithubApp { try { const installationId = ctx.payload.installation?.id; - const owner = installationId && (await this.findInstallationOwner(installationId)); + const cloneURL = ctx.payload.repository.clone_url; + const owner = installationId && (await this.findProjectOwner(cloneURL) || (await this.findInstallationOwner(installationId))); if (!owner) { log.info(`No installation or associated user found.`, { repo: ctx.payload.repository, installationId }); return; @@ -213,7 +220,8 @@ export class GithubApp { try { const installationId = ctx.payload.installation?.id; - const owner = installationId && (await this.findInstallationOwner(installationId)); + const cloneURL = ctx.payload.repository.clone_url; + const owner = installationId && (await this.findProjectOwner(cloneURL) || (await this.findInstallationOwner(installationId))); if (!owner) { log.warn("Did not find user for installation. Someone's Gitpod experience may be broken.", { repo: ctx.payload.repository, installationId }); return; @@ -329,11 +337,10 @@ export class GithubApp { return this.env.hostUrl.with({ pathname: '/button/open-in-gitpod.svg' }).toString(); } - protected async findInstallationOwner(installationId: number): Promise<{user: User, project?: Project} | undefined> { - + protected async findProjectOwner(cloneURL: string): Promise<{user: User, project?: Project} | undefined> { // Project mode // - const project = await this.projectDB.findProjectByInstallationId(String(installationId)); + const project = await this.projectDB.findProjectByCloneUrl(cloneURL); if (project) { const owner = !!project.userId ? { userId: project.userId } @@ -346,6 +353,8 @@ export class GithubApp { } } + } + protected async findInstallationOwner(installationId: number): Promise<{user: User, project?: Project} | undefined> { // Legacy mode // const installation = await this.appInstallationDB.findInstallation("github", String(installationId));