Ability to fetch the README file from a different AZD path, using an annotation on the entity

Signed-off-by: David Roberts <David.Roberts@orbis.com>
This commit is contained in:
David Roberts
2024-02-07 11:43:08 +00:00
parent dd44104e9f
commit 308b0eef3c
7 changed files with 26 additions and 3 deletions
@@ -527,11 +527,12 @@ export class AzureDevOpsApi {
org: string,
project: string,
repo: string,
path: string,
): Promise<{
url: string;
content: string;
}> {
const url = buildEncodedUrl(host, org, project, repo, 'README.md');
const url = buildEncodedUrl(host, org, project, repo, path);
const response = await this.urlReader.readUrl(url);
const buffer = await response.buffer();
const content = await replaceReadme(
@@ -216,12 +216,14 @@ export async function createRouter(
req.query.host?.toString() ?? config.getString('azureDevOps.host');
const org =
req.query.org?.toString() ?? config.getString('azureDevOps.organization');
const path = req.query.path?.toString() ?? 'README.md';
const { projectName, repoName } = req.params;
const readme = await azureDevOpsApi.getReadme(
host,
org,
projectName,
repoName,
path,
);
res.status(200).json(readme);
});
@@ -22,6 +22,8 @@ export const AZURE_DEVOPS_HOST_ORG_ANNOTATION = 'dev.azure.com/host-org';
/** @public */
export const AZURE_DEVOPS_PROJECT_ANNOTATION = 'dev.azure.com/project';
/** @public */
export const AZURE_DEVOPS_README_ANNOTATION = 'dev.azure.com/readme-path';
/** @public */
export const AZURE_DEVOPS_REPO_ANNOTATION = 'dev.azure.com/project-repo';
/** @public */
export const AZURE_DEVOPS_DEFAULT_TOP: number = 10;
+1
View File
@@ -212,6 +212,7 @@ export interface ReadmeConfig {
repo: string;
host?: string;
org?: string;
path?: string;
}
/** @public */
@@ -189,6 +189,9 @@ export class AzureDevOpsClient implements AzureDevOpsApi {
if (opts.org) {
queryString.append('org', opts.org);
}
if (opts.path) {
queryString.append('path', opts.path);
}
return await this.get(
`readme/${encodeURIComponent(opts.project)}/${encodeURIComponent(
opts.repo,
+9 -2
View File
@@ -30,8 +30,15 @@ export function useReadme(entity: Entity): {
const api = useApi(azureDevOpsApiRef);
const { value, loading, error } = useAsync(() => {
const { project, repo, host, org } = getAnnotationValuesFromEntity(entity);
return api.getReadme({ project, repo: repo as string, host, org });
const { project, repo, host, org, readmePath } =
getAnnotationValuesFromEntity(entity);
return api.getReadme({
project,
repo: repo as string,
host,
org,
path: readmePath,
});
}, [api]);
return {
@@ -18,6 +18,7 @@ import { Entity } from '@backstage/catalog-model';
import {
AZURE_DEVOPS_PROJECT_ANNOTATION,
AZURE_DEVOPS_BUILD_DEFINITION_ANNOTATION,
AZURE_DEVOPS_README_ANNOTATION,
AZURE_DEVOPS_REPO_ANNOTATION,
AZURE_DEVOPS_HOST_ORG_ANNOTATION,
} from '@backstage/plugin-azure-devops-common';
@@ -28,6 +29,7 @@ export function getAnnotationValuesFromEntity(entity: Entity): {
definition?: string;
host?: string;
org?: string;
readmePath?: string;
} {
const hostOrg = getHostOrg(entity.metadata.annotations);
const projectRepo = getProjectRepo(entity.metadata.annotations);
@@ -35,12 +37,15 @@ export function getAnnotationValuesFromEntity(entity: Entity): {
entity.metadata.annotations?.[AZURE_DEVOPS_PROJECT_ANNOTATION];
const definition =
entity.metadata.annotations?.[AZURE_DEVOPS_BUILD_DEFINITION_ANNOTATION];
const readmePath =
entity.metadata.annotations?.[AZURE_DEVOPS_README_ANNOTATION];
if (definition) {
if (project) {
return {
project,
definition,
readmePath: readmePath,
...hostOrg,
};
}
@@ -49,6 +54,7 @@ export function getAnnotationValuesFromEntity(entity: Entity): {
project: projectRepo.project,
repo: projectRepo.repo,
definition,
readmePath: readmePath,
...hostOrg,
};
}
@@ -60,6 +66,7 @@ export function getAnnotationValuesFromEntity(entity: Entity): {
return {
project: projectRepo.project,
repo: projectRepo.repo,
readmePath: readmePath,
...hostOrg,
};
}