Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
60 changes: 57 additions & 3 deletions testplanit/app/[locale]/admin/imports/TestmoImportPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,7 @@ export function TestmoImportPanel() {
setImportStarting(false);
setAnalysisReloadToken(0);
setMappingConfig(createEmptyMappingConfiguration());
setProcessingState("idle");
setActiveStep(WizardStep.Upload);
setSelectedExistingJobId("");
setUploadProgress({ state: "idle", percent: 0 });
Expand Down Expand Up @@ -1247,8 +1248,11 @@ export function TestmoImportPanel() {

if (currentJob.status === "FAILED") {
setProcessingState("idle");
setErrorKey("analysis-failed");
setAnalysis(null);
// Only clear analysis if the failure was during analysis, not import
if (!currentJob.lastImportStartedAt) {
setErrorKey("analysis-failed");
setAnalysis(null);
}
return;
}
}, [currentJob]);
Expand Down Expand Up @@ -2017,6 +2021,43 @@ export function TestmoImportPanel() {
}
}, [currentJob, isJobActive]);

const handleRetry = useCallback(async () => {
if (
!currentJob ||
(currentJob.status !== "FAILED" && currentJob.status !== "CANCELED")
) {
return;
}

setCancelLoading(true);
try {
const response = await fetch(
`/api/imports/testmo/jobs/${currentJob.id}`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ action: "retry" }),
}
);

if (!response.ok) {
throw new Error("retry-failed");
}

const { job } = (await response.json()) as {
job: TestmoImportJobPayload;
};
setCurrentJob(job);
setPollingError(null);
setActiveStep(WizardStep.Configure);
} catch (error) {
console.error("Failed to retry import", error);
setPollingError("retry-failed");
} finally {
setCancelLoading(false);
}
}, [currentJob]);

useEffect(() => {
if (!analysis) {
datasetDetailCache.current.clear();
Expand Down Expand Up @@ -2218,6 +2259,20 @@ export function TestmoImportPanel() {
<AlertDescription>{currentJob.error}</AlertDescription>
</Alert>
)}
{(currentJob.status === "FAILED" || currentJob.status === "CANCELED") &&
currentJob.lastImportStartedAt && (
<div className="flex gap-2">
<Button
type="button"
variant="outline"
size="sm"
onClick={handleRetry}
disabled={cancelLoading}
>
{tCommon("actions.reconfigure")}
</Button>
</div>
)}
</div>
) : null;

Expand Down Expand Up @@ -3254,7 +3309,6 @@ export function TestmoImportPanel() {
type="button"
variant="ghost"
onClick={resetSelections}
disabled={isProcessing}
>
{t("testmo.reset")}
</Button>
Expand Down
76 changes: 52 additions & 24 deletions testplanit/app/api/imports/testmo/jobs/[jobId]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,6 @@ export async function POST(request: NextRequest, context: RouteContext) {
const body = await request.json().catch(() => ({}));
const action = body?.action;

if (action !== "cancel") {
return NextResponse.json(
{ error: "Unsupported action" },
{ status: 400 }
);
}

const job = await db.testmoImportJob.findUnique({
where: { id: jobId },
include: { datasets: false },
Expand All @@ -107,27 +100,62 @@ export async function POST(request: NextRequest, context: RouteContext) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}

if (!ACTIVE_STATUSES.has(job.status)) {
const payload = serializeImportJob(job);
return NextResponse.json({ job: payload }, { status: 200 });
if (action === "retry") {
// Allow retrying a failed or canceled job — reset to READY so the user
// can reconfigure the mapping and start the import again.
if (job.status !== "FAILED" && job.status !== "CANCELED") {
return NextResponse.json(
{ error: "Only failed or canceled jobs can be retried" },
{ status: 400 }
);
}

const updatedJob = await db.testmoImportJob.update({
where: { id: jobId },
data: {
status: "READY",
phase: "CONFIGURING",
statusMessage: "Ready for reconfiguration",
cancelRequested: false,
currentEntity: null,
processedCount: 0,
errorCount: 0,
skippedCount: 0,
totalCount: 0,
estimatedTimeRemaining: null,
processingRate: null,
lastImportStartedAt: null,
},
});

const payload: TestmoImportJobPayload = serializeImportJob(updatedJob);
return NextResponse.json({ job: payload });
}

if (job.cancelRequested) {
const payload = serializeImportJob(job);
return NextResponse.json({ job: payload }, { status: 200 });
if (action === "cancel") {
if (!ACTIVE_STATUSES.has(job.status)) {
const payload = serializeImportJob(job);
return NextResponse.json({ job: payload }, { status: 200 });
}

if (job.cancelRequested) {
const payload = serializeImportJob(job);
return NextResponse.json({ job: payload }, { status: 200 });
}

const updatedJob = await db.testmoImportJob.update({
where: { id: jobId },
data: {
cancelRequested: true,
statusMessage: "Cancellation requested",
},
});

const payload: TestmoImportJobPayload = serializeImportJob(updatedJob);
return NextResponse.json({ job: payload });
}

const updatedJob = await db.testmoImportJob.update({
where: { id: jobId },
data: {
cancelRequested: true,
statusMessage: "Cancellation requested",
},
});

const payload: TestmoImportJobPayload = serializeImportJob(updatedJob);

return NextResponse.json({ job: payload });
return NextResponse.json({ error: "Unsupported action" }, { status: 400 });
} catch (error) {
console.error("Failed to update Testmo import job", error);
return NextResponse.json(
Expand Down
Loading
Loading