mirror of
https://github.com/prowler-cloud/prowler.git
synced 2026-11-08 12:24:45 +00:00
fix(ui): complete grouped Jira dispatch review
This commit is contained in:
26 files changed
+1668
-894
No files matched your search
@@ -91,7 +91,12 @@ describe("sendJiraDispatch", () => {
|
||||
pollTaskUntilSettledMock.mockResolvedValue({
|
||||
ok: true,
|
||||
state: "completed",
|
||||
result: { created_count: 2, failed_count: 1 },
|
||||
result: {
|
||||
created_count: 2,
|
||||
failed_count: 1,
|
||||
failed_finding_ids: ["finding-3"],
|
||||
error: "Jira rejected one Finding.",
|
||||
},
|
||||
});
|
||||
|
||||
// When
|
||||
@@ -102,7 +107,8 @@ describe("sendJiraDispatch", () => {
|
||||
success: true,
|
||||
message: "2 Jira issues were created or updated successfully.",
|
||||
warning:
|
||||
"Jira dispatch completed with 1 failed and 2 created/updated issues.",
|
||||
"Jira rejected one Finding. Jira dispatch completed with 1 failed and 2 created/updated issues.",
|
||||
failedFindingIds: ["finding-3"],
|
||||
});
|
||||
});
|
||||
|
||||
@@ -189,7 +195,7 @@ describe("sendJiraDispatch", () => {
|
||||
});
|
||||
expect(result).toEqual({
|
||||
success: true,
|
||||
message: "Finding successfully sent to Jira!",
|
||||
message: "2 Jira issues were created or updated successfully.",
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
import { pollTaskUntilSettled } from "@/actions/task/poll";
|
||||
import { apiBaseUrl, getAuthHeaders } from "@/lib";
|
||||
import { evaluateJiraDispatchTask } from "@/lib/jira-dispatch-result";
|
||||
import { handleApiError } from "@/lib/server-actions-helper";
|
||||
import type {
|
||||
IntegrationProps,
|
||||
@@ -9,13 +10,10 @@ import type {
|
||||
JiraDispatchRequest,
|
||||
JiraDispatchResponse,
|
||||
JiraDispatchTarget,
|
||||
JiraDispatchTaskResult,
|
||||
} from "@/types/integrations";
|
||||
import { JIRA_DISPATCH_MODE, JIRA_DISPATCH_TARGET } from "@/types/integrations";
|
||||
|
||||
type JiraTaskResult = NonNullable<
|
||||
JiraDispatchResponse["data"]["attributes"]["result"]
|
||||
>;
|
||||
|
||||
interface JiraDispatchInput {
|
||||
integrationId: string;
|
||||
targetIds: string[];
|
||||
@@ -25,53 +23,6 @@ interface JiraDispatchInput {
|
||||
dispatchMode?: JiraDispatchMode;
|
||||
}
|
||||
|
||||
const getArrayCount = (value: unknown[] | undefined) =>
|
||||
Array.isArray(value) ? value.length : 0;
|
||||
|
||||
const getJiraDispatchFailedCount = (result: JiraTaskResult | undefined) => {
|
||||
if (!result) return 0;
|
||||
return Math.max(
|
||||
result.failed_count ?? 0,
|
||||
getArrayCount(result.failed_groups),
|
||||
getArrayCount(result.failed_batches),
|
||||
);
|
||||
};
|
||||
|
||||
const getJiraDispatchSuccessCount = (result: JiraTaskResult | undefined) => {
|
||||
if (!result) return 0;
|
||||
return Math.max(
|
||||
result.successful_count ?? 0,
|
||||
result.created_count ?? 0,
|
||||
result.updated_count ?? 0,
|
||||
getArrayCount(result.created_issues),
|
||||
getArrayCount(result.updated_issues),
|
||||
result.issue_key || result.issue_url ? 1 : 0,
|
||||
);
|
||||
};
|
||||
|
||||
const buildJiraDispatchFailureMessage = (
|
||||
result: JiraTaskResult | undefined,
|
||||
failedCount: number,
|
||||
) => {
|
||||
if (result?.error) return result.error;
|
||||
|
||||
const createdCount = result?.created_count ?? 0;
|
||||
const updatedCount = result?.updated_count ?? 0;
|
||||
const successCount = createdCount + updatedCount;
|
||||
return `Jira dispatch completed with ${failedCount} failed and ${successCount} created/updated issue${successCount === 1 ? "" : "s"}.`;
|
||||
};
|
||||
|
||||
const buildJiraDispatchSuccessMessage = (
|
||||
result: JiraTaskResult | undefined,
|
||||
) => {
|
||||
const successCount = getJiraDispatchSuccessCount(result);
|
||||
if (successCount > 1) {
|
||||
return `${successCount} Jira issues were created or updated successfully.`;
|
||||
}
|
||||
|
||||
return "Finding successfully sent to Jira!";
|
||||
};
|
||||
|
||||
export const getJiraIssueTypes = async (
|
||||
integrationId: string,
|
||||
projectKey: string,
|
||||
@@ -242,51 +193,8 @@ export const pollJiraDispatchTask = async (
|
||||
if (!res.ok) {
|
||||
return { success: false, error: res.error };
|
||||
}
|
||||
const { state, result } = res;
|
||||
const jiraResult = (result ?? undefined) as JiraTaskResult | undefined;
|
||||
|
||||
if (state === "completed") {
|
||||
const failedCount = getJiraDispatchFailedCount(jiraResult);
|
||||
if (failedCount > 0) {
|
||||
const successCount = getJiraDispatchSuccessCount(jiraResult);
|
||||
if (successCount > 0) {
|
||||
return {
|
||||
success: true,
|
||||
message: buildJiraDispatchSuccessMessage(jiraResult),
|
||||
warning: buildJiraDispatchFailureMessage(jiraResult, failedCount),
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: false,
|
||||
error: buildJiraDispatchFailureMessage(jiraResult, failedCount),
|
||||
};
|
||||
}
|
||||
|
||||
if (jiraResult?.success === false || jiraResult?.error) {
|
||||
return {
|
||||
success: false,
|
||||
error: jiraResult?.error || "Failed to create Jira issue.",
|
||||
};
|
||||
}
|
||||
|
||||
if (!jiraResult || getJiraDispatchSuccessCount(jiraResult) === 0) {
|
||||
return {
|
||||
success: false,
|
||||
error:
|
||||
"Jira dispatch completed but did not create or update any issues.",
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: buildJiraDispatchSuccessMessage(jiraResult),
|
||||
};
|
||||
}
|
||||
|
||||
if (state === "failed") {
|
||||
return { success: false, error: jiraResult?.error || "Task failed." };
|
||||
}
|
||||
|
||||
return { success: false, error: `Unknown task state: ${state}` };
|
||||
return evaluateJiraDispatchTask(
|
||||
res.state,
|
||||
res.result as JiraDispatchTaskResult | undefined,
|
||||
);
|
||||
};
|
||||
Reference in new issue
Block a user