Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Workspace] Workspace assets page crashed after library_write permission revoked #8648

Merged
merged 3 commits into from
Oct 18, 2024
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
2 changes: 2 additions & 0 deletions changelogs/fragments/8648.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- [Workspace] Workspace assets page crashed after library_write permission revoked ([#8648](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8648))

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,13 @@ export class DuplicateResultFlyout extends React.Component<DuplicateResultFlyout
<EuiFlyout ownFocus onClose={onClose} size="s">
<EuiFlyoutHeader hasBorder>
<EuiTitle size="s">
<FormattedMessage
id="savedObjectsManagement.copyResult.title"
defaultMessage="Copy saved objects to {workspaceName}"
values={{ workspaceName }}
/>
<h2>
<FormattedMessage
id="savedObjectsManagement.copyResult.title"
defaultMessage="Copy saved objects to {workspaceName}"
values={{ workspaceName }}
/>
</h2>
</EuiTitle>
</EuiFlyoutHeader>
<EuiFlyoutBody>{this.copyResult({ failedCopies, successfulCopies })}</EuiFlyoutBody>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,38 @@ describe('SavedObjectsTable', () => {
});
});

it('should catch error when duplicating selected object is fail', async () => {
it('should catch error when duplicating selected object is failed', async () => {
const component = shallowRender({ applications, workspaces });
component.setState({ isShowingDuplicateModal: true });

const mockCopy = jest.fn().mockResolvedValue({ error: 'error' });
workspaces.client$.next({ copy: mockCopy });
const client = workspaces.client$.getValue();

// Ensure all promises resolve
await new Promise((resolve) => process.nextTick(resolve));
// Ensure the state changes are reflected
component.update();

await component.instance().onDuplicate(mockSelectedSavedObjects, false, 'workspace2', 'bar');

expect(client?.copy).toHaveBeenCalledWith(
[
{ id: '1', type: 'dashboard' },
{ id: '2', type: 'dashboard' },
],
'workspace2',
false
);
component.update();

expect(notifications.toasts.addDanger).toHaveBeenCalledWith({
title: 'Unable to copy 2 saved objects.',
text: 'error',
});
});

it('should show error toast when copy is fail', async () => {
const component = shallowRender({ applications, workspaces });
component.setState({ isShowingDuplicateModal: true });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -761,13 +761,14 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb
const { notifications, workspaces, useUpdatedUX } = this.props;
const workspaceClient = workspaces.client$.getValue();

const showErrorNotification = () => {
const showErrorNotification = (text?: string) => {
notifications.toasts.addDanger({
title: i18n.translate('savedObjectsManagement.objectsTable.duplicate.dangerNotification', {
defaultMessage:
'Unable to copy {useUpdatedUX, select, true {{errorCount, plural, one {# asset} other {# assets}}} other {{errorCount, plural, one {# saved object} other {# saved objects}}}}.',
values: { errorCount: savedObjects.length, useUpdatedUX },
}),
...(text && { text }),
});
};
if (!workspaceClient) {
Expand All @@ -781,18 +782,22 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb
targetWorkspace,
includeReferencesDeep
);

this.setState({
isShowingDuplicateResultFlyout: true,
failedCopies: result.success ? [] : result.errors,
successfulCopies: result.successCount > 0 ? result.successResults : [],
targetWorkspaceName,
});
if (result?.error) {
showErrorNotification(result.error);
} else {
this.setState({
isShowingDuplicateResultFlyout: true,
failedCopies: result?.errors || [],
successfulCopies: result?.successCount > 0 ? result.successResults : [],
targetWorkspaceName,
});
}
} catch (e) {
showErrorNotification();
} finally {
this.hideDuplicateModal();
await this.refreshObjects();
}
this.hideDuplicateModal();
await this.refreshObjects();
};

renderDuplicateModal() {
Expand Down
Loading