fix(plugin-catalog): Fix viewTechdocLink so kind is lowercase by default in url path

Signed-off-by: Jake Crews <jake.crews@daveramsey.com>
This commit is contained in:
Jake Crews
2022-07-14 07:34:41 -05:00
parent 42cd453424
commit 29d6cf0147
10 changed files with 149 additions and 9 deletions
+1
View File
@@ -44,6 +44,7 @@
"@backstage/plugin-catalog-react": "^1.1.2-next.3",
"@backstage/plugin-search-common": "^0.3.6-next.0",
"@backstage/plugin-search-react": "^0.2.2-next.3",
"@backstage/plugin-techdocs-react": "1.0.1-next.1",
"@backstage/theme": "^0.2.16-next.1",
"@backstage/types": "^1.0.0",
"@material-ui/core": "^4.12.2",
@@ -26,6 +26,7 @@ import {
CatalogApi,
entityRouteRef,
} from '@backstage/plugin-catalog-react';
import { ConfigApi, configApiRef } from '@backstage/core-plugin-api';
import { renderInTestApp, TestApiProvider } from '@backstage/test-utils';
import userEvent from '@testing-library/user-event';
import React from 'react';
@@ -393,6 +394,63 @@ describe('<AboutCard />', () => {
},
);
expect(getByText('View TechDocs').closest('a')).toHaveAttribute(
'href',
'/docs/default/component/software',
);
});
it('renders techdocs link if techdocs.legacyUseCaseSensitiveTripletPaths is true', async () => {
const configApi: ConfigApi = new ConfigReader({
integrations: {
github: [
{
host: 'github.com',
token: '...',
},
],
},
techdocs: {
legacyUseCaseSensitiveTripletPaths: true,
},
});
const entity = {
apiVersion: 'v1',
kind: 'Component',
metadata: {
name: 'software',
annotations: {
'backstage.io/techdocs-ref': './',
},
},
spec: {
owner: 'guest',
type: 'service',
lifecycle: 'production',
},
};
const { getByText } = await renderInTestApp(
<TestApiProvider
apis={[
[scmIntegrationsApiRef, ScmIntegrationsApi.fromConfig(configApi)],
[configApiRef, configApi],
[catalogApiRef, catalogApi],
]}
>
<EntityProvider entity={entity}>
<AboutCard />
</EntityProvider>
</TestApiProvider>,
{
mountedRoutes: {
'/docs/:namespace/:kind/:name': viewTechDocRouteRef,
'/catalog/:namespace/:kind/:name': entityRouteRef,
},
},
);
expect(getByText('View TechDocs').closest('a')).toHaveAttribute(
'href',
'/docs/default/Component/software',
@@ -18,6 +18,7 @@ import {
ANNOTATION_EDIT_URL,
ANNOTATION_LOCATION,
DEFAULT_NAMESPACE,
getCompoundEntityRef,
stringifyEntityRef,
} from '@backstage/catalog-model';
import {
@@ -26,7 +27,12 @@ import {
InfoCardVariants,
Link,
} from '@backstage/core-components';
import { alertApiRef, useApi, useRouteRef } from '@backstage/core-plugin-api';
import {
alertApiRef,
configApiRef,
useApi,
useRouteRef,
} from '@backstage/core-plugin-api';
import {
ScmIntegrationIcon,
scmIntegrationsApiRef,
@@ -50,6 +56,7 @@ import EditIcon from '@material-ui/icons/Edit';
import React, { useCallback } from 'react';
import { viewTechDocRouteRef } from '../../routes';
import { AboutContent } from './AboutContent';
import { toLowercaseEntityRefMaybe } from '@backstage/plugin-techdocs-react';
const useStyles = makeStyles({
gridItemCard: {
@@ -91,6 +98,8 @@ export function AboutCard(props: AboutCardProps) {
const catalogApi = useApi(catalogApiRef);
const alertApi = useApi(alertApiRef);
const viewTechdocLink = useRouteRef(viewTechDocRouteRef);
const config = useApi(configApiRef);
const entityRef = getCompoundEntityRef(entity);
const entitySourceLocation = getEntitySourceLocation(
entity,
@@ -113,11 +122,7 @@ export function AboutCard(props: AboutCardProps) {
icon: <DocsIcon />,
href:
viewTechdocLink &&
viewTechdocLink({
namespace: entity.metadata.namespace || DEFAULT_NAMESPACE,
kind: entity.kind,
name: entity.metadata.name,
}),
viewTechdocLink(toLowercaseEntityRefMaybe(entityRef, config)),
};
let cardClass = '';
+7
View File
@@ -7,6 +7,7 @@ import { ApiRef } from '@backstage/core-plugin-api';
import { AsyncState } from 'react-use/lib/useAsync';
import { ComponentType } from 'react';
import { CompoundEntityRef } from '@backstage/catalog-model';
import { Config } from '@backstage/config';
import { Dispatch } from 'react';
import { Entity } from '@backstage/catalog-model';
import { Extension } from '@backstage/core-plugin-api';
@@ -152,6 +153,12 @@ export interface TechDocsStorageApi {
// @public
export const techdocsStorageApiRef: ApiRef<TechDocsStorageApi>;
// @public
export function toLowercaseEntityRefMaybe(
entityRef: CompoundEntityRef,
config: Config,
): CompoundEntityRef;
// @public
export const useShadowDomStylesLoading: (element: Element | null) => boolean;
+1
View File
@@ -38,6 +38,7 @@
"@backstage/catalog-model": "^1.1.0-next.3",
"@backstage/core-components": "^0.10.0-next.3",
"@backstage/core-plugin-api": "^1.0.4-next.0",
"@backstage/config": "^1.0.1",
"@backstage/version-bridge": "^1.0.1",
"@material-ui/core": "^4.12.2",
"@material-ui/lab": "4.0.0-alpha.57",
+38
View File
@@ -0,0 +1,38 @@
/*
* Copyright 2022 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Config } from '@backstage/config';
import { CompoundEntityRef } from '@backstage/catalog-model';
/**
* Lower-case entity triplets by default, but allow override.
*
* @public
*/
export function toLowercaseEntityRefMaybe(
entityRef: CompoundEntityRef,
config: Config,
) {
return config.getOptionalBoolean(
'techdocs.legacyUseCaseSensitiveTripletPaths',
)
? entityRef
: {
kind: entityRef.kind.toLocaleLowerCase('en-US'),
name: entityRef.name.toLocaleLowerCase('en-US'),
namespace: entityRef.namespace.toLocaleLowerCase('en-US'),
};
}
+1
View File
@@ -51,3 +51,4 @@ export {
useShadowRootElements,
useShadowRootSelection,
} from './hooks';
export { toLowercaseEntityRefMaybe } from './helpers';