Skip to content

Commit 88aa78d

Browse files
committed
[public-api] migrate PrebuildService
1 parent 5c70155 commit 88aa78d

File tree

21 files changed

+3534
-258
lines changed

21 files changed

+3534
-258
lines changed

components/dashboard/src/components/PrebuildLogs.tsx

Lines changed: 19 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, watchPrebuild, workspaceClient } from "../service/public-api";
1918
import { GetWorkspaceRequest, WorkspacePhase_Phase } from "@gitpod/public-api/lib/gitpod/v1/workspace_pb";
19+
import { Prebuild, Prebuild_Status } from "@gitpod/public-api/lib/gitpod/v1/prebuild_pb";
2020

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

@@ -37,15 +37,18 @@ 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 === Prebuild_Status.ABORTED || prebuild.status === Prebuild_Status.TIMEOUT)
51+
) {
4952
props.onIgnorePrebuild();
5053
}
5154
// TODO(gpl) We likely want to move the "happy path" logic (for status "available")
@@ -78,20 +81,6 @@ export default function PrebuildLogs(props: PrebuildLogsProps) {
7881
setError(err);
7982
}
8083

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-
9584
// Register for future updates
9685
disposables.push(
9786
getGitpodService().registerClient({
@@ -112,13 +101,18 @@ export default function PrebuildLogs(props: PrebuildLogsProps) {
112101
}
113102
logsEmitter.emit("logs", content.text);
114103
},
115-
onPrebuildUpdate(update: PrebuildWithStatus) {
116-
if (update.info) {
117-
handlePrebuildUpdate(update);
118-
}
119-
},
120104
}),
121105
);
106+
disposables.push(
107+
Disposable.create(() =>
108+
watchPrebuild(
109+
{
110+
workspaceId: props.workspaceId,
111+
},
112+
handlePrebuildUpdate,
113+
),
114+
),
115+
);
122116
})();
123117
return function cleanup() {
124118
disposables.dispose();

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,31 @@
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.getPrebuild({
23+
configurationId: projectId,
24+
});
25+
return response.prebuild!;
26+
} catch (e) {
27+
if (ApplicationError.hasErrorCode(e) && e.code === ErrorCodes.NOT_FOUND) {
28+
return null;
29+
}
30+
throw e;
31+
}
2832
},
2933
});
3034
};

components/dashboard/src/data/setup.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import * as ConfigurationClasses from "@gitpod/public-api/lib/gitpod/v1/configur
2525
// This is used to version the cache
2626
// If data we cache changes in a non-backwards compatible way, increment this version
2727
// That will bust any previous cache versions a client may have stored
28-
const CACHE_VERSION = "2";
28+
const CACHE_VERSION = "3";
2929

