Skip to content

Commit 581408d

Browse files
authored
[dashboard] make new workspace aware of project (#16742)
1 parent 4d053ed commit 581408d

File tree

5 files changed

+63
-4
lines changed

5 files changed

+63
-4
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 { WorkspaceContext } from "@gitpod/gitpod-protocol";
8+
import { useQuery } from "@tanstack/react-query";
9+
import { getGitpodService } from "../../service/service";
10+
11+
export function useWorkspaceContext(contextUrl?: string) {
12+
const query = useQuery<WorkspaceContext, Error>(
13+
["workspace-context", contextUrl],
14+
() => {
15+
if (!contextUrl) {
16+
throw new Error("no contextURL. Query should be disabled.");
17+
}
18+
return getGitpodService().server.resolveContext(contextUrl);
19+
},
20+
{
21+
enabled: !!contextUrl,
22+
},
23+
);
24+
return query;
25+
}

components/dashboard/src/workspaces/CreateWorkspacePage.tsx

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
* See License.AGPL.txt in the project root for license information.
55
*/
66

7-
import { ContextURL, GitpodServer, WorkspaceInfo } from "@gitpod/gitpod-protocol";
7+
import { CommitContext, ContextURL, GitpodServer, WorkspaceInfo } from "@gitpod/gitpod-protocol";
88
import { SelectAccountPayload } from "@gitpod/gitpod-protocol/lib/auth";
99
import { ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
1010
import { Deferred } from "@gitpod/gitpod-protocol/lib/util/deferred";
11-
import { FunctionComponent, useCallback, useState } from "react";
11+
import { FunctionComponent, useCallback, useEffect, useMemo, useState } from "react";
1212
import { useHistory, useLocation } from "react-router";
1313
import { Button } from "../components/Button";
1414
import Modal, { ModalBody, ModalFooter, ModalHeader } from "../components/Modal";
@@ -19,7 +19,9 @@ import { Heading1 } from "../components/typography/headings";
1919
import { UsageLimitReachedModal } from "../components/UsageLimitReachedModal";
2020
import { useFeatureFlags } from "../contexts/FeatureFlagContext";
2121
import { useCurrentOrg } from "../data/organizations/orgs-query";
22+
import { useListProjectsQuery } from "../data/projects/list-projects-query";
2223
import { useCreateWorkspaceMutation } from "../data/workspaces/create-workspace-mutation";
24+
import { useWorkspaceContext } from "../data/workspaces/resolve-context-query";
2325
import { openAuthorizeWindow } from "../provider-utils";
2426
import { gitpodHostUrl } from "../service/service";
2527
import { LimitReachedOutOfHours, LimitReachedParallelWorkspacesModal } from "../start/CreateWorkspace";
@@ -37,6 +39,7 @@ export const useNewCreateWorkspacePage = () => {
3739
export function CreateWorkspacePage() {
3840
const user = useCurrentUser();
3941
const currentOrg = useCurrentOrg().data;
42+
const projects = useListProjectsQuery();
4043
const location = useLocation();
4144
const history = useHistory();
4245
const props = StartWorkspaceOptions.parseSearchParams(location.search);
@@ -55,6 +58,26 @@ export function CreateWorkspacePage() {
5558
const [selectedWsClass, setSelectedWsClass] = useState<string | undefined>(props.workspaceClass);
5659
const [errorWsClass, setErrorWsClass] = useState<string | undefined>(undefined);
5760
const [repo, setRepo] = useState<string | undefined>(location.hash.substring(1));
61+
const workspaceContext = useWorkspaceContext(repo);
62+
const isLoading = workspaceContext.isLoading || projects.isLoading;
63+
const project = useMemo(() => {
64+
if (!workspaceContext.data || !projects.data) {
65+
return undefined;
66+
}
67+
const cloneUrl = (workspaceContext.data as CommitContext).repository.cloneUrl;
68+
return projects.data.projects.find((p) => p.cloneUrl === cloneUrl);
69+
}, [projects.data, workspaceContext.data]);
70+
71+
useEffect(() => {
72+
if (!project || props.workspaceClass) {
73+
return;
74+
}
75+
const wsClass = project.settings?.workspaceClasses;
76+
if (wsClass?.regular) {
77+
setSelectedWsClass(wsClass?.regular);
78+
}
79+
}, [project, props.workspaceClass]);
80+
5881
const onSelectEditorChange = useCallback(
5982
(ide: string, useLatest: boolean) => {
6083
setSelectedIde(ide);
@@ -151,10 +174,14 @@ export function CreateWorkspacePage() {
151174
<div className="w-full flex justify-end mt-6 space-x-2 px-6">
152175
<Button
153176
onClick={onClickCreate}
154-
loading={createWorkspaceMutation.isLoading}
177+
loading={createWorkspaceMutation.isLoading || isLoading}
155178
disabled={!repo || repo.length === 0 || !!errorIde || !!errorWsClass}
156179
>
157-
{createWorkspaceMutation.isLoading ? "Creating Workspace ..." : "New Workspace"}
180+
{isLoading
181+
? "Loading ..."
182+
: createWorkspaceMutation.isLoading
183+
? "Creating Workspace ..."
184+
: "New Workspace"}
158185
</Button>
159186
</div>
160187
<div>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
IDESettings,
3030
EnvVarWithValue,
3131
WorkspaceTimeoutSetting,
32+
WorkspaceContext,
3233
} from "./protocol";
3334
import {
3435
Team,
@@ -132,6 +133,7 @@ export interface GitpodServer extends JsonRpcServer<GitpodClient>, AdminServer,
132133
deleteWorkspace(id: string): Promise<void>;
133134
setWorkspaceDescription(id: string, desc: string): Promise<void>;
134135
controlAdmission(id: string, level: GitpodServer.AdmissionLevel): Promise<void>;
136+
resolveContext(contextUrl: string): Promise<WorkspaceContext>;
135137

136138
updateWorkspaceUserPin(id: string, action: GitpodServer.PinAction): Promise<void>;
137139
sendHeartBeat(options: GitpodServer.SendHeartBeatOptions): Promise<void>;

components/server/src/auth/rate-limiter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ const defaultFunctions: FunctionsConfig = {
137137
getSnapshots: { group: "default", points: 1 },
138138
guessGitTokenScopes: { group: "default", points: 1 },
139139
getUsageBalance: { group: "default", points: 1 },
140+
resolveContext: { group: "default", points: 1 },
140141

141142
adminGetUsers: { group: "default", points: 1 },
142143
adminGetUser: { group: "default", points: 1 },

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3675,4 +3675,8 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
36753675
}
36763676

36773677
async getIDToken(): Promise<void> {}
3678+
public async resolveContext(ctx: TraceContextWithSpan, contextUrl: string): Promise<WorkspaceContext> {
3679+
const user = this.checkAndBlockUser("resolveContext");
3680+
return this.contextParser.handle(ctx, user, contextUrl);
3681+
}
36783682
}

0 commit comments

Comments
 (0)