Skip to content

Commit

Permalink
[dashboard] Use SCM Service (gRPC)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexTugarev committed Nov 22, 2023
1 parent 54798af commit 4dfd63e
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
*/

import { useQuery } from "@tanstack/react-query";
import { getGitpodService } from "../../service/service";
import { useCurrentOrg } from "../organizations/orgs-query";
import { useDebounce } from "../../hooks/use-debounce";
import { useFeatureFlag } from "../featureflag-query";
import { scmClient } from "../../service/public-api";

export const useSearchRepositories = ({ searchString, limit }: { searchString: string; limit: number }) => {
// This disables the search behavior when flag is disabled
Expand All @@ -19,11 +19,11 @@ export const useSearchRepositories = ({ searchString, limit }: { searchString: s
return useQuery(
["search-repositories", { organizationId: org?.id || "", searchString: debouncedSearchString, limit }],
async () => {
return await getGitpodService().server.searchRepositories({
const { repositories } = await scmClient.searchRepositories({
searchString,
organizationId: org?.id ?? "",
limit,
});
return repositories;
},
{
enabled: repositoryFinderSearchEnabled && !!org && debouncedSearchString.length >= 3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

export const useSuggestedRepositories = () => {
const { data: org } = useCurrentOrg();
Expand All @@ -18,7 +18,8 @@ export const useSuggestedRepositories = () => {
throw new Error("No org selected");
}

return await getGitpodService().server.getSuggestedRepositories(org.id);
const { repositories } = await scmClient.listSuggestedRepositories({ organizationId: org.id });
return repositories;
},
{
// Keeps data in cache for 7 days - will still refresh though
Expand Down
83 changes: 83 additions & 0 deletions components/dashboard/src/service/json-rpc-scm-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License.AGPL.txt in the project root for license information.
*/

import { PartialMessage } from "@bufbuild/protobuf";
import { PromiseClient } from "@connectrpc/connect";
import { SCMService } from "@gitpod/public-api/lib/gitpod/v1/scm_connect";
import { converter } from "./public-api";
import { getGitpodService } from "./service";
import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
import {
SearchSCMTokensRequest,
SearchSCMTokensResponse,
GuessTokenScopesRequest,
SearchRepositoriesRequest,
ListSuggestedRepositoriesRequest,
ListSuggestedRepositoriesResponse,
SearchRepositoriesResponse,
GuessTokenScopesResponse,
} from "@gitpod/public-api/lib/gitpod/v1/scm_pb";

export class JsonRpcScmClient implements PromiseClient<typeof SCMService> {
async searchSCMTokens({ host }: PartialMessage<SearchSCMTokensRequest>): Promise<SearchSCMTokensResponse> {
if (!host) {
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "host is required");
}
const response = new SearchSCMTokensResponse();
try {
const token = await getGitpodService().server.getToken({ host });
if (token) {
response.tokens.push(converter.toSCMToken(token));
}
} catch (error) {
throw new ApplicationError(ErrorCodes.NOT_FOUND, "token not found");
}
return response;
}

async guessTokenScopes({
gitCommand,
host,
repoUrl,
}: PartialMessage<GuessTokenScopesRequest>): Promise<GuessTokenScopesResponse> {
if (!host) {
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "host is required");
}
const response = await getGitpodService().server.guessGitTokenScopes({
gitCommand: gitCommand || "",
host,
repoUrl: repoUrl || "",
});
return new GuessTokenScopesResponse({
message: response.message,
scopes: response.scopes,
});
}

async searchRepositories(request: PartialMessage<SearchRepositoriesRequest>): Promise<SearchRepositoriesResponse> {
const { limit, searchString } = request;
if (!searchString) {
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "searchString is required");
}
const repos = await getGitpodService().server.searchRepositories({ searchString, limit });
return new SearchRepositoriesResponse({
repositories: repos.map((r) => converter.toSuggestedRepository(r)),
});
}

async listSuggestedRepositories(
request: PartialMessage<ListSuggestedRepositoriesRequest>,
): Promise<ListSuggestedRepositoriesResponse> {
const { organizationId } = request;
if (!organizationId) {
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "organizationId is required");
}
const repos = await getGitpodService().server.getSuggestedRepositories(organizationId);
return new SearchRepositoriesResponse({
repositories: repos.map((r) => converter.toSuggestedRepository(r)),
});
}
}
4 changes: 4 additions & 0 deletions components/dashboard/src/service/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import { JsonRpcEnvvarClient } from "./json-rpc-envvar-client";
import { Prebuild, WatchPrebuildRequest, WatchPrebuildResponse } from "@gitpod/public-api/lib/gitpod/v1/prebuild_pb";
import { JsonRpcPrebuildClient } from "./json-rpc-prebuild-client";
import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
import { JsonRpcScmClient } from "./json-rpc-scm-client";
import { SCMService } from "@gitpod/public-api/lib/gitpod/v1/scm_connect";

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

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

export const scmClient = createServiceClient(SCMService, new JsonRpcScmClient());

export const envVarClient = createServiceClient(EnvironmentVariableService, new JsonRpcEnvvarClient());

export async function listAllProjects(opts: { orgId: string }): Promise<ProtocolProject[]> {
Expand Down
6 changes: 3 additions & 3 deletions components/dashboard/src/user-settings/Integrations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
AuthProviderDescription,
AuthProviderType,
} from "@gitpod/public-api/lib/gitpod/v1/authprovider_pb";
import { authProviderClient } from "../service/public-api";
import { authProviderClient, scmClient } from "../service/public-api";
import { useCreateUserAuthProviderMutation } from "../data/auth-providers/create-user-auth-provider-mutation";
import { useUpdateUserAuthProviderMutation } from "../data/auth-providers/update-user-auth-provider-mutation";
import { useDeleteUserAuthProviderMutation } from "../data/auth-providers/delete-user-auth-provider-mutation";
Expand Down Expand Up @@ -76,7 +76,7 @@ function GitProviders() {
if (!provider) {
continue;
}
const token = await getGitpodService().server.getToken({ host: provider.host });
const token = (await scmClient.searchSCMTokens({ host: provider.host })).tokens[0];
scopesByProvider.set(provider.id, token?.scopes?.slice() || []);
}
setAllScopes(scopesByProvider);
Expand Down Expand Up @@ -181,7 +181,7 @@ function GitProviders() {
const startEditPermissions = async (provider: AuthProviderDescription) => {
// todo: add spinner

const token = await getGitpodService().server.getToken({ host: provider.host });
const token = (await scmClient.searchSCMTokens({ host: provider.host })).tokens[0];
if (token) {
setEditModal({ provider, prevScopes: new Set(token.scopes), nextScopes: new Set(token.scopes) });
}
Expand Down
3 changes: 2 additions & 1 deletion components/gitpod-protocol/src/gitpod-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,8 @@ export interface GetProviderRepositoriesParams {
maxPages?: number;
}
export interface SearchRepositoriesParams {
organizationId: string;
/** @deprecated unused */
organizationId?: string;
searchString: string;
limit?: number; // defaults to 30
}
Expand Down

0 comments on commit 4dfd63e

Please sign in to comment.