Provisioning: Use move API on dashboards settings page (#108671)
* Provisioning: Use Move API to move dashboards in Dashboards Settings Page * i18n
This commit is contained in:
@@ -5,7 +5,6 @@ import { getAppEvents } from '@grafana/runtime';
|
||||
import { useGetFolderQuery } from 'app/api/clients/folder/v1beta1';
|
||||
import {
|
||||
useCreateRepositoryFilesWithPathMutation,
|
||||
useDeleteRepositoryFilesWithPathMutation,
|
||||
useGetRepositoryFilesWithPathQuery,
|
||||
} from 'app/api/clients/provisioning/v0alpha1';
|
||||
import { AnnoKeySourcePath } from 'app/features/apiserver/types';
|
||||
@@ -26,7 +25,6 @@ jest.mock('@grafana/runtime', () => {
|
||||
jest.mock('app/api/clients/provisioning/v0alpha1', () => ({
|
||||
useGetRepositoryFilesWithPathQuery: jest.fn(),
|
||||
useCreateRepositoryFilesWithPathMutation: jest.fn(),
|
||||
useDeleteRepositoryFilesWithPathMutation: jest.fn(),
|
||||
provisioningAPIv0alpha1: {
|
||||
endpoints: {
|
||||
listRepository: {
|
||||
@@ -109,13 +107,6 @@ const mockCreateRequest = {
|
||||
error: null,
|
||||
};
|
||||
|
||||
const mockDeleteRequest = {
|
||||
isSuccess: false,
|
||||
isError: false,
|
||||
isLoading: false,
|
||||
error: null,
|
||||
};
|
||||
|
||||
describe('MoveProvisionedDashboardForm', () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
@@ -156,8 +147,6 @@ describe('MoveProvisionedDashboardForm', () => {
|
||||
|
||||
(useCreateRepositoryFilesWithPathMutation as jest.Mock).mockReturnValue([jest.fn(), mockCreateRequest]);
|
||||
|
||||
(useDeleteRepositoryFilesWithPathMutation as jest.Mock).mockReturnValue([jest.fn(), mockDeleteRequest]);
|
||||
|
||||
(useProvisionedRequestHandler as jest.Mock).mockReturnValue(undefined);
|
||||
});
|
||||
|
||||
|
||||
@@ -10,7 +10,6 @@ import { useGetFolderQuery } from 'app/api/clients/folder/v1beta1';
|
||||
import {
|
||||
RepositoryView,
|
||||
useCreateRepositoryFilesWithPathMutation,
|
||||
useDeleteRepositoryFilesWithPathMutation,
|
||||
useGetRepositoryFilesWithPathQuery,
|
||||
} from 'app/api/clients/provisioning/v0alpha1';
|
||||
import { AnnoKeySourcePath } from 'app/features/apiserver/types';
|
||||
@@ -64,8 +63,7 @@ export function MoveProvisionedDashboardForm({
|
||||
|
||||
const { data: targetFolder } = useGetFolderQuery({ name: targetFolderUID! }, { skip: !targetFolderUID });
|
||||
|
||||
const [createFile, createRequest] = useCreateRepositoryFilesWithPathMutation();
|
||||
const [deleteFile, deleteRequest] = useDeleteRepositoryFilesWithPathMutation();
|
||||
const [moveFile, moveRequest] = useCreateRepositoryFilesWithPathMutation();
|
||||
const [targetPath, setTargetPath] = useState<string>('');
|
||||
|
||||
const navigate = useNavigate();
|
||||
@@ -103,32 +101,15 @@ export function MoveProvisionedDashboardForm({
|
||||
const commitMessage = comment || `Move dashboard: ${dashboard.state.title}`;
|
||||
|
||||
try {
|
||||
await createFile({
|
||||
await moveFile({
|
||||
name: repo,
|
||||
path: targetPath,
|
||||
ref: branchRef,
|
||||
message: commitMessage,
|
||||
body: currentFileData.resource.file,
|
||||
}).unwrap();
|
||||
|
||||
await deleteFile({
|
||||
name: repo,
|
||||
path: path,
|
||||
ref: branchRef,
|
||||
message: commitMessage,
|
||||
originalPath: path,
|
||||
}).unwrap();
|
||||
} catch (error) {
|
||||
if (createRequest.isSuccess && !deleteRequest.isSuccess) {
|
||||
appEvents.publish({
|
||||
type: AppEvents.alertWarning.name,
|
||||
payload: [
|
||||
t(
|
||||
'dashboard-scene.move-provisioned-dashboard-form.partial-failure-warning',
|
||||
'Dashboard was created at new location but could not be deleted from original location. Please manually remove the old file.'
|
||||
),
|
||||
],
|
||||
});
|
||||
}
|
||||
appEvents.publish({
|
||||
type: AppEvents.alertError.name,
|
||||
payload: [t('dashboard-scene.move-provisioned-dashboard-form.api-error', 'Failed to move dashboard'), error],
|
||||
@@ -148,15 +129,15 @@ export function MoveProvisionedDashboardForm({
|
||||
panelEditor?.onDiscard();
|
||||
const url = buildResourceBranchRedirectUrl({
|
||||
paramName: 'new_pull_request_url',
|
||||
paramValue: createRequest?.data?.urls?.newPullRequestURL,
|
||||
repoType: createRequest?.data?.repository?.type,
|
||||
paramValue: moveRequest?.data?.urls?.newPullRequestURL,
|
||||
repoType: moveRequest?.data?.repository?.type,
|
||||
});
|
||||
navigate(url);
|
||||
};
|
||||
|
||||
useProvisionedRequestHandler({
|
||||
dashboard,
|
||||
request: createRequest,
|
||||
request: moveRequest,
|
||||
workflow,
|
||||
handlers: {
|
||||
onBranchSuccess,
|
||||
@@ -164,7 +145,7 @@ export function MoveProvisionedDashboardForm({
|
||||
},
|
||||
});
|
||||
|
||||
const isLoading = createRequest.isLoading || deleteRequest.isLoading;
|
||||
const isLoading = moveRequest.isLoading;
|
||||
|
||||
return (
|
||||
<Drawer
|
||||
|
||||
@@ -5794,7 +5794,6 @@
|
||||
"move-action": "Move dashboard",
|
||||
"move-read-only-message": "This dashboard cannot be moved directly from Grafana because the repository is read-only. To move this dashboard, please move the file in your Git repository.",
|
||||
"moving": "Moving...",
|
||||
"partial-failure-warning": "Dashboard was created at new location but could not be deleted from original location. Please manually remove the old file.",
|
||||
"target-path-label": "Target path",
|
||||
"title-this-repository-is-read-only": "This repository is read only"
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user