Skip to content

Commit f44c51c

Browse files
committed
[public-api] migrate PrebuildService
1 parent 58d75d7 commit f44c51c

File tree

13 files changed

+779
-283
lines changed

13 files changed

+779
-283
lines changed

components/dashboard/src/components/PrebuildLogs.tsx

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import {
1111
WorkspaceImageBuild,
1212
HEADLESS_LOG_STREAM_STATUS_CODE_REGEX,
1313
Disposable,
14-
PrebuildWithStatus,
1514
} from "@gitpod/gitpod-protocol";
1615
import { getGitpodService } from "../service/service";
1716
import { PrebuildStatus } from "../projects/Prebuilds";
18-
import { converter, workspaceClient } from "../service/public-api";
17+
import { converter, prebuildClient, watchPrebuild, workspaceClient } from "../service/public-api";
1918
import { GetWorkspaceRequest, WorkspacePhase_Phase } from "@gitpod/public-api/lib/gitpod/v1/workspace_pb";
19+
import { Prebuild, PrebuildPhase_Phase } from "@gitpod/public-api/lib/gitpod/v1/prebuild_pb";
2020

2121
const WorkspaceLogs = React.lazy(() => import("./WorkspaceLogs"));
2222

@@ -37,15 +37,19 @@ export default function PrebuildLogs(props: PrebuildLogsProps) {
3737
>();
3838
const [error, setError] = useState<Error | undefined>();
3939
const [logsEmitter] = useState(new EventEmitter());
40-
const [prebuild, setPrebuild] = useState<PrebuildWithStatus | undefined>();
40+
const [prebuild, setPrebuild] = useState<Prebuild | undefined>();
4141

4242
const handlePrebuildUpdate = useCallback(
43-
(prebuild: PrebuildWithStatus) => {
44-
if (prebuild.info.buildWorkspaceId === props.workspaceId) {
43+
(prebuild: Prebuild) => {
44+
if (prebuild.buildWorkspaceId === props.workspaceId) {
4545
setPrebuild(prebuild);
4646

4747
// In case the Prebuild got "aborted" or "time(d)out" we want to user to proceed anyway
48-
if (props.onIgnorePrebuild && (prebuild.status === "aborted" || prebuild.status === "timeout")) {
48+
if (
49+
props.onIgnorePrebuild &&
50+
(prebuild.status?.phase?.name === PrebuildPhase_Phase.ABORTED ||
51+
prebuild.status?.phase?.name === PrebuildPhase_Phase.TIMEOUT)
52+
) {
4953
props.onIgnorePrebuild();
5054
}
5155
// TODO(gpl) We likely want to move the "happy path" logic (for status "available")
@@ -78,20 +82,6 @@ export default function PrebuildLogs(props: PrebuildLogsProps) {
7882
setError(err);
7983
}
8084

81-
// Try get hold of a recent Prebuild
82-
try {
83-
const pbws = await getGitpodService().server.findPrebuildByWorkspaceID(props.workspaceId);
84-
if (pbws) {
85-
const foundPrebuild = await getGitpodService().server.getPrebuild(pbws.id);
86-
if (foundPrebuild) {
87-
handlePrebuildUpdate(foundPrebuild);
88-
}
89-
}
90-
} catch (err) {
91-
console.error(err);
92-
setError(err);
93-
}
94-
9585
// Register for future updates
9686
disposables.push(
9787
getGitpodService().registerClient({
@@ -112,13 +102,31 @@ export default function PrebuildLogs(props: PrebuildLogsProps) {
112102
}
113103
logsEmitter.emit("logs", content.text);
114104
},
115-
onPrebuildUpdate(update: PrebuildWithStatus) {
116-
if (update.info) {
117-
handlePrebuildUpdate(update);
118-
}
119-
},
120105
}),
121106
);
107+
108+
try {
109+
const response = await prebuildClient.listPrebuilds({ buildWorkspaceId: props.workspaceId });
110+
const prebuild = response.prebuilds[0];
111+
if (prebuild) {
112+
handlePrebuildUpdate(prebuild);
113+
disposables.push(
114+
Disposable.create(() =>
115+
watchPrebuild(
116+
{
117+
prebuildIds: [prebuild.id],
118+
},
119+
handlePrebuildUpdate,
120+
),
121+
),
122+
);
123+
} else {
124+
setError(new Error("Prebuild not found"));
125+
}
126+
} catch (err) {
127+
console.error(err);
128+
setError(err);
129+
}
122130
})();
123131
return function cleanup() {
124132
disposables.dispose();

components/dashboard/src/data/prebuilds/latest-project-prebuild-query.ts

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

7-
import { PrebuildWithStatus } from "@gitpod/gitpod-protocol";
87
import { useQuery } from "@tanstack/react-query";
9-
import { getGitpodService } from "../../service/service";
10-
11-
export type LatestProjectPrebuildQueryResult = PrebuildWithStatus;
8+
import { prebuildClient } from "../../service/public-api";
9+
import { Prebuild } from "@gitpod/public-api/lib/gitpod/v1/prebuild_pb";
10+
import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
1211

1312
type Args = {
1413
projectId: string;
1514
};
1615
export const useLatestProjectPrebuildQuery = ({ projectId }: Args) => {
17-
return useQuery<LatestProjectPrebuildQueryResult>({
16+
return useQuery<Prebuild | null>({
1817
queryKey: getLatestProjectPrebuildQueryKey(projectId),
1918
// Prevent bursting for latest project prebuilds too frequently
2019
staleTime: 1000 * 60 * 1, // 1 minute
2120
queryFn: async () => {
22-
const latestPrebuilds = await getGitpodService().server.findPrebuilds({
23-
projectId,
24-
latest: true,
25-
});
26-
27-
return latestPrebuilds[0] || null;
21+
try {
22+
const response = await prebuildClient.listPrebuilds({
23+
configurationId: projectId,
24+
pagination: {
25+
pageSize: 1,
26+
},
27+
});
28+
return response.prebuilds[0];
29+
} catch (e) {
30+
if (ApplicationError.hasErrorCode(e) && e.code === ErrorCodes.NOT_FOUND) {
31+
return null;
32+
}
33+
throw e;
34+
}
2835
},
2936
});
3037
};

components/dashboard/src/projects/Prebuild.tsx

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

7-
import { PrebuildWithStatus } from "@gitpod/gitpod-protocol";
87
import dayjs from "dayjs";
98
import { useEffect, useMemo, useState } from "react";
109
import { Redirect, useHistory, useParams } from "react-router";
1110
import Header from "../components/Header";
1211
import PrebuildLogs from "../components/PrebuildLogs";
1312
import { Subheading } from "../components/typography/headings";
1413
import Spinner from "../icons/Spinner.svg";
15-
import { getGitpodService, gitpodHostUrl } from "../service/service";
1614
import { useCurrentProject } from "./project-context";
1715
import { shortCommitMessage } from "./render-utils";
16+
import { prebuildClient, watchPrebuild } from "../service/public-api";
17+
import { Prebuild, PrebuildPhase_Phase } from "@gitpod/public-api/lib/gitpod/v1/prebuild_pb";
1818

1919
export default function PrebuildPage() {
2020
const history = useHistory();
2121
const { project, loading } = useCurrentProject();
2222

2323
const { prebuildId } = useParams<{ prebuildId: string }>();
2424

25-
const [prebuild, setPrebuild] = useState<PrebuildWithStatus | undefined>();
25+
const [prebuild, setPrebuild] = useState<Prebuild | undefined>();
2626
const [isRerunningPrebuild, setIsRerunningPrebuild] = useState<boolean>(false);
2727
const [isCancellingPrebuild, setIsCancellingPrebuild] = useState<boolean>(false);
2828

2929
useEffect(() => {
3030
if (!project || !prebuildId) {
3131
return;
3232
}
33-
(async () => {
34-
const prebuilds = await getGitpodService().server.findPrebuilds({
35-
projectId: project.id,
36-
prebuildId,
37-
});
38-
setPrebuild(prebuilds[0]);
39-
})();
40-
41-
return getGitpodService().registerClient({
42-
onPrebuildUpdate(update: PrebuildWithStatus) {
43-
if (update.info.id !== prebuildId) {
44-
return;
45-
}
46-
47-
setPrebuild(update);
48-
},
49-
}).dispose;
33+
return watchPrebuild({ prebuildIds: [prebuildId] }, (prebuild) => setPrebuild(prebuild));
5034
}, [prebuildId, project]);
5135

5236
const title = useMemo(() => {
5337
if (!prebuild) {
5438
return "unknown prebuild";
5539
}
56-
return prebuild.info.branch;
40+
return prebuild.ref;
5741
}, [prebuild]);
5842

5943
const renderSubtitle = () => {
6044
if (!prebuild) {
6145
return "";
6246
}
63-
const startedByAvatar = prebuild.info.startedByAvatar && (
64-
<img
65-
className="rounded-full w-4 h-4 inline-block align-text-bottom mr-2"
66-
src={prebuild.info.startedByAvatar || ""}
67-
alt={prebuild.info.startedBy}
68-
/>
69-
);
7047
return (
7148
<div className="flex">
7249
<div className="my-auto">
73-
<Subheading>
74-
{startedByAvatar}Triggered {dayjs(prebuild.info.startedAt).fromNow()}
75-
</Subheading>
50+
<Subheading>Triggered {dayjs(prebuild.status?.startTime?.toDate()).fromNow()}</Subheading>
7651
</div>
7752
<p className="mx-2 my-auto">·</p>
7853
<div className="my-auto">
79-
<p className="text-gray-500 dark:text-gray-50">{shortCommitMessage(prebuild.info.changeTitle)}</p>
54+
<p className="text-gray-500 dark:text-gray-50">
55+
{shortCommitMessage(prebuild.commit?.message || "")}
56+
</p>
8057
</div>
81-
{!!prebuild.info.basedOnPrebuildId && (
58+
{!!prebuild.basedOnPrebuildId && (
8259
<>
8360
<p className="mx-2 my-auto">·</p>
8461
<div className="my-auto">
8562
<p className="text-gray-500 dark:text-gray-50">
8663
Incremental Prebuild (
8764
<a
8865
className="gp-link"
89-
title={prebuild.info.basedOnPrebuildId}
90-
href={`./${prebuild.info.basedOnPrebuildId}`}
66+
title={prebuild.basedOnPrebuildId}
67+
href={`./${prebuild.basedOnPrebuildId}`}
9168
>
9269
base
9370
</a>
@@ -106,7 +83,10 @@ export default function PrebuildPage() {
10683
}
10784
try {
10885
setIsRerunningPrebuild(true);
109-
await getGitpodService().server.triggerPrebuild(prebuild.info.projectId, prebuild.info.branch);
86+
await prebuildClient.startPrebuild({
87+
configurationId: prebuild.configurationId,
88+
gitRef: prebuild.ref,
89+
});
11090
// TODO: Open a Prebuilds page that's specific to `prebuild.info.branch`?
11191
if (project) {
11292
history.push(`/projects/${project.id}/prebuilds`);
@@ -124,7 +104,9 @@ export default function PrebuildPage() {
124104
}
125105
try {
126106
setIsCancellingPrebuild(true);
127-
await getGitpodService().server.cancelPrebuild(prebuild.info.projectId, prebuild.info.id);
107+
await prebuildClient.cancelPrebuild({
108+
prebuildId: prebuild.id,
109+
});
128110
} catch (error) {
129111
console.error("Could not cancel prebuild", error);
130112
} finally {
@@ -140,8 +122,10 @@ export default function PrebuildPage() {
140122
<>
141123
<Header title={title} subtitle={renderSubtitle()} />
142124
<div className="app-container mt-8">
143-
<PrebuildLogs workspaceId={prebuild?.info?.buildWorkspaceId}>
144-
{["building", "queued"].includes(prebuild?.status || "") ? (
125+
<PrebuildLogs workspaceId={prebuild?.buildWorkspaceId}>
126+
{[PrebuildPhase_Phase.BUILDING, PrebuildPhase_Phase.QUEUED].includes(
127+
prebuild?.status?.phase?.name || PrebuildPhase_Phase.UNSPECIFIED,
128+
) ? (
145129
<button
146130
className="danger flex items-center space-x-2"
147131
disabled={isCancellingPrebuild}
@@ -162,20 +146,8 @@ export default function PrebuildPage() {
162146
{isRerunningPrebuild && (
163147
<img alt="" className="h-4 w-4 animate-spin filter brightness-150" src={Spinner} />
164148
)}
165-
<span>Rerun Prebuild ({prebuild?.info.branch})</span>
149+
<span>Rerun Prebuild ({prebuild?.ref})</span>
166150
</button>
167-
{prebuild?.status === "available" ? (
168-
<a
169-
className="my-auto"
170-
href={gitpodHostUrl
171-
.withContext(`open-prebuild/${prebuild?.info.id}/${prebuild?.info.changeUrl}`)
172-
.toString()}
173-
>
174-
<button>New Workspace (with this prebuild)</button>
175-
</a>
176-
) : (
177-
<button disabled={true}>New Workspace (with this prebuild)</button>
178-
)}
179151
</>
180152
)}
181153
</PrebuildLogs>

0 commit comments

Comments
 (0)