From 5c44115ffd961e04a4d8786f4c6705baa54dff83 Mon Sep 17 00:00:00 2001 From: branberry Date: Fri, 6 Oct 2023 12:28:32 -0500 Subject: [PATCH] [DOP-4033]: Update next-gen-deploy to work --- src/commands/src/helpers/execution-helper.ts | 26 +++++++++----- src/commands/src/helpers/index.ts | 22 ++++++++++-- src/commands/src/shared/next-gen-deploy.ts | 23 ++++++++++-- src/commands/src/shared/next-gen-stage.ts | 29 +++++++-------- src/entrypoints/localApp.ts | 37 ++++++++++++-------- 5 files changed, 96 insertions(+), 41 deletions(-) diff --git a/src/commands/src/helpers/execution-helper.ts b/src/commands/src/helpers/execution-helper.ts index 5341d2879..fd5037b0e 100644 --- a/src/commands/src/helpers/execution-helper.ts +++ b/src/commands/src/helpers/execution-helper.ts @@ -1,11 +1,18 @@ import path from 'path'; import fs from 'fs'; -import { checkIfPatched, getCommitBranch, getCommitHash, getPatchId, getRepoDir } from '.'; +import { executeCliCommand, getCommitBranch, getCommitHash, getPatchId, getRepoDir } from '.'; import { promisify } from 'util'; const existsAsync = promisify(fs.exists); const writeFileAsync = promisify(fs.writeFile); +async function cloneRepo(repoName: string) { + await executeCliCommand({ + command: 'git', + args: ['clone', `https://github.com/mongodb/${repoName}`], + options: { cwd: `${process.cwd()}/repos` }, + }); +} async function createEnvProdFile(repoDir: string, projectName: string, baseUrl: string, prefix = '') { const prodFileName = `${process.cwd()}/snooty/.env.production`; @@ -26,9 +33,12 @@ async function createEnvProdFile(repoDir: string, projectName: string, baseUrl: } } -export async function getCliBuildDependencies(repoDir: string, projectName: string, baseUrl: string) { +export async function getCliBuildDependencies(repoName: string, projectName: string, baseUrl: string) { + await cloneRepo(repoName); + + const repoDir = getRepoDir(repoName); + const commandPromises = [ - checkIfPatched(repoDir), getCommitHash(repoDir), getCommitBranch(repoDir), getPatchId(repoDir), @@ -38,11 +48,11 @@ export async function getCliBuildDependencies(repoDir: string, projectName: stri const deps = await Promise.all(commandPromises); return { - hasPatch: deps[0] as string, - commitHash: deps[1] as string, - commitBranch: deps[2] as string, - patchId: deps[3] as string | undefined, - hasRedirects: deps[4] as boolean, + commitHash: deps[0] as string, + commitBranch: deps[1] as string, + patchId: deps[2] as string | undefined, + hasRedirects: deps[3] as boolean, bundlePath: `${repoDir}/bundle.zip`, + repoDir, }; } diff --git a/src/commands/src/helpers/index.ts b/src/commands/src/helpers/index.ts index bbd936f08..41076072d 100644 --- a/src/commands/src/helpers/index.ts +++ b/src/commands/src/helpers/index.ts @@ -40,9 +40,27 @@ export async function executeAndPipeCommands( const cmdTo = spawn(cmdToParams.command, cmdToParams.args || [], cmdToParams.options || {}); cmdFrom.stdout?.on('data', (data: Buffer) => { + if (!cmdTo.stdin?.writable) { + cmdFrom.stdin?.end(); + cmdFrom.kill(); + return; + } + cmdTo.stdin?.write(data); }); + cmdFrom.stdout?.on('error', (err) => { + console.log('error on cmdFrom out', err); + }); + + cmdTo.stdin?.on('finish', () => { + console.log('finished stdin'); + }); + + cmdTo.stdin?.on('error', () => { + console.log('stdin done'); + }); + cmdFrom.on('error', (err) => { reject(new ExecuteCommandError('The first command failed', err)); hasRejected = true; @@ -75,11 +93,11 @@ export async function executeAndPipeCommands( console.error('Options provided: ', cmdToParams.options); if (outputText) { - console.error(outputText.join()); + console.error('output', outputText.join()); } if (errorText) { - console.error(errorText.join()); + console.error('error', errorText.join()); } reject(new ExecuteCommandError('The command failed', exitCode)); diff --git a/src/commands/src/shared/next-gen-deploy.ts b/src/commands/src/shared/next-gen-deploy.ts index d9ba13474..9167f1599 100644 --- a/src/commands/src/shared/next-gen-deploy.ts +++ b/src/commands/src/shared/next-gen-deploy.ts @@ -5,9 +5,10 @@ interface NextGenDeployParams { mutPrefix: string; gitBranch: string; hasConfigRedirects: boolean; + url: string; } -export async function nextGenDeploy({ bucket, mutPrefix, gitBranch, hasConfigRedirects }: NextGenDeployParams) { +export async function nextGenDeploy({ bucket, mutPrefix, gitBranch, hasConfigRedirects, url }: NextGenDeployParams) { // const hasConfigRedirects = await existsAsync(path.join(process.cwd(), 'config/redirects')); if (hasConfigRedirects && (gitBranch === 'main' || gitBranch === 'master')) { @@ -16,8 +17,24 @@ export async function nextGenDeploy({ bucket, mutPrefix, gitBranch, hasConfigRed } // yes | mut-publish public ${BUCKET} --prefix="${MUT_PREFIX}" --deploy --deployed-url-prefix=${URL} --json --all-subdirectories ${ARGS}; - await executeAndPipeCommands( + const { outputText } = await executeAndPipeCommands( { command: 'yes' }, - { command: 'mut-publish', args: ['public', bucket, `--prefix=${mutPrefix}`, '--json', '--all-subdirectories'] } + { + command: 'mut-publish', + args: [ + 'public', + bucket, + `--prefix=${mutPrefix}`, + '--deploy', + `--deployed-url-prefix=${url}`, + '--json', + '--all-subdirectories', + ], + options: { + cwd: `${process.cwd()}/snooty`, + }, + } ); + + return `${outputText}\n Hosted at ${url}/${mutPrefix}`; } diff --git a/src/commands/src/shared/next-gen-stage.ts b/src/commands/src/shared/next-gen-stage.ts index 49828c16a..64fbd686c 100644 --- a/src/commands/src/shared/next-gen-stage.ts +++ b/src/commands/src/shared/next-gen-stage.ts @@ -1,29 +1,30 @@ -import { - checkIfPatched, - executeAndWriteToFile, - executeCliCommand, - getCommitBranch, - getCommitHash, - getPatchId, -} from '../helpers'; +import { executeCliCommand, getCommitHash, getPatchId } from '../helpers'; interface StageParams { repoDir: string; mutPrefix: string; projectName: string; - bucketName: string; + bucket: string; url: string; + patchId?: string; + commitBranch: string; } -export async function nextGenStage({ repoDir, mutPrefix, projectName, bucketName, url }: StageParams) { - const [hasPatch, commitBranch] = await Promise.all([checkIfPatched(repoDir), getCommitBranch(repoDir)]); - +export async function nextGenStage({ + repoDir, + mutPrefix, + projectName, + bucket, + url, + patchId, + commitBranch, +}: StageParams) { let hostedAtUrl = `${url}/${mutPrefix}/docsworker/${commitBranch}/`; let prefix = mutPrefix; - const commandArgs = ['public', bucketName, '--stage']; + const commandArgs = ['public', bucket, '--stage']; - if (hasPatch && projectName === mutPrefix) { + if (patchId && projectName === mutPrefix) { const [commitHash, patchId] = await Promise.all([getCommitHash(repoDir), getPatchId(repoDir)]); prefix = `${commitHash}/${patchId}/${mutPrefix}`; hostedAtUrl = `${url}/${commitHash}/${patchId}/${mutPrefix}/docsworker/${commitBranch}/`; diff --git a/src/entrypoints/localApp.ts b/src/entrypoints/localApp.ts index 051dfa858..89cfdcbd8 100644 --- a/src/entrypoints/localApp.ts +++ b/src/entrypoints/localApp.ts @@ -1,25 +1,24 @@ -import { executeCliCommand, getRepoDir } from '../commands/src/helpers'; import { nextGenParse } from '../commands/src/shared/next-gen-parse'; import { nextGenHtml } from '../commands/src/shared/next-gen-html'; import { getCliBuildDependencies } from '../commands/src/helpers/execution-helper'; import { nextGenStage } from '../commands/src/shared/next-gen-stage'; import { oasPageBuild } from '../commands/src/shared/oas-page-build'; import { persistenceModule } from '../commands/src/shared/persistence-module'; +import { nextGenDeploy } from '../commands/src/shared/next-gen-deploy'; async function localApp() { + // TODO: Fetch this from repos_branches const repoName = 'docs-java'; const projectName = 'java'; const baseUrl = 'https://www.mongodb.com'; - const repoDir = getRepoDir(repoName); + const bucket = 'docs-java-dotcomstg'; + const mutPrefix = 'docs/drivers/java/sync'; - // mocking out the clone aspect of a job - await executeCliCommand({ - command: 'git', - args: ['clone', `https://github.com/mongodb/${repoName}`], - options: { cwd: `${process.cwd()}/repos` }, - }); - - const { commitHash, patchId, bundlePath } = await getCliBuildDependencies(repoDir, projectName, baseUrl); + const { commitHash, patchId, bundlePath, commitBranch, hasRedirects, repoDir } = await getCliBuildDependencies( + repoName, + projectName, + baseUrl + ); console.log('Hello'); @@ -43,7 +42,6 @@ async function localApp() { console.log('next-gen-html complete'); console.log('Begin oas-page-build...'); - const mutPrefix = 'docs'; const siteUrl = mutPrefix ? `${baseUrl}/${mutPrefix}` : `${baseUrl}`; const oasPageBuildRes = await oasPageBuild({ repoDir, bundlePath, siteUrl }); console.log('oas-page-build compelte'); @@ -52,16 +50,27 @@ async function localApp() { console.log('Begin next-gen-stage...'); const resultMessage = await nextGenStage({ + patchId, + commitBranch, repoDir, projectName, - bucketName: 'docs-mongodb-org-stg', + bucket, url: baseUrl, mutPrefix, }); console.log(resultMessage); - console.log('Begin next-gen-stage complete'); + console.log('next-gen-stage complete'); - console.log('Begin next-gen-deploy'); + console.log('Begin next-gen-deploy...'); + const deployRes = await nextGenDeploy({ + bucket, + hasConfigRedirects: hasRedirects, + gitBranch: commitBranch, + mutPrefix, + url: baseUrl, + }); + console.log(deployRes); + console.log('next-gen-deploy complete'); } localApp();