@@ -3,7 +3,8 @@ import { deserialize } from 'bson';
33import { ObjectId } from 'mongodb' ;
44import { db , deleteDocuments , insert } from '../connector' ;
55import { mergeAssociatedToCs , AssociatedProduct } from './associated_products' ;
6- import { ToC } from './ToC' ;
6+ import { getRepoBranchesEntry } from './repos_branches' ;
7+ import { project , ToC } from './ToC' ;
78
89const COLLECTION_NAME = 'metadata' ;
910
@@ -18,26 +19,55 @@ export interface Metadata {
1819// Service responsible for memoization of metadata entries.
1920// Any extraneous logic performed on metadata entries as part of upload should be added here
2021// or within subfolders of this module
21- const metadataFromZip = ( zip : AdmZip ) => {
22+ export const metadataFromZip = async ( zip : AdmZip ) => {
2223 const zipEntries = zip . getEntries ( ) ;
23- return zipEntries
24+ const metadata = zipEntries
2425 . filter ( ( entry ) => entry . entryName === 'site.bson' )
2526 . map ( ( entry ) => deserialize ( entry . getData ( ) ) ) [ 0 ] as Metadata ;
27+ await verifyMetadata ( metadata ) ;
28+ return metadata ;
2629} ;
2730
28- export const insertMetadata = async ( buildId : ObjectId , zip : AdmZip ) => {
31+ // Verifies the entries for associated_products in metadata
32+ const verifyMetadata = async ( metadata : Metadata ) => {
33+ try {
34+ if ( ! metadata [ 'associated_products' ] ?. length ) {
35+ return metadata ;
36+ }
37+ const invalidNames : project [ ] = [ ] ;
38+ const promises = metadata [ 'associated_products' ] . map ( async ( ap ) => {
39+ const branchEntry = await getRepoBranchesEntry ( ap . name ) ;
40+ if ( ! branchEntry ) {
41+ invalidNames . push ( ap . name ) ;
42+ }
43+ } ) ;
44+ await Promise . all ( promises ) ;
45+ if ( invalidNames . length ) {
46+ console . warn ( `No branches found for associated project(s) [${ invalidNames } ]. Removing such associated_products` ) ;
47+ metadata . associated_products = metadata . associated_products . filter ( ( ap ) => ! invalidNames . includes ( ap . name ) ) ;
48+ }
49+ if ( ! metadata [ 'associated_products' ] . length ) {
50+ delete metadata [ 'associated_products' ] ;
51+ }
52+ return metadata ;
53+ } catch ( e ) {
54+ console . error ( `Error while verifying metadata ${ e } ` ) ;
55+ throw e ;
56+ }
57+ } ;
58+
59+ export const insertMetadata = async ( buildId : ObjectId , metadata : Metadata ) => {
2960 try {
30- const metadata = metadataFromZip ( zip ) ;
3161 return insert ( [ metadata ] , COLLECTION_NAME , buildId ) ;
3262 } catch ( error ) {
3363 console . error ( `Error at insertion time for ${ COLLECTION_NAME } : ${ error } ` ) ;
3464 throw error ;
3565 }
3666} ;
3767
38- export const insertMergedMetadataEntries = async ( buildId : ObjectId , zip : AdmZip ) => {
68+ export const insertMergedMetadataEntries = async ( buildId : ObjectId , metadata : Metadata ) => {
3969 try {
40- const mergedMetadataEntries = await mergeAssociatedToCs ( metadataFromZip ( zip ) ) ;
70+ const mergedMetadataEntries = await mergeAssociatedToCs ( metadata ) ;
4171 return mergedMetadataEntries
4272 ? await Promise . all ( mergedMetadataEntries . map ( ( m ) => insert ( [ m ] , COLLECTION_NAME , buildId ) ) )
4373 : [ ] ;
@@ -47,9 +77,9 @@ export const insertMergedMetadataEntries = async (buildId: ObjectId, zip: AdmZip
4777 }
4878} ;
4979
50- export const deleteStaleMetadata = async ( zip : AdmZip ) => {
80+ export const deleteStaleMetadata = async ( metadata : Metadata ) => {
5181 try {
52- const { project, branch } = metadataFromZip ( zip ) ;
82+ const { project, branch } = metadata ;
5383 const LIMIT = 4 ;
5484 // get most recent metadata for this project-branch
5585 const snooty = await db ( ) ;
0 commit comments