Skip to content

Commit 6f7c8c3

Browse files
committed
Fix links to work with correct structure
1 parent 23a24d8 commit 6f7c8c3

5 files changed

Lines changed: 75 additions & 50 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
node_modules
1+
node_modules
2+
.env

dist/index.js

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21072,22 +21072,32 @@ exports.getRepos = getRepos
2107221072

2107321073
const core = __nccwpck_require__(272)
2107421074

21075-
const updateNotionLink = async (linkId, link, { notion }) => {
21075+
const processRows = (data) => {
21076+
const parent = {}
21077+
data.results.forEach((row) => {
21078+
const name = row.properties.Name.title[0].plain_text.toLowerCase()
21079+
const url = row.properties.URL.url
21080+
if (name) parent[name] = { id: row.id, url }
21081+
})
21082+
return parent
21083+
}
21084+
21085+
const updateNotionLink = async (linkId, link, linkUrl, { notion }) => {
2107621086
try {
2107721087
await notion.pages.update({
2107821088
page_id: linkId,
2107921089
properties: {
2108021090
URL: {
21081-
url: link.url
21091+
url: linkUrl
2108221092
}
2108321093
}
2108421094
})
2108521095
} catch (ex) {
21086-
core.error(`Error updating notion link for ${link.title}: ${ex.message} ...`)
21096+
core.error(`Error updating notion link for ${link}: ${ex.message} ...`)
2108721097
}
2108821098
}
2108921099

21090-
const createNotionLink = async (linkDatabaseId, link, { notion }) => {
21100+
const createNotionLink = async (linkDatabaseId, link, linkUrl, { notion }) => {
2109121101
try {
2109221102
await notion.pages.create({
2109321103
parent: {
@@ -21098,13 +21108,13 @@ const createNotionLink = async (linkDatabaseId, link, { notion }) => {
2109821108
title: [
2109921109
{
2110021110
text: {
21101-
content: link.title
21111+
content: link
2110221112
}
2110321113
}
2110421114
]
2110521115
},
2110621116
URL: {
21107-
url: link.url
21117+
url: linkUrl
2110821118
}
2110921119
}
2111021120
})
@@ -21114,22 +21124,23 @@ const createNotionLink = async (linkDatabaseId, link, { notion }) => {
2111421124
}
2111521125

2111621126
const updateLinks = async (linkDatabaseId, links, { notion }) => {
21117-
for (const link of links) {
21127+
// First get all of the existing rows in one go
21128+
const existingLinkRows = await notion.databases.query({
21129+
database_id: linkDatabaseId
21130+
})
21131+
const existingLinks = processRows(existingLinkRows)
21132+
// Now scan for any that have changed or are new
21133+
const linkKeys = Object.keys(links)
21134+
for (const link of linkKeys) {
2111821135
// Lets see if we can find the row
21119-
const search = await notion.databases.query({
21120-
database_id: linkDatabaseId,
21121-
filter: {
21122-
property: 'Name',
21123-
text: {
21124-
equals: link.title
21125-
}
21136+
if (existingLinks[link]) {
21137+
const linkId = existingLinks[link].id
21138+
const linkUrl = existingLinks[link].url
21139+
if (links[link] !== linkUrl) { // url has changed
21140+
await updateNotionLink(linkId, link, links[link], { notion })
2112621141
}
21127-
})
21128-
if (search.results.length > 0) {
21129-
const linkId = search.results[0].id
21130-
await updateNotionLink(linkId, link, { notion })
2113121142
} else {
21132-
await createNotionLink(linkDatabaseId, link, { notion })
21143+
await createNotionLink(linkDatabaseId, link, links[link], { notion })
2113321144
}
2113421145
}
2113521146
}
@@ -21350,14 +21361,14 @@ const updateNotionRow = async (repo, pageId, pageHash, { notion, database, syste
2135021361
page_id: pageId,
2135121362
properties
2135221363
})
21364+
if (repo.metadata?.links) {
21365+
await ensureLinks(pageId, repo.metadata.links, { notion })
21366+
}
2135321367
updatedServices++
2135421368
} else {
2135521369
core.debug(`Not updating notion info for ${repo._repo.name} as hash unchanged`)
2135621370
skippedServices++
2135721371
}
21358-
if (repo.metadata?.links) {
21359-
await ensureLinks(pageId, repo.metadata.links, { notion })
21360-
}
2136121372
} catch (ex) {
2136221373
erroredServices++
2136321374
core.warning(`Error updating notion document for ${repo._repo.name}: ${ex.message} ...`)
@@ -21396,8 +21407,9 @@ const createProperties = (repo, pageHash, dependsOn, { systems, owners, structur
2139621407
properties[field.name] = mappingFn[field.name](repo, { dependsOn, systems, owners })
2139721408
}
2139821409
}
21399-
// Always have to check the hash afterwards, excluding the hash and the key
21400-
const newPageHash = hash(properties, {
21410+
21411+
// Always have to check the hash afterwards, excluding the hash and the key but including links
21412+
const newPageHash = hash({ links: repo.metadata.links, ...properties }, {
2140121413
excludeKeys: (key) => {
2140221414
return key === 'Hash' || key === 'Updated'
2140321415
}

src/links.js

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
const core = require('@actions/core')
22

3-
const updateNotionLink = async (linkId, link, { notion }) => {
3+
const processRows = (data) => {
4+
const parent = {}
5+
data.results.forEach((row) => {
6+
const name = row.properties.Name.title[0].plain_text.toLowerCase()
7+
const url = row.properties.URL.url
8+
if (name) parent[name] = { id: row.id, url }
9+
})
10+
return parent
11+
}
12+
13+
const updateNotionLink = async (linkId, link, linkUrl, { notion }) => {
414
try {
515
await notion.pages.update({
616
page_id: linkId,
717
properties: {
818
URL: {
9-
url: link.url
19+
url: linkUrl
1020
}
1121
}
1222
})
1323
} catch (ex) {
14-
core.error(`Error updating notion link for ${link.title}: ${ex.message} ...`)
24+
core.error(`Error updating notion link for ${link}: ${ex.message} ...`)
1525
}
1626
}
1727

18-
const createNotionLink = async (linkDatabaseId, link, { notion }) => {
28+
const createNotionLink = async (linkDatabaseId, link, linkUrl, { notion }) => {
1929
try {
2030
await notion.pages.create({
2131
parent: {
@@ -26,13 +36,13 @@ const createNotionLink = async (linkDatabaseId, link, { notion }) => {
2636
title: [
2737
{
2838
text: {
29-
content: link.title
39+
content: link
3040
}
3141
}
3242
]
3343
},
3444
URL: {
35-
url: link.url
45+
url: linkUrl
3646
}
3747
}
3848
})
@@ -42,22 +52,23 @@ const createNotionLink = async (linkDatabaseId, link, { notion }) => {
4252
}
4353

4454
const updateLinks = async (linkDatabaseId, links, { notion }) => {
45-
for (const link of links) {
55+
// First get all of the existing rows in one go
56+
const existingLinkRows = await notion.databases.query({
57+
database_id: linkDatabaseId
58+
})
59+
const existingLinks = processRows(existingLinkRows)
60+
// Now scan for any that have changed or are new
61+
const linkKeys = Object.keys(links)
62+
for (const link of linkKeys) {
4663
// Lets see if we can find the row
47-
const search = await notion.databases.query({
48-
database_id: linkDatabaseId,
49-
filter: {
50-
property: 'Name',
51-
text: {
52-
equals: link.title
53-
}
64+
if (existingLinks[link]) {
65+
const linkId = existingLinks[link].id
66+
const linkUrl = existingLinks[link].url
67+
if (links[link] !== linkUrl) { // url has changed
68+
await updateNotionLink(linkId, link, links[link], { notion })
5469
}
55-
})
56-
if (search.results.length > 0) {
57-
const linkId = search.results[0].id
58-
await updateNotionLink(linkId, link, { notion })
5970
} else {
60-
await createNotionLink(linkDatabaseId, link, { notion })
71+
await createNotionLink(linkDatabaseId, link, links[link], { notion })
6172
}
6273
}
6374
}

src/local.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ process.env.INPUT_NOTION_TOKEN = process.env.NOTION_TOKEN
66
process.env.INPUT_GITHUB_TOKEN = process.env.GITHUB_TOKEN
77
process.env.INPUT_REPOSITORY_TYPE = 'public'
88
process.env.INPUT_GITHUB_OWNER = 'infinitaslearning'
9-
process.env.INPUT_REPOSITORY_FILTER = '.*'
9+
process.env.INPUT_REPOSITORY_FILTER = 'notion-github-catalog' // '.*'
1010
process.env.INPUT_REPOSITORY_BATCH_SIZE = '50'
1111
process.env.INPUT_PUSH_MISSING = 'true'
1212
process.env.INPUT_DATABASE = 'cecaf0beb15945158d155866ff9acce8'

src/services.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ const updateNotionRow = async (repo, pageId, pageHash, { notion, database, syste
3838
page_id: pageId,
3939
properties
4040
})
41+
if (repo.metadata?.links) {
42+
await ensureLinks(pageId, repo.metadata.links, { notion })
43+
}
4144
updatedServices++
4245
} else {
4346
core.debug(`Not updating notion info for ${repo._repo.name} as hash unchanged`)
4447
skippedServices++
4548
}
46-
if (repo.metadata?.links) {
47-
await ensureLinks(pageId, repo.metadata.links, { notion })
48-
}
4949
} catch (ex) {
5050
erroredServices++
5151
core.warning(`Error updating notion document for ${repo._repo.name}: ${ex.message} ...`)
@@ -84,8 +84,9 @@ const createProperties = (repo, pageHash, dependsOn, { systems, owners, structur
8484
properties[field.name] = mappingFn[field.name](repo, { dependsOn, systems, owners })
8585
}
8686
}
87-
// Always have to check the hash afterwards, excluding the hash and the key
88-
const newPageHash = hash(properties, {
87+
88+
// Always have to check the hash afterwards, excluding the hash and the key but including links
89+
const newPageHash = hash({ links: repo.metadata.links, ...properties }, {
8990
excludeKeys: (key) => {
9091
return key === 'Hash' || key === 'Updated'
9192
}

0 commit comments

Comments
 (0)