Skip to content

Commit

Permalink
feat: fetch redirects from udr for migrated repos (#2658)
Browse files Browse the repository at this point in the history
* feat: fetch redirects from udr for migrated repos

* add tests and make new code more testable

* PR feedback: add check for HASHI_ENV and log error for no results
  • Loading branch information
LeahMarieBush authored Dec 19, 2024
1 parent 7d81d7c commit 4fa992b
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
59 changes: 59 additions & 0 deletions build-libs/__tests__/redirects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
splitRedirectsByType,
groupSimpleRedirects,
filterInvalidRedirects,
getRedirectsFromContentRepo,
} from '../redirects'

function withHashiEnv(value, fn) {
Expand All @@ -19,6 +20,10 @@ function withHashiEnv(value, fn) {
process.env.HASHI_ENV = originalValue
}

afterEach(() => {
vi.restoreAllMocks()
})

describe('splitRedirectsByType', () => {
test('splits simple and glob redirects', () => {
const { simpleRedirects, complexRedirects } = splitRedirectsByType([
Expand Down Expand Up @@ -239,4 +244,58 @@ describe('filterInvalidRedirects', () => {
})
})

describe('getRedirectsFromContentRepo', () => {
it('returns redirects from UDR for a migrated repo', async () => {
const mockData = [
{
source: '/terraform/cloud-docs/policy-enforcement/opa/vcs',
destination:
'/terraform/cloud-docs/policy-enforcement/manage-policy-sets/opa-vcs',
permanent: true,
},
{
source: '/terraform/cloud-docs/policy-enforcement/policy-results',
destination: '/terraform/cloud-docs/policy-enforcement/view-results',
permanent: true,
},
]
global.fetch = vi.fn().mockResolvedValue({
json: () => new Promise((resolve) => resolve(mockData)),
ok: true,
})
vi.stubEnv('HASHI_ENV', 'unified-docs-sandbox')

const redirects = await getRedirectsFromContentRepo(
'terraform-docs-common',
'test',
{
flags: {
unified_docs_migrated_repos: ['terraform-docs-common'],
},
}
)

expect(redirects).toEqual(mockData)
})

it('returns empty array if there are not any redirects from UDR for a migrated repo', async () => {
global.fetch = vi.fn().mockResolvedValue({ ok: false })
vi.stubEnv('HASHI_ENV', 'unified-docs-sandbox')
const mockConsole = vi.spyOn(console, 'error').mockImplementation(() => {})

const redirects = await getRedirectsFromContentRepo(
'ptfe-releases',
'test',
{
flags: {
unified_docs_migrated_repos: ['ptfe-releases'],
},
}
)

expect(redirects).toEqual([])
expect(mockConsole).toHaveBeenCalledOnce()
})
})

export {}
27 changes: 25 additions & 2 deletions build-libs/redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const {
getDocsDotHashiCorpRedirects,
} = require('./docs-dot-hashicorp-redirects')
const { packerPluginRedirects } = require('./integration-packer-redirects')
const { loadHashiConfigForEnvironment } = require('../config')

require('isomorphic-unfetch')

Expand Down Expand Up @@ -52,12 +53,32 @@ const HOSTNAME_MAP = {
* @param {string} redirectsPath Path within the repo to the redirects file.
* @returns {Promise<Redirect[]>}
*/
async function getRedirectsFromContentRepo(repoName, redirectsPath) {
async function getRedirectsFromContentRepo(repoName, redirectsPath, config) {
/**
* Note: These constants are declared for clarity in build context intent.
*/
const isDeveloperBuild = !process.env.IS_CONTENT_PREVIEW
const isLocalContentBuild = isDeployPreview(repoName)
/**
* Load redirects from the unified docs repo if it's in the list of migrated repos.
* Return early if there are not any redirects found for that specific repo.
*/
if (
process.env.HASHI_ENV === 'unified-docs-sandbox' &&
config.flags?.unified_docs_migrated_repos?.find((repo) => repo === repoName)
) {
const getUDRRedirects = await fetch(
`${process.env.UNIFIED_DOCS_API}/api/content/${repoName}/redirects`
)
if (getUDRRedirects.ok) {
const udrRedirects = await getUDRRedirects.json()
return udrRedirects
}
console.error(
`Error fetching redirects from the unified docs repo for ${repoName}`
)
return []
}
/**
* Load redirects from the target repo (or return early for non-target repos).
*/
Expand Down Expand Up @@ -114,11 +135,12 @@ async function buildProductRedirects() {
if (process.env.SKIP_BUILD_PRODUCT_REDIRECTS) {
return []
}
const config = loadHashiConfigForEnvironment()

const productRedirects = (
await Promise.all(
PRODUCT_REDIRECT_ENTRIES.map((entry) =>
getRedirectsFromContentRepo(entry.repo, entry.path)
getRedirectsFromContentRepo(entry.repo, entry.path, config)
)
)
).flat()
Expand Down Expand Up @@ -406,4 +428,5 @@ module.exports = {
splitRedirectsByType,
groupSimpleRedirects,
filterInvalidRedirects,
getRedirectsFromContentRepo,
}

0 comments on commit 4fa992b

Please sign in to comment.