Skip to content

[Projects] Fix prebuild association #5054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is undocumented stuff, I'm afraid. The event payload contains:

"changes": { "repository": { "name": { "from": "test-repo-123" } } }

To implement this in a robust way, we'd need to store repository.id to the project. next to the cloneUrl.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense. maybe put this as a TODO into the code

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