Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/redoc snooty #56

Merged
merged 29 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
d5823b7
Fix issue with redoc and snooty
branberry Jan 2, 2025
f0f4a94
Use repo name instead
branberry Jan 2, 2025
ccfb644
Check if site is snooty frontend
branberry Jan 2, 2025
2eedef0
Use reponame
branberry Jan 2, 2025
64ae1b0
Resolve merge conflicts
branberry Jan 3, 2025
1276f01
Update netlify.toml
branberry Jan 3, 2025
5143b6e
Remove bson paths
branberry Jan 3, 2025
26af085
Update tsconfig
branberry Jan 3, 2025
74dff74
Update tsconfig
branberry Jan 3, 2025
027d862
Merge branch 'main' into fix/redoc-snooty
branberry Jan 9, 2025
53d87de
Fix bson version
branberry Jan 9, 2025
23fc825
update lockfile
branberry Jan 9, 2025
7ac1f9d
fix/redoc update repoName conditional
anabellabuckvar Jan 10, 2025
bce4c98
fix/redoc logging
anabellabuckvar Jan 10, 2025
ac54016
fix/redoc fix site name conditional
anabellabuckvar Jan 10, 2025
f7af50f
fix/redoc logging
anabellabuckvar Jan 10, 2025
69ecacd
fix/redoc logging more
anabellabuckvar Jan 10, 2025
a36656d
fix/redoc-snooty testing
anabellabuckvar Jan 10, 2025
32d596c
fix/redoc-snooty cleaning
anabellabuckvar Jan 10, 2025
04e9844
fix/redoc-snooty move run redoc to separate function
anabellabuckvar Jan 10, 2025
1ee934d
fix/redoc-snooty separate out runRedoc function
anabellabuckvar Jan 10, 2025
f1eed23
fix/redoc-snooty refactor
anabellabuckvar Jan 13, 2025
43d07b0
fix/redoc-snooty refactor
anabellabuckvar Jan 13, 2025
79ec65b
fix/redoc-snooty clean up
anabellabuckvar Jan 13, 2025
052c211
fix/redoc-snooty clean up
anabellabuckvar Jan 13, 2025
8289483
fix/redoc-snooty clean up
anabellabuckvar Jan 13, 2025
cdcc87f
fix/redoc-snooty fix q flag
anabellabuckvar Jan 13, 2025
2597f6a
fix/redoc-snooty cleaning
anabellabuckvar Jan 13, 2025
c8b9b0f
fix/redoc-snooty cleaning
anabellabuckvar Jan 13, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions extensions/redoc/netlify.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
[build]
command="pnpm build"
publish=".ntli/site/static"

[functions]
directory = "src/endpoints"
command = "pnpm build"
publish = ".ntli/site/static"

