Skip to content

Commit f6990e7

Browse files
committed
[projects] serve GitLab Projects
1 parent 4995b8e commit f6990e7

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

components/dashboard/src/projects/NewProject.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ export default function NewProject() {
7777
setRepoSearchFilter("");
7878
}, [selectedAccount]);
7979

80+
useEffect(() => {
81+
(async () => {
82+
const repos = await updateReposInAccounts();
83+
const first = repos[0];
84+
if (first) {
85+
setSelectedAccount(first.account);
86+
}
87+
})();
88+
}, [provider]);
89+
8090
const isGitHub = () => provider === "github.com";
8191

8292
const updateReposInAccounts = async (installationId?: string) => {
@@ -263,6 +273,8 @@ export default function NewProject() {
263273
</span>
264274
<br/>
265275
<button className="mt-6" onClick={() => reconfigure()}>Authorize Provider</button>
276+
<br/>
277+
<button className="mt-6" onClick={() => setShowGitProviders(true)}>Select Git Provider</button>
266278
</div>
267279
</div>
268280
</div>)
@@ -275,7 +287,7 @@ export default function NewProject() {
275287
}
276288

277289
return (<>
278-
{(loaded && empty) ? renderEmptyState() : (showGitProviders ? (<GitProviders onHostSelected={onGitProviderSeleted} />) : renderRepos())}
290+
{(loaded && empty && !showGitProviders) ? renderEmptyState() : (showGitProviders ? (<GitProviders onHostSelected={onGitProviderSeleted} />) : renderRepos())}
279291
</>)
280292
};
281293

components/server/ee/src/container-module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import { BlockedUserFilter } from "../../src/auth/blocked-user-filter";
5353
import { EMailDomainService, EMailDomainServiceImpl } from "./auth/email-domain-service";
5454
import { UserDeletionServiceEE } from "./user/user-deletion-service";
5555
import { GitHubAppSupport } from "./github/github-app-support";
56+
import { GitLabAppSupport } from "./gitlab/gitlab-app-support";
5657

5758
export const productionEEContainerModule = new ContainerModule((bind, unbind, isBound, rebind) => {
5859
rebind(Server).to(ServerEE).inSingletonScope();
@@ -71,6 +72,7 @@ export const productionEEContainerModule = new ContainerModule((bind, unbind, is
7172
bind(GithubAppRules).toSelf().inSingletonScope();
7273
bind(PrebuildStatusMaintainer).toSelf().inSingletonScope();
7374
bind(GitLabApp).toSelf().inSingletonScope();
75+
bind(GitLabAppSupport).toSelf().inSingletonScope();
7476
bind(BitbucketApp).toSelf().inSingletonScope();
7577

7678
bind(LicenseEvaluator).toSelf().inSingletonScope();
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright (c) 2020 Gitpod GmbH. All rights reserved.
3+
* Licensed under the Gitpod Enterprise Source Code License,
4+
* See License.enterprise.txt in the project root folder.
5+
*/
6+
7+
import { ProviderRepository, User } from "@gitpod/gitpod-protocol";
8+
import { inject, injectable } from "inversify";
9+
import { TokenProvider } from "../../../src/user/token-provider";
10+
import { UserDB } from "@gitpod/gitpod-db/lib";
11+
import { Gitlab } from "@gitbeaker/node";
12+
13+
@injectable()
14+
export class GitLabAppSupport {
15+
16+
@inject(UserDB) protected readonly userDB: UserDB;
17+
@inject(TokenProvider) protected readonly tokenProvider: TokenProvider;
18+
19+
async getProviderRepositoriesForUser(params: { user: User, provider: string, hints?: object }): Promise<ProviderRepository[]> {
20+
const token = await this.tokenProvider.getTokenForHost(params.user, "gitlab.com");
21+
const oauthToken = token.value;
22+
const api = new Gitlab({ oauthToken });
23+
24+
const result: ProviderRepository[] = [];
25+
26+
// cf. https://docs.gitlab.com/ee/api/projects.html#list-all-projects
27+
// we are listing only those projects with access level of maintainers.
28+
// also cf. https://docs.gitlab.com/ee/api/members.html#valid-access-levels
29+
//
30+
const projectsWithAccess = await api.Projects.all({ min_access_level: "40" });
31+
for (const project of projectsWithAccess) {
32+
const anyProject = project as any;
33+
const fullPath = anyProject.path_with_namespace as string;
34+
const cloneUrl = anyProject.http_url_to_repo as string;
35+
const updatedAt = anyProject.last_activity_at as string;
36+
const accountAvatarUrl = anyProject.owner?.avatar_url as string;
37+
38+
result.push({
39+
name: project.name,
40+
account: fullPath.split("/")[0],
41+
cloneUrl,
42+
updatedAt,
43+
accountAvatarUrl,
44+
// inUse: // todo(at) compute usage via ProjectHooks API
45+
})
46+
}
47+
48+
return result;
49+
}
50+
51+
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import { Chargebee as chargebee } from '@gitpod/gitpod-payment-endpoint/lib/char
4040
import { EnvEE } from "../env";
4141

4242
import { GitHubAppSupport } from "../github/github-app-support";
43+
import { GitLabAppSupport } from "../gitlab/gitlab-app-support";
4344

4445
@injectable()
4546
export class GitpodServerEEImpl extends GitpodServerImpl<GitpodClient, GitpodServer> {
@@ -68,6 +69,7 @@ export class GitpodServerEEImpl extends GitpodServerImpl<GitpodClient, GitpodSer
6869
@inject(ChargebeeService) protected readonly chargebeeService: ChargebeeService;
6970

7071
@inject(GitHubAppSupport) protected readonly githubAppSupport: GitHubAppSupport;
72+
@inject(GitLabAppSupport) protected readonly gitLabAppSupport: GitLabAppSupport;
7173

7274
@inject(EnvEE) protected readonly env: EnvEE;
7375

@@ -1450,7 +1452,14 @@ export class GitpodServerEEImpl extends GitpodServerImpl<GitpodClient, GitpodSer
14501452
async getProviderRepositoriesForUser(params: { provider: string, hints?: object }): Promise<ProviderRepository[]> {
14511453
const user = this.checkAndBlockUser("getProviderRepositoriesForUser");
14521454

1453-
const repositories = await this.githubAppSupport.getProviderRepositoriesForUser({ user, ...params });
1455+
const repositories: ProviderRepository[] = [];
1456+
if (params.provider === "github.com") {
1457+
repositories.push(...(await this.githubAppSupport.getProviderRepositoriesForUser({ user, ...params })));
1458+
} else if (params.provider === "gitlab.com") {
1459+
repositories.push(...(await this.gitLabAppSupport.getProviderRepositoriesForUser({ user, ...params })));
1460+
} else {
1461+
log.info({ userId: user.id }, `Unsupported provider: "${params.provider}"`, { params });
1462+
}
14541463
const projects = await this.projectsService.getProjectsByCloneUrls(repositories.map(r => r.cloneUrl));
14551464

14561465
const cloneUrlsInUse = new Set(projects.map(p => p.cloneUrl));

0 commit comments

Comments
 (0)