Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/dashboard/src/data/prebuilds/prebuild-queries.ts
2501 views
1
/**
2
* Copyright (c) 2024 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 { useMutation, useQuery } from "@tanstack/react-query";
8
import { prebuildClient, stream } from "../../service/public-api";
9
import { Prebuild, PrebuildPhase_Phase } from "@gitpod/public-api/lib/gitpod/v1/prebuild_pb";
10
import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
11
12
export function usePrebuildQuery(prebuildId: string) {
13
return useQuery<Prebuild, Error>(
14
prebuildQueryKey(prebuildId),
15
async () => {
16
const prebuild = await prebuildClient.getPrebuild({ prebuildId }).then((response) => response.prebuild);
17
if (!prebuild) {
18
throw new Error("Prebuild not found");
19
}
20
21
return prebuild;
22
},
23
{
24
retry: false,
25
},
26
);
27
}
28
29
function prebuildQueryKey(prebuildId: string) {
30
return ["prebuild", prebuildId];
31
}
32
33
export function watchPrebuild(prebuildId: string, cb: (data: Prebuild) => boolean) {
34
const disposable = stream(
35
(options) => prebuildClient.watchPrebuild({ scope: { case: "prebuildId", value: prebuildId } }, options),
36
(resp) => {
37
if (resp.prebuild) {
38
const done = cb(resp.prebuild);
39
if (done) {
40
disposable.dispose();
41
}
42
}
43
},
44
);
45
return disposable;
46
}
47
48
export function isPrebuildDone(prebuild: Prebuild) {
49
switch (prebuild.status?.phase?.name) {
50
case PrebuildPhase_Phase.UNSPECIFIED:
51
case PrebuildPhase_Phase.QUEUED:
52
case PrebuildPhase_Phase.BUILDING:
53
return false;
54
default:
55
return true;
56
}
57
}
58
59
export function useCancelPrebuildMutation() {
60
return useMutation({
61
mutationFn: async (prebuildId: string) => {
62
await prebuildClient.cancelPrebuild({ prebuildId });
63
},
64
});
65
}
66
67
export function useTriggerPrebuildMutation(configurationId?: string, gitRef?: string) {
68
return useMutation({
69
mutationFn: async () => {
70
if (!configurationId) {
71
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "prebuild configurationId is missing");
72
}
73
74
return prebuildClient.startPrebuild({ configurationId, gitRef }).then((resp) => resp.prebuildId);
75
},
76
});
77
}
78
79