From 2adf502faec63b841d0cd77751e2be134c730210 Mon Sep 17 00:00:00 2001 From: Matt Meigs Date: Mon, 11 Dec 2023 15:30:47 -0500 Subject: [PATCH] add to v1? --- Dockerfile.local | 8 +-- api/controllers/v1/github.ts | 95 +++++++++++++++++++++++++++++++++--- api/controllers/v2/github.ts | 4 -- 3 files changed, 93 insertions(+), 14 deletions(-) diff --git a/Dockerfile.local b/Dockerfile.local index 30bbc3c0e..6ab5cf308 100644 --- a/Dockerfile.local +++ b/Dockerfile.local @@ -43,10 +43,10 @@ RUN git clone -b v${MUT_VERSION} --depth 1 https://github.com/mongodb/mut.git \ && mv dist/mut /opt/ # install redoc fork -RUN git clone -b @dop/redoc-cli@${REDOC_CLI_VERSION} --depth 1 https://github.com/mongodb-forks/redoc.git redoc \ - # Install dependencies for Redoc CLI - && cd redoc/ \ - && npm ci --prefix cli/ --omit=dev +# RUN git clone -b @dop/redoc-cli@${REDOC_CLI_VERSION} --depth 1 https://github.com/mongodb-forks/redoc.git redoc \ +# # Install dependencies for Redoc CLI +# && cd redoc/ \ +# && npm ci --prefix cli/ --omit=dev ENV PATH="${PATH}:/opt/snooty:/opt/mut:/${WORK_DIRECTORY}/.local/bin" diff --git a/api/controllers/v1/github.ts b/api/controllers/v1/github.ts index f86677800..c7d0ff5c9 100644 --- a/api/controllers/v1/github.ts +++ b/api/controllers/v1/github.ts @@ -7,12 +7,17 @@ import { markBuildArtifactsForDeletion, validateJsonWebhook } from '../../handle import { DocsetsRepository } from '../../../src/repositories/docsetsRepository'; import { ReposBranchesDocsetsDocument } from '../../../modules/persistence/src/services/metadata/repos_branches'; import { PushEvent } from '@octokit/webhooks-types'; +import { MONOREPO_NAME } from '../../../src/monorepo/utils/monorepo-constants'; +import { getMonorepoPaths } from '../../../src/monorepo'; +import { getUpdatedFilePaths } from '../../../src/monorepo/utils/path-utils'; +import { JobStatus } from '../../../src/entities/job'; async function prepGithubPushPayload( githubEvent: any, repoBranchesRepository: RepoBranchesRepository, prefix: string, - repoInfo: ReposBranchesDocsetsDocument + repoInfo: ReposBranchesDocsetsDocument, + directory?: string ) { const branch_name = githubEvent.ref.split('/')[2]; const branch_info = await repoBranchesRepository.getRepoBranchAliases( @@ -56,6 +61,7 @@ async function prepGithubPushPayload( urlSlug: urlSlug, prefix: prefix, project: project, + directory: directory, }, logs: [], }; @@ -110,14 +116,70 @@ export const TriggerBuild = async (event: any = {}, context: any = {}): Promise< } const env = c.get('env'); - const repoInfo = await docsetsRepository.getRepo(body.repository.name); - const jobPrefix = repoInfo?.prefix ? repoInfo['prefix'][env] : ''; - // TODO: Make job be of type Job - const job = await prepGithubPushPayload(body, repoBranchesRepository, jobPrefix, repoInfo); - try { + + async function createAndInsertJob(path?: string) { + const repoInfo = await docsetsRepository.getRepo(body.repository.name, path); + const jobPrefix = repoInfo?.prefix ? repoInfo['prefix'][env] : ''; + const job = await prepGithubPushPayload(body, repoBranchesRepository, jobPrefix, repoInfo, path); + consoleLogger.info(job.title, 'Creating Job'); const jobId = await jobRepository.insertJob(job, c.get('jobsQueueUrl')); + jobRepository.notify(jobId, c.get('jobUpdatesQueueUrl'), JobStatus.inQueue, 0); consoleLogger.info(job.title, `Created Job ${jobId}`); + } + + if (body.repository.name === MONOREPO_NAME) { + // TODO: MONOREPO feature flag needed here + consoleLogger.info(body.repository.full_name, `past feature flag and monorepo conditional`); + let monorepoPaths: string[] = []; + try { + if (body.head_commit && body.repository.owner.name) { + consoleLogger.info( + body.repository.full_name, + `commitSha: ${body.head_commit.id}\nrepoName: ${body.repository.name}\nownerName: ${ + body.repository.owner.name + }\nUpdatedfilepaths: ${getUpdatedFilePaths(body.head_commit)}` + ); + monorepoPaths = await getMonorepoPaths({ + commitSha: body.head_commit.id, + repoName: body.repository.name, + ownerName: body.repository.owner.name, + updatedFilePaths: getUpdatedFilePaths(body.head_commit), + consoleLogger, + }); + consoleLogger.info(body.repository.full_name, `Monorepo Paths with new changes: ${monorepoPaths}`); + } + } catch (error) { + console.warn('Warning, attempting to get repo paths caused an error', error); + } + + /* Create and insert Job for each monorepo project that has changes */ + for (const path of monorepoPaths) { + consoleLogger.info(body.repository.full_name, `Each path: ${path}`); + // TODO: Deal with nested monorepo projects + /* For now, we will ignore nested monorepo projects until necessary */ + if (path.split('/').length > 1) continue; + + try { + await createAndInsertJob(path); + } catch (err) { + return { + statusCode: 500, + headers: { 'Content-Type': 'text/plain' }, + body: err, + }; + } + } + + return { + statusCode: 202, + headers: { 'Content-Type': 'text/plain' }, + body: 'Jobs Queued', + }; + } + + try { + await createAndInsertJob(); } catch (err) { return { statusCode: 500, @@ -130,6 +192,27 @@ export const TriggerBuild = async (event: any = {}, context: any = {}): Promise< headers: { 'Content-Type': 'text/plain' }, body: 'Job Queued', }; + + // const repoInfo = await docsetsRepository.getRepo(body.repository.name); + // const jobPrefix = repoInfo?.prefix ? repoInfo['prefix'][env] : ''; + // // TODO: Make job be of type Job + // const job = await prepGithubPushPayload(body, repoBranchesRepository, jobPrefix, repoInfo); + // try { + // consoleLogger.info(job.title, 'Creating Job'); + // const jobId = await jobRepository.insertJob(job, c.get('jobsQueueUrl')); + // consoleLogger.info(job.title, `Created Job ${jobId}`); + // } catch (err) { + // return { + // statusCode: 500, + // headers: { 'Content-Type': 'text/plain' }, + // body: err, + // }; + // } + // return { + // statusCode: 202, + // headers: { 'Content-Type': 'text/plain' }, + // body: 'Job Queued', + // }; }; export const MarkBuildArtifactsForDeletion = markBuildArtifactsForDeletion; diff --git a/api/controllers/v2/github.ts b/api/controllers/v2/github.ts index 16d2ef2eb..612b443de 100644 --- a/api/controllers/v2/github.ts +++ b/api/controllers/v2/github.ts @@ -120,10 +120,6 @@ export const TriggerBuild = async (event: APIGatewayEvent): Promise