Skip to content

Commit

Permalink
Fix inaccurate import error count
Browse files Browse the repository at this point in the history
  • Loading branch information
jportner committed Aug 19, 2020
1 parent 9d9b177 commit 32d828e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,33 @@ describe('processImportResponse()', () => {
expect(result.status).toBe('idle');
});

test('missing references get added to unmatchedReferences, but are not duplicated', () => {
const response = {
success: false,
successCount: 0,
errors: [
{
type: 'a',
id: '1',
error: {
type: 'missing_references',
references: [
{ type: 'index-pattern', id: '2' },
{ type: 'index-pattern', id: '3' },
{ type: 'index-pattern', id: '2' }, // duplicate that should not show in the result's unmatchedReferences
],
} as SavedObjectsImportMissingReferencesError,
meta: {},
},
],
};
const result = processImportResponse(response);
expect(result.unmatchedReferences).toEqual([
expect.objectContaining({ existingIndexPatternId: '2' }),
expect.objectContaining({ existingIndexPatternId: '3' }),
]);
});

test('success results get added to successfulImports and result in success status', () => {
const response = {
success: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ export interface FailedImport {
| SavedObjectsImportUnknownError;
}

interface UnmatchedReference {
existingIndexPatternId: string;
list: Array<Omit<SavedObjectsImportError, 'error'>>;
newIndexPatternId?: string;
}

export interface ProcessedImportResponse {
failedImports: FailedImport[];
successfulImports: SavedObjectsImportSuccess[];
unmatchedReferences: Array<{
existingIndexPatternId: string;
list: Array<Record<string, any>>;
newIndexPatternId: string | undefined;
}>;
unmatchedReferences: UnmatchedReference[];
status: 'success' | 'idle';
importCount: number;
conflictedSavedObjectsLinkedToSavedSearches: undefined;
Expand All @@ -60,7 +62,7 @@ export function processImportResponse(
): ProcessedImportResponse {
// Go through the failures and split between unmatchedReferences and failedImports
const failedImports = [];
const unmatchedReferences = new Map();
const unmatchedReferences = new Map<string, UnmatchedReference>();
for (const { error, ...obj } of response.errors || []) {
failedImports.push({ obj, error });
if (error.type !== 'missing_references') {
Expand All @@ -76,8 +78,10 @@ export function processImportResponse(
list: [],
newIndexPatternId: undefined,
};
conflict.list.push(obj);
unmatchedReferences.set(`${missingReference.type}:${missingReference.id}`, conflict);
if (!conflict.list.some(({ type, id }) => type === obj.type && id === obj.id)) {
conflict.list.push(obj);
unmatchedReferences.set(`${missingReference.type}:${missingReference.id}`, conflict);
}
}
}

Expand Down

0 comments on commit 32d828e

Please sign in to comment.