Skip to content

chore(site): remove template version editor xservice #10490

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Extract logs
  • Loading branch information
BrunoQuaresma committed Nov 1, 2023
commit c024a1ddb5490082ee1c53dbcea21ab5e331a8b6
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend reviewing this file by looking into it without the diff.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
createTemplateVersionFileTree,
isAllowedFile,
} from "utils/templateVersion";
import { watchBuildLogsByTemplateVersionId } from "api/api";
import { ProvisionerJobLog, TemplateVersion } from "api/typesGenerated";

type Params = {
version: string;
Expand Down Expand Up @@ -47,9 +49,12 @@ export const TemplateVersionEditorPage: FC = () => {
const createTemplateVersionMutation = useMutation(
createTemplateVersion(orgId),
);
const [currentVersionOnEditor, setCurrentVersionOnEditor] =
useState<TemplateVersion>();

// Initialize file tree
useEffect(() => {
const initialize = async (file: ArrayBuffer) => {
const initializeFileTree = async (file: ArrayBuffer) => {
const tarReader = new TarReader();
await tarReader.readFile(file);
currentTarFileRef.current = tarReader;
Expand All @@ -58,24 +63,56 @@ export const TemplateVersionEditorPage: FC = () => {
};

if (fileQuery.data) {
initialize(fileQuery.data).catch(() => {
initializeFileTree(fileQuery.data).catch(() => {
console.error("Error on initializing the editor");
});
}
}, [fileQuery.data, sendEvent]);

// Initialize current version used on editor
useEffect(() => {
if (templateVersionQuery.data) {
setCurrentVersionOnEditor(templateVersionQuery.data);
}
}, [templateVersionQuery.data]);

// Watch version logs
const [logs, setLogs] = useState<ProvisionerJobLog[]>([]);
useEffect(() => {
if (!currentVersionOnEditor) {
return;
}

const socket = watchBuildLogsByTemplateVersionId(
currentVersionOnEditor.id,
{
onMessage: (log) => {
setLogs((logs) => [...logs, log]);
},
onDone: () => {
sendEvent({ type: "BUILD_DONE" });
},
onError: (error) => {
console.error(error);
},
},
);

return () => {
socket.close();
};
}, [currentVersionOnEditor, sendEvent]);

return (
<>
<Helmet>
<title>{pageTitle(`${templateName} · Template Editor`)}</title>
</Helmet>

{templateQuery.data && templateVersionQuery.data && fileTree && (
{templateQuery.data && currentVersionOnEditor && fileTree && (
<TemplateVersionEditor
template={templateQuery.data}
templateVersion={
editorState.context.version || templateVersionQuery.data
}
templateVersion={currentVersionOnEditor}
isBuildingNewVersion={Boolean(editorState.context.version)}
defaultFileTree={fileTree}
onPreview={async (newFileTree) => {
Expand All @@ -96,6 +133,8 @@ export const TemplateVersionEditorPage: FC = () => {
template_id: templateQuery.data.id,
file_id: serverFile.hash,
});
setLogs([]);
setCurrentVersionOnEditor(newVersion);
sendEvent({
type: "CREATED_VERSION",
data: newVersion,
Expand Down Expand Up @@ -140,7 +179,7 @@ export const TemplateVersionEditorPage: FC = () => {
editorState.context.version?.job.status !== "succeeded"
}
resources={editorState.context.resources}
buildLogs={editorState.context.buildLogs}
buildLogs={logs}
isPromptingMissingVariables={editorState.matches("promptVariables")}
missingVariables={editorState.context.missingVariables}
onSubmitMissingVariableValues={(values) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {
ProvisionerJobLog,
ProvisionerJobStatus,
TemplateVersion,
TemplateVersionVariable,
VariableValue,
Expand All @@ -15,7 +13,6 @@ export interface TemplateVersionEditorMachineContext {
templateId?: string;
version?: TemplateVersion;
resources?: WorkspaceResource[];
buildLogs?: ProvisionerJobLog[];
publishingError?: unknown;
lastSuccessfulPublishedVersion?: TemplateVersion;
missingVariables?: TemplateVersionVariable[];
Expand All @@ -40,7 +37,6 @@ export const templateVersionEditorMachine = createMachine(
fileId: string;
}
| { type: "CANCEL_MISSING_VARIABLE_VALUES" }
| { type: "ADD_BUILD_LOG"; log: ProvisionerJobLog }
| { type: "BUILD_DONE" }
| { type: "PUBLISH" }
| ({ type: "CONFIRM_PUBLISH" } & PublishVersionData)
Expand Down Expand Up @@ -118,14 +114,7 @@ export const templateVersionEditorMachine = createMachine(

watchingBuildLogs: {
tags: "loading",
invoke: {
id: "watchBuildLogs",
src: "watchBuildLogs",
},
on: {
ADD_BUILD_LOG: {
actions: "addBuildLog",
},
BUILD_DONE: "fetchingVersion",
},
},
Expand Down Expand Up @@ -192,7 +181,6 @@ export const templateVersionEditorMachine = createMachine(
{
actions: {
resetCreateBuildData: assign({
buildLogs: (_, _1) => [],
resources: (_, _1) => [],
}),
assignResources: assign({
Expand All @@ -205,28 +193,6 @@ export const templateVersionEditorMachine = createMachine(
lastSuccessfulPublishedVersion: (ctx) => ctx.version,
version: () => undefined,
}),
addBuildLog: assign({
buildLogs: (context, event) => {
const previousLogs = context.buildLogs ?? [];
return [...previousLogs, event.log];
},
// Instead of periodically fetching the version,
// we just assume the state is running after the first log.
//
// The machine fetches the version after the log stream ends anyways!
version: (context) => {
if (!context.version || context.buildLogs?.length !== 0) {
return context.version;
}
return {
...context.version,
job: {
...context.version.job,
status: "running" as ProvisionerJobStatus,
},
};
},
}),
assignPublishingError: assign({
publishingError: (_, event) => event.data,
}),
Expand Down Expand Up @@ -261,29 +227,6 @@ export const templateVersionEditorMachine = createMachine(
}
return API.getTemplateVersion(ctx.version.id);
},
watchBuildLogs:
({ version }) =>
async (callback) => {
if (!version) {
throw new Error("version must be set");
}

const socket = API.watchBuildLogsByTemplateVersionId(version.id, {
onMessage: (log) => {
callback({ type: "ADD_BUILD_LOG", log });
},
onDone: () => {
callback({ type: "BUILD_DONE" });
},
onError: (error) => {
console.error(error);
},
});

return () => {
socket.close();
};
},
getResources: (ctx) => {
if (!ctx.version) {
throw new Error("template version must be set");
Expand Down