Skip to content
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 create version
  • Loading branch information
BrunoQuaresma committed Nov 1, 2023
commit 7eb5c91932e7cdf9fdeed640a220a0d98bd989f8
9 changes: 9 additions & 0 deletions site/src/api/queries/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ export const templateVersionVariables = (versionId: string) => {
};
};

export const createTemplateVersion = (orgId: string) => {
return {
mutationFn: async (request: CreateTemplateVersionRequest) => {
const newVersion = await API.createTemplateVersion(orgId, request);
return newVersion;
},
};
};

export const createAndBuildTemplateVersion = (orgId: string) => {
return {
mutationFn: async (request: CreateTemplateVersionRequest) => {
Expand Down
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 @@ -7,7 +7,11 @@ import { useNavigate, useParams } from "react-router-dom";
import { pageTitle } from "utils/page";
import { templateVersionEditorMachine } from "xServices/templateVersionEditor/templateVersionEditorXService";
import { useMutation, useQuery } from "react-query";
import { templateByName, templateVersionByName } from "api/queries/templates";
import {
createTemplateVersion,
templateByName,
templateVersionByName,
} from "api/queries/templates";
import { file, uploadFile } from "api/queries/files";
import { TarReader, TarWriter } from "utils/tar";
import { FileTree, traverse } from "utils/filetree";
Expand Down Expand Up @@ -40,6 +44,9 @@ export const TemplateVersionEditorPage: FC = () => {
const [fileTree, setFileTree] = useState<FileTree>();
const uploadFileMutation = useMutation(uploadFile());
const currentTarFileRef = useRef<TarReader | null>(null);
const createTemplateVersionMutation = useMutation(
createTemplateVersion(orgId),
);

useEffect(() => {
const initialize = async (file: ArrayBuffer) => {
Expand Down Expand Up @@ -75,17 +82,23 @@ export const TemplateVersionEditorPage: FC = () => {
if (!currentTarFileRef.current) {
return;
}

const newVersionFile = await generateVersionFiles(
currentTarFileRef.current,
newFileTree,
);
const newVersionUpload = await uploadFileMutation.mutateAsync(
const serverFile = await uploadFileMutation.mutateAsync(
newVersionFile,
);
const newVersion = await createTemplateVersionMutation.mutateAsync({
provisioner: "terraform",
storage_method: "file",
tags: {},
template_id: templateQuery.data.id,
file_id: serverFile.hash,
});
sendEvent({
type: "CREATE_VERSION",
fileId: newVersionUpload.hash,
type: "CREATED_VERSION",
data: newVersion,
});
}}
onPublish={() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export const templateVersionEditorMachine = createMachine(
context: {} as TemplateVersionEditorMachineContext,
events: {} as
| {
type: "CREATE_VERSION";
fileId: string;
type: "CREATED_VERSION";
data: TemplateVersion;
}
| {
type: "SET_MISSING_VARIABLE_VALUES";
Expand Down Expand Up @@ -69,9 +69,9 @@ export const templateVersionEditorMachine = createMachine(
states: {
idle: {
on: {
CREATE_VERSION: {
actions: ["resetCreateBuildData"],
target: "creatingBuild",
CREATED_VERSION: {
actions: ["resetCreateBuildData", "assignBuild"],
target: "watchingBuildLogs",
},
PUBLISH: {
target: "askPublishParameters",
Expand Down Expand Up @@ -104,7 +104,6 @@ export const templateVersionEditorMachine = createMachine(
},
},
},

creatingBuild: {
tags: "loading",
invoke: {
Expand Down