|
| 1 | +import { AggregationCursor } from 'mongodb'; |
| 2 | +import { pool, db } from '../../connector'; |
| 3 | +import { ToC, ToCInsertions, TocOrderInsertions, traverseAndMerge } from '../ToC'; |
| 4 | + |
| 5 | +export interface AssociatedProduct { |
| 6 | + name: string; |
| 7 | + versions: string[]; |
| 8 | +} |
| 9 | + |
| 10 | +export interface SharedMetadata { |
| 11 | + project: string; |
| 12 | + branch: string; |
| 13 | + associated_products?: AssociatedProduct[]; |
| 14 | + toctree: ToC; |
| 15 | + toctreeOrder: any[]; |
| 16 | + [key: string]: any; |
| 17 | +} |
| 18 | + |
| 19 | +// TODO: move the branch/repobranch interfaces into their own file, or into a seperate abstraction? |
| 20 | +interface BranchEntry { |
| 21 | + name: string; |
| 22 | + gitBranchName: string; |
| 23 | + [key: string]: any; |
| 24 | +} |
| 25 | + |
| 26 | +export interface ReposBranchesDocument { |
| 27 | + repoName: string; |
| 28 | + project: string; |
| 29 | + branches: BranchEntry[]; |
| 30 | + [key: string]: any; |
| 31 | +} |
| 32 | + |
| 33 | +// Queries pool*.repos_branches for any entries for the given project and branch from a metadata entry. |
| 34 | +const getRepoBranchesEntry = async (project, branch) => { |
| 35 | + const db = await pool(); |
| 36 | + return db.collection('repos_branches').findOne({ branches: { $elemMatch: { gitBranchName: branch } }, project }); |
| 37 | +}; |
| 38 | + |
| 39 | +// Queries pool*.repos_branches for all entries for associated_products in a shared metadata entry |
| 40 | +const getAllAssociatedRepoBranchesEntries = async (metadata: SharedMetadata) => { |
| 41 | + const { associated_products } = metadata; |
| 42 | + if (!associated_products || !associated_products.length) return []; |
| 43 | + const associatedProductNames = associated_products.map((a) => a.name); |
| 44 | + const db = await pool(); |
| 45 | + const entries = await db |
| 46 | + .collection('repos_branches') |
| 47 | + .find({ project: { $in: associatedProductNames } }) |
| 48 | + .toArray(); |
| 49 | + return entries as unknown as ReposBranchesDocument[]; |
| 50 | +}; |
| 51 | + |
| 52 | +const mapRepoBranches = (repoBranches: ReposBranchesDocument[]) => |
| 53 | + Object.fromEntries( |
| 54 | + repoBranches.map((entry) => { |
| 55 | + const branches = Object.fromEntries(entry.branches.map((branch) => [branch.gitBranchName, branch])); |
| 56 | + return [entry.project, branches]; |
| 57 | + }) |
| 58 | + ); |
| 59 | + |
| 60 | +const hasAssociations = (metadata) => !!metadata.associated_products?.length; |
| 61 | + |
| 62 | +const umbrellaMetadataEntry = async (metadata): Promise<SharedMetadata> => { |
| 63 | + try { |
| 64 | + const snooty = await db(); |
| 65 | + const entry = await snooty |
| 66 | + .collection('metadata') |
| 67 | + .find({ |
| 68 | + 'associated_products.name': metadata.project, |
| 69 | + }) |
| 70 | + .sort({ build_id: -1 }) |
| 71 | + .limit(1) |
| 72 | + .toArray(); |
| 73 | + return entry[0] as unknown as SharedMetadata; |
| 74 | + } catch (error) { |
| 75 | + console.log(`Error at time of querying for umbrella metadata entry: ${error}`); |
| 76 | + throw error; |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | +// Convert our cursor from the shared ToC aggregation query into a series of ToC objects |
| 81 | +const shapeToCsCursor = async ( |
| 82 | + tocCursor: AggregationCursor, |
| 83 | + repoBranchesMap |
| 84 | +): Promise<{ tocInsertions: ToCInsertions; tocOrderInsertions: TocOrderInsertions }> => { |
| 85 | + const tocInsertions = {}; |
| 86 | + const tocOrderInsertions = {}; |
| 87 | + |
| 88 | + await tocCursor.forEach((doc) => { |
| 89 | + // Initialize to empty object if we haven't already, for a given project. |
| 90 | + if (!tocInsertions[doc._id.project]) tocInsertions[doc._id.project] = {}; |
| 91 | + if (!tocOrderInsertions[doc._id.project]) tocOrderInsertions[doc._id.project] = {}; |
| 92 | + |
| 93 | + // TODO: If we want staging builds with embedded versions, it needs to be added here |
| 94 | + if (repoBranchesMap?.[doc._id.project]?.[doc._id.branch]) { |
| 95 | + tocInsertions[doc._id.project][doc._id.branch] = doc.most_recent.toctree; |
| 96 | + tocOrderInsertions[doc._id.project][doc._id.branch] = doc.most_recent.toctreeOrder; |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | + return { tocInsertions, tocOrderInsertions }; |
| 101 | +}; |
| 102 | + |
| 103 | +const getAssociatedProducts = async (umbrellaMetadata) => { |
| 104 | + try { |
| 105 | + const { associated_products } = umbrellaMetadata; |
| 106 | + const associatedProductNames = associated_products.map((a) => a.name); |
| 107 | + const snooty = await db(); |
| 108 | + |
| 109 | + // This query matches on projects in associated_products for our given metadata that have a build_id and that do not have merged tocs |
| 110 | + // then groups per branch and per project from those matches |
| 111 | + // and gets the most recent doc entry (by build_id), with the toctree and toctreeOrder fields. |
| 112 | + const tocs = snooty.collection('metadata').aggregate([ |
| 113 | + { |
| 114 | + $match: { project: { $in: associatedProductNames }, build_id: { $exists: true }, is_merged_toc: { $ne: true } }, |
| 115 | + }, |
| 116 | + { |
| 117 | + $group: { |
| 118 | + _id: { project: '$project', branch: '$branch' }, |
| 119 | + most_recent: { |
| 120 | + $max: { |
| 121 | + build_id: '$build_id', |
| 122 | + toctree: '$toctree', |
| 123 | + toctreeOrder: '$toctreeOrder', |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + }, |
| 128 | + ]); |
| 129 | + return tocs; |
| 130 | + } catch (error) { |
| 131 | + console.log(`Error at time of aggregating existing associated product metadata entries: ${error}`); |
| 132 | + throw error; |
| 133 | + } |
| 134 | +}; |
| 135 | + |
| 136 | +export const mergeAssociatedToCs = async (metadata) => { |
| 137 | + const { project, branch } = metadata; |
| 138 | + const umbrellaMetadata = hasAssociations(metadata) ? metadata : await umbrellaMetadataEntry(metadata); |
| 139 | + |
| 140 | + // Short circuit execution here if there's no umbrella product metadata found |
| 141 | + if (!umbrellaMetadata) return; |
| 142 | + // Short circuit execution if the project branch is NOT in repo branches |
| 143 | + // If we want to embed with staging builds, then this needs to be turned off |
| 144 | + // or converted so that local metadata ToC is added to tocInsertions |
| 145 | + const isStagingBranch = await !getRepoBranchesEntry(project, branch); |
| 146 | + if (isStagingBranch) return; |
| 147 | + |
| 148 | + const repoBranchesEntries = await getAllAssociatedRepoBranchesEntries(umbrellaMetadata); |
| 149 | + const repoBranchesMap = mapRepoBranches(repoBranchesEntries); |
| 150 | + const tocsCursor = await getAssociatedProducts(umbrellaMetadata); |
| 151 | + const { tocInsertions, tocOrderInsertions } = await shapeToCsCursor(tocsCursor, repoBranchesMap); |
| 152 | + const mergedMetadataEntry = traverseAndMerge(umbrellaMetadata, tocInsertions, tocOrderInsertions); |
| 153 | + |
| 154 | + // Remove the _id and treat the entry as a brand new document. |
| 155 | + delete mergedMetadataEntry._id; |
| 156 | + // Add a flag to denote that the entry contains a merged ToC. |
| 157 | + mergedMetadataEntry.is_merged_toc = true; |
| 158 | + |
| 159 | + return mergedMetadataEntry; |
| 160 | +}; |
0 commit comments