From e196923362b4f378489ed90cef4872b56a955781 Mon Sep 17 00:00:00 2001 From: Heewon Oh Date: Sun, 15 Nov 2026 06:50:08 +0900 Subject: [PATCH] feat(web): search and clone GitHub repositories from the Add repository dialog (AGT-4005) The supervisor page's Add repository dialog gets a "Connect GitHub repo" section that consumes the existing GET /api/github/repos?q= and POST /api/repos/clone endpoints (#581). - Debounced search with stale-response protection; results show the full name, a private badge, and a cloned badge in place of the Clone button. - Per-row clone progress (Cloning..., then cloned or the error with Retry); a missing GH_TOKEN (503) is explained instead of rendering an empty list. - On success the Repositories panel and local picker list are reloaded via reloadProjectList() in supervisor.js. - GitHub names are written with textContent/dataset only. Requests go through the webToken.js fetch seam like every other dashboard call. The issue's file scope (dashboardHtml.ts) predates the static supervisor page redesign, so the change lives in web/static instead. --- src/support/githubConnectBrowser.test.ts | 133 ++++++++++++++ web/static/css/supervisor.css | 12 ++ web/static/js/githubConnect.js | 213 +++++++++++++++++++++++ web/static/js/supervisor.js | 13 ++ web/static/supervisor.html | 9 + 5 files changed, 380 insertions(+) create mode 100644 src/support/githubConnectBrowser.test.ts create mode 100644 web/static/js/githubConnect.js diff --git a/src/support/githubConnectBrowser.test.ts b/src/support/githubConnectBrowser.test.ts new file mode 100644 index 00000000..e04cbe3a --- /dev/null +++ b/src/support/githubConnectBrowser.test.ts @@ -0,0 +1,133 @@ +// @vitest-environment jsdom +// +// AGT-4005: the Add-repository dialog can search the operator's GitHub +// repositories and clone one. These tests pin the API contract it consumes, +// the per-row clone state, and that GitHub-supplied names never reach HTML. + +import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'; + +interface GithubConnectApi { + searchGithubRepos(query: string): Promise; + cloneGithubRepo(button: HTMLButtonElement): Promise; + toggleGithubConnect(): void; +} + +const json = (status: number, body: unknown) => + new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json' } }); + +const fetchMock = vi.fn<(input: RequestInfo | URL, init?: RequestInit) => Promise>(); +const reloadProjectList = vi.fn(async () => undefined); +let api: GithubConnectApi; + +beforeAll(async () => { + document.body.innerHTML = ` + + `; + vi.stubGlobal('fetch', fetchMock); + (window as unknown as { reloadProjectList: typeof reloadProjectList }).reloadProjectList = reloadProjectList; + await import('../../web/static/js/githubConnect.js'); + api = (window as unknown as { OpenSwarmGithubConnect: GithubConnectApi }).OpenSwarmGithubConnect; +}); + +beforeEach(() => { + fetchMock.mockReset(); + reloadProjectList.mockClear(); + document.getElementById('gh-repo-list')!.replaceChildren(); +}); + +const rows = () => [...document.querySelectorAll('#gh-repo-list .gh-repo')]; +const status = () => document.getElementById('gh-status')!; + +describe('GitHub repository search (AGT-4005)', () => { + it('requests /api/github/repos with an encoded query and renders identity, private and cloned state', async () => { + fetchMock.mockResolvedValueOnce(json(200, [ + { fullName: 'acme/app', private: true, cloned: false, defaultBranch: 'main', updatedAt: '' }, + { fullName: 'acme/lib', private: false, cloned: true, defaultBranch: 'main', updatedAt: '' }, + ])); + await api.searchGithubRepos('a&b c'); + + expect(String(fetchMock.mock.calls[0][0])).toBe('/api/github/repos?q=a%26b%20c'); + const [app, lib] = rows(); + expect(app.querySelector('.repo-item-name')!.textContent).toBe('acme/app'); + expect(app.textContent).toContain('private'); + expect(app.querySelector('button.gh-clone')).not.toBeNull(); + // An already-cloned repository offers no clone action. + expect(lib.textContent).toContain('cloned'); + expect(lib.querySelector('button.gh-clone')).toBeNull(); + }); + + it('renders a hostile repository name as text, never as markup', async () => { + fetchMock.mockResolvedValueOnce(json(200, [ + { fullName: '/x', private: false, cloned: false }, + ])); + await api.searchGithubRepos(''); + expect(document.querySelector('#gh-repo-list img')).toBeNull(); + expect(rows()[0].querySelector('.repo-item-name')!.textContent).toBe('/x'); + }); + + it('explains a missing GH_TOKEN instead of showing an empty list', async () => { + fetchMock.mockResolvedValueOnce(json(503, { error: 'GH_TOKEN not configured' })); + await api.searchGithubRepos(''); + expect(status().hidden).toBe(false); + expect(status().textContent).toContain('GH_TOKEN'); + expect(status().dataset.tone).toBe('danger'); + }); + + it('drops a stale response that resolves after a newer search', async () => { + let releaseFirst!: (r: Response) => void; + fetchMock + .mockImplementationOnce(() => new Promise((resolve) => { releaseFirst = resolve; })) + .mockResolvedValueOnce(json(200, [{ fullName: 'acme/new', private: false, cloned: false }])); + const first = api.searchGithubRepos('old'); + await api.searchGithubRepos('new'); + releaseFirst(json(200, [{ fullName: 'acme/old', private: false, cloned: false }])); + await first; + expect(rows().map((r) => r.dataset.fullName)).toEqual(['acme/new']); + }); +}); + +describe('GitHub repository clone (AGT-4005)', () => { + async function renderOne() { + fetchMock.mockResolvedValueOnce(json(200, [{ fullName: 'acme/app', private: false, cloned: false }])); + await api.searchGithubRepos(''); + return rows()[0].querySelector('button.gh-clone')!; + } + + it('POSTs { fullName }, shows progress, then marks the row cloned and refreshes the repository panel', async () => { + const button = await renderOne(); + let release!: (r: Response) => void; + fetchMock.mockImplementationOnce(() => new Promise((resolve) => { release = resolve; })); + + const pending = api.cloneGithubRepo(button); + expect(button.disabled).toBe(true); + expect(button.textContent).toBe('Cloning…'); + const [url, init] = fetchMock.mock.calls[1]; + expect(String(url)).toBe('/api/repos/clone'); + expect(init?.method).toBe('POST'); + expect(JSON.parse(String(init?.body))).toEqual({ fullName: 'acme/app' }); + + release(json(201, { fullName: 'acme/app', destination: '/work/acme/app' })); + await pending; + const row = rows()[0]; + expect(row.dataset.state).toBe('cloned'); + expect(row.querySelector('button.gh-clone')).toBeNull(); + expect(row.textContent).toContain('/work/acme/app'); + expect(reloadProjectList).toHaveBeenCalledTimes(1); + }); + + it('shows the failure on the row and re-enables the action', async () => { + const button = await renderOne(); + fetchMock.mockResolvedValueOnce(json(502, { error: 'Git clone failed' })); + await api.cloneGithubRepo(button); + const row = rows()[0]; + expect(row.dataset.state).toBe('failed'); + expect(row.querySelector('.gh-repo-note')!.textContent).toBe('Git clone failed'); + expect(button.disabled).toBe(false); + expect(button.textContent).toBe('Retry'); + expect(reloadProjectList).not.toHaveBeenCalled(); + }); +}); diff --git a/web/static/css/supervisor.css b/web/static/css/supervisor.css index 81444183..bc97c151 100644 --- a/web/static/css/supervisor.css +++ b/web/static/css/supervisor.css @@ -569,6 +569,18 @@ tr.is-failed .c-title { text-decoration: line-through; text-decoration-color: va .repo-item-path { flex: 1; color: var(--fg-muted); font-family: var(--font-mono); font-size: var(--text-xs); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .repo-item-badge { flex-shrink: 0; padding: 0 var(--space-2); border: 1px solid var(--border); border-radius: var(--radius-full); font-size: var(--text-xs); color: var(--fg-muted); } .fb-row { border-color: var(--border) !important; } + +/* Connect GitHub repo (AGT-4005): search + clone inside the Add dialog. */ +.gh-connect-foot #gh-connect { margin-top: var(--space-2); } +.gh-connect-foot #gh-search { width: 100%; } +.gh-repo-list { max-height: 240px; overflow-y: auto; margin-top: var(--space-2); } +.gh-repo { cursor: default; padding-left: 0; padding-right: 0; } +.gh-repo:hover { background: transparent; } +.gh-repo-text { flex: 1; min-width: 0; } +.gh-repo[data-state="failed"] .gh-repo-note { color: var(--danger); white-space: normal; } +.gh-status { margin: var(--space-2) 0 0; font-size: var(--text-xs); color: var(--fg-muted); } +.gh-status[data-tone="danger"] { color: var(--danger); } +.fb-row { border-color: var(--border) !important; } .fb-row:hover { background: var(--bg-hover); } .scan-path-row { display: flex; align-items: center; gap: var(--space-2); padding: var(--space-1) 0; font-size: var(--text-xs); } diff --git a/web/static/js/githubConnect.js b/web/static/js/githubConnect.js new file mode 100644 index 00000000..8817cff8 --- /dev/null +++ b/web/static/js/githubConnect.js @@ -0,0 +1,213 @@ +// GitHub repository search and clone, inside the "Add repository" dialog +// (AGT-4005). Consumes GET /api/github/repos?q= and POST /api/repos/clone. +// +// A classic script so supervisor.html can load it beside supervisor.js. Every +// fetch goes through the webToken.js seam, which adds the session token to +// same-origin API calls, so nothing here handles credentials. +// +// GitHub names are untrusted: they are written with textContent / dataset, +// never interpolated into HTML. +(function () { + 'use strict'; + + const SEARCH_DEBOUNCE_MS = 250; + const MAX_ROWS = 50; + + let searchSeq = 0; + let debounceTimer = null; + let loaded = false; + + function el(id) { + return document.getElementById(id); + } + + /** Readable message for a failed API response; never echoes a stack. */ + function errorMessage(status, body) { + if (status === 503 && body && body.error === 'GH_TOKEN not configured') { + return 'GitHub is not connected: set GH_TOKEN for the daemon.'; + } + if (status === 409) return 'Already cloned at the destination.'; + const detail = body && typeof body.error === 'string' ? body.error : 'HTTP ' + status; + return detail.slice(0, 200); + } + + async function readJson(res) { + try { + return await res.json(); + } catch { + return null; + } + } + + function setStatus(text, tone) { + const status = el('gh-status'); + if (!status) return; + status.textContent = text || ''; + status.dataset.tone = tone || ''; + status.hidden = !text; + } + + function badge(text, cls) { + const span = document.createElement('span'); + span.className = 'repo-item-badge' + (cls ? ' ' + cls : ''); + span.textContent = text; + return span; + } + + function renderRepos(repos) { + const list = el('gh-repo-list'); + if (!list) return; + list.replaceChildren(); + if (!repos.length) { + const empty = document.createElement('div'); + empty.className = 'empty'; + empty.textContent = 'No matching GitHub repositories'; + list.appendChild(empty); + return; + } + for (const repo of repos.slice(0, MAX_ROWS)) { + const row = document.createElement('div'); + row.className = 'repo-item gh-repo'; + row.dataset.fullName = repo.fullName; + + const text = document.createElement('div'); + text.className = 'gh-repo-text'; + const name = document.createElement('div'); + name.className = 'repo-item-name'; + name.textContent = repo.fullName; + text.appendChild(name); + const note = document.createElement('div'); + note.className = 'repo-item-path gh-repo-note'; + text.appendChild(note); + row.appendChild(text); + + if (repo.private) row.appendChild(badge('private', 'is-private')); + if (repo.cloned) { + row.appendChild(badge('cloned', 'is-cloned')); + } else { + const button = document.createElement('button'); + button.type = 'button'; + button.className = 'btn btn-secondary btn-sm gh-clone'; + button.textContent = 'Clone'; + button.dataset.fullName = repo.fullName; + row.appendChild(button); + } + list.appendChild(row); + } + } + + async function searchGithubRepos(query) { + const seq = ++searchSeq; + setStatus('Searching GitHub…', 'muted'); + const q = String(query || '').trim(); + const url = '/api/github/repos' + (q ? '?q=' + encodeURIComponent(q) : ''); + try { + const res = await fetch(url); + const body = await readJson(res); + if (seq !== searchSeq) return; // a newer search superseded this one + if (!res.ok) { + renderRepos([]); + setStatus(errorMessage(res.status, body), 'danger'); + return; + } + const repos = Array.isArray(body) ? body.filter(function (r) { + return r && typeof r.fullName === 'string'; + }) : []; + renderRepos(repos); + setStatus(repos.length > MAX_ROWS ? 'Showing the first ' + MAX_ROWS + '; refine the search.' : '', 'muted'); + } catch (e) { + if (seq !== searchSeq) return; + renderRepos([]); + setStatus('GitHub search failed: ' + String(e && e.message || e).slice(0, 200), 'danger'); + } + } + + function scheduleSearch(query) { + if (debounceTimer) clearTimeout(debounceTimer); + debounceTimer = setTimeout(function () { + debounceTimer = null; + void searchGithubRepos(query); + }, SEARCH_DEBOUNCE_MS); + } + + async function cloneGithubRepo(button) { + const fullName = button.dataset.fullName; + if (!fullName || button.disabled) return; + const row = button.closest('.gh-repo'); + const note = row && row.querySelector('.gh-repo-note'); + button.disabled = true; + button.textContent = 'Cloning…'; + if (note) note.textContent = ''; + try { + const res = await fetch('/api/repos/clone', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ fullName: fullName }), + }); + const body = await readJson(res); + if (!res.ok) { + if (note) note.textContent = errorMessage(res.status, body); + if (row) row.dataset.state = 'failed'; + button.disabled = false; + button.textContent = 'Retry'; + return; + } + if (row) row.dataset.state = 'cloned'; + button.replaceWith(badge('cloned', 'is-cloned')); + if (note) note.textContent = body && typeof body.destination === 'string' ? body.destination : 'Cloned'; + // supervisor.js owns the Repositories panel; refresh it when present. + if (typeof window.reloadProjectList === 'function') { + try { await window.reloadProjectList(); } catch { /* panel refresh is best effort */ } + } + } catch (e) { + if (note) note.textContent = 'Clone failed: ' + String(e && e.message || e).slice(0, 200); + if (row) row.dataset.state = 'failed'; + button.disabled = false; + button.textContent = 'Retry'; + } + } + + function toggleGithubConnect() { + const section = el('gh-connect'); + if (!section) return; + section.hidden = !section.hidden; + const toggle = el('gh-connect-toggle'); + if (toggle) toggle.setAttribute('aria-expanded', String(!section.hidden)); + if (!section.hidden) { + const input = el('gh-search'); + if (input) input.focus(); + if (!loaded) { + loaded = true; + void searchGithubRepos(input ? input.value : ''); + } + } + } + + function bind() { + const toggle = el('gh-connect-toggle'); + if (toggle) toggle.addEventListener('click', toggleGithubConnect); + const input = el('gh-search'); + if (input) input.addEventListener('input', function () { scheduleSearch(input.value); }); + const list = el('gh-repo-list'); + if (list) { + list.addEventListener('click', function (event) { + const target = event.target; + const button = target && target.closest ? target.closest('button.gh-clone') : null; + if (button) void cloneGithubRepo(button); + }); + } + } + + window.OpenSwarmGithubConnect = { + searchGithubRepos: searchGithubRepos, + cloneGithubRepo: cloneGithubRepo, + toggleGithubConnect: toggleGithubConnect, + bind: bind, + }; + + if (document.readyState === 'loading') { + document.addEventListener('DOMContentLoaded', bind); + } else { + bind(); + } +})(); diff --git a/web/static/js/supervisor.js b/web/static/js/supervisor.js index b6cb127f..6a7a5288 100644 --- a/web/static/js/supervisor.js +++ b/web/static/js/supervisor.js @@ -1283,6 +1283,19 @@ function filterRepos(q) { }).join(""); } +// Re-read the Repositories panel and the local picker list. githubConnect.js +// calls this after a clone registers a new repository (AGT-4005). +async function reloadProjectList() { + const res = await fetch("/api/projects"); + projects = await res.json(); + renderProjects(); + if (pickerOpen) { + const local = await fetch("/api/local-projects"); + allLocalProjects = await local.json(); + filterRepos(document.getElementById("repo-search").value); + } +} + async function pickRepo(el) { const path = el.getAttribute("data-path"); if (!path) return; diff --git a/web/static/supervisor.html b/web/static/supervisor.html index 6aebc8ae..c173c714 100644 --- a/web/static/supervisor.html +++ b/web/static/supervisor.html @@ -190,6 +190,14 @@

Add repository

oninput="filterRepos(this.value)" onkeydown="if(event.key==='Escape')closeRepoPicker()">
+
+ + +
Scan paths
@@ -227,6 +235,7 @@

Choose folder to scan

+