diff --git a/.github/workflows/import-ships.yml b/.github/workflows/import-ships.yml new file mode 100644 index 0000000000..a023857291 --- /dev/null +++ b/.github/workflows/import-ships.yml @@ -0,0 +1,52 @@ +# Import ship JSON files from Notion + +name: Import Ship JSON files from Notion + +on: + workflow_dispatch: + inputs: + ship_name: + description: The specific ship to update (leave blank for all ships) + default: '' + required: false + type: string + +permissions: + contents: write + pull-requests: write + +jobs: + import: + runs-on: ubuntu-latest + + steps: + # Setup the runtime environment + - uses: actions/checkout@v4 + - name: Setup dependencies + working-directory: scripts/notion-import + run: | + npm install + # Import the data from Notion + - name: Import ship data + working-directory: scripts/notion-import + env: + NOTION_TOKEN: ${{ secrets.NOTION_TOKEN }} + run: | + npm run import ${{ inputs.ship_name }} + # + - name: Create Pull Request + uses: peter-evans/create-pull-request@v7 + with: + add-paths: data/ships/*.json + branch: action/update-ship-json + commit-message: "auto-commit: import ship json files" + # Auto-generated pull request details + title: Update Ship JSON Files + body: | + Manually-triggered import of Ship JSON files from [Notion][1]. + + Auto-generated by the [import-ships Github Action][2]. + + [1]: https://bszlrd.notion.site/Pioneer-redesign-dashboard-b0ffc505ff3c4302b6ee873d75319c75 + [2]: https://github.com/pioneerspacesim/pioneer/actions/workflows/import-ships.yml + reviewers: bszlrd diff --git a/scripts/notion-import/.gitignore b/scripts/notion-import/.gitignore new file mode 100644 index 0000000000..5e5272738b --- /dev/null +++ b/scripts/notion-import/.gitignore @@ -0,0 +1,2 @@ +/node_modules +package-lock.json diff --git a/scripts/notion-import/notion-import.mjs b/scripts/notion-import/notion-import.mjs new file mode 100644 index 0000000000..8f4b9e8747 --- /dev/null +++ b/scripts/notion-import/notion-import.mjs @@ -0,0 +1,79 @@ +// Copyright © 2008-2025 Pioneer Developers. See AUTHORS.txt for details +// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt + +import { Client } from "@notionhq/client" +import path from "node:path" +import fs from "node:fs/promises" + +const notion = new Client({ auth: process.env.NOTION_TOKEN }) + +const pageId = "1018c82e283f80cdab99f35ef6e34211" + +// Developer toggle to test script +const doExport = true +// Log DB schema for manual run +// const db = await notion.databases.retrieve({ database_id: pageId }) +// console.log(db) + +let start_cursor = undefined + +const dataPath = path.resolve(path.join(import.meta.dirname, '../../data/ships')) + +console.log(`Writing ship JSON data files to path ${dataPath}`) + +const name_to_filter = process.argv[2] + +let filter_export = { + property: "i%3Au%3E", + checkbox: { equals: true } +} + +let filter_name = { + property: "title", + rich_text: { + equals: name_to_filter + } +} + +while (true) { + let { results, next_cursor, has_more } = await notion.databases.query({ + database_id: pageId, + start_cursor: start_cursor, + filter: name_to_filter ? { "and": [ filter_export, filter_name ] } : filter_export, + filter_properties: [ 'title', 'mSL%3A' /* model */, '_R%5DR' /* JSON (Copy) */ ] + }) + + for (const result of results) { + let name = result.properties['Name'].title[0].plain_text.toLowerCase() + // Replace spaces and parentheses with underscores, trim trailing underscores + name = name.replace(/[ \(\)]+/g, "_").replace(/_+$/, "") + + // FIXME: to avoid breaking savefile compatibility, we currently use the model name instead of ship name to construct the output filename + let customName = result.properties['model'].rich_text[0] + if (customName !== undefined) { + name = customName.plain_text + } else { + console.log(`${name} is missing model field`) + } + + const filePath = path.join(dataPath, name + '.json') + const json = result.properties['JSON (Copy)'].formula.string + '\n' + + try { + await fs.access(filePath) + console.log(`Importing ship file ${filePath}`) + } catch { + console.log(`Ship file ${filePath} not found - first import or incorrectly-named ship?`) + } + + if (doExport) + await fs.writeFile(filePath, json) + } + + start_cursor = next_cursor + + if (!has_more) + break; +} + +process.exit(0) diff --git a/scripts/notion-import/package.json b/scripts/notion-import/package.json new file mode 100644 index 0000000000..06cc0de5dc --- /dev/null +++ b/scripts/notion-import/package.json @@ -0,0 +1,14 @@ +{ + "name": "notion-import", + "version": "0.1.0", + "description": "Imports Pioneer ship data from Notion.so", + "main": "notion-import.mjs", + "scripts": { + "import": "node notion-import.mjs" + }, + "author": "Pioneer Space Sim", + "license": "GPL-3.0", + "dependencies": { + "@notionhq/client": "^2.2.15" + } +}