Skip to content

Commit

Permalink
[projects/prebuilds] get rid of findProjectByInstallationId
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexTugarev authored and roboquat committed Aug 5, 2021
1 parent d4c5800 commit 8448f30
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
1 change: 0 additions & 1 deletion components/gitpod-db/src/project-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export interface ProjectDB {
findProjectByCloneUrl(cloneUrl: string): Promise<Project | undefined>;
findProjectsByCloneUrls(cloneUrls: string[]): Promise<Project[]>;
findProjectByTeamAndName(teamId: string, projectName: string): Promise<Project | undefined>;
findProjectByInstallationId(installationId: string): Promise<Project | undefined>;
findTeamProjects(teamId: string): Promise<Project[]>;
findUserProjects(userId: string): Promise<Project[]>;
storeProject(project: Project): Promise<Project>;
Expand Down
5 changes: 0 additions & 5 deletions components/gitpod-db/src/typeorm/project-db-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ export class ProjectDBImpl implements ProjectDB {
return projects.find(p => p.name === projectName);
}

public async findProjectByInstallationId(appInstallationId: string): Promise<Project | undefined> {
const repo = await this.getRepo();
return repo.findOne({ appInstallationId, markedDeleted: false });
}

public async findTeamProjects(teamId: string): Promise<Project[]> {
const repo = await this.getRepo();
return repo.find({ teamId, markedDeleted: false });
Expand Down
27 changes: 18 additions & 9 deletions components/server/ee/src/prebuilds/github-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 }
Expand All @@ -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));
Expand Down

0 comments on commit 8448f30

Please sign in to comment.