3030
export function noPersistence(queryKey: QueryKey): QueryKey {
3131
return [...queryKey, "no-persistence"];

components/dashboard/src/projects/Prebuild.tsx

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,90 +4,77 @@
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";
14+
import { gitpodHostUrl } from "../service/service";
1615
import { useCurrentProject } from "./project-context";
1716
import { shortCommitMessage } from "./render-utils";
17+
import { prebuildClient, watchPrebuild } from "../service/public-api";
18+
import { Prebuild, Prebuild_Status } from "@gitpod/public-api/lib/gitpod/v1/prebuild_pb";
1819

1920
export default function PrebuildPage() {
2021
const history = useHistory();
2122
const { project, loading } = useCurrentProject();
2223

2324
const { prebuildId } = useParams<{ prebuildId: string }>();
2425

25-
const [prebuild, setPrebuild] = useState<PrebuildWithStatus | undefined>();
26+
const [prebuild, setPrebuild] = useState<Prebuild | undefined>();
2627
const [isRerunningPrebuild, setIsRerunningPrebuild] = useState<boolean>(false);
2728
const [isCancellingPrebuild, setIsCancellingPrebuild] = useState<boolean>(false);
2829

2930
useEffect(() => {
3031
if (!project || !prebuildId) {
3132
return;
3233
}
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;
34+
return watchPrebuild({ prebuildId }, (prebuild) => setPrebuild(prebuild));
5035
}, [prebuildId, project]);
5136

5237
const title = useMemo(() => {
5338
if (!prebuild) {
5439
return "unknown prebuild";
5540
}
56-
return prebuild.info.branch;
41+
return prebuild.branch;
5742
}, [prebuild]);
5843

5944
const renderSubtitle = () => {
6045
if (!prebuild) {
6146
return "";
6247
}
63-
const startedByAvatar = prebuild.info.startedByAvatar && (
48+
const startedByAvatar = prebuild.startedBy && (
6449
<img
6550
className="rounded-full w-4 h-4 inline-block align-text-bottom mr-2"
66-
src={prebuild.info.startedByAvatar || ""}
67-
alt={prebuild.info.startedBy}
51+
src={prebuild.startedBy.avatarUrl}
52+
alt={prebuild.startedBy.name}
6853
/>
6954
);
7055
return (
7156
<div className="flex">
7257
<div className="my-auto">
7358
<Subheading>
74-
{startedByAvatar}Triggered {dayjs(prebuild.info.startedAt).fromNow()}
59+
{startedByAvatar}Triggered {dayjs(prebuild.startTime?.toDate()).fromNow()}
7560
</Subheading>
7661
</div>
7762
<p className="mx-2 my-auto">·</p>
7863
<div className="my-auto">
79-
<p className="text-gray-500 dark:text-gray-50">{shortCommitMessage(prebuild.info.changeTitle)}</p>
64+
<p className="text-gray-500 dark:text-gray-50">
65+
{shortCommitMessage(prebuild.commit?.message || "")}
66+
</p>
8067
</div>
81-
{!!prebuild.info.basedOnPrebuildId && (
68+
{!!prebuild.basedOnPrebuildId && (
8269
<>
8370
<p className="mx-2 my-auto">·</p>
8471
<div className="my-auto">
8572
<p className="text-gray-500 dark:text-gray-50">
8673
Incremental Prebuild (
8774
<a
8875
className="gp-link"
89-
title={prebuild.info.basedOnPrebuildId}
90-
href={`./${prebuild.info.basedOnPrebuildId}`}
76+
title={prebuild.basedOnPrebuildId}
77+
href={`./${prebuild.basedOnPrebuildId}`}
9178
>
9279
base
9380
</a>
@@ -106,7 +93,10 @@ export default function PrebuildPage() {
10693
}
10794
try {
10895
setIsRerunningPrebuild(true);
109-
await getGitpodService().server.triggerPrebuild(prebuild.info.projectId, prebuild.info.branch);
96+
await prebuildClient.startPrebuild({
97+
configurationId: prebuild.configurationId,
98+
branch: prebuild.branch,
99+
});
110100
// TODO: Open a Prebuilds page that's specific to `prebuild.info.branch`?
111101
if (project) {
112102
history.push(`/projects/${project.id}/prebuilds`);
@@ -124,7 +114,10 @@ export default function PrebuildPage() {
124114
}
125115
try {
126116
setIsCancellingPrebuild(true);
127-
await getGitpodService().server.cancelPrebuild(prebuild.info.projectId, prebuild.info.id);
117+
await prebuildClient.stopPrebuild({
118+
prebuildId: prebuild.id,
119+
configurationId: prebuild.configurationId,
120+
});
128121
} catch (error) {
129122
console.error("Could not cancel prebuild", error);
130123
} finally {
@@ -140,8 +133,10 @@ export default function PrebuildPage() {
140133
<>
141134
<Header title={title} subtitle={renderSubtitle()} />
142135
<div className="app-container mt-8">
143-
<PrebuildLogs workspaceId={prebuild?.info?.buildWorkspaceId}>
144-
{["building", "queued"].includes(prebuild?.status || "") ? (
136+
<PrebuildLogs workspaceId={prebuild?.buildWorkspaceId}>
137+
{[Prebuild_Status.BUILDING, Prebuild_Status.QUEUED].includes(
138+
prebuild?.status || Prebuild_Status.UNSPECIFIED,
139+
) ? (
145140
<button
146141
className="danger flex items-center space-x-2"
147142
disabled={isCancellingPrebuild}
@@ -162,13 +157,13 @@ export default function PrebuildPage() {
162157
{isRerunningPrebuild && (
163158
<img alt="" className="h-4 w-4 animate-spin filter brightness-150" src={Spinner} />
164159
)}
165-
<span>Rerun Prebuild ({prebuild?.info.branch})</span>
160+
<span>Rerun Prebuild ({prebuild?.branch})</span>
166161
</button>
167-
{prebuild?.status === "available" ? (
162+
{prebuild?.status === Prebuild_Status.AVAILABLE ? (
168163
<a
169164
className="my-auto"
170165
href={gitpodHostUrl
171-
.withContext(`open-prebuild/${prebuild?.info.id}/${prebuild?.info.changeUrl}`)
166+
.withContext(`open-prebuild/${prebuild?.id}/${prebuild?.url}`)
172167
.toString()}
173168
>
174169
<button>New Workspace (with this prebuild)</button>

0 commit comments

Comments
 (0)