Skip to content

Commit e025ceb

Browse files
committed
fixup: circular dependency problem
1 parent 5306aea commit e025ceb

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
@@ -25,7 +25,6 @@ import { ErrorCodes, ApplicationError } from "@gitpod/gitpod-protocol/lib/messag
2525
import { URL } from "url";
2626
import { Authorizer } from "../authorization/authorizer";
2727
import { TransactionalContext } from "@gitpod/gitpod-db/lib/typeorm/transactional-db-impl";
28-
import { GitHubAppSupport } from "../github/github-app-support";
2928

3029
@injectable()
3130
export class ProjectsService {
@@ -37,7 +36,6 @@ export class ProjectsService {
3736
@inject(IAnalyticsWriter) private readonly analytics: IAnalyticsWriter,
3837
@inject(WebhookEventDB) private readonly webhookEventDB: WebhookEventDB,
3938
@inject(Authorizer) private readonly auth: Authorizer,
40-
@inject(GitHubAppSupport) private readonly githubAppSupport: GitHubAppSupport,
4139
) {}
4240

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

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2643,7 +2643,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
26432643
} catch (err) {
26442644
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "Clone URL must be a valid URL.");
26452645
}
2646-
const canCreateProject = await this.projectsService.canCreateProject(user, params.cloneUrl);
2646+
const canCreateProject = await this.canCreateProject(user, params.cloneUrl);
26472647
if (!canCreateProject) {
26482648
throw new ApplicationError(
26492649
ErrorCodes.BAD_REQUEST,
@@ -2673,6 +2673,35 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
26732673
return project;
26742674
}
26752675

2676+
/**
2677+
* Checks if a project can be created, i.e. the current user has the required permissions
2678+
* at the given git provider.
2679+
*/
2680+
private async canCreateProject(currentUser: User, cloneURL: string) {
2681+
try {
2682+
const parsedUrl = RepoURL.parseRepoUrl(cloneURL);
2683+
const host = parsedUrl?.host;
2684+
if (!host) {
2685+
throw Error("Unknown host: " + parsedUrl?.host);
2686+
}
2687+
if (host === "github.com" && this.config.githubApp?.enabled) {
2688+
const availableRepositories = await this.githubAppSupport.getProviderRepositoriesForUser({
2689+
user: currentUser,
2690+
provider: "github.com",
2691+
});
2692+
return availableRepositories.some((r) => r.cloneUrl === cloneURL);
2693+
} else {
2694+
return await this.projectsService.canCreateProject(currentUser, cloneURL);
2695+
2696+
// note: the GitHub App based check is not included in the ProjectService due
2697+
// to a circular dependency problem which would otherwise occur.
2698+
}
2699+
} catch (error) {
2700+
log.error("Failed to check precondition for creating a project.");
2701+
}
2702+
return false;
2703+
}
2704+
26762705
public async updateProjectPartial(ctx: TraceContext, partialProject: PartialProject): Promise<void> {
26772706
traceAPIParams(ctx, {
26782707
// censor everything irrelevant

0 commit comments

Comments
 (0)