11const core = require ( '@actions/core' )
22const { Client, LogLevel } = require ( '@notionhq/client' )
33const { Octokit } = require ( 'octokit' )
4+ const YAML = require ( 'yaml' )
5+ const { markdownToBlocks } = require ( '@tryfabric/martian' )
46
57try {
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 ...' )
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 }
0 commit comments