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:
Robert Bunning
2022-12-19 08:20:20 -05:00
parent 28c1b3aec5
commit fc1d240190
12 changed files with 209 additions and 11 deletions
+1
View File
@@ -32,6 +32,7 @@
"@backstage/catalog-model": "workspace:^",
"@backstage/config": "workspace:^",
"@backstage/integration": "workspace:^",
"@backstage/plugin-adr-backend": "workspace:^",
"@backstage/plugin-app-backend": "workspace:^",
"@backstage/plugin-auth-backend": "workspace:^",
"@backstage/plugin-auth-node": "workspace:^",
+4 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright 2020 The Backstage Authors
* 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.
@@ -61,6 +61,7 @@ import badges from './plugins/badges';
import jenkins from './plugins/jenkins';
import permission from './plugins/permission';
import playlist from './plugins/playlist';
import adr from './plugins/adr';
import { PluginEnvironment } from './types';
import { ServerPermissionClient } from '@backstage/plugin-permission-node';
import { DefaultIdentityClient } from '@backstage/plugin-auth-node';
@@ -139,6 +140,7 @@ async function main() {
const appEnv = useHotMemoize(module, () => createEnv('app'));
const badgesEnv = useHotMemoize(module, () => createEnv('badges'));
const jenkinsEnv = useHotMemoize(module, () => createEnv('jenkins'));
const adrEnv = useHotMemoize(module, () => createEnv('adr'));
const techInsightsEnv = useHotMemoize(module, () =>
createEnv('tech-insights'),
);
@@ -175,6 +177,7 @@ async function main() {
apiRouter.use('/permission', await permission(permissionEnv));
apiRouter.use('/playlist', await playlist(playlistEnv));
apiRouter.use('/explore', await explore(exploreEnv));
apiRouter.use('/adr', await adr(adrEnv));
apiRouter.use(notFoundHandler());
const service = createServiceBuilder(module)
+25
View File
@@ -0,0 +1,25 @@
/*
* 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 { createRouter } from '@backstage/plugin-adr-backend';
import { Router } from 'express';
import { PluginEnvironment } from '../types';
export default async function createPlugin(
env: PluginEnvironment,
): Promise<Router> {
return await createRouter(env.reader);
}
+2
View File
@@ -36,6 +36,8 @@
"@backstage/integration": "workspace:^",
"@backstage/plugin-adr-common": "workspace:^",
"@backstage/plugin-search-common": "workspace:^",
"express": "^4.18.2",
"express-promise-router": "^4.1.1",
"luxon": "^3.0.0",
"marked": "^4.0.14",
"node-fetch": "^2.6.5",
+1
View File
@@ -21,3 +21,4 @@
*/
export * from './search';
export * from './service';
+17
View File
@@ -0,0 +1,17 @@
/*
* 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.
*/
export { createRouter } from './router';
+61
View File
@@ -0,0 +1,61 @@
/*
* 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 { UrlReader } from '@backstage/backend-common';
import express from 'express';
import Router from 'express-promise-router';
export async function createRouter(reader: UrlReader): Promise<express.Router> {
const router = Router();
router.use(express.json());
router.get('/getAdrFilesAtUrl', async (req, res) => {
const urlToProcess = req.query.url as string;
if (!urlToProcess) {
res.statusCode = 400;
res.json({ message: 'No URL provided' });
return;
}
const treeGetResponse = await reader.readTree(urlToProcess);
const files = await treeGetResponse.files();
const fileData = files.map(file => {
return {
type: 'file',
name: file.path.substring(file.path.lastIndexOf('/') + 1),
path: file.path,
};
});
res.json({ data: fileData });
});
router.get('/readAdrFileAtUrl', async (req, res) => {
const urlToProcess = req.query.url as string;
if (!urlToProcess) {
res.statusCode = 400;
res.json({ message: 'No URL provided' });
return;
}
const fileGetResponse = await reader.readUrl(urlToProcess);
const fileBuffer = await fileGetResponse.buffer();
res.json({ data: fileBuffer.toString() });
});
return router;
}
+1
View File
@@ -28,6 +28,7 @@
"@backstage/integration-react": "workspace:^",
"@backstage/plugin-adr-common": "workspace:^",
"@backstage/plugin-catalog-react": "workspace:^",
"@backstage/plugin-permission-common": "workspace:^",
"@backstage/plugin-search-common": "workspace:^",
"@backstage/plugin-search-react": "workspace:^",
"@backstage/theme": "workspace:^",
@@ -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>
) : (
+69
View File
@@ -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),
};
+7 -3
View File
@@ -4247,7 +4247,7 @@ __metadata:
languageName: unknown
linkType: soft
"@backstage/plugin-adr-backend@workspace:plugins/adr-backend":
"@backstage/plugin-adr-backend@workspace:^, @backstage/plugin-adr-backend@workspace:plugins/adr-backend":
version: 0.0.0-use.local
resolution: "@backstage/plugin-adr-backend@workspace:plugins/adr-backend"
dependencies:
@@ -4262,6 +4262,8 @@ __metadata:
"@backstage/plugin-search-common": "workspace:^"
"@types/marked": ^4.0.0
"@types/supertest": ^2.0.8
express: ^4.18.2
express-promise-router: ^4.1.1
luxon: ^3.0.0
marked: ^4.0.14
msw: ^0.49.0
@@ -4296,6 +4298,7 @@ __metadata:
"@backstage/integration-react": "workspace:^"
"@backstage/plugin-adr-common": "workspace:^"
"@backstage/plugin-catalog-react": "workspace:^"
"@backstage/plugin-permission-common": "workspace:^"
"@backstage/plugin-search-common": "workspace:^"
"@backstage/plugin-search-react": "workspace:^"
"@backstage/test-utils": "workspace:^"
@@ -22303,6 +22306,7 @@ __metadata:
"@backstage/cli": "workspace:^"
"@backstage/config": "workspace:^"
"@backstage/integration": "workspace:^"
"@backstage/plugin-adr-backend": "workspace:^"
"@backstage/plugin-app-backend": "workspace:^"
"@backstage/plugin-auth-backend": "workspace:^"
"@backstage/plugin-auth-node": "workspace:^"
@@ -22501,7 +22505,7 @@ __metadata:
languageName: node
linkType: hard
"express-promise-router@npm:^4.1.0":
"express-promise-router@npm:^4.1.0, express-promise-router@npm:^4.1.1":
version: 4.1.1
resolution: "express-promise-router@npm:4.1.1"
dependencies:
@@ -22543,7 +22547,7 @@ __metadata:
languageName: node
linkType: hard
"express@npm:^4.17.1, express@npm:^4.17.3, express@npm:^4.18.1":
"express@npm:^4.17.1, express@npm:^4.17.3, express@npm:^4.18.1, express@npm:^4.18.2":
version: 4.18.2
resolution: "express@npm:4.18.2"
dependencies: