|
5 | 5 | type CreateTemplateVersionRequest,
|
6 | 6 | type ProvisionerJobStatus,
|
7 | 7 | type TemplateVersion,
|
| 8 | + CreateTemplateRequest, |
| 9 | + ProvisionerJob, |
8 | 10 | } from "api/typesGenerated";
|
9 | 11 | import { type QueryClient, type QueryOptions } from "react-query";
|
10 | 12 | import { delay } from "utils/delay";
|
@@ -80,25 +82,10 @@ export const templateVersionVariables = (versionId: string) => {
|
80 | 82 |
|
81 | 83 | export const createAndBuildTemplateVersion = (orgId: string) => {
|
82 | 84 | return {
|
83 |
| - mutationFn: async ( |
84 |
| - request: CreateTemplateVersionRequest, |
85 |
| - ): Promise<string> => { |
| 85 | + mutationFn: async (request: CreateTemplateVersionRequest) => { |
86 | 86 | const newVersion = await API.createTemplateVersion(orgId, request);
|
87 |
| - |
88 |
| - let data: TemplateVersion; |
89 |
| - let jobStatus: ProvisionerJobStatus; |
90 |
| - do { |
91 |
| - await delay(1000); |
92 |
| - data = await API.getTemplateVersion(newVersion.id); |
93 |
| - jobStatus = data.job.status; |
94 |
| - |
95 |
| - if (jobStatus === "succeeded") { |
96 |
| - return newVersion.id; |
97 |
| - } |
98 |
| - } while (jobStatus === "pending" || jobStatus === "running"); |
99 |
| - |
100 |
| - // No longer pending/running, but didn't succeed |
101 |
| - throw data.job.error; |
| 87 | + await waitBuildToBeFinished(newVersion); |
| 88 | + return newVersion; |
102 | 89 | },
|
103 | 90 | };
|
104 | 91 | };
|
@@ -133,3 +120,53 @@ export const templateVersionExternalAuth = (versionId: string) => {
|
133 | 120 | queryFn: () => API.getTemplateVersionExternalAuth(versionId),
|
134 | 121 | };
|
135 | 122 | };
|
| 123 | + |
| 124 | +export const createTemplate = () => { |
| 125 | + return { |
| 126 | + mutationFn: createTemplateFn, |
| 127 | + }; |
| 128 | +}; |
| 129 | + |
| 130 | +const createTemplateFn = async (options: { |
| 131 | + organizationId: string; |
| 132 | + version: CreateTemplateVersionRequest; |
| 133 | + template: Omit<CreateTemplateRequest, "template_version_id">; |
| 134 | +}) => { |
| 135 | + const version = await API.createTemplateVersion( |
| 136 | + options.organizationId, |
| 137 | + options.version, |
| 138 | + ); |
| 139 | + await waitBuildToBeFinished(version); |
| 140 | + return API.createTemplate(options.organizationId, { |
| 141 | + ...options.template, |
| 142 | + template_version_id: version.id, |
| 143 | + }); |
| 144 | +}; |
| 145 | + |
| 146 | +const waitBuildToBeFinished = async (version: TemplateVersion) => { |
| 147 | + let data: TemplateVersion; |
| 148 | + let jobStatus: ProvisionerJobStatus; |
| 149 | + do { |
| 150 | + await delay(1000); |
| 151 | + data = await API.getTemplateVersion(version.id); |
| 152 | + jobStatus = data.job.status; |
| 153 | + |
| 154 | + if (jobStatus === "succeeded") { |
| 155 | + return version.id; |
| 156 | + } |
| 157 | + } while (jobStatus === "pending" || jobStatus === "running"); |
| 158 | + |
| 159 | + // No longer pending/running, but didn't succeed |
| 160 | + throw new JobError(data.job, version); |
| 161 | +}; |
| 162 | + |
| 163 | +export class JobError extends Error { |
| 164 | + public job: ProvisionerJob; |
| 165 | + public version: TemplateVersion; |
| 166 | + |
| 167 | + constructor(job: ProvisionerJob, version: TemplateVersion) { |
| 168 | + super(job.error); |
| 169 | + this.job = job; |
| 170 | + this.version = version; |
| 171 | + } |
| 172 | +} |
0 commit comments