-
Notifications
You must be signed in to change notification settings - Fork 2
sync: standalone CLI engine with SQLite + bulk GraphQL #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
05ac4a3
packages: drop @github-dashboard/ prefix from workspace names
AntonNiklasson 4549555
sync: scaffold package (manifest, tsconfig, vitest)
AntonNiklasson 7186b62
sync(cache): SQLite cache file — open, schema, path
AntonNiklasson 103fa19
sync(cache): store repository over the cache
AntonNiklasson fc5ea97
sync(config): load instances from config.yml
AntonNiklasson 09d5dcd
sync(github): provider fetchers for PRs, reviews, notifications
AntonNiklasson 45fb1f7
sync(engine): once/loop orchestration with rate-limit guard
AntonNiklasson aaf6cd8
sync(cli): ghd-sync CLI (once/loop/status/wipe)
AntonNiklasson 18eb087
sync: public package surface
AntonNiklasson 37379b9
sync(config): cache resolved username across cycles
AntonNiklasson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| { | ||
| "name": "@github-dashboard/server", | ||
| "name": "server", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| { | ||
| "name": "sync", | ||
| "private": true, | ||
| "type": "module", | ||
| "bin": { | ||
| "ghd-sync": "./dist/cli.js" | ||
| }, | ||
| "exports": { | ||
| ".": { | ||
| "types": "./dist/index.d.ts", | ||
| "default": "./dist/index.js" | ||
| } | ||
| }, | ||
| "scripts": { | ||
| "dev": "tsx watch src/cli.ts", | ||
| "cli": "tsx src/cli.ts", | ||
| "build": "tsgo", | ||
| "typecheck": "tsgo --noEmit", | ||
| "test": "vitest run --passWithNoTests" | ||
| }, | ||
| "dependencies": { | ||
| "@octokit/rest": "^22.0.1", | ||
| "better-sqlite3": "^12.4.1", | ||
| "yaml": "^2.8.3", | ||
| "zod": "^4.4.3" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/better-sqlite3": "^7.6.13", | ||
| "@types/node": "^25.5.0", | ||
| "tsx": "^4.19.3", | ||
| "vitest": "^4.1.2" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import { mkdtempSync, rmSync } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import { afterEach, beforeEach, describe, expect, test } from "vitest"; | ||
| import { openCache, wipeCacheFile } from "./open.js"; | ||
| import { CACHE_SCHEMA_VERSION } from "./schema.js"; | ||
|
|
||
| describe("cache open", () => { | ||
| let cacheRoot: string; | ||
| let prevXdg: string | undefined; | ||
|
|
||
| beforeEach(() => { | ||
| cacheRoot = mkdtempSync(join(tmpdir(), "ghd-cache-")); | ||
| prevXdg = process.env.XDG_CACHE_HOME; | ||
| process.env.XDG_CACHE_HOME = cacheRoot; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| if (prevXdg === undefined) delete process.env.XDG_CACHE_HOME; | ||
| else process.env.XDG_CACHE_HOME = prevXdg; | ||
| rmSync(cacheRoot, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| test("creates file with current schema version on first open", () => { | ||
| const { db, path, wiped } = openCache(); | ||
| expect(wiped).toBe(false); | ||
| expect(path).toContain("github-dashboard/cache.sqlite"); | ||
| const row = db | ||
| .prepare("SELECT value FROM meta WHERE key = 'schema_version'") | ||
| .get() as { value: string }; | ||
| expect(Number(row.value)).toBe(CACHE_SCHEMA_VERSION); | ||
| db.close(); | ||
| }); | ||
|
|
||
| test("reuses existing file when version matches", () => { | ||
| const first = openCache(); | ||
| first.db | ||
| .prepare( | ||
| "INSERT INTO instances (id, label, base_url, username) VALUES ('x', 'X', 'https://api.github.com', 'u')", | ||
| ) | ||
| .run(); | ||
| first.db.close(); | ||
|
|
||
| const second = openCache(); | ||
| expect(second.wiped).toBe(false); | ||
| const count = ( | ||
| second.db.prepare("SELECT COUNT(*) AS n FROM instances").get() as { | ||
| n: number; | ||
| } | ||
| ).n; | ||
| expect(count).toBe(1); | ||
| second.db.close(); | ||
| }); | ||
|
|
||
| test("wipes and recreates when stored version doesn't match code version", () => { | ||
| const first = openCache(); | ||
| first.db | ||
| .prepare("UPDATE meta SET value = ? WHERE key = 'schema_version'") | ||
| .run("999"); | ||
| first.db | ||
| .prepare( | ||
| "INSERT INTO instances (id, label, base_url, username) VALUES ('x', 'X', 'https://api.github.com', 'u')", | ||
| ) | ||
| .run(); | ||
| first.db.close(); | ||
|
|
||
| const second = openCache(); | ||
| expect(second.wiped).toBe(true); | ||
| const count = ( | ||
| second.db.prepare("SELECT COUNT(*) AS n FROM instances").get() as { | ||
| n: number; | ||
| } | ||
| ).n; | ||
| expect(count).toBe(0); | ||
| second.db.close(); | ||
| }); | ||
|
|
||
| test("wipeCacheFile removes the file", () => { | ||
| const { db } = openCache(); | ||
| db.close(); | ||
| const result = wipeCacheFile(); | ||
| expect(result.existed).toBe(true); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import { existsSync, mkdirSync, rmSync } from "node:fs"; | ||
| import { dirname } from "node:path"; | ||
| import Database from "better-sqlite3"; | ||
| import { resolveCachePath } from "./path.js"; | ||
| import { CACHE_SCHEMA_VERSION, SCHEMA_DDL } from "./schema.js"; | ||
|
|
||
| export type Cache = Database.Database; | ||
|
|
||
| export interface OpenCacheResult { | ||
| db: Cache; | ||
| path: string; | ||
| wiped: boolean; | ||
| } | ||
|
|
||
| export function openCache(): OpenCacheResult { | ||
| const path = resolveCachePath(); | ||
| mkdirSync(dirname(path), { recursive: true }); | ||
|
|
||
| let db = new Database(path); | ||
| db.exec(SCHEMA_DDL); | ||
|
|
||
| const stored = readSchemaVersion(db); | ||
| if (stored !== null && stored !== CACHE_SCHEMA_VERSION) { | ||
| db.close(); | ||
| deleteCacheFiles(path); | ||
| db = new Database(path); | ||
| db.exec(SCHEMA_DDL); | ||
| writeSchemaVersion(db, CACHE_SCHEMA_VERSION); | ||
| return { db, path, wiped: true }; | ||
| } | ||
| if (stored === null) { | ||
| writeSchemaVersion(db, CACHE_SCHEMA_VERSION); | ||
| } | ||
| return { db, path, wiped: false }; | ||
| } | ||
|
|
||
| function readSchemaVersion(db: Cache): number | null { | ||
| const row = db | ||
| .prepare("SELECT value FROM meta WHERE key = 'schema_version'") | ||
| .get() as { value: string } | undefined; | ||
| if (!row) return null; | ||
| const n = Number.parseInt(row.value, 10); | ||
| return Number.isFinite(n) ? n : null; | ||
| } | ||
|
|
||
| function writeSchemaVersion(db: Cache, version: number): void { | ||
| db.prepare( | ||
| "INSERT OR REPLACE INTO meta (key, value) VALUES ('schema_version', ?)", | ||
| ).run(String(version)); | ||
| } | ||
|
|
||
| export function wipeCacheFile(): { existed: boolean; path: string } { | ||
| const path = resolveCachePath(); | ||
| const existed = existsSync(path); | ||
| deleteCacheFiles(path); | ||
| return { existed, path }; | ||
| } | ||
|
|
||
| function deleteCacheFiles(path: string): void { | ||
| for (const p of [path, `${path}-wal`, `${path}-shm`]) { | ||
| if (existsSync(p)) rmSync(p, { force: true }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { homedir } from "node:os"; | ||
| import { join } from "node:path"; | ||
|
|
||
| export function resolveCachePath(): string { | ||
| const xdg = process.env.XDG_CACHE_HOME?.trim(); | ||
| const base = | ||
| xdg && xdg.length > 0 | ||
| ? xdg | ||
| : process.env.HOME | ||
| ? join(homedir(), ".cache") | ||
| : null; | ||
| if (!base) { | ||
| throw new Error( | ||
| "cannot resolve cache path: neither $XDG_CACHE_HOME nor $HOME is set", | ||
| ); | ||
| } | ||
| return join(base, "github-dashboard", "cache.sqlite"); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| export const CACHE_SCHEMA_VERSION = 1; | ||
|
|
||
| export const SCHEMA_DDL = ` | ||
| PRAGMA journal_mode = WAL; | ||
| PRAGMA foreign_keys = ON; | ||
|
|
||
| CREATE TABLE IF NOT EXISTS meta ( | ||
| key TEXT PRIMARY KEY, | ||
| value TEXT NOT NULL | ||
| ); | ||
|
|
||
| CREATE TABLE IF NOT EXISTS instances ( | ||
| id TEXT PRIMARY KEY, | ||
| label TEXT NOT NULL, | ||
| base_url TEXT NOT NULL, | ||
| username TEXT NOT NULL | ||
| ); | ||
|
|
||
| CREATE TABLE IF NOT EXISTS prs ( | ||
| instance_id TEXT NOT NULL, | ||
| kind TEXT NOT NULL CHECK (kind IN ('authored', 'review_requested')), | ||
| provider_ref TEXT NOT NULL, | ||
| number INTEGER NOT NULL, | ||
| repo TEXT NOT NULL, | ||
| title TEXT NOT NULL, | ||
| author TEXT NOT NULL, | ||
| draft INTEGER NOT NULL, | ||
| ci_status TEXT NOT NULL, | ||
| in_merge_queue INTEGER NOT NULL, | ||
| auto_merge INTEGER NOT NULL, | ||
| unresolved_threads INTEGER NOT NULL, | ||
| additions INTEGER NOT NULL, | ||
| deletions INTEGER NOT NULL, | ||
| commits INTEGER NOT NULL, | ||
| comment_count INTEGER NOT NULL, | ||
| mergeable TEXT, | ||
| updated_at TEXT NOT NULL, | ||
| payload TEXT NOT NULL CHECK (json_valid(payload)), | ||
| PRIMARY KEY (instance_id, kind, provider_ref), | ||
| FOREIGN KEY (instance_id) REFERENCES instances(id) ON DELETE CASCADE | ||
| ); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS idx_prs_instance_kind ON prs(instance_id, kind); | ||
| CREATE INDEX IF NOT EXISTS idx_prs_updated ON prs(updated_at); | ||
|
|
||
| CREATE TABLE IF NOT EXISTS notifications ( | ||
| instance_id TEXT NOT NULL, | ||
| id TEXT NOT NULL, | ||
| title TEXT NOT NULL, | ||
| type TEXT, | ||
| reason TEXT NOT NULL, | ||
| repo TEXT NOT NULL, | ||
| url TEXT NOT NULL, | ||
| unread INTEGER NOT NULL, | ||
| updated_at TEXT NOT NULL, | ||
| PRIMARY KEY (instance_id, id), | ||
| FOREIGN KEY (instance_id) REFERENCES instances(id) ON DELETE CASCADE | ||
| ); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS idx_notifications_instance ON notifications(instance_id); | ||
|
|
||
| CREATE TABLE IF NOT EXISTS sync_state ( | ||
| instance_id TEXT NOT NULL, | ||
| kind TEXT NOT NULL, | ||
| last_run_at TEXT, | ||
| last_etag TEXT, | ||
| last_modified TEXT, | ||
| rate_remaining INTEGER, | ||
| rate_reset_at TEXT, | ||
| PRIMARY KEY (instance_id, kind), | ||
| FOREIGN KEY (instance_id) REFERENCES instances(id) ON DELETE CASCADE | ||
| ); | ||
| `; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.