|
4 | 4 | * See License.AGPL.txt in the project root for license information.
|
5 | 5 | */
|
6 | 6 |
|
7 |
| -import { Project, User } from "@gitpod/gitpod-protocol"; |
| 7 | +import { CommitContext, Project, SuggestedRepository, User, WorkspaceInfo } from "@gitpod/gitpod-protocol"; |
8 | 8 | import { RepoURL } from "../repohost";
|
9 | 9 | import { inject, injectable } from "inversify";
|
10 | 10 | import { HostContextProvider } from "../auth/host-context-provider";
|
11 | 11 | import { Config } from "../config";
|
12 |
| -import { log } from "@gitpod/gitpod-protocol/lib/util/logging"; |
| 12 | +import { LogContext, log } from "@gitpod/gitpod-protocol/lib/util/logging"; |
| 13 | +import { ProjectsService } from "./projects-service"; |
| 14 | +import { WorkspaceService } from "../workspace/workspace-service"; |
| 15 | +import { AuthProviderService } from "../auth/auth-provider-service"; |
| 16 | +import { TraceContext } from "@gitpod/gitpod-protocol/lib/util/tracing"; |
| 17 | + |
| 18 | +type SuggestedRepositoryWithSorting = SuggestedRepository & { |
| 19 | + priority: number; |
| 20 | + lastUse?: string; |
| 21 | +}; |
13 | 22 |
|
14 | 23 | @injectable()
|
15 | 24 | export class ScmService {
|
16 | 25 | constructor(
|
17 | 26 | @inject(HostContextProvider) private readonly hostContextProvider: HostContextProvider,
|
18 | 27 | @inject(Config) private readonly config: Config,
|
| 28 | + @inject(ProjectsService) private readonly projectsService: ProjectsService, |
| 29 | + @inject(WorkspaceService) private readonly workspaceService: WorkspaceService, |
| 30 | + @inject(AuthProviderService) private readonly authProviderService: AuthProviderService, |
19 | 31 | ) {}
|
20 | 32 |
|
21 | 33 | async canInstallWebhook(currentUser: User, cloneURL: string) {
|
@@ -74,4 +86,156 @@ export class ScmService {
|
74 | 86 | }
|
75 | 87 | }
|
76 | 88 | }
|
| 89 | + |
| 90 | + public async getSuggestedRepositories(ctx: TraceContext, user: User, organizationId: string) { |
| 91 | + const logCtx: LogContext = { userId: user.id }; |
| 92 | + const span = TraceContext.startSpan("SCMService.getSuggestedRepositories", ctx); |
| 93 | + |
| 94 | + const repoResults = await Promise.allSettled([ |
| 95 | + this.fetchProjects(ctx, user, organizationId).catch((e) => |
| 96 | + log.error(logCtx, "Could not fetch projects", e), |
| 97 | + ), |
| 98 | + this.fetchUserRepos(ctx, user).catch((e) => log.error(logCtx, "Could not fetch user repositories", e)), |
| 99 | + this.fetchRecentRepos(ctx, user, organizationId).catch((e) => |
| 100 | + log.error(logCtx, "Could not fetch recent repositories", e), |
| 101 | + ), |
| 102 | + ]); |
| 103 | + |
| 104 | + const sortedRepos = repoResults |
| 105 | + // filter out rejected promises |
| 106 | + .map((r) => (r.status === "fulfilled" ? r.value || [] : [])) |
| 107 | + // flatten out results from all promises |
| 108 | + .flat() |
| 109 | + .sort((a, b) => { |
| 110 | + // priority first |
| 111 | + if (a.priority !== b.priority) { |
| 112 | + return a.priority < b.priority ? 1 : -1; |
| 113 | + } |
| 114 | + // Most recently used second |
| 115 | + if (b.lastUse || a.lastUse) { |
| 116 | + const la = a.lastUse || ""; |
| 117 | + const lb = b.lastUse || ""; |
| 118 | + return la < lb ? 1 : la === lb ? 0 : -1; |
| 119 | + } |
| 120 | + // Otherwise, alphasort |
| 121 | + const ua = a.url.toLowerCase(); |
| 122 | + const ub = b.url.toLowerCase(); |
| 123 | + return ua > ub ? 1 : ua === ub ? 0 : -1; |
| 124 | + }); |
| 125 | + |
| 126 | + const uniqueRepositories = new Map<string, SuggestedRepositoryWithSorting>(); |
| 127 | + |
| 128 | + for (const repo of sortedRepos) { |
| 129 | + const existingRepo = uniqueRepositories.get(repo.url); |
| 130 | + |
| 131 | + uniqueRepositories.set(repo.url, { |
| 132 | + ...(existingRepo || {}), |
| 133 | + ...repo, |
| 134 | + }); |
| 135 | + } |
| 136 | + |
| 137 | + // Convert to return type |
| 138 | + const result = Array.from(uniqueRepositories.values()).map( |
| 139 | + (repo): SuggestedRepository => ({ |
| 140 | + url: repo.url, |
| 141 | + projectId: repo.projectId, |
| 142 | + projectName: repo.projectName, |
| 143 | + }), |
| 144 | + ); |
| 145 | + |
| 146 | + span.finish(); |
| 147 | + |
| 148 | + return result; |
| 149 | + } |
| 150 | + |
| 151 | + private async fetchProjects( |
| 152 | + ctx: TraceContext, |
| 153 | + user: User, |
| 154 | + organizationId: string, |
| 155 | + ): Promise<SuggestedRepositoryWithSorting[]> { |
| 156 | + const span = TraceContext.startSpan("SCMService.fetchProjects", ctx); |
| 157 | + const projects = await this.projectsService.getProjects(user.id, organizationId); |
| 158 | + span.finish(); |
| 159 | + |
| 160 | + return projects.map((project) => ({ |
| 161 | + url: project.cloneUrl.replace(/\.git$/, ""), |
| 162 | + projectId: project.id, |
| 163 | + projectName: project.name, |
| 164 | + priority: 1, |
| 165 | + })); |
| 166 | + } |
| 167 | + |
| 168 | + // Load user repositories (from Git hosts directly) |
| 169 | + private async fetchUserRepos(ctx: TraceContext, user: User): Promise<SuggestedRepositoryWithSorting[]> { |
| 170 | + const span = TraceContext.startSpan("SCMService.fetchUserRepos", ctx); |
| 171 | + const logCtx: LogContext = { userId: user.id }; |
| 172 | + |
| 173 | + const authProviders = await this.authProviderService.getAuthProvidersInfo(user); |
| 174 | + |
| 175 | + const providerRepos = await Promise.all( |
| 176 | + authProviders.map(async (p): Promise<SuggestedRepositoryWithSorting[]> => { |
| 177 | + try { |
| 178 | + span.setTag("host", p.host); |
| 179 | + |
| 180 | + const hostContext = this.hostContextProvider.get(p.host); |
| 181 | + const services = hostContext?.services; |
| 182 | + if (!services) { |
| 183 | + log.error(logCtx, "Unsupported repository host: " + p.host); |
| 184 | + return []; |
| 185 | + } |
| 186 | + const userRepos = await services.repositoryProvider.getUserRepos(user); |
| 187 | + |
| 188 | + return userRepos.map((r) => ({ |
| 189 | + url: r.replace(/\.git$/, ""), |
| 190 | + priority: 5, |
| 191 | + })); |
| 192 | + } catch (error) { |
| 193 | + log.debug(logCtx, "Could not get user repositories from host " + p.host, error); |
| 194 | + } |
| 195 | + |
| 196 | + return []; |
| 197 | + }), |
| 198 | + ); |
| 199 | + |
| 200 | + span.finish(); |
| 201 | + |
| 202 | + return providerRepos.flat(); |
| 203 | + } |
| 204 | + |
| 205 | + private async fetchRecentRepos( |
| 206 | + ctx: TraceContext, |
| 207 | + user: User, |
| 208 | + organizationId: string, |
| 209 | + ): Promise<SuggestedRepositoryWithSorting[]> { |
| 210 | + const span = TraceContext.startSpan("SCMService.fetchRecentRepos", ctx); |
| 211 | + |
| 212 | + // TODO: do we need to check permissions on each ws here like we do in gitpod server? |
| 213 | + const workspaces = await this.workspaceService.getWorkspaces(user.id, { organizationId }); |
| 214 | + |
| 215 | + const recentRepos: SuggestedRepositoryWithSorting[] = []; |
| 216 | + |
| 217 | + for (const ws of workspaces) { |
| 218 | + let repoUrl; |
| 219 | + if (CommitContext.is(ws.workspace.context)) { |
| 220 | + repoUrl = ws.workspace.context?.repository?.cloneUrl?.replace(/\.git$/, ""); |
| 221 | + } |
| 222 | + if (!repoUrl) { |
| 223 | + repoUrl = ws.workspace.contextURL; |
| 224 | + } |
| 225 | + if (repoUrl) { |
| 226 | + const lastUse = WorkspaceInfo.lastActiveISODate(ws); |
| 227 | + |
| 228 | + recentRepos.push({ |
| 229 | + url: repoUrl, |
| 230 | + projectId: ws.workspace.projectId, |
| 231 | + priority: 10, |
| 232 | + lastUse, |
| 233 | + }); |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + span.finish(); |
| 238 | + |
| 239 | + return recentRepos; |
| 240 | + } |
77 | 241 | }
|
0 commit comments