@@ -2,6 +2,12 @@ const core = require('@actions/core')
22const { ensureLinks } = require ( './links' )
33const { getDependsOn } = require ( './depends' )
44const { mappingFn } = require ( './mappingFn' )
5+ const hash = require ( 'object-hash' )
6+
7+ let createdServices = 0
8+ let updatedServices = 0
9+ let skippedServices = 0
10+ let erroredServices = 0
511
612const updateServices = async ( repositories , { notion, database, systems, owners, structure } ) => {
713 for ( const repo of repositories ) {
@@ -22,29 +28,38 @@ const updateServices = async (repositories, { notion, database, systems, owners,
2228 // Lets just update the first one to not make the problem worse
2329 if ( search . results . length > 0 ) {
2430 const pageId = search . results [ 0 ] . id
31+ const pageHash = search . results [ 0 ] . properties ?. Hash ?. rich_text [ 0 ] ?. text ?. content
2532 core . debug ( `Updating notion info for ${ repoName } ` )
26- await updateNotionRow ( repo , pageId , { notion, database, systems, owners, structure } )
33+ await updateNotionRow ( repo , pageId , pageHash , { notion, database, systems, owners, structure } )
2734 } else {
2835 core . debug ( `Creating notion info for ${ repoName } ` )
2936 await createNotionRow ( repo , { notion, database, systems, owners, structure } )
3037 }
3138 }
39+ core . info ( `Completed with ${ createdServices } created, ${ updatedServices } updated, ${ skippedServices } unchanged and ${ erroredServices } with errors` )
3240}
3341
34- const updateNotionRow = async ( repo , pageId , { notion, database, systems, owners, structure } ) => {
42+ const updateNotionRow = async ( repo , pageId , pageHash , { notion, database, systems, owners, structure } ) => {
3543 try {
3644 let dependsOn = [ ]
3745 if ( repo . spec ?. dependsOn ?. length > 0 ) {
3846 dependsOn = await getDependsOn ( repo . spec . dependsOn , { notion, database } )
3947 }
40- await notion . pages . update ( {
41- page_id : pageId ,
42- properties : createProperties ( repo , dependsOn , { systems, owners, structure } )
43- } )
48+ const { properties, doUpdate } = createProperties ( repo , pageHash , dependsOn , { systems, owners, structure } )
49+ if ( doUpdate ) {
50+ await notion . pages . update ( {
51+ page_id : pageId ,
52+ properties
53+ } )
54+ updatedServices ++
55+ } else {
56+ skippedServices ++
57+ }
4458 if ( repo . metadata ?. links ) {
4559 await ensureLinks ( pageId , repo . metadata . links , { notion } )
4660 }
4761 } catch ( ex ) {
62+ erroredServices ++
4863 core . warning ( `Error updating notion document for ${ repo . _repo . name } : ${ ex . message } ...` )
4964 }
5065}
@@ -55,29 +70,49 @@ const createNotionRow = async (repo, { notion, database, systems, owners, struct
5570 if ( repo . spec ?. dependsOn ?. length > 0 ) {
5671 dependsOn = await getDependsOn ( repo . spec . dependsOn , { notion, database } )
5772 }
73+ const { properties } = createProperties ( repo , null , dependsOn , { systems, owners, structure } )
5874 const page = await notion . pages . create ( {
5975 parent : {
6076 database_id : database
6177 } ,
62- properties : createProperties ( repo , dependsOn , { systems , owners , structure } )
78+ properties
6379 } )
80+ createdServices ++
6481 if ( repo . metadata ?. links ) {
6582 await ensureLinks ( page . id , repo . metadata . links , { notion } )
6683 }
6784 } catch ( ex ) {
85+ erroredServices ++
6886 core . warning ( `Error creating notion document for ${ repo . _repo . name } : ${ ex . message } ` )
6987 }
7088}
7189
72- const createProperties = ( repo , dependsOn , { systems, owners, structure } ) => {
90+ const createProperties = ( repo , pageHash , dependsOn , { systems, owners, structure } ) => {
7391 // This iterates over the structure, executes a mapping function for each based on the data provided
74- const page = { }
92+ const properties = { }
7593 for ( const field of structure ) {
7694 if ( mappingFn [ field . name ] ) {
77- page [ field . name ] = mappingFn [ field . name ] ( repo , { dependsOn, systems, owners } )
95+ properties [ field . name ] = mappingFn [ field . name ] ( repo , { dependsOn, systems, owners } )
96+ }
97+ }
98+ // Always have to check the hash afterwards, excluding the hash and the key
99+ const newPageHash = hash ( properties , {
100+ excludeKeys : ( key ) => {
101+ return key === 'Hash' || key === 'Updated'
78102 }
103+ } )
104+
105+ const doUpdate = newPageHash && newPageHash !== pageHash
106+ properties . Hash = {
107+ rich_text : [
108+ {
109+ text : {
110+ content : newPageHash
111+ }
112+ }
113+ ]
79114 }
80- return page
115+ return { properties , doUpdate }
81116}
82117
83118exports . updateServices = updateServices
0 commit comments