Skip to content

Commit 2d63e81

Browse files
committed
Add hash to not update Notion if nothing changes (avoids flooding your Notion updates un-necessarily)
1 parent ece3465 commit 2d63e81

File tree

8 files changed

+599
-26
lines changed

8 files changed

+599
-26
lines changed

dist/index.js

Lines changed: 508 additions & 12 deletions
Large diffs are not rendered by default.

dist/licenses.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,32 @@ SOFTWARE.
14551455

14561456

14571457

1458+
object-hash
1459+
MIT
1460+
The MIT License (MIT)
1461+
1462+
Copyright (c) 2014 object-hash contributors
1463+
1464+
Permission is hereby granted, free of charge, to any person obtaining a copy
1465+
of this software and associated documentation files (the "Software"), to deal
1466+
in the Software without restriction, including without limitation the rights
1467+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1468+
copies of the Software, and to permit persons to whom the Software is
1469+
furnished to do so, subject to the following conditions:
1470+
1471+
The above copyright notice and this permission notice shall be included in all
1472+
copies or substantial portions of the Software.
1473+
1474+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1475+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1476+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1477+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1478+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1479+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1480+
SOFTWARE.
1481+
1482+
1483+
14581484
octokit
14591485
MIT
14601486
The MIT License

package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"@actions/github": "^5.0.0",
2020
"@notionhq/client": "^0.4.11",
2121
"@tryfabric/martian": "^1.1.1",
22+
"object-hash": "^2.2.0",
2223
"octokit": "^1.7.1",
2324
"yaml": "^1.10.2"
2425
},

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ This action expects a Notion database with the following properties, this will b
2727
- Language: select
2828
- Status: select
2929
- Updated: date
30+
- Hash: text
3031

3132
The following notion page and database is connected to the tests by default, and can be used as a template: https://infinitaslearning.notion.site/Notion-Github-Catalogue-ac2395eda37144e698e6b8faef1003c7
3233

src/github.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const getRepos = async () => {
1313

1414
const parseServiceDefinition = async (repo, path, pushMissing) => {
1515
const repoData = []
16-
core.debug(`Processing ${path} ...`)
16+
core.debug(`Processing ${path} in ${repo.name} ...`)
1717
try {
1818
const { data } = await octokit.request('GET /repos/{owner}/{repo}/contents/{path}', {
1919
owner: repo.full_name.split('/')[0],

src/local.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ const process = require('process')
44

55
process.env.INPUT_NOTION_TOKEN = process.env.NOTION_TOKEN
66
process.env.INPUT_GITHUB_TOKEN = process.env.GITHUB_TOKEN
7-
process.env.INPUT_REPOSITORY_TYPE = 'all'
7+
process.env.INPUT_REPOSITORY_TYPE = 'public'
88
process.env.INPUT_GITHUB_OWNER = 'infinitaslearning'
9-
process.env.INPUT_REPOSITORY_FILTER = 'notion-github-catalog'
9+
process.env.INPUT_REPOSITORY_FILTER = '.*'
1010
process.env.INPUT_DATABASE = 'cecaf0beb15945158d155866ff9acce8'
1111
process.env.INPUT_OWNER_DATABASE = '7943615f4dba43b3a3b0f991f4f7136d'
1212
process.env.INPUT_SYSTEM_DATABASE = '121534684fe840a1953500e603c2b602'

src/services.js

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ const core = require('@actions/core')
22
const { ensureLinks } = require('./links')
33
const { getDependsOn } = require('./depends')
44
const { 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

612
const 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

83118
exports.updateServices = updateServices

0 commit comments

Comments
 (0)