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

Fixes 1974: Added manual snapshot #186

Merged
merged 14 commits into from
Jan 18, 2024
21 changes: 14 additions & 7 deletions src/Pages/ContentListTable/ContentListTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
defaultSnapshotItem,
testRepositoryParamsResponse,
} from '../../testingHelpers';
import { render, waitFor } from '@testing-library/react';
import { render, waitFor, fireEvent } from '@testing-library/react';
import ContentListTable from './ContentListTable';
import { useContentListQuery, useRepositoryParams } from '../../services/Content/ContentQueries';
import AddContent from './components/AddContent/AddContent';
Expand All @@ -20,10 +20,13 @@ jest.mock('../../services/Content/ContentQueries', () => ({
useBulkDeleteContentItemMutate: () => ({ isLoading: false }),
useIntrospectRepositoryMutate: () => ({ isLoading: false }),
useFetchGpgKey: () => ({ fetchGpgKey: () => '' }),
useTriggerSnapshot: () => ({ isLoading: false }),
}));

jest.mock('../../middleware/AppContext', () => ({
useAppContext: () => ({
features: { snapshots: { accessible: true } },
rbac: { write: true, read: true },
contentOrigin: ContentOrigin.EXTERNAL,
setContentOrigin: () => {},
}),
Expand Down Expand Up @@ -72,11 +75,7 @@ it('Render a loading state', () => {
expect(queryByLabelText('Loading')).toBeInTheDocument();
});

it('Render with a single row', () => {
jest.mock('../../middleware/AppContext', () => ({
useAppContext: (features) => ({ features: features.snapshot.accessible }),
}));

it('Render with a single row', async () => {
(useRepositoryParams as jest.Mock).mockImplementation(() => ({
isLoading: false,
data: testRepositoryParamsResponse,
Expand All @@ -89,7 +88,7 @@ it('Render with a single row', () => {
},
}));

const { queryByText } = render(
const { queryByText, getByRole } = render(
<ReactQueryTestWrapper>
<ContentListTable />
</ReactQueryTestWrapper>,
Expand Down Expand Up @@ -120,6 +119,14 @@ it('Render with a single row', () => {
),
).toBeInTheDocument(),
);

const kebabButton = getByRole('button', { name: 'Kebab toggle' });
fireEvent.click(kebabButton);

getByRole('menuitem', { name: 'Edit' });
getByRole('menuitem', { name: 'Trigger Snapshot' });
getByRole('menuitem', { name: 'Introspect Now' });
getByRole('menuitem', { name: 'Delete' });
});

it('Render with a single redhat repository', () => {
Expand Down
14 changes: 14 additions & 0 deletions src/Pages/ContentListTable/ContentListTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
useContentListQuery,
useIntrospectRepositoryMutate,
useRepositoryParams,
useTriggerSnapshot,
} from '../../services/Content/ContentQueries';
import ContentListFilters from './components/ContentListFilters';
import Hide from '../../components/Hide/Hide';
Expand Down Expand Up @@ -167,6 +168,12 @@ const ContentListTable = () => {
const introspectRepoForUuid = (uuid: string): Promise<void> =>
introspectRepository({ uuid: uuid, reset_count: true } as IntrospectRepositoryRequestItem);

const { mutateAsync: triggerSnapshotMutation } = useTriggerSnapshot(queryClient);

const triggerSnapshot = async (uuid: string): Promise<void> => {
Andrewgdewar marked this conversation as resolved.
Show resolved Hide resolved
triggerSnapshotMutation(uuid);
};

const { mutateAsync: deleteItems, isLoading: isDeletingItems } = useBulkDeleteContentItemMutate(
queryClient,
checkedRepositories,
Expand Down Expand Up @@ -274,6 +281,13 @@ const ContentListTable = () => {
navigate(`${rowData.uuid}/snapshots`);
},
},
{
isDisabled: actionTakingPlace || rowData?.status === 'Retrying',
title: 'Trigger Snapshot',
onClick: () => {
triggerSnapshot(rowData.uuid);
},
},
]
: []),
{
Expand Down
10 changes: 10 additions & 0 deletions src/services/Content/ContentApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,16 @@ export const introspectRepository: (
return data;
};

export const triggerSnapshot: (repositoryUUID: string) => Promise<void> = async (
repositoryUUID,
) => {
const { data } = await axios.post(
`/api/content-sources/v1.0/repositories/${repositoryUUID}/snapshot/`,
{},
);
return data;
};

export const getRepoConfigFile: (
repo_uuid: string,
snapshot_uuid: string,
Expand Down
18 changes: 18 additions & 0 deletions src/services/Content/ContentQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
SnapshotListResponse,
ContentOrigin,
getRepoConfigFile,
triggerSnapshot,
} from './ContentApi';
import { ADMIN_TASK_LIST_KEY } from '../AdminTasks/AdminTaskQueries';
import useErrorNotification from '../../Hooks/useErrorNotification';
Expand Down Expand Up @@ -515,6 +516,23 @@ export const useGetPackagesQuery = (
);
};

export const useTriggerSnapshot = (queryClient: QueryClient) => {
const errorNotifier = useErrorNotification();
const { notify } = useNotification();
return useMutation(triggerSnapshot, {
onSuccess: () => {
notify({
variant: AlertVariant.success,
title: 'Snapshot triggered successfully',
});
queryClient.invalidateQueries(CONTENT_LIST_KEY);
},
onError: (err) => {
errorNotifier('Error triggering snapshot', 'An error occurred', err);
},
});
};

export const useIntrospectRepositoryMutate = (
queryClient: QueryClient,
page: number,
Expand Down
Loading