TechDocs: browse metadata (#2682)
* feat(techdocs): TechDocsPageWrapper to take in labels prop and use TechDocsHeader component * feat(techdocs): new TechDocsHeader component as wrapper around Header from core * feat(techdocs): TechDocsPage metadata * feat(techdocs): TechDocsMetadata class responsible for reading metadata from generated techdocs metadata json file * fix(techdocs): delete TechDocsPageWrapper component * fix(app-config): delete /docs from request url * feat(techdocs): TechDocsHeader with labels to browse metadata * fix(techdocs): move metadata to backend plugin * feat(techdocs-backend): introduce two new metadata routes * feat(techdocs): new techdocs api to be responsible to request metadata from backend * feat(techdocs): register new api for plugin * fix(techdocs): reader to take in onReady prop * fix(techdocs): TechDocsHome component to use its own header * fix(techdocs): TechDocsPage responsible to get metadata and pass it down to the TechDocsHeader * fix(techdocs): component tests for both TechDocsHeader and TechDocsPage * fix(techdocs): adjust api to use /docs in url since it was taken away in requestURL * fix(tests): move assertion into act * fix(techdocs): rename TechDocsHeader to TechDocsPageHeader * fix(techdocs): rename TechDocsHeader to TechDocsPageHeader * fix(techdocs-backend): add some logs * Update plugins/techdocs/src/reader/components/TechDocsPage.test.tsx * delete unused import
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import fetch from 'node-fetch';
|
||||
/*
|
||||
* Copyright 2020 Spotify AB
|
||||
*
|
||||
* 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 class TechDocsMetadata {
|
||||
private async getMetadataFile(docsUrl: String) {
|
||||
const metadataURL = `${docsUrl}/techdocs_metadata.json`;
|
||||
|
||||
try {
|
||||
const req = await fetch(metadataURL);
|
||||
|
||||
return await req.json();
|
||||
} catch (error) {
|
||||
throw new Error(error);
|
||||
}
|
||||
}
|
||||
|
||||
public async getMkDocsMetaData(docsUrl: any) {
|
||||
const mkDocsMetadata = await this.getMetadataFile(docsUrl);
|
||||
|
||||
if (!mkDocsMetadata) return null;
|
||||
|
||||
return {
|
||||
...mkDocsMetadata,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,7 @@ import {
|
||||
} from '@backstage/backend-common';
|
||||
import { Entity } from '@backstage/catalog-model';
|
||||
import { DocsBuilder } from './helpers';
|
||||
import { getLocationForEntity } from '../helpers';
|
||||
|
||||
type RouterOptions = {
|
||||
preparers: PreparerBuilder;
|
||||
@@ -60,6 +61,46 @@ export async function createRouter({
|
||||
}: RouterOptions): Promise<express.Router> {
|
||||
const router = Router();
|
||||
|
||||
router.get('/metadata/mkdocs/*', async (req, res) => {
|
||||
const storageUrl = config.getString('techdocs.storageUrl');
|
||||
const { '0': path } = req.params;
|
||||
|
||||
const metadataURL = `${storageUrl}/${path}/techdocs_metadata.json`;
|
||||
|
||||
try {
|
||||
const mkDocsMetadata = await (await fetch(metadataURL)).json();
|
||||
res.send(mkDocsMetadata);
|
||||
} catch (err) {
|
||||
logger.info(
|
||||
`[TechDocs] Unable to get metadata for ${path} with error ${err}`,
|
||||
);
|
||||
throw new Error(`Unable to get metadata for ${path} with error ${err}`);
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/metadata/entity/:kind/:namespace/:name', async (req, res) => {
|
||||
const baseUrl = config.getString('backend.baseUrl');
|
||||
const { kind, namespace, name } = req.params;
|
||||
|
||||
try {
|
||||
const entity = (await (
|
||||
await fetch(
|
||||
`${baseUrl}/api/catalog/entities/by-name/${kind}/${namespace}/${name}`,
|
||||
)
|
||||
).json()) as Entity;
|
||||
|
||||
const locationMetadata = getLocationForEntity(entity);
|
||||
res.send({ ...entity, locationMetadata });
|
||||
} catch (err) {
|
||||
logger.info(
|
||||
`[TechDocs] Unable to get metadata for ${kind}/${namespace}/${name} with error ${err}`,
|
||||
);
|
||||
throw new Error(
|
||||
`Unable to get metadata for ${kind}/${namespace}/${name} with error ${err}`,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/docs/:kind/:namespace/:name/*', async (req, res) => {
|
||||
const storageUrl = config.getString('techdocs.storageUrl');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user