[dev]
command = "netlify-extension dev"
Expand Down
2 changes: 1 addition & 1 deletion extensions/redoc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"dependencies": {
"@netlify/netlify-plugin-netlify-extension": "^1.0.3",
"@netlify/sdk": "^2.7.1",
"bson": "^6.8.0",
"bson": "^4.6.3",
"mongodb": "^6.8.0",
"node-fetch": "^3.3.2",
"typescript": "^5.5.3"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { NetlifyPluginUtils } from '@netlify/build';
import type { OASPageMetadata } from '.';
import type { OASPageMetadata } from './runRedoc';
import { getAtlasSpecUrl } from './atlas';
import { db } from './utils/db';
import { writeFileAsync } from './utils/fs-async';
Expand Down Expand Up @@ -36,6 +36,7 @@ interface GetOASpecParams {
resourceVersions?: string[];
apiVersion?: string;
resourceVersion?: string;
contentRepoName?: string;
}

export async function getBuildOasSpecCommand({
Expand All @@ -45,17 +46,17 @@ export async function getBuildOasSpecCommand({
output,
siteUrl,
siteTitle,
contentRepoName,
apiVersion,
resourceVersion,
}: GetOASpecParams) {
try {
let spec = '';
let isSuccessfulBuild = true;

if (sourceType === 'url') {
spec = source;
} else if (sourceType === 'local') {
const localFilePath = `source${source}`;
const localFilePath = `${contentRepoName ? `${contentRepoName}/` : ''}source${source}`;
spec = localFilePath;
} else if (sourceType === 'atlas') {
const { oasFileURL, successfulGitHash } = await getAtlasSpecUrl({
Expand Down Expand Up @@ -176,7 +177,10 @@ export async function buildOpenAPIPages(
entries: [string, OASPageMetadata][],
{ siteUrl, siteTitle }: PageBuilderOptions,
run: NetlifyPluginUtils['run'],
contentRepoName?: string,
) {
const output = `${process.cwd()}${contentRepoName ? '' : '/snooty'}/public`;

for (const [pageSlug, data] of entries) {
const {
source_type: sourceType,
Expand All @@ -186,7 +190,6 @@ export async function buildOpenAPIPages(
} = data;

let isSuccessfulBuild = true;

if (resourceVersions) {
const isRunSuccessfulArray = await Promise.all(
resourceVersions.map(async (resourceVersion) => {
Expand All @@ -196,13 +199,14 @@ export async function buildOpenAPIPages(
const command = await getBuildOasSpecCommand({
source,
sourceType,
output: `${process.cwd()}/snooty/public`,
output,
pageSlug,
siteUrl,
siteTitle,
apiVersion,
resourceVersions,
resourceVersion,
contentRepoName,
});

await run.command(command);
Expand All @@ -224,11 +228,12 @@ export async function buildOpenAPIPages(
const command = await getBuildOasSpecCommand({
source,
sourceType,
output: `${process.cwd()}/snooty/public`,
output,
pageSlug,
siteUrl,
siteTitle,
apiVersion,
contentRepoName,
});
await run.command(command);

Expand Down
46 changes: 46 additions & 0 deletions extensions/redoc/src/cloneRedoc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import type { NetlifyPluginUtils } from '@netlify/build';
import { deserialize, type Document } from 'bson';
import { readFileAsync } from './utils/fs-async';
const BUNDLE_PATH = `${process.cwd()}/bundle`;
const REDOC_CLI_VERSION = '1.2.3';

export const hasOpenAPIPages = async (
run: NetlifyPluginUtils['run'],
): Promise<false | Document> => {
await run.command('unzip -o -q bundle.zip -d bundle');

const siteBson = await readFileAsync(`${BUNDLE_PATH}/site.bson`);
const buildMetadata = deserialize(siteBson);

if (!buildMetadata.openapi_pages) {
console.log('No OpenAPI pages found');
return false;
}
console.log('OpenAPI pages found');
return buildMetadata;
};

export const cloneRedoc = async (
run: NetlifyPluginUtils['run'],
cache: NetlifyPluginUtils['cache'],
) => {
const hasRedoc = await cache.has('redoc');

if (hasRedoc) {
console.log('Restoring redoc from cache');
cache.restore('redoc');
return;
}

console.log('Cloning redoc');

await run.command(
`git clone -b @dop/redoc-cli@${REDOC_CLI_VERSION} --depth 1 https://github.com/mongodb-forks/redoc.git redoc`,
);

await run.command('npm ci --prefix cli/ --omit=dev', {
cwd: `${process.cwd()}/redoc`,
});

await cache.save('redoc');
};
81 changes: 7 additions & 74 deletions extensions/redoc/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,85 +1,18 @@
import { deserialize } from 'bson';
import { buildOpenAPIPages } from './build-pages';
import { readFileAsync } from './utils/fs-async';
import { envVarToBool, Extension } from 'util/extension';
import { runRedoc } from './runRedoc';

const extension = new Extension({
isEnabled: envVarToBool(process.env.REDOC_ENABLED),
});
const BUNDLE_PATH = `${process.cwd()}/bundle`;

//TODO: set this in netlify.toml or db env vars
const REDOC_CLI_VERSION = '1.2.3';

export interface OASPageMetadata {
source_type: string;
source: string;
api_version?: string;
resource_versions?: string[];
}

export type OASPagesMetadata = Record<string, OASPageMetadata>;

// handle installing redoc cli if it's not already installed
// Handle building of redoc pages if they should exist
extension.addBuildEventHandler(
'onPreBuild',
async ({ utils: { run, cache } }) => {
console.log('Running redoc prebuild');
const hasRedoc = await cache.has('redoc');

if (hasRedoc) {
console.log('Restoring redoc from cache');
cache.restore('redoc');
return;
}

await run.command(
`git clone -b @dop/redoc-cli@${REDOC_CLI_VERSION} --depth 1 https://github.com/mongodb-forks/redoc.git redoc`,
);

await run.command('npm ci --prefix cli/ --omit=dev', {
cwd: `${process.cwd()}/redoc`,
});

await cache.save('redoc');
'onPostBuild',
async ({ utils: { run, cache }, netlifyConfig }) => {
const siteName = netlifyConfig?.build?.environment?.SITE_NAME as string;
const repoName = netlifyConfig?.build?.environment?.REPO_NAME as string;
await runRedoc({ run, cache, siteName, repoName });
},
);

// handle building the redoc pages
extension.addBuildEventHandler('onPostBuild', async ({ utils: { run } }) => {
// TODO: remove this conditional
if (!process.env.REDOC_ENABLED) return;
console.log('=========== Redoc Extension Begin ================');
await run.command('unzip -o -q bundle.zip -d bundle');

const siteBson = await readFileAsync(`${BUNDLE_PATH}/site.bson`);

const buildMetadata = deserialize(siteBson);
const siteTitle: string = buildMetadata.title;
const openapiPages: OASPagesMetadata | undefined =
buildMetadata.openapi_pages;

if (!openapiPages) {
console.log('No OpenAPI pages found');
return;
}

const openapiPagesEntries = Object.entries(openapiPages);
//TODO: set this in netlify.toml or db env vars
const siteUrl = process.env.DEPLOY_PRIME_URL || '';

await buildOpenAPIPages(openapiPagesEntries, { siteTitle, siteUrl }, run);

console.log('=========== Redoc Integration End ================');
});

// cache redoc
extension.addBuildEventHandler('onSuccess', async ({ utils: { cache } }) => {
const hasRedoc = await cache.has('redoc');
if (!hasRedoc) {
console.log('saving redoc to cache');
await cache.save('redoc');
}
});

export { extension };
68 changes: 68 additions & 0 deletions extensions/redoc/src/runRedoc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import type { NetlifyPluginUtils } from '@netlify/build';
import { buildOpenAPIPages } from './buildPages';
import { cloneRedoc, hasOpenAPIPages } from './cloneRedoc';

export interface OASPageMetadata {
source_type: string;
source: string;
api_version?: string;
resource_versions?: string[];
}

type OASPagesMetadata = Record<string, OASPageMetadata>;

const FRONTEND_SITES = [
'docs-frontend-stg',
'docs-frontend-dotcomstg',
'docs-frontend-dotcomprd',
];

export const runRedoc = async ({
run,
cache,
siteName,
repoName,
}: {
run: NetlifyPluginUtils['run'];
cache: NetlifyPluginUtils['cache'];
siteName: string;
repoName: string;
}) => {
console.log('=========== Checking For OpenAPI Pages ================');

const buildMetadata = await hasOpenAPIPages(run);
if (!buildMetadata) {
return;
}
console.log('=========== Redoc Extension Begin ================');

const siteTitle: string = buildMetadata.title;
const openapiPages: OASPagesMetadata = buildMetadata.openapi_pages;

const openapiPagesEntries = Object.entries(openapiPages);
const siteUrl = process.env.DEPLOY_PRIME_URL || '';
const isFrontendBuild = FRONTEND_SITES.includes(siteName);

const contentRepoName = isFrontendBuild ? repoName : undefined;

await cloneRedoc(run, cache);

await buildOpenAPIPages(
openapiPagesEntries,
{ siteTitle, siteUrl },
run,
contentRepoName,
);

await cacheRedoc(cache);

console.log('=========== Redoc Integration End ================');
};

const cacheRedoc = async (cache: NetlifyPluginUtils['cache']) => {
const hasRedoc = await cache.has('redoc');
if (!hasRedoc) {
console.log('saving redoc to cache');
await cache.save('redoc');
}
};
24 changes: 16 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading