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 { useGetFolderQuery } from 'app/api/clients/folder/v1beta1';
|
||||||
import {
|
import {
|
||||||
useCreateRepositoryFilesWithPathMutation,
|
useCreateRepositoryFilesWithPathMutation,
|
||||||
useDeleteRepositoryFilesWithPathMutation,
|
|
||||||
useGetRepositoryFilesWithPathQuery,
|
useGetRepositoryFilesWithPathQuery,
|
||||||
} from 'app/api/clients/provisioning/v0alpha1';
|
} from 'app/api/clients/provisioning/v0alpha1';
|
||||||
import { AnnoKeySourcePath } from 'app/features/apiserver/types';
|
import { AnnoKeySourcePath } from 'app/features/apiserver/types';
|
||||||
@@ -26,7 +25,6 @@ jest.mock('@grafana/runtime', () => {
|
|||||||
jest.mock('app/api/clients/provisioning/v0alpha1', () => ({
|
jest.mock('app/api/clients/provisioning/v0alpha1', () => ({
|
||||||
useGetRepositoryFilesWithPathQuery: jest.fn(),
|
useGetRepositoryFilesWithPathQuery: jest.fn(),
|
||||||
useCreateRepositoryFilesWithPathMutation: jest.fn(),
|
useCreateRepositoryFilesWithPathMutation: jest.fn(),
|
||||||
useDeleteRepositoryFilesWithPathMutation: jest.fn(),
|
|
||||||
provisioningAPIv0alpha1: {
|
provisioningAPIv0alpha1: {
|
||||||
endpoints: {
|
endpoints: {
|
||||||
listRepository: {
|
listRepository: {
|
||||||
@@ -109,13 +107,6 @@ const mockCreateRequest = {
|
|||||||
error: null,
|
error: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
const mockDeleteRequest = {
|
|
||||||
isSuccess: false,
|
|
||||||
isError: false,
|
|
||||||
isLoading: false,
|
|
||||||
error: null,
|
|
||||||
};
|
|
||||||
|
|
||||||
describe('MoveProvisionedDashboardForm', () => {
|
describe('MoveProvisionedDashboardForm', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
jest.clearAllMocks();
|
jest.clearAllMocks();
|
||||||
@@ -156,8 +147,6 @@ describe('MoveProvisionedDashboardForm', () => {
|
|||||||
|
|
||||||
(useCreateRepositoryFilesWithPathMutation as jest.Mock).mockReturnValue([jest.fn(), mockCreateRequest]);
|
(useCreateRepositoryFilesWithPathMutation as jest.Mock).mockReturnValue([jest.fn(), mockCreateRequest]);
|
||||||
|
|
||||||
(useDeleteRepositoryFilesWithPathMutation as jest.Mock).mockReturnValue([jest.fn(), mockDeleteRequest]);
|
|
||||||
|
|
||||||
(useProvisionedRequestHandler as jest.Mock).mockReturnValue(undefined);
|
(useProvisionedRequestHandler as jest.Mock).mockReturnValue(undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import { useGetFolderQuery } from 'app/api/clients/folder/v1beta1';
|
|||||||
import {
|
import {
|
||||||
RepositoryView,
|
RepositoryView,
|
||||||
useCreateRepositoryFilesWithPathMutation,
|
useCreateRepositoryFilesWithPathMutation,
|
||||||
useDeleteRepositoryFilesWithPathMutation,
|
|
||||||
useGetRepositoryFilesWithPathQuery,
|
useGetRepositoryFilesWithPathQuery,
|
||||||
} from 'app/api/clients/provisioning/v0alpha1';
|
} from 'app/api/clients/provisioning/v0alpha1';
|
||||||
import { AnnoKeySourcePath } from 'app/features/apiserver/types';
|
import { AnnoKeySourcePath } from 'app/features/apiserver/types';
|
||||||
@@ -64,8 +63,7 @@ export function MoveProvisionedDashboardForm({
|
|||||||
|
|
||||||
const { data: targetFolder } = useGetFolderQuery({ name: targetFolderUID! }, { skip: !targetFolderUID });
|
const { data: targetFolder } = useGetFolderQuery({ name: targetFolderUID! }, { skip: !targetFolderUID });
|
||||||
|
|
||||||
const [createFile, createRequest] = useCreateRepositoryFilesWithPathMutation();
|
const [moveFile, moveRequest] = useCreateRepositoryFilesWithPathMutation();
|
||||||
const [deleteFile, deleteRequest] = useDeleteRepositoryFilesWithPathMutation();
|
|
||||||
const [targetPath, setTargetPath] = useState<string>('');
|
const [targetPath, setTargetPath] = useState<string>('');
|
||||||
|
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
@@ -103,32 +101,15 @@ export function MoveProvisionedDashboardForm({
|
|||||||
const commitMessage = comment || `Move dashboard: ${dashboard.state.title}`;
|
const commitMessage = comment || `Move dashboard: ${dashboard.state.title}`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await createFile({
|
await moveFile({
|
||||||
name: repo,
|
name: repo,
|
||||||
path: targetPath,
|
path: targetPath,
|
||||||
ref: branchRef,
|
ref: branchRef,
|
||||||
message: commitMessage,
|
message: commitMessage,
|
||||||
body: currentFileData.resource.file,
|
body: currentFileData.resource.file,
|
||||||
}).unwrap();
|
originalPath: path,
|
||||||
|
|
||||||
await deleteFile({
|
|
||||||
name: repo,
|
|
||||||
path: path,
|
|
||||||
ref: branchRef,
|
|
||||||
message: commitMessage,
|
|
||||||
}).unwrap();
|
}).unwrap();
|
||||||
} catch (error) {
|
} 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({
|
appEvents.publish({
|
||||||
type: AppEvents.alertError.name,
|
type: AppEvents.alertError.name,
|
||||||
payload: [t('dashboard-scene.move-provisioned-dashboard-form.api-error', 'Failed to move dashboard'), error],
|
payload: [t('dashboard-scene.move-provisioned-dashboard-form.api-error', 'Failed to move dashboard'), error],
|
||||||
@@ -148,15 +129,15 @@ export function MoveProvisionedDashboardForm({
|
|||||||
panelEditor?.onDiscard();
|
panelEditor?.onDiscard();
|
||||||
const url = buildResourceBranchRedirectUrl({
|
const url = buildResourceBranchRedirectUrl({
|
||||||
paramName: 'new_pull_request_url',
|
paramName: 'new_pull_request_url',
|
||||||
paramValue: createRequest?.data?.urls?.newPullRequestURL,
|
paramValue: moveRequest?.data?.urls?.newPullRequestURL,
|
||||||
repoType: createRequest?.data?.repository?.type,
|
repoType: moveRequest?.data?.repository?.type,
|
||||||
});
|
});
|
||||||
navigate(url);
|
navigate(url);
|
||||||
};
|
};
|
||||||
|
|
||||||
useProvisionedRequestHandler({
|
useProvisionedRequestHandler({
|
||||||
dashboard,
|
dashboard,
|
||||||
request: createRequest,
|
request: moveRequest,
|
||||||
workflow,
|
workflow,
|
||||||
handlers: {
|
handlers: {
|
||||||
onBranchSuccess,
|
onBranchSuccess,
|
||||||
@@ -164,7 +145,7 @@ export function MoveProvisionedDashboardForm({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const isLoading = createRequest.isLoading || deleteRequest.isLoading;
|
const isLoading = moveRequest.isLoading;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Drawer
|
<Drawer
|
||||||
|
|||||||
@@ -5794,7 +5794,6 @@
|
|||||||
"move-action": "Move dashboard",
|
"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.",
|
"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...",
|
"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",
|
"target-path-label": "Target path",
|
||||||
"title-this-repository-is-read-only": "This repository is read only"
|
"title-this-repository-is-read-only": "This repository is read only"
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user