Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/dashboard/src/data/workspaces/resolve-context-query.ts
2501 views
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 { useQuery } from "@tanstack/react-query";
8
import {
9
GitInitializer,
10
GitInitializer_CloneTargetMode,
11
ParseContextURLResponse,
12
} from "@gitpod/public-api/lib/gitpod/v1/workspace_pb";
13
import { workspaceClient } from "../../service/public-api";
14
15
export function useWorkspaceContext(contextUrl?: string) {
16
const query = useQuery<{
17
data: ParseContextURLResponse;
18
refererIDE?: string;
19
cloneUrl?: string;
20
revision?: string;
21
} | null>(
22
["workspace-context", contextUrl],
23
async () => {
24
if (!contextUrl) {
25
return null;
26
}
27
const data = await workspaceClient.parseContextURL({ contextUrl });
28
const commitInfo = getCommitInfo(data);
29
return {
30
data,
31
refererIDE: matchRefererIDE(contextUrl),
32
cloneUrl: commitInfo?.cloneUrl,
33
revision: commitInfo?.revision,
34
};
35
},
36
{
37
retry: false,
38
},
39
);
40
return query;
41
}
42
43
// TODO: Compatible code, remove me
44
function getCommitInfo(response: ParseContextURLResponse | null) {
45
if (!response) {
46
return undefined;
47
}
48
const specs = response.spec?.initializer?.specs;
49
if (!specs || specs.length === 0) {
50
return undefined;
51
}
52
const gitInit: GitInitializer | undefined = specs.find((item) => item.spec.case === "git")?.spec.value as any;
53
if (!gitInit) {
54
return undefined;
55
}
56
const result: { cloneUrl: string; revision?: string } = { cloneUrl: gitInit.remoteUri };
57
if (gitInit.targetMode === GitInitializer_CloneTargetMode.REMOTE_BRANCH) {
58
result.revision = gitInit.cloneTarget;
59
}
60
return result;
61
}
62
63
// TODO: Compatible code, remove me
64
function matchRefererIDE(url: string) {
65
const regex = /^\/?referrer:([^/:]*)(?::([^/]*))?\//;
66
const matches = regex.exec(url);
67
const referrerIde = matches?.[2];
68
return referrerIde;
69
}
70
71