Skip to content

Commit 5e7f233

Browse files
committed
[dashboard] Use SCM Service (gRPC)
1 parent 54798af commit 5e7f233

File tree

6 files changed

+98
-9
lines changed

6 files changed

+98
-9
lines changed

components/dashboard/src/data/git-providers/search-repositories-query.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
*/
66

77
import { useQuery } from "@tanstack/react-query";
8-
import { getGitpodService } from "../../service/service";
98
import { useCurrentOrg } from "../organizations/orgs-query";
109
import { useDebounce } from "../../hooks/use-debounce";
1110
import { useFeatureFlag } from "../featureflag-query";
11+
import { scmClient } from "../../service/public-api";
1212

1313
export const useSearchRepositories = ({ searchString, limit }: { searchString: string; limit: number }) => {
1414
// This disables the search behavior when flag is disabled
@@ -19,11 +19,11 @@ export const useSearchRepositories = ({ searchString, limit }: { searchString: s
1919
return useQuery(
2020
["search-repositories", { organizationId: org?.id || "", searchString: debouncedSearchString, limit }],
2121
async () => {
22-
return await getGitpodService().server.searchRepositories({
22+
const { repositories } = await scmClient.searchRepositories({
2323
searchString,
24-
organizationId: org?.id ?? "",
2524
limit,
2625
});
26+
return repositories;
2727
},
2828
{
2929
enabled: repositoryFinderSearchEnabled && !!org && debouncedSearchString.length >= 3,

components/dashboard/src/data/git-providers/suggested-repositories-query.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import { useQuery } from "@tanstack/react-query";
88
import { useCurrentOrg } from "../organizations/orgs-query";
9-
import { getGitpodService } from "../../service/service";
9+
import { scmClient } from "../../service/public-api";
1010

1111
export const useSuggestedRepositories = () => {
1212
const { data: org } = useCurrentOrg();
@@ -18,7 +18,8 @@ export const useSuggestedRepositories = () => {
1818
throw new Error("No org selected");
1919
}
2020

21-
return await getGitpodService().server.getSuggestedRepositories(org.id);
21+
const { repositories } = await scmClient.listSuggestedRepositories({ organizationId: org.id });
22+
return repositories;
2223
},
2324
{
2425
// Keeps data in cache for 7 days - will still refresh though
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
3+
* Licensed under the GNU Affero General Public License (AGPL).
4+
* See License.AGPL.txt in the project root for license information.
5+
*/
6+
7+
import { PartialMessage } from "@bufbuild/protobuf";
8+
import { PromiseClient } from "@connectrpc/connect";
9+
import { SCMService } from "@gitpod/public-api/lib/gitpod/v1/scm_connect";
10+
import { converter } from "./public-api";
11+
import { getGitpodService } from "./service";
12+
import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
13+
import {
14+
SearchSCMTokensRequest,
15+
SearchSCMTokensResponse,
16+
GuessTokenScopesRequest,
17+
SearchRepositoriesRequest,
18+
ListSuggestedRepositoriesRequest,
19+
ListSuggestedRepositoriesResponse,
20+
SearchRepositoriesResponse,
21+
GuessTokenScopesResponse,
22+
} from "@gitpod/public-api/lib/gitpod/v1/scm_pb";
23+
24+
export class JsonRpcScmClient implements PromiseClient<typeof SCMService> {
25+
async searchSCMTokens({ host }: PartialMessage<SearchSCMTokensRequest>): Promise<SearchSCMTokensResponse> {
26+
if (!host) {
27+
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "host is required");
28+
}
29+
const response = new SearchSCMTokensResponse();
30+
try {
31+
const token = await getGitpodService().server.getToken({ host });
32+
if (token) {
33+
response.tokens.push(converter.toSCMToken(token));
34+
}
35+
} catch (error) {
36+
throw new ApplicationError(ErrorCodes.NOT_FOUND, "token not found");
37+
}
38+
return response;
39+
}
40+
41+
async guessTokenScopes({
42+
gitCommand,
43+
host,
44+
repoUrl,
45+
}: PartialMessage<GuessTokenScopesRequest>): Promise<GuessTokenScopesResponse> {
46+
if (!host) {
47+
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "host is required");
48+
}
49+
const response = await getGitpodService().server.guessGitTokenScopes({
50+
gitCommand: gitCommand || "",
51+
host,
52+
repoUrl: repoUrl || "",
53+
});
54+
return new GuessTokenScopesResponse({
55+
message: response.message,
56+
scopes: response.scopes,
57+
});
58+
}
59+
60+
async searchRepositories(request: PartialMessage<SearchRepositoriesRequest>): Promise<SearchRepositoriesResponse> {
61+
const { limit, searchString } = request;
62+
if (!searchString) {
63+
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "searchString is required");
64+
}
65+
const repos = await getGitpodService().server.searchRepositories({ searchString, limit });
66+
return new SearchRepositoriesResponse({
67+
repositories: repos.map((r) => converter.toSuggestedRepository(r)),
68+
});
69+
}
70+
71+
async listSuggestedRepositories(
72+
request: PartialMessage<ListSuggestedRepositoriesRequest>,
73+
): Promise<ListSuggestedRepositoriesResponse> {
74+
const { organizationId } = request;
75+
if (!organizationId) {
76+
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "organizationId is required");
77+
}
78+
const repos = await getGitpodService().server.getSuggestedRepositories(organizationId);
79+
return new SearchRepositoriesResponse({
80+
repositories: repos.map((r) => converter.toSuggestedRepository(r)),
81+
});
82+
}
83+
}

components/dashboard/src/service/public-api.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import { JsonRpcEnvvarClient } from "./json-rpc-envvar-client";
3232
import { Prebuild, WatchPrebuildRequest, WatchPrebuildResponse } from "@gitpod/public-api/lib/gitpod/v1/prebuild_pb";
3333
import { JsonRpcPrebuildClient } from "./json-rpc-prebuild-client";
3434
import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
35+
import { JsonRpcScmClient } from "./json-rpc-scm-client";
36+
import { SCMService } from "@gitpod/public-api/lib/gitpod/v1/scm_connect";
3537

3638
const transport = createConnectTransport({
3739
baseUrl: `${window.location.protocol}//${window.location.host}/public-api`,
@@ -61,6 +63,8 @@ export const prebuildClient = createServiceClient(PrebuildService, new JsonRpcPr
6163

6264
export const authProviderClient = createServiceClient(AuthProviderService, new JsonRpcAuthProviderClient());
6365

66+
export const scmClient = createServiceClient(SCMService, new JsonRpcScmClient());
67+
6468
export const envVarClient = createServiceClient(EnvironmentVariableService, new JsonRpcEnvvarClient());
6569

6670
export async function listAllProjects(opts: { orgId: string }): Promise<ProtocolProject[]> {

components/dashboard/src/user-settings/Integrations.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {
3535
AuthProviderDescription,
3636
AuthProviderType,
3737
} from "@gitpod/public-api/lib/gitpod/v1/authprovider_pb";
38-
import { authProviderClient } from "../service/public-api";
38+
import { authProviderClient, scmClient } from "../service/public-api";
3939
import { useCreateUserAuthProviderMutation } from "../data/auth-providers/create-user-auth-provider-mutation";
4040
import { useUpdateUserAuthProviderMutation } from "../data/auth-providers/update-user-auth-provider-mutation";
4141
import { useDeleteUserAuthProviderMutation } from "../data/auth-providers/delete-user-auth-provider-mutation";
@@ -76,7 +76,7 @@ function GitProviders() {
7676
if (!provider) {
7777
continue;
7878
}
79-
const token = await getGitpodService().server.getToken({ host: provider.host });
79+
const { token } = await scmClient.getSCMToken({ host: provider.host });
8080
scopesByProvider.set(provider.id, token?.scopes?.slice() || []);
8181
}
8282
setAllScopes(scopesByProvider);
@@ -181,7 +181,7 @@ function GitProviders() {
181181
const startEditPermissions = async (provider: AuthProviderDescription) => {
182182
// todo: add spinner
183183

184-
const token = await getGitpodService().server.getToken({ host: provider.host });
184+
const { token } = await scmClient.getSCMToken({ host: provider.host });
185185
if (token) {
186186
setEditModal({ provider, prevScopes: new Set(token.scopes), nextScopes: new Set(token.scopes) });
187187
}

components/gitpod-protocol/src/gitpod-service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ export interface GetProviderRepositoriesParams {
325325
maxPages?: number;
326326
}
327327
export interface SearchRepositoriesParams {
328-
organizationId: string;
328+
/** @deprecated unused */
329+
organizationId?: string;
329330
searchString: string;
330331
limit?: number; // defaults to 30
331332
}

0 commit comments

Comments
 (0)