Skip to content

Commit 79666cd

Browse files
authored
[dashboard] Use SCM Service (gRPC) (#19101)
* [dashboard] Use SCM Service (gRPC) * address review comments * bump cache version
1 parent fa3cca4 commit 79666cd

File tree

6 files changed

+94
-9
lines changed

6 files changed

+94
-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: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
const token = await getGitpodService().server.getToken({ host });
31+
if (token) {
32+
response.tokens.push(converter.toSCMToken(token));
33+
}
34+
return response;
35+
}
36+
37+
async guessTokenScopes({
38+
gitCommand,
39+
host,
40+
repoUrl,
41+
}: PartialMessage<GuessTokenScopesRequest>): Promise<GuessTokenScopesResponse> {
42+
if (!host) {
43+
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "host is required");
44+
}
45+
const response = await getGitpodService().server.guessGitTokenScopes({
46+
gitCommand: gitCommand || "",
47+
host,
48+
repoUrl: repoUrl || "",
49+
});
50+
return new GuessTokenScopesResponse({
51+
message: response.message,
52+
scopes: response.scopes || [],
53+
});
54+
}
55+
56+
async searchRepositories(request: PartialMessage<SearchRepositoriesRequest>): Promise<SearchRepositoriesResponse> {
57+
const { limit, searchString } = request;
58+
if (!searchString) {
59+
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "searchString is required");
60+
}
61+
const repos = await getGitpodService().server.searchRepositories({ searchString, limit });
62+
return new SearchRepositoriesResponse({
63+
repositories: repos.map((r) => converter.toSuggestedRepository(r)),
64+
});
65+
}
66+
67+
async listSuggestedRepositories(
68+
request: PartialMessage<ListSuggestedRepositoriesRequest>,
69+
): Promise<ListSuggestedRepositoriesResponse> {
70+
const { organizationId } = request;
71+
if (!organizationId) {
72+
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "organizationId is required");
73+
}
74+
const repos = await getGitpodService().server.getSuggestedRepositories(organizationId);
75+
return new SearchRepositoriesResponse({
76+
repositories: repos.map((r) => converter.toSuggestedRepository(r)),
77+
});
78+
}
79+
}

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";
@@ -77,7 +77,7 @@ function GitProviders() {
7777
if (!provider) {
7878
continue;
7979
}
80-
const token = await getGitpodService().server.getToken({ host: provider.host });
80+
const token = (await scmClient.searchSCMTokens({ host: provider.host })).tokens[0];
8181
scopesByProvider.set(provider.id, token?.scopes?.slice() || []);
8282
}
8383
setAllScopes(scopesByProvider);
@@ -182,7 +182,7 @@ function GitProviders() {
182182
const startEditPermissions = async (provider: AuthProviderDescription) => {
183183
// todo: add spinner
184184

185-
const token = await getGitpodService().server.getToken({ host: provider.host });
185+
const token = (await scmClient.searchSCMTokens({ host: provider.host })).tokens[0];
186186
if (token) {
187187
setEditModal({ provider, prevScopes: new Set(token.scopes), nextScopes: new Set(token.scopes) });
188188
}

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)