@@ -27184,6 +27184,9 @@ try {
2718427184 const NOTION_TOKEN = core.getInput('notion_token')
2718527185 const GITHUB_TOKEN = core.getInput('github_token')
2718627186 const database = core.getInput('database')
27187+ const systemDb = core.getInput('system_database')
27188+ const segmentDb = core.getInput('segment_database')
27189+ const teamDb = core.getInput('team_database')
2718727190 const owner = core.getInput('github_owner')
2718827191 const catalogFile = core.getInput('catalog_file') || 'catalog-info.yaml'
2718927192 const repositoryType = core.getInput('repository_type') || 'all'
@@ -27194,8 +27197,6 @@ try {
2719427197 logLevel: LogLevel.ERROR
2719527198 })
2719627199
27197- console.log(database, owner)
27198-
2719927200 const octokit = new Octokit({ auth: GITHUB_TOKEN })
2720027201
2720127202 const getRepos = async () => {
@@ -27232,7 +27233,60 @@ try {
2723227233 return repoData
2723327234 }
2723427235
27235- const createProperties = (repo) => {
27236+ const createProperties = (repo, { systems, segments, teams }) => {
27237+ let segment, team, system
27238+ const segmentAnnotation = repo?.metadata?.annotations?.segment
27239+ const systemAnnotation = repo?.metadata?.annotations?.system
27240+ const teamAnnotation = repo?.metadata?.annotations?.team
27241+
27242+ if (segments) {
27243+ // Segments are a relation
27244+ segment = {
27245+ relation: [
27246+ { id: segments[segmentAnnotation?.toLowerCase()] || segments.unknown }
27247+ ]
27248+ }
27249+ } else {
27250+ // Segments are a tag
27251+ segment = {
27252+ select: {
27253+ name: segmentAnnotation || 'Unknown'
27254+ }
27255+ }
27256+ }
27257+
27258+ if (teams) {
27259+ // Teams are a relation
27260+ team = {
27261+ relation: [
27262+ { id: teams[teamAnnotation?.toLowerCase()] || teams.unknown }
27263+ ]
27264+ }
27265+ } else {
27266+ // Teams are a tag
27267+ team = {
27268+ select: {
27269+ name: teamAnnotation || 'Unknown'
27270+ }
27271+ }
27272+ }
27273+
27274+ if (systems) {
27275+ // Segments are a relation
27276+ system = {
27277+ relation: [
27278+ { id: systems[systemAnnotation?.toLowerCase()] || systems.unknown }
27279+ ]
27280+ }
27281+ } else {
27282+ // Segments are a tag
27283+ system = {
27284+ select: {
27285+ name: systemAnnotation || 'Unknown'
27286+ }
27287+ }
27288+ }
27289+
2723627290 return {
2723727291 Name: {
2723827292 title: [
@@ -27260,16 +27314,9 @@ try {
2726027314 URL: {
2726127315 url: repo._repo.html_url
2726227316 },
27263- Segment: {
27264- select: {
27265- name: repo?.metadata?.annotations?.segment || 'Unknown'
27266- }
27267- },
27268- Team: {
27269- select: {
27270- name: repo?.metadata?.annotations?.team || 'Unknown'
27271- }
27272- },
27317+ Segment: segment,
27318+ Team: team,
27319+ System: system,
2727327320 Visibility: {
2727427321 select: {
2727527322 name: repo._repo.visibility
@@ -27296,31 +27343,31 @@ try {
2729627343 }
2729727344 }
2729827345
27299- const updateNotionRow = async (repo, pageId) => {
27346+ const updateNotionRow = async (repo, pageId, { systems, segments, teams } ) => {
2730027347 try {
2730127348 await notion.pages.update({
2730227349 page_id: pageId,
27303- properties: createProperties(repo)
27350+ properties: createProperties(repo, { systems, segments, teams } )
2730427351 })
2730527352 } catch (ex) {
2730627353 core.error(`Error updating notion document for ${repo._repo.name}: ${ex.message} ...`)
2730727354 }
2730827355 }
2730927356
27310- const createNotionRow = async (repo) => {
27357+ const createNotionRow = async (repo, { systems, segments, teams } ) => {
2731127358 try {
2731227359 await notion.pages.create({
2731327360 parent: {
2731427361 database_id: database
2731527362 },
27316- properties: createProperties(repo)
27363+ properties: createProperties(repo, { systems, segments, teams } )
2731727364 })
2731827365 } catch (ex) {
2731927366 core.error(`Error creating notion document for ${repo._repo.name}: ${ex.message} ...`)
2732027367 }
2732127368 }
2732227369
27323- const updateNotion = async (repositories) => {
27370+ const updateNotion = async (repositories, { systems, segments, teams } ) => {
2732427371 for (const repo of repositories) {
2732527372 // Lets see if we can find the row
2732627373 const search = await notion.databases.query({
@@ -27338,19 +27385,84 @@ try {
2733827385 // Lets just update the first one to not make the problem worse
2733927386 if (search.results.length > 0) {
2734027387 const pageId = search.results[0].id
27341- await updateNotionRow(repo, pageId)
27388+ await updateNotionRow(repo, pageId, { systems, segments, teams } )
2734227389 } else {
27343- await createNotionRow(repo)
27390+ await createNotionRow(repo, { systems, segments, teams } )
2734427391 }
2734527392 }
2734627393 }
2734727394
27395+ const loadData = async () => {
27396+ const processRows = (data) => {
27397+ const parent = {}
27398+ data.results.forEach((row) => {
27399+ const name = row.properties.Name.title[0].plain_text.toLowerCase()
27400+ if (name) parent[name] = row.id
27401+ })
27402+ return parent
27403+ }
27404+
27405+ let systemRows, segmentRows, teamRows
27406+
27407+ if (systemDb) {
27408+ systemRows = await notion.databases.query({
27409+ database_id: systemDb
27410+ })
27411+ }
27412+
27413+ if (segmentDb) {
27414+ segmentRows = await notion.databases.query({
27415+ database_id: segmentDb
27416+ })
27417+ }
27418+
27419+ if (teamDb) {
27420+ teamRows = await notion.databases.query({
27421+ database_id: teamDb
27422+ })
27423+ }
27424+
27425+ const systems = processRows(systemRows) || null
27426+ const teams = processRows(teamRows) || null
27427+ const segments = processRows(segmentRows) || null
27428+ let error = false
27429+
27430+ if (segmentDb && !segments.unknown) {
27431+ error = true
27432+ core.error('Your segment table does not contain an "unknown" row!')
27433+ }
27434+ if (teamDb && !teams.unknown) {
27435+ error = true
27436+ core.error('Your team table does not contain an "unknown" row!')
27437+ }
27438+ if (systemDb && !systems.unknown) {
27439+ error = true
27440+ core.error('Your system table does not contain an "unknown" row!')
27441+ }
27442+
27443+ if (error) {
27444+ process.exit(1)
27445+ }
27446+
27447+ return {
27448+ systems,
27449+ segments,
27450+ teams
27451+ }
27452+ }
27453+
2734827454 const refreshData = async () => {
27455+ core.startGroup('Loading systems, segments and teams')
27456+ const { systems, segments, teams } = await loadData()
27457+ core.info(`Loaded ${Object.keys(systems || {}).length} systems`)
27458+ core.info(`Loaded ${Object.keys(segments || {}).length} segments`)
27459+ core.info(`Loaded ${Object.keys(teams || {}).length} teams`)
27460+ core.endGroup()
2734927461 core.startGroup('🌀 Getting github repositories')
2735027462 const repositories = await getRepos()
2735127463 core.endGroup()
2735227464 core.startGroup(`✨ Updating notion with ${repositories.length} services ...`)
27353- await updateNotion(repositories)
27465+ await updateNotion(repositories, { systems, segments, teams } )
2735427466 core.endGroup()
2735527467 }
2735627468
0 commit comments