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

fix: 13555 fix latest commit link #13672

Merged
merged 12 commits into from
Oct 18, 2024
3 changes: 3 additions & 0 deletions frontend/app-development/enums/GiteaRoutePaths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum GiteaRoutePaths {
LatestCommit = 'latest-commit',
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ describe('ReleaseContainer', () => {
name: textMock('app_release.release_title_link'),
});
await user.click(latestCommitLink);
expect(mockGetBranchStatus).toHaveBeenCalledTimes(2);
expect(mockGetRepoStatus).toHaveBeenCalledTimes(1);
expect(latestCommitLink).toBeInTheDocument();
});

it('renders status that latest commit fetched from master is the same as commit for latest release', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ import { QueryKey } from 'app-shared/types/QueryKey';
import { useRepoStatusQuery } from 'app-shared/hooks/queries';
import { useStudioEnvironmentParams } from 'app-shared/hooks/useStudioEnvironmentParams';
import { Link } from '@digdir/designsystemet-react';
import { PackagesRouter } from 'app-shared/navigation/PackagesRouter';

export function ReleaseContainer() {
const { org, app } = useStudioEnvironmentParams();
const [popoverOpenClick, setPopoverOpenClick] = useState<boolean>(false);
const [popoverOpenHover, setPopoverOpenHover] = useState<boolean>(false);
const packagesRouter = new PackagesRouter({ app, org });

const { data: releases = [] } = useAppReleasesQuery(org, app);
const { data: repoStatus, isPending: isRepoStatusPending } = useRepoStatusQuery(org, app);
const {
data: masterBranchStatus,
isPending: masterBranchStatusIsPending,
refetch: getMasterBranchStatus,
} = useBranchStatusQuery(org, app, 'master');
const { data: masterBranchStatus, isPending: masterBranchStatusIsPending } = useBranchStatusQuery(
org,
app,
'master',
);

const latestRelease: AppReleaseType = releases && releases[0] ? releases[0] : null;

Expand Down Expand Up @@ -146,17 +148,6 @@ export function ReleaseContainer() {
}

function renderCreateReleaseTitle() {
const handleLinkClick = async (event) => {
event.preventDefault(); // Prevent default link behavior
const url = await getLatestCommitOnMaster();
window.open(url, '#', 'noopener,noreferrer');
};

const getLatestCommitOnMaster = async () => {
const { data: newMasterBranchStatus } = await getMasterBranchStatus();
return gitCommitPath(org, app, newMasterBranchStatus.commit.id);
};

if (!masterBranchStatus || !repoStatus?.contentStatus) {
return null;
}
Expand All @@ -169,7 +160,11 @@ export function ReleaseContainer() {
return (
<>
{t('app_release.release_title')}
<a href='#' onClick={handleLinkClick}>
<a
href={packagesRouter.getPackageNavigationUrl('latestCommit')}
target='_blank'
rel='noopener noreferrer'
>
{t('app_release.release_title_link')}
</a>
</>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React from 'react';
import { screen } from '@testing-library/react';
import { textMock } from '@studio/testing/mocks/i18nMock';
import { renderWithProviders } from '../../test/mocks';
import { createQueryClientMock } from 'app-shared/mocks/queryClientMock';
import { QueryKey } from 'app-shared/types/QueryKey';
import { app, org } from '@studio/testing/testids';
import { branchStatus } from 'app-shared/mocks/mocks';
import type { BranchStatus } from 'app-shared/types/BranchStatus';
import { NavigateToLatestCommitInGitea } from './NavigateToLatestCommitInGitea';

describe('NavigateToLatestCommitInGitea', () => {
afterEach(() => jest.clearAllMocks);

it('sets window location when the latest commit is received', async () => {
const commitId = 'some-commit-id';
delete window.location;
window.location = { ...window.location, assign: jest.fn() };
renderLatestCommit({ ...branchStatus, commit: { ...branchStatus.commit, id: commitId } });
expect(window.location.href).toBe(`/repos/${org}/${app}/commit/${commitId}`);
});

it('renders a spinner if master branch status is pending', () => {
renderLatestCommit();
expect(screen.getByText(textMock('general.loading'))).toBeInTheDocument();
});
});

const renderLatestCommit = (branchStatusMock?: BranchStatus) => {
const queryClientMock = createQueryClientMock();
if (branchStatusMock) {
queryClientMock.setQueryData([QueryKey.BranchStatus, org, app, 'master'], branchStatusMock);
}
renderWithProviders({}, queryClientMock)(<NavigateToLatestCommitInGitea />);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React, { useEffect } from 'react';
import { StudioPageSpinner } from '@studio/components';
import { gitCommitPath } from 'app-shared/api/paths';
import { useStudioEnvironmentParams } from 'app-shared/hooks/useStudioEnvironmentParams';
import { useBranchStatusQuery } from 'app-development/hooks/queries';
import { useTranslation } from 'react-i18next';

export const NavigateToLatestCommitInGitea = (): React.ReactElement => {
const { t } = useTranslation();
const { org, app } = useStudioEnvironmentParams();
const { data: masterBranchStatus } = useBranchStatusQuery(org, app, 'master');
const latestCommitId = masterBranchStatus?.commit?.id;

useEffect(() => {
if (latestCommitId) {
window.location.href = gitCommitPath(org, app, latestCommitId);
}
}, [app, latestCommitId, org]);
return <StudioPageSpinner spinnerTitle={t('general.loading')} />;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { NavigateToLatestCommitInGitea } from './NavigateToLatestCommitInGitea';
10 changes: 10 additions & 0 deletions frontend/app-development/router/PageRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
NotFoundRouteErrorBoundary,
RouteErrorBoundary,
} from './PageRouterErrorBoundry';
import { GiteaRoutePaths } from '../enums/GiteaRoutePaths';
import { NavigateToLatestCommitInGitea } from '../features/navigateToLatestCommitInGitea';

const BASE_PATH = '/:org/:app';

Expand All @@ -40,6 +42,14 @@ const router = createBrowserRouter(
))}
<Route path='*' element={<NotFoundPage />} errorElement={<NotFoundRouteErrorBoundary />} />
</Route>
<Route path={BASE_PATH}>
{/* Additional BasePath route to avoid using PageLayout around NavigateToLatestCommitInGitea */}
<Route
path={GiteaRoutePaths.LatestCommit}
element={<NavigateToLatestCommitInGitea />}
errorElement={<RouteErrorBoundary />}
/>
</Route>
<Route path='*' element={<NotFoundPage />} errorElement={<NotFoundRouteErrorBoundary />} />
</Route>,
),
Expand Down
2 changes: 0 additions & 2 deletions frontend/app-development/test/mocks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import { queriesMock } from 'app-shared/mocks/queriesMock';
import type { QueryClient } from '@tanstack/react-query';
import { queryClientConfigMock } from 'app-shared/mocks/queryClientMock';

export const textLanguagesMock = ['nb', 'nn', 'en'];

export const renderWithProviders =
(queries: Partial<ServicesContextProps> = {}, queryClient?: QueryClient) =>
(component: ReactNode) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ type PackagesRoute =
| 'editorOverview'
| 'editorUiEditor'
| 'preview'
| 'editorPublish';
| 'editorPublish'
| 'latestCommit';

const packagesRoutes: Record<PackagesRoute, string> = {
dashboard: '/dashboard',
dataModel: '/editor/{{org}}/{{app}}/data-model',
editorOverview: '/editor/{{org}}/{{app}}/overview',
editorUiEditor: '/editor/{{org}}/{{app}}/ui-editor',
editorPublish: '/editor/{{org}}/{{app}}/deploy',
latestCommit: '/editor/{{org}}/{{app}}/latest-commit',
preview: '/preview/{{org}}/{{app}}',
};

Expand Down