Skip to content

Commit

Permalink
Fixes 1974: Added manual snapshot (content-services#186)
Browse files Browse the repository at this point in the history
* Fixes 1974: Added manual snapshot

* My changes

* My new changes

* my new changes

* My final change

* My final change

* My new change

* My test for the TriggerSnapshot

* Added theTrigger Snapshot still exist

* ..

* Removed lines

* Add snapshots accessible requirement

* Update tests to use correct context state

---------

Co-authored-by: Andrew Dewar <[email protected]>
  • Loading branch information
kwarnerredhat and Andrewgdewar authored Jan 18, 2024
1 parent 06c13a9 commit a0178e9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
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> => {
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 @@ -333,6 +333,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

0 comments on commit a0178e9

Please sign in to comment.