diff --git a/plugins/azure-devops-backend/src/api/AzureDevOpsApi.ts b/plugins/azure-devops-backend/src/api/AzureDevOpsApi.ts index 453f28809a..f737eac28b 100644 --- a/plugins/azure-devops-backend/src/api/AzureDevOpsApi.ts +++ b/plugins/azure-devops-backend/src/api/AzureDevOpsApi.ts @@ -402,17 +402,29 @@ export class AzureDevOpsApi { organization: string, projectName: string, repoName: string, - ): Promise { - const getContentFile = async ( + ): Promise<{ + url: string; + content: string; + }> { + const getFileContent = async ( path: string, encoding?: BufferEncoding, - ): Promise => { + ): Promise<{ + url: string; + content: string; + }> => { const url = `https://${host}/${organization}/${projectName}/_git/${repoName}?path=${path}`; const response = await this.urlReader.read(url); - return Buffer.from(response).toString(encoding); + return { + url, + content: Buffer.from(response).toString(encoding), + }; + }; + const { url, content } = await getFileContent('README.md'); + return { + url, + content: await replaceReadme(content, getFileContent), }; - const readmeContent = await getContentFile('README.md'); - return await replaceReadme(readmeContent, getContentFile); } } diff --git a/plugins/azure-devops-backend/src/service/router.ts b/plugins/azure-devops-backend/src/service/router.ts index 36bacba8be..8ea9b20e2c 100644 --- a/plugins/azure-devops-backend/src/service/router.ts +++ b/plugins/azure-devops-backend/src/service/router.ts @@ -194,16 +194,13 @@ export async function createRouter( router.get('/readme/:projectName/:repoName', async (req, res) => { const { projectName, repoName } = req.params; - const content = await azureDevOpsApi.getReadme( + const readme = await azureDevOpsApi.getReadme( host, organization, projectName, repoName, ); - res.status(200).json({ - content, - url: `https://${host}/${organization}/${projectName}/_git/${repoName}?path=README.md`, - }); + res.status(200).json(readme); }); router.use(errorHandler()); diff --git a/plugins/azure-devops-backend/src/utils/azure-devops-utils.ts b/plugins/azure-devops-backend/src/utils/azure-devops-utils.ts index ce23fe8ef1..bf82ed265b 100644 --- a/plugins/azure-devops-backend/src/utils/azure-devops-utils.ts +++ b/plugins/azure-devops-backend/src/utils/azure-devops-utils.ts @@ -206,26 +206,27 @@ export function convertPolicy( } export async function replaceReadme( - content: string, - getContentFile: (path: string, encoding?: BufferEncoding) => Promise, + readme: string, + getFileContent: ( + path: string, + encoding?: BufferEncoding, + ) => Promise<{ + url: string; + content: string; + }>, ) { - const regExp = - /\[([^\[\]]*)\]\((?!https?:\/\/)(.*?)(\.png|\.jpg|\.jpeg|\.gif|\.webp)(.*)\)/gim; - const filesPath = content.match(regExp) || []; - - if (filesPath.length === 0) return content; - - let replacedContent = content; + const filesPath = extractAssets(readme); + let content = readme; for (const filePath of filesPath) { - const { label, path, ext } = getPartsFromFilePath(filePath); + const { label, path, ext } = extractPartsFromAsset(filePath); const mime = getMimeByExtension(ext); - const base64 = await getContentFile(path + ext, 'base64'); - replacedContent = replacedContent.replace( + const { content: base64 } = await getFileContent(path + ext, 'base64'); + content = content.replace( filePath, `[${label}](data:${mime};base64,${base64})`, ); } - return replacedContent; + return content; } function convertReviewer( @@ -287,7 +288,13 @@ function hasAutoComplete(pullRequest: GitPullRequest): boolean { return pullRequest.isDraft !== true && !!pullRequest.completionOptions; } -function getPartsFromFilePath(content: string): { +function extractAssets(content: string) { + const regExp = + /\[([^\[\]]*)\]\((?!https?:\/\/)(.*?)(\.png|\.jpg|\.jpeg|\.gif|\.webp)(.*)\)/gim; + return content.match(regExp) || []; +} + +function extractPartsFromAsset(content: string): { label: string; path: string; ext: string; diff --git a/plugins/azure-devops/src/routes.ts b/plugins/azure-devops/src/routes.ts index 418cd89a53..5280457bae 100644 --- a/plugins/azure-devops/src/routes.ts +++ b/plugins/azure-devops/src/routes.ts @@ -33,5 +33,5 @@ export const azurePullRequestsEntityContentRouteRef = createRouteRef({ }); export const azureReadMeEntityContentRouteRef = createRouteRef({ - id: 'azure-read-me-entity-content', + id: 'azure-readme-entity-content', });