Skip to content

Commit 34e1657

Browse files
committed
fixup: circular dependency problem
1 parent 8c04321 commit 34e1657

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

components/server/src/projects/projects-service.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import { ErrorCodes, ApplicationError } from "@gitpod/gitpod-protocol/lib/messag
2626
import { URL } from "url";
2727
import { Authorizer } from "../authorization/authorizer";
2828
import { TransactionalContext } from "@gitpod/gitpod-db/lib/typeorm/transactional-db-impl";
29-
import { GitHubAppSupport } from "../github/github-app-support";
3029

3130
@injectable()
3231
export class ProjectsService {
@@ -38,7 +37,6 @@ export class ProjectsService {
3837
@inject(IAnalyticsWriter) private readonly analytics: IAnalyticsWriter,
3938
@inject(WebhookEventDB) private readonly webhookEventDB: WebhookEventDB,
4039
@inject(Authorizer) private readonly auth: Authorizer,
41-
@inject(GitHubAppSupport) private readonly githubAppSupport: GitHubAppSupport,
4240
) {}
4341

4442
async getProject(userId: string, projectId: string): Promise<Project> {
@@ -217,13 +215,7 @@ export class ProjectsService {
217215
return await repositoryService.canInstallAutomatedPrebuilds(currentUser, cloneURL);
218216
}
219217
}
220-
if (host === "github.com" && this.config.githubApp?.enabled) {
221-
const availableRepositories = await this.githubAppSupport.getProviderRepositoriesForUser({
222-
user: currentUser,
223-
provider: "github.com",
224-
});
225-
return availableRepositories.some((r) => r.cloneUrl === cloneURL);
226-
}
218+
// The GitHub App case isn't handled here due to a circular dependency problem.
227219
} catch (error) {
228220
log.error("Failed to check precondition for creating a project.");
229221
}

components/server/src/workspace/gitpod-server-impl.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2793,7 +2793,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
27932793
} catch (err) {
27942794
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "Clone URL must be a valid URL.");
27952795
}
2796-
const canCreateProject = await this.projectsService.canCreateProject(user, params.cloneUrl);
2796+
const canCreateProject = await this.canCreateProject(user, params.cloneUrl);
27972797
if (!canCreateProject) {
27982798
throw new ApplicationError(
27992799
ErrorCodes.BAD_REQUEST,
@@ -2823,6 +2823,35 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
28232823
return project;
28242824
}
28252825

2826+
/**
2827+
* Checks if a project can be created, i.e. the current user has the required permissions
2828+
* at the given git provider.
2829+
*/
2830+
private async canCreateProject(currentUser: User, cloneURL: string) {
2831+
try {
2832+
const parsedUrl = RepoURL.parseRepoUrl(cloneURL);
2833+
const host = parsedUrl?.host;
2834+
if (!host) {
2835+
throw Error("Unknown host: " + parsedUrl?.host);
2836+
}
2837+
if (host === "github.com" && this.config.githubApp?.enabled) {
2838+
const availableRepositories = await this.githubAppSupport.getProviderRepositoriesForUser({
2839+
user: currentUser,
2840+
provider: "github.com",
2841+
});
2842+
return availableRepositories.some((r) => r.cloneUrl === cloneURL);
2843+
} else {
2844+
return await this.projectsService.canCreateProject(currentUser, cloneURL);
2845+
2846+
// note: the GitHub App based check is not included in the ProjectService due
2847+
// to a circular dependency problem which would otherwise occur.
2848+
}
2849+
} catch (error) {
2850+
log.error("Failed to check precondition for creating a project.");
2851+
}
2852+
return false;
2853+
}
2854+
28262855
public async updateProjectPartial(ctx: TraceContext, partialProject: PartialProject): Promise<void> {
28272856
traceAPIParams(ctx, {
28282857
// censor everything irrelevant

0 commit comments

Comments
 (0)