Add UrlReader support for adr plugin
The adr-backend plugin has been expanded with api endpoints that use url readers to read adr docs. This allows the adr frontend plugin to work with sites other than github, such as Azure DevOps. Created the concept of an AdrFileFetcher, which is used for retrieving adr file listings and content. EntityAdrContent and AdrReader have an optional prop to specify an override of the AdrFileFetcher to be used. By default, it uses the octokit fetcher for the octokit service. Signed-off-by: Robert Bunning <rbunning@webstaurantstore.com>
This commit is contained in:
@@ -26,9 +26,12 @@ import { scmIntegrationsApiRef } from '@backstage/integration-react';
|
||||
import { getAdrLocationUrl } from '@backstage/plugin-adr-common';
|
||||
import { useEntity } from '@backstage/plugin-catalog-react';
|
||||
|
||||
import { useOctokitRequest } from '../../hooks';
|
||||
import { adrDecoratorFactories } from './decorators';
|
||||
import { AdrContentDecorator } from './types';
|
||||
import {
|
||||
AdrFileFetcher,
|
||||
octokitAdrFileFetcher,
|
||||
} from '../../hooks/adrFileFetcher';
|
||||
|
||||
/**
|
||||
* Component to fetch and render an ADR.
|
||||
@@ -38,13 +41,15 @@ import { AdrContentDecorator } from './types';
|
||||
export const AdrReader = (props: {
|
||||
adr: string;
|
||||
decorators?: AdrContentDecorator[];
|
||||
adrFileFetcher?: AdrFileFetcher;
|
||||
}) => {
|
||||
const { adr, decorators } = props;
|
||||
const { adr, decorators, adrFileFetcher } = props;
|
||||
const { entity } = useEntity();
|
||||
const scmIntegrations = useApi(scmIntegrationsApiRef);
|
||||
const adrLocationUrl = getAdrLocationUrl(entity, scmIntegrations);
|
||||
|
||||
const { value, loading, error } = useOctokitRequest(
|
||||
const targetAdrFileFetcher = adrFileFetcher ?? octokitAdrFileFetcher;
|
||||
const { value, loading, error } = targetAdrFileFetcher.useReadAdrFileAtUrl(
|
||||
`${adrLocationUrl.replace(/\/$/, '')}/${adr}`,
|
||||
);
|
||||
|
||||
|
||||
@@ -44,9 +44,12 @@ import {
|
||||
Typography,
|
||||
} from '@material-ui/core';
|
||||
|
||||
import { useOctokitRequest } from '../../hooks';
|
||||
import { rootRouteRef } from '../../routes';
|
||||
import { AdrContentDecorator, AdrReader } from '../AdrReader';
|
||||
import {
|
||||
AdrFileFetcher,
|
||||
octokitAdrFileFetcher,
|
||||
} from '../../hooks/adrFileFetcher';
|
||||
|
||||
const useStyles = makeStyles((theme: Theme) => ({
|
||||
adrMenu: {
|
||||
@@ -61,8 +64,9 @@ const useStyles = makeStyles((theme: Theme) => ({
|
||||
export const EntityAdrContent = (props: {
|
||||
contentDecorators?: AdrContentDecorator[];
|
||||
filePathFilterFn?: AdrFilePathFilterFn;
|
||||
adrFileFetcher?: AdrFileFetcher;
|
||||
}) => {
|
||||
const { contentDecorators, filePathFilterFn } = props;
|
||||
const { contentDecorators, filePathFilterFn, adrFileFetcher } = props;
|
||||
const classes = useStyles();
|
||||
const { entity } = useEntity();
|
||||
const rootLink = useRouteRef(rootRouteRef);
|
||||
@@ -71,7 +75,8 @@ export const EntityAdrContent = (props: {
|
||||
const scmIntegrations = useApi(scmIntegrationsApiRef);
|
||||
const entityHasAdrs = isAdrAvailable(entity);
|
||||
|
||||
const { value, loading, error } = useOctokitRequest(
|
||||
const targetAdrFileFetcher = adrFileFetcher ?? octokitAdrFileFetcher;
|
||||
const { value, loading, error } = targetAdrFileFetcher.useGetAdrFilesAtUrl(
|
||||
getAdrLocationUrl(entity, scmIntegrations),
|
||||
);
|
||||
|
||||
@@ -142,7 +147,11 @@ export const EntityAdrContent = (props: {
|
||||
</List>
|
||||
</Grid>
|
||||
<Grid item xs={9}>
|
||||
<AdrReader adr={selectedAdr} decorators={contentDecorators} />
|
||||
<AdrReader
|
||||
adr={selectedAdr}
|
||||
adrFileFetcher={targetAdrFileFetcher}
|
||||
decorators={contentDecorators}
|
||||
/>
|
||||
</Grid>
|
||||
</Grid>
|
||||
) : (
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* 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 { discoveryApiRef, useApi } from '@backstage/core-plugin-api';
|
||||
import { DiscoveryApi } from '@backstage/plugin-permission-common';
|
||||
import useAsync from 'react-use/lib/useAsync';
|
||||
import { useOctokitRequest } from './useOctokitRequest';
|
||||
|
||||
const useAdrApi = (
|
||||
endpoint: string,
|
||||
fileUrl: string,
|
||||
discoveryApi: DiscoveryApi,
|
||||
) => {
|
||||
return async () => {
|
||||
const baseUrl = await discoveryApi.getBaseUrl('adr');
|
||||
const targetUrl = `${baseUrl}/${endpoint}?url=${encodeURIComponent(
|
||||
fileUrl,
|
||||
)}`;
|
||||
|
||||
const result = await fetch(targetUrl);
|
||||
const data = await result.json();
|
||||
|
||||
if (!result.ok) {
|
||||
throw data;
|
||||
}
|
||||
return data;
|
||||
};
|
||||
};
|
||||
|
||||
export interface AdrFileFetcher {
|
||||
useGetAdrFilesAtUrl: (url: string) => any;
|
||||
useReadAdrFileAtUrl: (url: string) => any;
|
||||
}
|
||||
|
||||
const getAdrFilesEndpoint = 'getAdrFilesAtUrl';
|
||||
const readAdrFileEndpoint = 'readAdrFileAtUrl';
|
||||
|
||||
export const urlReaderAdrFileFetcher: AdrFileFetcher = {
|
||||
useGetAdrFilesAtUrl: function (url: string) {
|
||||
const discoveryApi = useApi(discoveryApiRef);
|
||||
return useAsync<any>(useAdrApi(getAdrFilesEndpoint, url, discoveryApi), [
|
||||
url,
|
||||
]);
|
||||
},
|
||||
useReadAdrFileAtUrl: function (url: string) {
|
||||
const discoveryApi = useApi(discoveryApiRef);
|
||||
return useAsync<any>(useAdrApi(readAdrFileEndpoint, url, discoveryApi), [
|
||||
url,
|
||||
]);
|
||||
},
|
||||
};
|
||||
|
||||
export const octokitAdrFileFetcher: AdrFileFetcher = {
|
||||
useGetAdrFilesAtUrl: (url: string) => useOctokitRequest(url),
|
||||
useReadAdrFileAtUrl: (url: string) => useOctokitRequest(url),
|
||||
};
|
||||
Reference in New Issue
Block a user