Path: blob/main/components/dashboard/src/data/prebuilds/prebuild-queries.ts
2501 views
/**1* Copyright (c) 2024 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/56import { useMutation, useQuery } from "@tanstack/react-query";7import { prebuildClient, stream } from "../../service/public-api";8import { Prebuild, PrebuildPhase_Phase } from "@gitpod/public-api/lib/gitpod/v1/prebuild_pb";9import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";1011export function usePrebuildQuery(prebuildId: string) {12return useQuery<Prebuild, Error>(13prebuildQueryKey(prebuildId),14async () => {15const prebuild = await prebuildClient.getPrebuild({ prebuildId }).then((response) => response.prebuild);16if (!prebuild) {17throw new Error("Prebuild not found");18}1920return prebuild;21},22{23retry: false,24},25);26}2728function prebuildQueryKey(prebuildId: string) {29return ["prebuild", prebuildId];30}3132export function watchPrebuild(prebuildId: string, cb: (data: Prebuild) => boolean) {33const disposable = stream(34(options) => prebuildClient.watchPrebuild({ scope: { case: "prebuildId", value: prebuildId } }, options),35(resp) => {36if (resp.prebuild) {37const done = cb(resp.prebuild);38if (done) {39disposable.dispose();40}41}42},43);44return disposable;45}4647export function isPrebuildDone(prebuild: Prebuild) {48switch (prebuild.status?.phase?.name) {49case PrebuildPhase_Phase.UNSPECIFIED:50case PrebuildPhase_Phase.QUEUED:51case PrebuildPhase_Phase.BUILDING:52return false;53default:54return true;55}56}5758export function useCancelPrebuildMutation() {59return useMutation({60mutationFn: async (prebuildId: string) => {61await prebuildClient.cancelPrebuild({ prebuildId });62},63});64}6566export function useTriggerPrebuildMutation(configurationId?: string, gitRef?: string) {67return useMutation({68mutationFn: async () => {69if (!configurationId) {70throw new ApplicationError(ErrorCodes.BAD_REQUEST, "prebuild configurationId is missing");71}7273return prebuildClient.startPrebuild({ configurationId, gitRef }).then((resp) => resp.prebuildId);74},75});76}777879