diff --git a/public/app/features/dashboard-scene/saving/SaveProvisionedDashboard.tsx b/public/app/features/dashboard-scene/saving/SaveProvisionedDashboard.tsx index 6de5ca4c4c2..1e3a6e862f6 100644 --- a/public/app/features/dashboard-scene/saving/SaveProvisionedDashboard.tsx +++ b/public/app/features/dashboard-scene/saving/SaveProvisionedDashboard.tsx @@ -1,13 +1,14 @@ import { useEffect } from 'react'; -import { useForm } from 'react-hook-form'; +import { Controller, useForm } from 'react-hook-form'; import { AppEvents } from '@grafana/data'; import { getAppEvents } from '@grafana/runtime'; -import { Button, Stack, TextArea, Field, Input, Alert, LinkButton } from '@grafana/ui'; +import { Alert, Button, Field, Input, LinkButton, RadioButtonGroup, Stack, TextArea } from '@grafana/ui'; import { AnnoKeyRepoName, AnnoKeyRepoPath } from 'app/features/apiserver/types'; import { DashboardMeta } from 'app/types'; import { useGetRepositoryQuery, useUpdateRepositoryFilesMutation } from '../../provisioning/api'; +import { WorkflowOption } from '../../provisioning/types'; import { createPRLink, validateBranchName } from '../../provisioning/utils/git'; import { DashboardScene } from '../scene/DashboardScene'; @@ -20,6 +21,7 @@ type FormData = { path: string; comment?: string; repo: string; + workflow?: WorkflowOption; }; function getDefaultValues(meta: DashboardMeta) { @@ -32,7 +34,20 @@ function getDefaultValues(meta: DashboardMeta) { ref = path.substring(idx + 1); path = path.substring(0, idx); } - return { ref, path, repo, comment: '' }; + return { + ref: ref || `dashboard/${Date.now()}`, + path, + repo, + comment: '', + workflow: WorkflowOption.Branch, + }; +} + +function getWorkflowOptions(branch = 'main') { + return [ + { label: `Commit to ${branch}`, value: WorkflowOption.Branch }, + { label: 'Create pull request', value: WorkflowOption.PullRequest }, + ]; } export interface Props { @@ -50,12 +65,14 @@ export function SaveProvisionedDashboard({ meta, drawer, changeInfo, dashboard } handleSubmit, watch, formState: { errors }, + control, } = useForm({ defaultValues }); const repositoryConfigQuery = useGetRepositoryQuery({ name: defaultValues.repo }); const repositoryConfig = repositoryConfigQuery?.data?.spec; const isGitHub = repositoryConfig?.type === 'github'; - const [repo, ref] = watch(['repo', 'ref']); + const [repo, ref, workflow] = watch(['repo', 'ref', 'workflow']); const href = createPRLink(repositoryConfig, repo, ref); + const { isDirty } = dashboard.state; useEffect(() => { const appEvents = getAppEvents(); @@ -65,8 +82,6 @@ export function SaveProvisionedDashboard({ meta, drawer, changeInfo, dashboard } payload: ['Dashboard saved'], }); dashboard.setState({ isDirty: false }); - // TODO Avoid full reload - window.location.reload(); } else if (request.isError) { appEvents.publish({ type: AppEvents.alertError.name, @@ -98,18 +113,31 @@ export function SaveProvisionedDashboard({ meta, drawer, changeInfo, dashboard } - + {isGitHub && ( - : ''} - > - - + <> + + ( + + )} + /> + + {workflow === WorkflowOption.PullRequest && ( + : ''} + > + + + )} + )} @@ -122,15 +150,28 @@ export function SaveProvisionedDashboard({ meta, drawer, changeInfo, dashboard } /> + {workflow === WorkflowOption.PullRequest && isDirty && ( + + You have unsaved changes. Please save them before opening a pull request. + + )} + - - {isGitHub && ( - + {isGitHub && workflow === WorkflowOption.PullRequest && ( + Open pull request )} diff --git a/public/app/features/provisioning/dashboard.ts b/public/app/features/provisioning/dashboard.ts index ec8a6ff7031..532ee35dbec 100644 --- a/public/app/features/provisioning/dashboard.ts +++ b/public/app/features/provisioning/dashboard.ts @@ -35,7 +35,7 @@ export async function loadDashboardFromProvisioning(repo: string, path: string): anno[AnnoKeyRepoName] = repo; anno[AnnoKeyRepoPath] = path; if (ref) { - anno[AnnoKeyRepoPath] += path + '#' + ref; + anno[AnnoKeyRepoPath] = path + '#' + ref; } return { diff --git a/public/app/features/provisioning/types.ts b/public/app/features/provisioning/types.ts index 42d7b868ae9..8603b34af50 100644 --- a/public/app/features/provisioning/types.ts +++ b/public/app/features/provisioning/types.ts @@ -11,3 +11,8 @@ export interface ProvisioningPreview { file: string; ref?: string; } + +export enum WorkflowOption { + Branch = 'branch', + PullRequest = 'pr', +}