Skip to content

Commit

Permalink
feat(website): add version badge to external plugin docs pages (#11451)
Browse files Browse the repository at this point in the history
  • Loading branch information
zchsh authored Dec 17, 2021
1 parent cae7bd9 commit 63eaf1f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
13 changes: 13 additions & 0 deletions website/components/remote-plugin-docs/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from '@hashicorp/react-docs-page/server'
import renderPageMdx from '@hashicorp/react-docs-page/render-page-mdx'
import resolveNavData from './utils/resolve-nav-data'
import fetchLatestReleaseTag from './utils/fetch-latest-release-tag'

async function generateStaticPaths({
navDataFile,
Expand Down Expand Up @@ -49,6 +50,14 @@ async function generateStaticProps({
const githubFileUrl = remoteFile
? remoteFile.sourceUrl
: `https://github.com/hashicorp/${product.slug}/blob/${mainBranch}/website/${filePath}`
// If this is a plugin, and if
// the version has been specified as "latest",
// determine the tag this corresponds to, so that
// we can show this explicit version number in docs
const latestReleaseTag =
pluginData?.version === 'latest'
? await fetchLatestReleaseTag(pluginData.repo)
: pluginData?.version
// For plugin pages, prefix the MDX content with a
// label that reflects the plugin tier
// (current options are "Official" or "Community")
Expand All @@ -64,6 +73,10 @@ async function generateStaticProps({
if (pluginData?.isHcpPackerReady) {
badgesMdx.push(`<PluginBadge type="hcp_packer_ready" />`)
}
// Add badge showing the latest release version number
if (latestReleaseTag) {
badgesMdx.push(`<Badge label="${latestReleaseTag}" theme="light-gray"/>`)
}
// If we have badges to add, inject them into the MDX
if (badgesMdx.length > 0) {
const badgeChildrenMdx = badgesMdx.join('')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
async function fetchLatestReleaseTag(repo) {
const latestReleaseUrl = `https://github.com/${repo}/releases/latest`
const redirectedUrl = await getRedirectedUrl(latestReleaseUrl)
if (!redirectedUrl) return false
const latestTag = redirectedUrl.match(/tag\/(.*)/)[1]
return latestTag
}

async function getRedirectedUrl(url) {
return new Promise((resolve, reject) => {
const https = require('https')
const req = https.request(url, (res) => {
if (res.statusCode >= 300 && res.statusCode < 400) {
resolve(res.headers.location)
} else {
resolve(false)
}
})
req.on('error', reject)
req.end()
})
}

module.exports = fetchLatestReleaseTag
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,10 @@ async function resolvePluginEntryDocs(pluginConfigEntry, currentPath) {
path: urlPath,
remoteFile: { filePath, fileString, sourceUrl },
pluginData: {
repo,
tier: parsedPluginTier,
isHcpPackerReady,
version,
},
}
})
Expand Down

0 comments on commit 63eaf1f

Please sign in to comment.