Skip to content

Commit c43e630

Browse files
committed
Refactor to dynamically generate Notion doc based on structure of database, resolves #1
1 parent b113215 commit c43e630

6 files changed

Lines changed: 329 additions & 214 deletions

File tree

dist/index.js

Lines changed: 168 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -20359,6 +20359,7 @@ const core = __nccwpck_require__(272)
2035920359
const loadData = async ({ notion }) => {
2036020360
const systemDb = core.getInput('system_database')
2036120361
const ownerDb = core.getInput('owner_database')
20362+
const database = core.getInput('database')
2036220363

2036320364
const processRows = (data) => {
2036420365
const parent = {}
@@ -20369,6 +20370,16 @@ const loadData = async ({ notion }) => {
2036920370
return parent
2037020371
}
2037120372

20373+
// Get core DB structure
20374+
const dbStructure = await notion.databases.retrieve({
20375+
database_id: database
20376+
})
20377+
const structure = Object.keys(dbStructure.properties).map((property) => {
20378+
console.log(dbStructure.properties[property])
20379+
return { name: dbStructure.properties[property].name, type: dbStructure.properties[property].type }
20380+
})
20381+
20382+
// Get the system and owner db
2037220383
let systemRows, ownerRows
2037320384

2037420385
if (systemDb) {
@@ -20402,7 +20413,8 @@ const loadData = async ({ notion }) => {
2040220413

2040320414
return {
2040420415
systems,
20405-
owners
20416+
owners,
20417+
structure
2040620418
}
2040720419
}
2040820420

@@ -20496,6 +20508,7 @@ const getRepos = async () => {
2049620508
for (const target of targets) {
2049720509
const pushMissing = false
2049820510
const targetDefinition = await parseServiceDefinition(repo, target, pushMissing)
20511+
targetDefinition.fromLocation = true
2049920512
repoData.push(...targetDefinition)
2050020513
}
2050120514
} else {
@@ -20644,6 +20657,141 @@ const ensureLinks = async (pageId, links, { notion }) => {
2064420657
exports.ensureLinks = ensureLinks
2064520658

2064620659

20660+
/***/ }),
20661+
20662+
/***/ 3913:
20663+
/***/ ((__unused_webpack_module, exports) => {
20664+
20665+
/**
20666+
* For each possible field name in the service catalogue, expose a mapping function
20667+
* If a function is not found the field will be skipped
20668+
*/
20669+
const mappingFn = {
20670+
Name: (repo) => {
20671+
return {
20672+
title: [
20673+
{
20674+
text: {
20675+
content: repo.metadata?.name || repo._repo.name
20676+
}
20677+
}
20678+
]
20679+
}
20680+
},
20681+
Description: (repo) => {
20682+
return {
20683+
rich_text: [
20684+
{
20685+
text: {
20686+
content: repo.metadata?.description || repo._repo.description || repo._repo.name
20687+
}
20688+
}
20689+
]
20690+
}
20691+
},
20692+
Updated: () => {
20693+
return {
20694+
date: {
20695+
start: new Date().toISOString()
20696+
}
20697+
}
20698+
},
20699+
System: (repo, { systems }) => {
20700+
let system
20701+
const systemSpec = repo?.spec?.system
20702+
if (systems) {
20703+
// Segments are a relation
20704+
system = {
20705+
relation: [
20706+
{ id: systems[systemSpec?.toLowerCase()] || systems.unknown }
20707+
]
20708+
}
20709+
} else {
20710+
// Segments are a tag
20711+
system = {
20712+
select: {
20713+
name: systemSpec || 'Unknown'
20714+
}
20715+
}
20716+
}
20717+
return system
20718+
},
20719+
URL: (repo) => {
20720+
// We can use the catalog file location to locate the right path within the repo if it is a monorepo
20721+
const htmlUrl = repo.fromLocation ? repo._catalog_file.substring(0, repo._catalog_file.lastIndexOf('/')) : repo._repo.html_url
20722+
return {
20723+
url: htmlUrl
20724+
}
20725+
},
20726+
Kind: (repo) => {
20727+
return {
20728+
select: {
20729+
name: repo.kind || 'Unknown'
20730+
}
20731+
}
20732+
},
20733+
Tags: (repo) => {
20734+
return {
20735+
multi_select: repo.metadata?.tags ? repo.metadata.tags.flatMap(tag => { return { name: tag } }) : []
20736+
}
20737+
},
20738+
Lifecycle: (repo) => {
20739+
return {
20740+
select: {
20741+
name: repo.spec?.lifecycle || 'Unknown'
20742+
}
20743+
}
20744+
},
20745+
Owner: (repo, { owners }) => {
20746+
let owner
20747+
const ownerSpec = repo?.spec?.owner
20748+
if (owners) {
20749+
// Owners are a relation
20750+
owner = {
20751+
relation: [
20752+
{ id: owners[ownerSpec?.toLowerCase()] || owners.unknown }
20753+
]
20754+
}
20755+
} else {
20756+
// owners are a tag
20757+
owner = {
20758+
select: {
20759+
name: ownerSpec || 'Unknown'
20760+
}
20761+
}
20762+
}
20763+
return owner
20764+
},
20765+
Visibility: (repo) => {
20766+
return {
20767+
select: {
20768+
name: repo._repo.visibility
20769+
}
20770+
}
20771+
},
20772+
Language: (repo) => {
20773+
return {
20774+
select: {
20775+
name: repo._repo.language || 'Unknown'
20776+
}
20777+
}
20778+
},
20779+
Status: (repo) => {
20780+
return {
20781+
select: {
20782+
name: repo.status
20783+
}
20784+
}
20785+
},
20786+
DependsOn: (repo, { dependsOn }) => {
20787+
return { relation: dependsOn }
20788+
},
20789+
DependencyOf: null // Skip this field
20790+
}
20791+
20792+
exports.mappingFn = mappingFn
20793+
20794+
2064720795
/***/ }),
2064820796

2064920797
/***/ 3180:
@@ -20652,8 +20800,9 @@ exports.ensureLinks = ensureLinks
2065220800
const core = __nccwpck_require__(272)
2065320801
const { ensureLinks } = __nccwpck_require__(5475)
2065420802
const { getDependsOn } = __nccwpck_require__(4154)
20803+
const { mappingFn } = __nccwpck_require__(3913)
2065520804

20656-
const updateServices = async (repositories, { notion, database, systems, owners }) => {
20805+
const updateServices = async (repositories, { notion, database, systems, owners, structure }) => {
2065720806
for (const repo of repositories) {
2065820807
// Lets see if we can find the row
2065920808
const repoName = repo.metadata?.name || repo._repo.name
@@ -20673,23 +20822,23 @@ const updateServices = async (repositories, { notion, database, systems, owners
2067320822
if (search.results.length > 0) {
2067420823
const pageId = search.results[0].id
2067520824
core.debug(`Updating notion info for ${repoName}`)
20676-
await updateNotionRow(repo, pageId, { notion, database, systems, owners })
20825+
await updateNotionRow(repo, pageId, { notion, database, systems, owners, structure })
2067720826
} else {
2067820827
core.debug(`Creating notion info for ${repoName}`)
20679-
await createNotionRow(repo, { notion, database, systems, owners })
20828+
await createNotionRow(repo, { notion, database, systems, owners, structure })
2068020829
}
2068120830
}
2068220831
}
2068320832

20684-
const updateNotionRow = async (repo, pageId, { notion, database, systems, owners }) => {
20833+
const updateNotionRow = async (repo, pageId, { notion, database, systems, owners, structure }) => {
2068520834
try {
2068620835
let dependsOn = []
2068720836
if (repo.spec?.dependsOn?.length > 0) {
2068820837
dependsOn = await getDependsOn(repo.spec.dependsOn, { notion, database })
2068920838
}
2069020839
await notion.pages.update({
2069120840
page_id: pageId,
20692-
properties: createProperties(repo, dependsOn, { systems, owners })
20841+
properties: createProperties(repo, dependsOn, { systems, owners, structure })
2069320842
})
2069420843
if (repo.metadata?.links) {
2069520844
await ensureLinks(pageId, repo.metadata.links, { notion })
@@ -20699,7 +20848,7 @@ const updateNotionRow = async (repo, pageId, { notion, database, systems, owners
2069920848
}
2070020849
}
2070120850

20702-
const createNotionRow = async (repo, { notion, database, systems, owners }) => {
20851+
const createNotionRow = async (repo, { notion, database, systems, owners, structure }) => {
2070320852
try {
2070420853
let dependsOn = []
2070520854
if (repo.spec?.dependsOn?.length > 0) {
@@ -20709,7 +20858,7 @@ const createNotionRow = async (repo, { notion, database, systems, owners }) => {
2070920858
parent: {
2071020859
database_id: database
2071120860
},
20712-
properties: createProperties(repo, dependsOn, { systems, owners })
20861+
properties: createProperties(repo, dependsOn, { systems, owners, structure })
2071320862
})
2071420863
if (repo.metadata?.links) {
2071520864
await ensureLinks(page.id, repo.metadata.links, { notion })
@@ -20719,105 +20868,16 @@ const createNotionRow = async (repo, { notion, database, systems, owners }) => {
2071920868
}
2072020869
}
2072120870

20722-
const createProperties = (repo, dependsOn, { systems, owners }) => {
20723-
let owner, system
20724-
const ownerSpec = repo?.spec?.owner
20725-
const systemSpec = repo?.spec?.system
20726-
20727-
if (owners) {
20728-
// Owners are a relation
20729-
owner = {
20730-
relation: [
20731-
{ id: owners[ownerSpec?.toLowerCase()] || owners.unknown }
20732-
]
20733-
}
20734-
} else {
20735-
// owners are a tag
20736-
owner = {
20737-
select: {
20738-
name: ownerSpec || 'Unknown'
20739-
}
20740-
}
20741-
}
20742-
20743-
if (systems) {
20744-
// Segments are a relation
20745-
system = {
20746-
relation: [
20747-
{ id: systems[systemSpec?.toLowerCase()] || systems.unknown }
20748-
]
20749-
}
20750-
} else {
20751-
// Segments are a tag
20752-
system = {
20753-
select: {
20754-
name: systemSpec || 'Unknown'
20755-
}
20756-
}
20757-
}
20758-
20759-
// We can use the catalog file location to locate the right path within the repo
20760-
const htmlUrl = repo._catalog_file ? repo._catalog_file.substring(0, repo._catalog_file.lastIndexOf('/')) : repo._repo.html_url
20761-
20762-
return {
20763-
Name: {
20764-
title: [
20765-
{
20766-
text: {
20767-
content: repo.metadata?.name || repo._repo.name
20768-
}
20769-
}
20770-
]
20771-
},
20772-
Description: {
20773-
rich_text: [
20774-
{
20775-
text: {
20776-
content: repo.metadata?.description || repo._repo.description || repo._repo.name
20777-
}
20778-
}
20779-
]
20780-
},
20781-
Kind: {
20782-
select: {
20783-
name: repo.kind || 'Unknown'
20784-
}
20785-
},
20786-
URL: {
20787-
url: htmlUrl
20788-
},
20789-
Owner: owner,
20790-
System: system,
20791-
DependsOn: { relation: dependsOn },
20792-
Visibility: {
20793-
select: {
20794-
name: repo._repo.visibility
20795-
}
20796-
},
20797-
Language: {
20798-
select: {
20799-
name: repo._repo.language || 'Unknown'
20800-
}
20801-
},
20802-
Lifecycle: {
20803-
select: {
20804-
name: repo.spec?.lifecycle || 'Unknown'
20805-
}
20806-
},
20807-
Status: {
20808-
select: {
20809-
name: repo.status
20810-
}
20811-
},
20812-
Tags: {
20813-
multi_select: repo.metadata?.tags ? repo.metadata.tags.flatMap(tag => { return { name: tag } }) : []
20814-
},
20815-
Updated: {
20816-
date: {
20817-
start: new Date().toISOString()
20818-
}
20871+
const createProperties = (repo, dependsOn, { systems, owners, structure }) => {
20872+
// This iterates over the structure, executes a mapping function for each based on the data provided
20873+
const page = {}
20874+
for (const field of structure) {
20875+
if (mappingFn[field.name]) {
20876+
page[field.name] = mappingFn[field.name](repo, { dependsOn, systems, owners })
2081920877
}
2082020878
}
20879+
console.log(page)
20880+
return page
2082120881
}
2082220882

2082320883
exports.updateServices = updateServices
@@ -27667,15 +27727,16 @@ try {
2766727727

2766827728
const refreshData = async () => {
2766927729
core.startGroup('Loading systems and owners ...')
27670-
const { systems, owners } = await loadData({ core, notion })
27730+
const { systems, owners, structure } = await loadData({ core, notion })
27731+
core.info(`Found ${structure.length} fields in the Service database: ${structure.map((item) => item.name)}`)
2767127732
core.info(`Loaded ${Object.keys(systems || {}).length} systems`)
2767227733
core.info(`Loaded ${Object.keys(owners || {}).length} owners`)
2767327734
core.endGroup()
2767427735
core.startGroup('🌀 Getting github repositories')
2767527736
const repositories = await getRepos({ core })
2767627737
core.endGroup()
2767727738
core.startGroup(`✨ Updating notion with ${repositories.length} services ...`)
27678-
await updateServices(repositories, { core, notion, database, systems, owners })
27739+
await updateServices(repositories, { core, notion, database, systems, owners, structure })
2767927740
core.endGroup()
2768027741
}
2768127742

0 commit comments

Comments
 (0)