diff --git a/CHANGELOG.md b/CHANGELOG.md index dbedcd0e..e199826a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ Anything that changes what the dashboard, the admin or a widget can do gets a li rather than only the one that declared it. ### Added +- **GitHub rows open on the Mac.** Open a saved run, notification or pull request from a widget without marking it read. - **Local Docker and AI models.** Registry widgets can show up to 200 local containers and observed Ollama or MLX models. Fremkit refuses remote Docker contexts, distinguishes a missing CLI from a stopped daemon, and keeps the container list when stats fail. - **Borrow the Edge display.** Hide or show the dashboard from the helper menu without quitting it. Hidden state survives display rearrangements, and the mouse fence pauses while the Edge is borrowed. Touch and data collection continue; the dashboard reappears after a helper restart. - **Claude sessions: go to the session.** Touch a card and its window comes forward — the diff --git a/server/src/providers/github.ts b/server/src/providers/github.ts index 55f8f6a6..af2a3ece 100644 --- a/server/src/providers/github.ts +++ b/server/src/providers/github.ts @@ -1,4 +1,5 @@ import { z } from 'zod' +import { execFile } from 'node:child_process' import type { ConnectionProviderContext } from '../connections/types.js' import type { Provider } from './types.js' import { USER_AGENT } from '../version.js' @@ -352,7 +353,15 @@ const MarkAllReadPayload = z.object({ lastReadAt: z.string().refine((s) => Number.isFinite(Date.parse(s)), 'not a date').optional(), }) +const OpenRowPayload = z.object({ + // The host bridge stamps every widget command with its instance id. + instanceId: z.string().min(1).max(200).optional(), + kind: z.enum(['run', 'notification', 'review', 'pull']), + id: z.string().regex(/^\d{1,20}$/), +}) + export interface GithubDeps { + openRunner?: (cmd: string, args: string[]) => Promise fetchFn?: typeof fetch now?: () => number } @@ -366,6 +375,8 @@ export interface GithubDeps { export function createGithubProvider(ctx: ConnectionProviderContext, deps: GithubDeps = {}): Provider { const fetchFn = deps.fetchFn ?? fetch const now = deps.now ?? Date.now + const openRunner = deps.openRunner ?? ((cmd: string, args: string[]) => new Promise((resolve, reject) => + execFile(cmd, args, { timeout: 5000 }, err => err ? reject(err) : resolve()))) const base = githubBaseUrl(ctx.fields.host) const token = ctx.secrets.token ?? '' @@ -593,6 +604,27 @@ export function createGithubProvider(ctx: ConnectionProviderContext, deps: Githu }, commands: { + // The widget supplies an id, never a destination. Only rows already received for this + // connection can be opened, on its own web host, from the local dashboard. + open: async (payload, context) => { + if (!context?.loopback) return { ok: false, error: tr(undefined, 'provider.localOnly') } + const parsed = OpenRowPayload.safeParse(payload) + if (!parsed.success) throw new Error(tr(undefined, 'github.invalidCommand')) + const { kind, id } = parsed.data + const rows = kind === 'run' ? last.runs : kind === 'notification' ? last.notifications + : kind === 'review' ? last.reviewRequests : last.myPrs + const row = rows.find(item => item.id === id) + if (!row || !base || !OWNER_REPO_RE.test(row.repo)) return { ok: false } + let url: URL + try { url = new URL(row.url) } catch { return { ok: false } } + const host = base === GITHUB_DEFAULT_HOST ? 'github.com' : new URL(base).hostname + // Opening a snapshot URL on a non-standard port is deliberately unsupported, + // including for Enterprise hosts, until the configured API and web ports are paired. + if (url.protocol !== 'https:' || url.hostname !== host || url.username || url.password || url.port || + !(url.pathname === `/${row.repo}` || url.pathname.startsWith(`/${row.repo}/`))) return { ok: false } + try { await openRunner('/usr/bin/open', [url.href]); return { ok: true } } + catch { return { ok: false } } + }, /** Marks one notification thread read — what a tap on an inbox row asks for. */ markRead: async (payload) => { const parsed = MarkReadPayload.safeParse(payload) diff --git a/server/test/github.test.ts b/server/test/github.test.ts index 0d834aa8..74fd6f32 100644 --- a/server/test/github.test.ts +++ b/server/test/github.test.ts @@ -14,6 +14,7 @@ import { } from '../src/providers/github.js' import { githubType } from '../src/connections/types/github.js' import { OptionsError } from '../src/connections/types.js' +import { tr } from '../src/i18n.js' const fixture = (name: string): unknown => JSON.parse(readFileSync(fileURLToPath(new URL(`./fixtures/${name}.json`, import.meta.url)), 'utf8')) @@ -526,3 +527,47 @@ describe('githubType', () => { .rejects.toBeInstanceOf(OptionsError) }) }) + + +describe('opening GitHub rows', () => { + it('opens the saved run URL locally, without making a GitHub write', async () => { + const { fetchFn, calls } = responder() + const opened: string[][] = [] + const provider = createGithubProvider(context(), { fetchFn, openRunner: async (cmd, args) => { opened.push([cmd, ...args]) } }) + const snapshot = await provider.poll!() as GhSnapshot + expect(await provider.commands!.open({ kind: 'run', id: snapshot.runs[0].id, instanceId: 'github-actions-1' }, { loopback: true })).toEqual({ ok: true }) + expect(opened).toEqual([['/usr/bin/open', snapshot.runs[0].url]]) + expect(calls.every(c => c.method === 'GET')).toBe(true) + }) + it('opens a notification and a review without marking them read', async () => { + const { fetchFn, calls } = responder() + const opened: string[][] = [] + const provider = createGithubProvider(context(), { fetchFn, openRunner: async (cmd, args) => { opened.push([cmd, ...args]) } }) + const snapshot = await provider.poll!() as GhSnapshot + for (const [kind, item] of [['notification', snapshot.notifications[0]], ['review', snapshot.reviewRequests[0]], ['pull', snapshot.myPrs[0]]] as const) { + expect(await provider.commands!.open({ kind, id: item.id }, { loopback: true })).toEqual({ ok: true }) + expect(opened.at(-1)).toEqual(['/usr/bin/open', item.url]) + } + expect(calls.every(c => c.method === 'GET')).toBe(true) + }) + it('refuses remote callers, unknown ids and foreign response hosts', async () => { + const { fetchFn } = responder() + const opened: string[][] = [] + const provider = createGithubProvider(context(), { fetchFn, openRunner: async (cmd, args) => { opened.push([cmd, ...args]) } }) + const snapshot = await provider.poll!() as GhSnapshot + expect(await provider.commands!.open({ kind: 'run', id: snapshot.runs[0].id }, { loopback: false })).toEqual({ ok: false, error: tr(undefined, 'provider.localOnly') }) + expect(await provider.commands!.open({ kind: 'run', id: '999999' }, { loopback: true })).toEqual({ ok: false }) + const foreign = snapshot.notifications.find(n => n.url.includes('github.example.com'))! + expect(await provider.commands!.open({ kind: 'notification', id: foreign.id }, { loopback: true })).toEqual({ ok: false }) + expect(await provider.commands!.open({ kind: 'run', id: snapshot.runs[0].id })).toEqual({ ok: false, error: tr(undefined, 'provider.localOnly') }) + expect(opened).toEqual([]) + }) + it('ignores extra host-stamped fields and never uses a caller-supplied URL', async () => { + const { fetchFn } = responder() + const opened: string[][] = [] + const provider = createGithubProvider(context(), { fetchFn, openRunner: async (cmd, args) => { opened.push([cmd, ...args]) } }) + const snapshot = await provider.poll!() as GhSnapshot + expect(await provider.commands!.open({ kind: 'run', id: snapshot.runs[0].id, url: 'https://evil.test', futureHostField: true }, { loopback: true })).toEqual({ ok: true }) + expect(opened).toEqual([['/usr/bin/open', snapshot.runs[0].url]]) + }) +})