From e9add7a79624a2524a98a83b3b7b89bad5f57eeb Mon Sep 17 00:00:00 2001 From: Zach Shilton <4624598+zchsh@users.noreply.github.com> Date: Mon, 9 Sep 2024 11:15:30 -0400 Subject: [PATCH 1/3] split target function into separate file --- .../get-latest-content-ref-for-product.js | 23 +++++++++++++++++++ build-libs/redirects.js | 16 +------------ 2 files changed, 24 insertions(+), 15 deletions(-) create mode 100644 build-libs/get-latest-content-ref-for-product.js diff --git a/build-libs/get-latest-content-ref-for-product.js b/build-libs/get-latest-content-ref-for-product.js new file mode 100644 index 0000000000..2f84d76132 --- /dev/null +++ b/build-libs/get-latest-content-ref-for-product.js @@ -0,0 +1,23 @@ +/** + * Fetch the latest ref from the content API to ensure the redirects are accurate. + * + * TODO: refactor to fetch from known nav-data, rather than from version + * metadata. Intent being to allow upstream changes to version metadata such + * that version metadata `ref` will only be updated when version metadata is + * actually updated (as opposed to on every content upload, as is currently + * the case). + * + * @param {string} product + * @returns {Promise} + */ +async function getLatestContentRefForProduct(product) { + const contentUrl = new URL('https://content.hashicorp.com') + contentUrl.pathname = `/api/content/${product}/version-metadata/latest` + const latestRef = await fetch(contentUrl.toString()) + .then((resp) => resp.json()) + .then((json) => json.result.ref) + + return latestRef +} + +module.exports = getLatestContentRefForProduct diff --git a/build-libs/redirects.js b/build-libs/redirects.js index d39addf36f..09a151502a 100644 --- a/build-libs/redirects.js +++ b/build-libs/redirects.js @@ -10,6 +10,7 @@ const path = require('path') const { isDeployPreview } = require('../src/lib/env-checks') const fetchGithubFile = require('./fetch-github-file') +const getLatestContentRefForProduct = require('./get-latest-content-ref-for-product') const { getTutorialRedirects } = require('./tutorial-redirects') const { getDocsDotHashiCorpRedirects, @@ -26,21 +27,6 @@ const HOSTNAME_MAP = { 'test-st.hashi-mktg.com': 'sentinel', } -/** - * Fetch the latest ref from the content API to ensure the redirects are accurate. - * - * @TODO save the redirects to the content database and expose them directly via the API - */ -async function getLatestContentRefForProduct(product) { - const contentUrl = new URL('https://content.hashicorp.com') - contentUrl.pathname = `/api/content/${product}/version-metadata/latest` - const latestRef = await fetch(contentUrl.toString()) - .then((resp) => resp.json()) - .then((json) => json.result.ref) - - return latestRef -} - /** * Load redirects from a content repository. * From 2c6abbdc4ebbec51e82fae98ec477d889d5858f3 Mon Sep 17 00:00:00 2001 From: Zach Shilton <4624598+zchsh@users.noreply.github.com> Date: Mon, 9 Sep 2024 12:20:10 -0400 Subject: [PATCH 2/3] refactor to fetch with latest sha not ref --- ...get-latest-content-sha-for-product.test.ts | 33 ++++++++++++ .../get-latest-content-ref-for-product.js | 23 -------- .../get-latest-content-sha-for-product.js | 53 +++++++++++++++++++ build-libs/redirects.js | 49 ++++++++++------- 4 files changed, 115 insertions(+), 43 deletions(-) create mode 100644 build-libs/__tests__/get-latest-content-sha-for-product.test.ts delete mode 100644 build-libs/get-latest-content-ref-for-product.js create mode 100644 build-libs/get-latest-content-sha-for-product.js diff --git a/build-libs/__tests__/get-latest-content-sha-for-product.test.ts b/build-libs/__tests__/get-latest-content-sha-for-product.test.ts new file mode 100644 index 0000000000..c759a3bacc --- /dev/null +++ b/build-libs/__tests__/get-latest-content-sha-for-product.test.ts @@ -0,0 +1,33 @@ +/** + * Copyright (c) HashiCorp, Inc. + * SPDX-License-Identifier: MPL-2.0 + */ + +import getLatestContentShaForProduct from '../get-latest-content-sha-for-product' +import fetchGithubFile from '@build-libs/fetch-github-file' +import { PRODUCT_REDIRECT_ENTRIES } from '@build-libs/redirects' + +describe('getLatestContentShaForProduct', () => { + PRODUCT_REDIRECT_ENTRIES.forEach(({ repo, path }) => { + it(`fetches the latest SHA for the "${repo}" repo`, async () => { + const latestSha = await getLatestContentShaForProduct(repo) + expect(typeof latestSha).toBe('string') + }) + + if (['hcp-docs', 'sentinel', 'ptfe-releases'].includes(repo)) { + console.log(`Skipping test for "${repo}" repo, as it's a private repo.`) + } else { + it(`fetches the latest SHA for the "${repo}" repo, then validates the SHA by fetching redirects`, async () => { + const latestSha = await getLatestContentShaForProduct(repo) + expect(typeof latestSha).toBe('string') + const redirectsFileString = await fetchGithubFile({ + owner: 'hashicorp', + repo: repo, + path: path, + ref: latestSha, + }) + expect(typeof redirectsFileString).toBe('string') + }) + } + }) +}) diff --git a/build-libs/get-latest-content-ref-for-product.js b/build-libs/get-latest-content-ref-for-product.js deleted file mode 100644 index 2f84d76132..0000000000 --- a/build-libs/get-latest-content-ref-for-product.js +++ /dev/null @@ -1,23 +0,0 @@ -/** - * Fetch the latest ref from the content API to ensure the redirects are accurate. - * - * TODO: refactor to fetch from known nav-data, rather than from version - * metadata. Intent being to allow upstream changes to version metadata such - * that version metadata `ref` will only be updated when version metadata is - * actually updated (as opposed to on every content upload, as is currently - * the case). - * - * @param {string} product - * @returns {Promise} - */ -async function getLatestContentRefForProduct(product) { - const contentUrl = new URL('https://content.hashicorp.com') - contentUrl.pathname = `/api/content/${product}/version-metadata/latest` - const latestRef = await fetch(contentUrl.toString()) - .then((resp) => resp.json()) - .then((json) => json.result.ref) - - return latestRef -} - -module.exports = getLatestContentRefForProduct diff --git a/build-libs/get-latest-content-sha-for-product.js b/build-libs/get-latest-content-sha-for-product.js new file mode 100644 index 0000000000..6ea83fe127 --- /dev/null +++ b/build-libs/get-latest-content-sha-for-product.js @@ -0,0 +1,53 @@ +/** + * Copyright (c) HashiCorp, Inc. + * SPDX-License-Identifier: MPL-2.0 + */ + +/** + * We're using the docs endpoints to fetch the latest SHA, so we use + * the env var for the docs API. + */ +const MKTG_CONTENT_DOCS_API = process.env.MKTG_CONTENT_DOCS_API + +/** + * A map of all possible `product` slugs to known content API endpoints that + * will return an object with a `ref` property that accurately reflects the + * ref from which the latest content was uploaded. + */ +const KNOWN_LATEST_REF_ENDPOINTS = { + boundary: '/api/content/boundary/nav-data/latest/docs', + nomad: '/api/content/nomad/nav-data/latest/docs', + vault: '/api/content/vault/nav-data/latest/docs', + vagrant: '/api/content/vagrant/nav-data/latest/docs', + packer: '/api/content/packer/nav-data/latest/docs', + consul: '/api/content/consul/nav-data/latest/docs', + 'terraform-docs-common': + '/api/content/terraform-docs-common/nav-data/latest/docs', + 'hcp-docs': '/api/content/hcp-docs/nav-data/latest/docs', + 'ptfe-releases': '/api/content/ptfe-releases/nav-data/latest/enterprise', + sentinel: '/api/content/sentinel/nav-data/latest/sentinel', +} + +/** + * Fetch the latest sha from the content API for a given product. + * This relies on known `nav-data` endpoints for each product. + * + * @param {string} product + * @returns {Promise} + */ +async function getLatestContentShaForProduct(product) { + const contentUrl = new URL(MKTG_CONTENT_DOCS_API) + const knownEndpoint = KNOWN_LATEST_REF_ENDPOINTS[product] + if (!knownEndpoint) { + throw new Error( + `getLatestContentShaForProduct failed, with unknown product: ${product}. Please add a known endpoint for this product to KNOWN_LATEST_REF_ENDPOINTS.` + ) + } + contentUrl.pathname = knownEndpoint + const latestSha = await fetch(contentUrl.toString()) + .then((resp) => resp.json()) + .then((json) => json.result.sha) + return latestSha +} + +module.exports = getLatestContentShaForProduct diff --git a/build-libs/redirects.js b/build-libs/redirects.js index 09a151502a..8158cbff3e 100644 --- a/build-libs/redirects.js +++ b/build-libs/redirects.js @@ -10,7 +10,7 @@ const path = require('path') const { isDeployPreview } = require('../src/lib/env-checks') const fetchGithubFile = require('./fetch-github-file') -const getLatestContentRefForProduct = require('./get-latest-content-ref-for-product') +const getLatestContentShaForProduct = require('./get-latest-content-sha-for-product') const { getTutorialRedirects } = require('./tutorial-redirects') const { getDocsDotHashiCorpRedirects, @@ -49,13 +49,10 @@ const HOSTNAME_MAP = { * redirects and return early with an empty array. * * @param {string} repoName The name of the repo, owner is always `hashicorp`. - * @param {string?} redirectsPath Optional, custom path to the redirects file. + * @param {string} redirectsPath Path within the repo to the redirects file. * @returns {Promise} */ -async function getRedirectsFromContentRepo( - repoName, - redirectsPath = 'website/redirects.js' -) { +async function getRedirectsFromContentRepo(repoName, redirectsPath) { /** * Note: These constants are declared for clarity in build context intent. */ @@ -68,12 +65,12 @@ async function getRedirectsFromContentRepo( let redirectsFileString if (isDeveloperBuild) { // For `hashicorp/dev-portal` builds, load redirects remotely - const latestContentRef = await getLatestContentRefForProduct(repoName) + const latestContentSha = await getLatestContentShaForProduct(repoName) redirectsFileString = await fetchGithubFile({ owner: 'hashicorp', repo: repoName, path: redirectsPath, - ref: latestContentRef, + ref: latestContentSha, }) } else if (isLocalContentBuild) { // Load redirects from the filesystem, so that authors can see their changes @@ -92,6 +89,24 @@ async function getRedirectsFromContentRepo( return validRedirects } +/** + * @type {{ repo: string, path: string}[]} An array of redirect + * entries. Each entry specifies a repo and the path within that repo to the + * redirects file. + */ +const PRODUCT_REDIRECT_ENTRIES = [ + { repo: 'boundary', path: 'website/redirects.js' }, + { repo: 'nomad', path: 'website/redirects.js' }, + { repo: 'vault', path: 'website/redirects.js' }, + { repo: 'vagrant', path: 'website/redirects.js' }, + { repo: 'packer', path: 'website/redirects.js' }, + { repo: 'consul', path: 'website/redirects.js' }, + { repo: 'terraform-docs-common', path: 'website/redirects.js' }, + { repo: 'hcp-docs', path: '/redirects.js' }, + { repo: 'ptfe-releases', path: 'website/redirects.js' }, + { repo: 'sentinel', path: 'website/redirects.js' }, +] + async function buildProductRedirects() { // Fetch author-oriented redirects from product repos, // and merge those with dev-oriented redirects from @@ -101,18 +116,11 @@ async function buildProductRedirects() { } const productRedirects = ( - await Promise.all([ - getRedirectsFromContentRepo('boundary'), - getRedirectsFromContentRepo('nomad'), - getRedirectsFromContentRepo('vault'), - getRedirectsFromContentRepo('vagrant'), - getRedirectsFromContentRepo('packer'), - getRedirectsFromContentRepo('consul'), - getRedirectsFromContentRepo('terraform-docs-common'), - getRedirectsFromContentRepo('hcp-docs', '/redirects.js'), - getRedirectsFromContentRepo('ptfe-releases'), - getRedirectsFromContentRepo('sentinel'), - ]) + await Promise.all( + PRODUCT_REDIRECT_ENTRIES.map((entry) => + getRedirectsFromContentRepo(entry.repo, entry.path) + ) + ) ).flat() return productRedirects @@ -393,6 +401,7 @@ async function redirectsConfig() { } module.exports = { + PRODUCT_REDIRECT_ENTRIES, redirectsConfig, splitRedirectsByType, groupSimpleRedirects, From bbc9c8aa4ff213946dbed1c8b504c2b41729b2d4 Mon Sep 17 00:00:00 2001 From: Zach Shilton <4624598+zchsh@users.noreply.github.com> Date: Thu, 12 Sep 2024 11:53:06 -0400 Subject: [PATCH 3/3] update working in test log Co-authored-by: Heat Hamilton <55773810+heatlikeheatwave@users.noreply.github.com> --- build-libs/__tests__/get-latest-content-sha-for-product.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build-libs/__tests__/get-latest-content-sha-for-product.test.ts b/build-libs/__tests__/get-latest-content-sha-for-product.test.ts index c759a3bacc..d345d39682 100644 --- a/build-libs/__tests__/get-latest-content-sha-for-product.test.ts +++ b/build-libs/__tests__/get-latest-content-sha-for-product.test.ts @@ -15,7 +15,7 @@ describe('getLatestContentShaForProduct', () => { }) if (['hcp-docs', 'sentinel', 'ptfe-releases'].includes(repo)) { - console.log(`Skipping test for "${repo}" repo, as it's a private repo.`) + console.log(`Skipping test for private repo "${repo}"`) } else { it(`fetches the latest SHA for the "${repo}" repo, then validates the SHA by fetching redirects`, async () => { const latestSha = await getLatestContentShaForProduct(repo)