Skip to content

Commit 51efd1c

Browse files
committed
Refactored, added backstage yaml
1 parent 58d3bae commit 51efd1c

8 files changed

Lines changed: 169 additions & 81 deletions

File tree

action.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ inputs:
1616
description: 'Parent database to add to'
1717
required: true
1818
repository_type:
19-
description: 'Github repository type (e.g. all|private|internal)'
19+
description: 'Github repository type (e.g. all|private|internal), defaults to `all`'
20+
catalog_file:
21+
description: 'Catalog file name to look for in the root of repo, defaults to `catalog_file.yaml`'
2022
outputs:
2123
status:
2224
description: 'The status of the scan'

catalog-info.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: backstage.io/v1alpha1
2+
kind: Component
3+
metadata:
4+
name: notion-github-catalog
5+
description: A githb action to keep a notion database in sync with Github repository info
6+
annotations:
7+
segment: general
8+
team: all
9+
tags:
10+
- notion
11+
- github
12+
- action
13+
spec:
14+
type: action
15+
lifecycle: production
16+
owner: opensource

package-lock.json

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"@actions/github": "^5.0.0",
2020
"@notionhq/client": "^0.4.11",
2121
"@tryfabric/martian": "^1.1.1",
22-
"octokit": "^1.7.1"
22+
"octokit": "^1.7.1",
23+
"yaml": "^1.10.2"
2324
},
2425
"devDependencies": {
2526
"eslint": "^7.32.0",

readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ By default integrations cant access any contentm so you you *must* share your da
1313
This action expects a Notion database with the following properties:
1414

1515
- Name: text
16+
- Description: text
1617
- URL: url
1718
- Segment: select
1819
- Team: select
1920
- Tags: multi_select
21+
- Visibility: select
22+
- Language: select
23+
- Status: select
2024
- Updated: date
2125

2226
It looks like this after it has run:

src/index.js

Lines changed: 125 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
const core = require('@actions/core')
22
const { Client, LogLevel } = require('@notionhq/client')
33
const { Octokit } = require('octokit')
4+
const YAML = require('yaml')
5+
const { markdownToBlocks } = require('@tryfabric/martian')
46

57
try {
68
const NOTION_TOKEN = core.getInput('notion_token')
79
const GITHUB_TOKEN = core.getInput('github_token')
810
const database = core.getInput('database')
911
const owner = core.getInput('github_owner')
12+
const catalogFile = core.getInput('catalog_file') || 'catalog-info.yaml'
1013
const repositoryType = core.getInput('repository_type') || 'all'
1114

1215
core.debug('Creating notion client ...')
@@ -15,6 +18,8 @@ try {
1518
logLevel: LogLevel.ERROR
1619
})
1720

21+
console.log(database, owner)
22+
1823
const octokit = new Octokit({ auth: GITHUB_TOKEN })
1924

2025
const getRepos = async () => {
@@ -26,37 +31,144 @@ try {
2631
per_page: 100
2732
})
2833
core.info(`Found ${repos.length} github repositories, now getting service data ...`)
29-
const repoData = []
30-
for (const repo of repos) {
34+
const repoData = []
35+
for (const repo of repos) {
3136
try {
3237
const { data } = await octokit.request('GET /repos/{owner}/{repo}/contents/{path}', {
3338
owner: repo.full_name.split('/')[0],
3439
repo: repo.name,
35-
path: 'service.json'
40+
path: catalogFile
3641
})
3742
if (data) {
3843
const base64content = Buffer.from(data.content, 'base64')
39-
const serviceJson = JSON.parse(base64content.toString('utf8'))
40-
serviceJson._repo = repo
41-
repoData.push(serviceJson)
44+
const serviceDefintion = YAML.parse(base64content.toString('utf8'))
45+
serviceDefintion._repo = repo
46+
serviceDefintion.status = 'OK'
47+
repoData.push(serviceDefintion)
4248
}
4349
} catch (ex) {
4450
repoData.push({
4551
segment: 'Unknown',
4652
team: 'Unknown',
47-
status: 'No service.json found',
53+
status: `${catalogFile} missing`,
4854
_repo: repo
4955
})
5056
}
5157
}
5258
return repoData
5359
}
5460

55-
const updateNotion = async (repositories) => {
56-
for (const repo of repositories) {
57-
// Date to log as updated
58-
const date = new Date().toISOString()
61+
const updateNotionRow = async (repo, pageId) => {
62+
try {
63+
await notion.pages.update({
64+
page_id: pageId,
65+
properties: {
66+
Name: {
67+
title: [
68+
{
69+
text: {
70+
content: repo._repo.name
71+
}
72+
}
73+
]
74+
},
75+
Description: {
76+
rich_text: [
77+
{
78+
text: {
79+
content: repo._repo.description || repo._repo.name
80+
}
81+
}
82+
]
83+
},
84+
URL: {
85+
url: repo._repo.html_url
86+
},
87+
Segment: {
88+
select: {
89+
name: repo.segment
90+
}
91+
},
92+
Team: {
93+
select: {
94+
name: repo.team
95+
}
96+
},
97+
Visibility: {
98+
select: {
99+
name: repo._repo.visibility
100+
}
101+
},
102+
Language: {
103+
select: {
104+
name: repo._repo.language || 'Unknown'
105+
}
106+
},
107+
Status: {
108+
select: {
109+
name: repo.status
110+
}
111+
},
112+
Updated: {
113+
date: {
114+
start: new Date().toISOString()
115+
}
116+
}
117+
}
118+
})
119+
} catch(ex) {
120+
core.error(`Error updating notion document for ${repo._repo.name}: ${ex.message} ...`);
121+
}
122+
}
123+
124+
const createNotionRow = async (repo) => {
125+
try {
126+
await notion.pages.create({
127+
parent: {
128+
database_id: database
129+
},
130+
properties: {
131+
Name: {
132+
title: [
133+
{
134+
text: {
135+
content: repo._repo.name
136+
}
137+
}
138+
]
139+
},
140+
URL: {
141+
url: repo._repo.html_url
142+
},
143+
Segment: {
144+
select: {
145+
name: repo.segment
146+
}
147+
},
148+
Team: {
149+
select: {
150+
name: repo.team
151+
}
152+
},
153+
Status: {
154+
select: {
155+
name: repo.status
156+
}
157+
},
158+
Updated: {
159+
date: {
160+
start: new Date().toISOString()
161+
}
162+
}
163+
}
164+
})
165+
} catch(ex) {
166+
core.error(`Error creating notion document for ${repo._repo.name}: ${ex.message} ...`);
167+
}
168+
}
59169

170+
const updateNotion = async (repositories) => {
171+
for (const repo of repositories) {
60172
// Lets see if we can find the row
61173
const search = await notion.databases.query({
62174
database_id: database,
@@ -73,73 +185,9 @@ try {
73185
// Lets just update the first one to not make the problem worse
74186
if (search.results.length > 0) {
75187
const pageId = search.results[0].id
76-
await notion.pages.update({
77-
page_id: pageId,
78-
properties: {
79-
Name: {
80-
title: [
81-
{
82-
text: {
83-
content: repo._repo.name
84-
}
85-
}
86-
]
87-
},
88-
URL: {
89-
url: repo._repo.html_url
90-
},
91-
Segment: {
92-
select: {
93-
name: repo.segment
94-
}
95-
},
96-
Team: {
97-
select: {
98-
name: repo.team
99-
}
100-
},
101-
Updated: {
102-
date: {
103-
start: date
104-
}
105-
}
106-
}
107-
})
188+
await updateNotionRow(repo, pageId)
108189
} else {
109-
await notion.pages.create({
110-
parent: {
111-
database_id: database
112-
},
113-
properties: {
114-
Name: {
115-
title: [
116-
{
117-
text: {
118-
content: repo._repo.name
119-
}
120-
}
121-
]
122-
},
123-
URL: {
124-
url: repo._repo.html_url
125-
},
126-
Segment: {
127-
select: {
128-
name: repo.segment
129-
}
130-
},
131-
Team: {
132-
select: {
133-
name: repo.team
134-
}
135-
},
136-
Updated: {
137-
date: {
138-
start: date
139-
}
140-
}
141-
}
142-
})
190+
await createNotionRow(repo)
143191
}
144192
}
145193
}

src/index.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ test('complete input should succeed with default inputs', () => {
1717
const result = cp.execSync(`node ${ip}`, options).toString()
1818
expect(result).toBeDefined()
1919
})
20+
s

src/local.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ const { spawn } = require('child_process')
22
const path = require('path')
33
const process = require('process')
44

5-
process.env.INPUT_DATABASE = '2b26b4290cc84d95ad3e93c3255277a1'
5+
process.env.INPUT_DATABASE = 'cecaf0beb15945158d155866ff9acce8' //'2b26b4290cc84d95ad3e93c3255277a1'
66
process.env.INPUT_NOTION_TOKEN = process.env.NOTION_TOKEN
77
process.env.INPUT_GITHUB_TOKEN = process.env.GITHUB_TOKEN
8+
process.env.INPUT_REPOSITORY_TYPE = 'public'
9+
process.env.INPUT_GITHUB_OWNER = 'infinitaslearning'
810

911
const ip = path.join(__dirname, 'index.js')
1012
const options = {

0 commit comments

Comments
 (0)