From 952c0d417a4c9633c0b40cb0c59a6f49247a6c48 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 31 May 2022 10:59:49 -0400 Subject: [PATCH] cope with removed files in content-changes-table-comment.js (#27979) * cope with removed files in content-changes-table-comment.js * handle content changes files async Co-authored-by: Hector Alfaro --- .../content-changes-table-comment.js | 161 ++++++++++-------- 1 file changed, 90 insertions(+), 71 deletions(-) diff --git a/.github/actions-scripts/content-changes-table-comment.js b/.github/actions-scripts/content-changes-table-comment.js index dc8df8a81e..1ec5aa3df4 100755 --- a/.github/actions-scripts/content-changes-table-comment.js +++ b/.github/actions-scripts/content-changes-table-comment.js @@ -39,78 +39,97 @@ const pathPrefix = 'content/' const articleFiles = files.filter( ({ filename }) => filename.startsWith(pathPrefix) && !filename.endsWith('/index.md') ) -for (const file of articleFiles) { - const sourceUrl = file.blob_url - const fileName = file.filename.slice(pathPrefix.length) - const fileUrl = fileName.slice(0, fileName.lastIndexOf('.')) - // get the file contents and decode them - // this script is called from the main branch, so we need the API call to get the contents from the branch, instead - const fileContents = await getContents( - context.repo.owner, - context.payload.repository.name, - context.payload.pull_request.head.sha, - file.filename - ) +const lines = await Promise.all( + articleFiles.map(async (file) => { + const sourceUrl = file.blob_url + const fileName = file.filename.slice(pathPrefix.length) + const fileUrl = fileName.slice(0, fileName.lastIndexOf('.')) - // parse the frontmatter - const { data } = parse(fileContents) - - let contentCell = '' - let previewCell = '' - let prodCell = '' - - if (file.status === 'added') contentCell = `New file: ` - contentCell += `[\`${fileName}\`](${sourceUrl})` - - try { - // the try/catch is needed because getApplicableVersions() returns either [] or throws an error when it can't parse the versions frontmatter - // try/catch can be removed if docs-engineering#1821 is resolved - // i.e. for feature based versioning, like ghae: 'issue-6337' - const fileVersions = getApplicableVersions(data.versions) - - for (const plan in allVersionShortnames) { - // plan is the shortName (i.e., fpt) - // allVersionShortNames[plan] is the planName (i.e., free-pro-team) - - // walk by the plan names since we generate links differently for most plans - const versions = fileVersions.filter((fileVersion) => - fileVersion.includes(allVersionShortnames[plan]) - ) - - if (versions.length === 1) { - // for fpt, ghec, and ghae - - if (versions.toString() === nonEnterpriseDefaultVersion) { - // omit version from fpt url - - previewCell += `[${plan}](${APP_URL}/${fileUrl})
` - prodCell += `[${plan}](${PROD_URL}/${fileUrl})
` - } else { - // for non-versioned releases (ghae, ghec) use full url - - previewCell += `[${plan}](${APP_URL}/${versions}/${fileUrl})
` - prodCell += `[${plan}](${PROD_URL}/${versions}/${fileUrl})
` - } - } else if (versions.length) { - // for ghes releases, link each version - - previewCell += `${plan}@ ` - prodCell += `${plan}@ ` - - versions.forEach((version) => { - previewCell += `[${version.split('@')[1]}](${APP_URL}/${version}/${fileUrl}) ` - prodCell += `[${version.split('@')[1]}](${PROD_URL}/${version}/${fileUrl}) ` - }) - previewCell += '
' - prodCell += '
' - } - } - } catch (e) { - console.error( - `Version information for ${file.filename} couldn't be determined from its frontmatter.` + // get the file contents and decode them + // this script is called from the main branch, so we need the API call to get the contents from the branch, instead + const fileContents = await getContents( + context.repo.owner, + context.payload.repository.name, + // Can't get its content if it no longer exists. + // Meaning, you'd get a 404 on the `getContents()` utility function. + // So, to be able to get necessary meta data about what it *was*, + // if it was removed, fall back to the 'base'. + file.status === 'removed' + ? context.payload.pull_request.base.sha + : context.payload.pull_request.head.sha, + file.filename ) - } - markdownTable += `| ${contentCell} | ${previewCell} | ${prodCell} | |\n` -} + + // parse the frontmatter + const { data } = parse(fileContents) + + let contentCell = '' + let previewCell = '' + let prodCell = '' + + if (file.status === 'added') contentCell = 'New file: ' + else if (file.status === 'removed') contentCell = 'Removed: ' + contentCell += `[\`${fileName}\`](${sourceUrl})` + + try { + // the try/catch is needed because getApplicableVersions() returns either [] or throws an error when it can't parse the versions frontmatter + // try/catch can be removed if docs-engineering#1821 is resolved + // i.e. for feature based versioning, like ghae: 'issue-6337' + const fileVersions = getApplicableVersions(data.versions) + + for (const plan in allVersionShortnames) { + // plan is the shortName (i.e., fpt) + // allVersionShortNames[plan] is the planName (i.e., free-pro-team) + + // walk by the plan names since we generate links differently for most plans + const versions = fileVersions.filter((fileVersion) => + fileVersion.includes(allVersionShortnames[plan]) + ) + + if (versions.length === 1) { + // for fpt, ghec, and ghae + + if (versions.toString() === nonEnterpriseDefaultVersion) { + // omit version from fpt url + + previewCell += `[${plan}](${APP_URL}/${fileUrl})
` + prodCell += `[${plan}](${PROD_URL}/${fileUrl})
` + } else { + // for non-versioned releases (ghae, ghec) use full url + + previewCell += `[${plan}](${APP_URL}/${versions}/${fileUrl})
` + prodCell += `[${plan}](${PROD_URL}/${versions}/${fileUrl})
` + } + } else if (versions.length) { + // for ghes releases, link each version + + previewCell += `${plan}@ ` + prodCell += `${plan}@ ` + + versions.forEach((version) => { + previewCell += `[${version.split('@')[1]}](${APP_URL}/${version}/${fileUrl}) ` + prodCell += `[${version.split('@')[1]}](${PROD_URL}/${version}/${fileUrl}) ` + }) + previewCell += '
' + prodCell += '
' + } + } + } catch (e) { + console.error( + `Version information for ${file.filename} couldn't be determined from its frontmatter.` + ) + } + let note = '' + if (file.status === 'removed') { + note = 'removed' + // If the file was removed, the `previewCell` no longer makes sense + // since it was based on looking at the base sha. + previewCell = 'n/a' + } + + return `| ${contentCell} | ${previewCell} | ${prodCell} | ${note} |` + }) +) +markdownTable += lines.join('\n') setOutput('changesTable', markdownTable)