From 238fe20709a1e502e37afa68f3800085c2c39d8c Mon Sep 17 00:00:00 2001 From: Jiu Liancheng Date: Thu, 22 Jul 2021 16:55:45 +0800 Subject: [PATCH] feat: add both full & partial sync --- .github/workflows/full-sync.yml | 30 +++++++++++++++++++ .../workflows/{blank.yml => partial-sync.yml} | 24 +++++++-------- libs/notion.ts | 7 ++++- main.ts | 5 ++-- 4 files changed, 49 insertions(+), 17 deletions(-) create mode 100644 .github/workflows/full-sync.yml rename .github/workflows/{blank.yml => partial-sync.yml} (70%) diff --git a/.github/workflows/full-sync.yml b/.github/workflows/full-sync.yml new file mode 100644 index 0000000..e9ccad6 --- /dev/null +++ b/.github/workflows/full-sync.yml @@ -0,0 +1,30 @@ +name: FullSync Notion Star + +on: + workflow_dispatch: + +jobs: + full-sync: + runs-on: ubuntu-latest + environment: notion-sync + steps: + - uses: actions/checkout@v2 + + - name: Setup environment + uses: actions/setup-node@v2.2.0 + with: + always-auth: false + node-version: 16.x + cache: yarn + + - run: yarn + + - name: Run Full Sync + env: + NOTION_API_KEY: ${{ secrets.NOTION_API_KEY }} + NOTION_DATABASE_ID: ${{ secrets.NOTION_DATABASE_ID }} + TOKEN_OF_GITHUB: ${{ secrets.TOKEN_OF_GITHUB }} + FULLSYNC_LIMIT: 2000 + PARTIALSYNC_LIMIT: 10 + SYNC_NOTION_FIRST: ${{ steps.cache.outputs.cache-hit != 'true' }} + run: npm start diff --git a/.github/workflows/blank.yml b/.github/workflows/partial-sync.yml similarity index 70% rename from .github/workflows/blank.yml rename to .github/workflows/partial-sync.yml index 9409a7c..faaf458 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/partial-sync.yml @@ -1,29 +1,25 @@ -# This is a basic workflow to help you get started with Actions -name: Sync Notion Star +name: Partial Sync Notion Star -# Controls when the workflow will run on: - workflow_dispatch: - + schedule: + - cron: '*/10 * * * *' jobs: - full-sync: + partial-sync: runs-on: ubuntu-latest environment: notion-sync steps: - uses: actions/checkout@v2 - - - name: Download artifact of last build + + - name: Download cache storage of last build uses: dawidd6/action-download-artifact@v2 continue-on-error: true with: - workflow: blank.yml + workflow: partial-sync.yml workflow_conclusion: success name: notion-sync-storage path: .cache - - - run: pwd && ls -al && ls .cache && cat .cache/notion-page.json - - name: Setup Node.js environment + - name: Setup environment uses: actions/setup-node@v2.2.0 with: always-auth: false @@ -32,7 +28,7 @@ jobs: - run: yarn - - name: Run Sync + - name: Run Partial Sync env: NOTION_API_KEY: ${{ secrets.NOTION_API_KEY }} NOTION_DATABASE_ID: ${{ secrets.NOTION_DATABASE_ID }} @@ -41,7 +37,7 @@ jobs: PARTIALSYNC_LIMIT: 10 SYNC_NOTION_FIRST: ${{ steps.cache.outputs.cache-hit != 'true' }} run: npm start - + - uses: actions/upload-artifact@v2 with: name: notion-sync-storage diff --git a/libs/notion.ts b/libs/notion.ts index 60e944b..d0c1665 100644 --- a/libs/notion.ts +++ b/libs/notion.ts @@ -34,7 +34,12 @@ export class Notion { /** * full-sync pages in database */ - async fullSync() { + async fullSyncIfNeeded() { + if (Object.keys(this.pages).length) { + console.log(`Notion: skipped sync due to cache`); + return; + } + console.log('Notion: Start to get all pages'); let hasNext = true; diff --git a/main.ts b/main.ts index a1256d2..5c38635 100644 --- a/main.ts +++ b/main.ts @@ -3,7 +3,7 @@ import { notion } from './libs/notion'; import assert from 'assert'; async function fullSync() { - await Promise.all([github.fullSync(), notion.fullSync()]); + await Promise.all([github.fullSync(), notion.fullSyncIfNeeded()]); for (const repo of github.repoList) { if (!notion.hasPage(repo.nameWithOwner)) { @@ -13,13 +13,14 @@ async function fullSync() { } async function partialSync() { - await github.getList(); + await Promise.all([github.getList(), notion.fullSyncIfNeeded()]); for (const repo of github.repoList) { if (notion.hasPage(repo.nameWithOwner)) { console.log(`Skip saved page ${repo.nameWithOwner}`); continue; } + await notion.insertPage(repo); } }