@aryacode/dsh-aryacode-polish 0.3.9 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +85 -2
- package/README.md +5 -2
- package/client/face.js +436 -5
- package/client/switches.json +12 -0
- package/docs/github-workspaces.md +63 -0
- package/docs/workspace-folder.md +73 -0
- package/entries/github.js +347 -0
- package/entries/registry.js +14 -0
- package/lib/client.js +449 -6
- package/package.json +2 -2
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# GitHub workspaces
|
|
2
|
+
|
|
3
|
+
**Add workspace from GitHub** turns a repository you can already reach with the
|
|
4
|
+
GitHub CLI into a workspace, without a second sign-in and without storing a
|
|
5
|
+
token.
|
|
6
|
+
|
|
7
|
+
## What it does
|
|
8
|
+
|
|
9
|
+
1. **Reads the CLI session.** The host half runs `gh auth token`. That is the
|
|
10
|
+
whole authentication story: no OAuth app, no device code, no token in a
|
|
11
|
+
settings file, no token in the browser. When `gh` is missing or signed out,
|
|
12
|
+
the dialog says so and names the command that fixes it (`gh auth login`).
|
|
13
|
+
2. **Lists what that session can clone.** `GET /user/repos` with
|
|
14
|
+
`affiliation=owner,collaborator,organization_member`, paged through the
|
|
15
|
+
`link` header, most recently updated first.
|
|
16
|
+
3. **Clones into your own folder.** `<home>\AryaCode\<owner>\<repo>`, created if
|
|
17
|
+
missing. A directory that is already the same repository is reused instead of
|
|
18
|
+
cloned again; a directory that is a *different* repository is an error, never
|
|
19
|
+
a silent overwrite.
|
|
20
|
+
4. **Creates the workspace.** The clone path is handed to the workspace
|
|
21
|
+
controller (`workspaces.create({ path })`), which is the same call the "+" menu
|
|
22
|
+
makes, so the new workspace behaves like any other.
|
|
23
|
+
|
|
24
|
+
## Where the token goes, and where it does not
|
|
25
|
+
|
|
26
|
+
The token is read on the Host and passed to `git` as an `http.extraHeader` value
|
|
27
|
+
in git's own environment (`GIT_CONFIG_COUNT`/`GIT_CONFIG_KEY_0`/
|
|
28
|
+
`GIT_CONFIG_VALUE_0`), never in the remote URL and never on the command line — so
|
|
29
|
+
it cannot leak through `ps`, a stored remote, or a shell history. GitHub's git
|
|
30
|
+
server rejects a bearer token for clone, which is why the header carries
|
|
31
|
+
`x-access-token` basic auth. Every line of git output is scrubbed of the token
|
|
32
|
+
before it can reach the browser, and the route answers `cache-control: no-store`.
|
|
33
|
+
|
|
34
|
+
## Why the entry point is the sidebar foot
|
|
35
|
+
|
|
36
|
+
The "+" menu's **Add workspace** entry only appears when something occupies
|
|
37
|
+
`sidebar.workspaces.directoryFlow`, and that slot is `kind: 'single'`: it already
|
|
38
|
+
holds the folder picker, so occupying it would mean re-implementing folder
|
|
39
|
+
choosing (browse *and* the native desktop picker) just to add a second source.
|
|
40
|
+
Rather than replace the shipped picker, the feature adds its own entry in the
|
|
41
|
+
sidebar foot next to it. If a multi-source workspace socket ever lands in the
|
|
42
|
+
harness, only the registration moves; the route, the clone rules and the tests
|
|
43
|
+
stay as they are.
|
|
44
|
+
|
|
45
|
+
## Not used from the AryaAI-era plugin
|
|
46
|
+
|
|
47
|
+
`@deepseek-ai/dsh-github` was read as a reference and deliberately not reused:
|
|
48
|
+
it is a typert remote service with generated protocol packages, and it
|
|
49
|
+
authenticates through an OAuth device flow that needs a registered OAuth app.
|
|
50
|
+
Its ideas that were kept: reject anything that is not one path segment, reuse a
|
|
51
|
+
checkout whose origin already matches, present the token as basic auth in git's
|
|
52
|
+
environment, page the repository list, and mark a repository that is already
|
|
53
|
+
cloned.
|
|
54
|
+
|
|
55
|
+
## Tests
|
|
56
|
+
|
|
57
|
+
`test/github.spec.mjs` covers the path rules (`.`, `..`, separators, empty),
|
|
58
|
+
the destination layout, origin comparison across https/ssh forms, token
|
|
59
|
+
scrubbing, the CLI token's three failure modes, signed-out and signed-in status,
|
|
60
|
+
the `cloned` marker against a real temporary tree, the spawn shape of a clone
|
|
61
|
+
(command, `--`, URL, destination, and the header in the environment rather than
|
|
62
|
+
in the arguments), the reuse path, the refusal to overwrite another repository,
|
|
63
|
+
and a failing clone whose message is scrubbed.
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# Open a workspace folder in the file browser
|
|
2
|
+
|
|
3
|
+
## What ships
|
|
4
|
+
|
|
5
|
+
A row in a Session's "…" menu: **Open workspace folder**. It resolves the workspace
|
|
6
|
+
that owns the Session and asks the Host to open that directory in the platform file
|
|
7
|
+
manager (Explorer on Windows, Finder on macOS, the desktop's file manager elsewhere).
|
|
8
|
+
|
|
9
|
+
- Switch: `openWorkspaceFolder` (on by default).
|
|
10
|
+
- Seat: `sidebar.workspaces.session.menu.item`, registered as `polish-open-folder`
|
|
11
|
+
at `order: 460`, after the shipped `session-id` row at `450`.
|
|
12
|
+
- Host call: `ctx.remote.session.openWorkspacePath({ path })`, gated by
|
|
13
|
+
`ctx.remote.session.canOpenWorkspacePath()`.
|
|
14
|
+
- Path source: the Workspace snapshot (`ctx.workspaces.list`), matched through
|
|
15
|
+
`WorkspaceView.sessionIds`.
|
|
16
|
+
|
|
17
|
+
The row renders nothing when the switch is off, when the Session belongs to no
|
|
18
|
+
workspace, or when the Host reports it cannot open a path. A menu row that cannot
|
|
19
|
+
act is worse than no row.
|
|
20
|
+
|
|
21
|
+
## What cannot ship, and why
|
|
22
|
+
|
|
23
|
+
The requested surface was **right-click on the workspace row itself**, with a menu
|
|
24
|
+
like the one AryaAI has (`Open in Explorer`, `Rename`, `Manage folders`, `Delete
|
|
25
|
+
workspace`). That is not reachable from a plugin, for three independent reasons:
|
|
26
|
+
|
|
27
|
+
1. The workspace row is drawn by `packages/client/ui-workspace/src/client/rows/Rows.tsx`.
|
|
28
|
+
The row element carries `data-row-key`, `role="treeitem"` and `onClick`, and has
|
|
29
|
+
no `onContextMenu`. Its "…" button opens a menu whose entries are hardcoded
|
|
30
|
+
(`rename`, `delete.workspace`).
|
|
31
|
+
2. No slot exists for per-workspace menu entries. The only workspace-related list
|
|
32
|
+
slots are `sidebar.workspaces.session.menu.item` and
|
|
33
|
+
`sidebar.workspaces.session.row.action`, both scoped to a **Session** row. The
|
|
34
|
+
single `sidebar.workspaces` slot replaces the whole browser
|
|
35
|
+
(`replaceRisk: 'shadows-shipped-ui'`), which would mean rebuilding the shipped
|
|
36
|
+
sidebar.
|
|
37
|
+
3. Native menus are closed to plugins: `apps/desktop/src/main.ts` allows only the
|
|
38
|
+
`application` and `edit` roles from the main window's main frame, and the
|
|
39
|
+
desktop IPC surface (`apps/desktop/src/ipc.ts`, `DshDesktopProductApi`) exposes
|
|
40
|
+
no open/reveal call.
|
|
41
|
+
|
|
42
|
+
Attaching an `onContextMenu` to the row's DOM by selector would be a hack against
|
|
43
|
+
private markup — the thing this bundle's ground rules forbid, and the first thing
|
|
44
|
+
to break when the sidebar changes. The honest options were:
|
|
45
|
+
|
|
46
|
+
- **Ship on the Session row menu** (chosen): documented slot, supported API,
|
|
47
|
+
survives harness updates.
|
|
48
|
+
- Change the shell to declare `sidebar.workspaces.menu.item` and render it from the
|
|
49
|
+
workspace row, then release a harness patch. Declined: the owner wants to keep
|
|
50
|
+
taking harness updates from public releases.
|
|
51
|
+
- Park the feature. Declined: the underlying capability (open a workspace folder)
|
|
52
|
+
is worth shipping now.
|
|
53
|
+
|
|
54
|
+
If the harness ever declares a workspace-scoped menu slot, this feature should move
|
|
55
|
+
there: the Host call and the path resolution stay exactly as they are, and only the
|
|
56
|
+
registration line changes.
|
|
57
|
+
|
|
58
|
+
**Owner's decision (2026-10-27):** asked again, after the Session row menu shipped,
|
|
59
|
+
whether to add that slot to the harness and release a harness patch, the owner
|
|
60
|
+
declined — "then forget it". The workspace row stays untouched and the feature stays
|
|
61
|
+
on the Session row menu. The question has been asked and answered; do not re-open it
|
|
62
|
+
without a new instruction.
|
|
63
|
+
|
|
64
|
+
## Evidence for the next reader
|
|
65
|
+
|
|
66
|
+
- Slot contract: `packages/client/ui-workspace/src/client/contract/slots.ts:152-205`.
|
|
67
|
+
- Shipped row to copy: `packages/client/ui-workspace/src/client/session-actions/SessionId.tsx`.
|
|
68
|
+
- Remote methods: `packages/api/session-controller/src/index.ts:336-339`
|
|
69
|
+
(`canOpenWorkspacePath`) and `:357-367` (`openWorkspacePath`).
|
|
70
|
+
- Windows reveal path: `packages/util/native-command/src/path-opener.ts:307`
|
|
71
|
+
(`runExplorer(['/select,', …])`); `open-in-app` is mounted in the web bundle at
|
|
72
|
+
`packages/bundle/web-app/cordis.patch.yml:73-81`, so this works under `start:web`.
|
|
73
|
+
- Workspace view: `packages/api/workspace-controller/src/types.ts:18-30`.
|
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GitHub workspaces: sign in through the GitHub CLI's existing session, list
|
|
3
|
+
* repositories, and clone one under the clone root so it can be adopted as a
|
|
4
|
+
* workspace.
|
|
5
|
+
*
|
|
6
|
+
* The browser half never sees the token. It posts to one route and gets back
|
|
7
|
+
* JSON; every GitHub call and every `git` invocation happens here.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { spawn } from 'node:child_process'
|
|
11
|
+
import { mkdir, realpath, stat } from 'node:fs/promises'
|
|
12
|
+
import { homedir } from 'node:os'
|
|
13
|
+
import { dirname, join } from 'node:path'
|
|
14
|
+
|
|
15
|
+
/** Absolute path the Host registers; the browser addresses `api/polish.github`. */
|
|
16
|
+
export const GITHUB_ROUTE = '/api/polish.github'
|
|
17
|
+
|
|
18
|
+
/** Root a clone lands in: `<home>/AryaCode/<owner>/<repo>`. */
|
|
19
|
+
export function cloneRoot(home = homedir()) {
|
|
20
|
+
return join(home, 'AryaCode')
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const SEGMENT = /^[A-Za-z0-9_.-]+$/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Reject a repository part that could escape the clone root.
|
|
27
|
+
* @param value - owner or repository name from GitHub.
|
|
28
|
+
* @returns the same value when it is one plain path segment.
|
|
29
|
+
*/
|
|
30
|
+
export function assertSegment(value) {
|
|
31
|
+
if (typeof value !== 'string' || !SEGMENT.test(value) || value === '.' || value === '..') {
|
|
32
|
+
throw new Error('repository name must be a single path segment')
|
|
33
|
+
}
|
|
34
|
+
return value
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Directory one repository clones into.
|
|
39
|
+
* @param owner - repository owner.
|
|
40
|
+
* @param name - repository name.
|
|
41
|
+
* @param home - home directory to root the default clone tree in.
|
|
42
|
+
* @returns absolute destination path.
|
|
43
|
+
*/
|
|
44
|
+
export function cloneDestination(owner, name, home = homedir()) {
|
|
45
|
+
return join(cloneRoot(home), assertSegment(owner), assertSegment(name))
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Whether a git origin URL already points at this repository.
|
|
50
|
+
* @param origin - `git remote get-url origin` output.
|
|
51
|
+
* @param owner - repository owner.
|
|
52
|
+
* @param name - repository name.
|
|
53
|
+
* @returns true for either the https or the ssh form of the same repo.
|
|
54
|
+
*/
|
|
55
|
+
export function sameOrigin(origin, owner, name) {
|
|
56
|
+
const normalized = String(origin).trim().replace(/\.git$/i, '').replace(/\/+$/, '').toLowerCase()
|
|
57
|
+
return normalized === `https://github.com/${owner}/${name}`.toLowerCase()
|
|
58
|
+
|| normalized === `git@github.com:${owner}/${name}`.toLowerCase()
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Run a program and collect its output. Never throws on a non-zero exit.
|
|
63
|
+
* @param command - executable name.
|
|
64
|
+
* @param args - arguments.
|
|
65
|
+
* @param options - extra environment and working directory.
|
|
66
|
+
* @returns exit code plus captured stdout and stderr.
|
|
67
|
+
*/
|
|
68
|
+
export function runCommand(command, args, options = {}) {
|
|
69
|
+
return new Promise((resolve, reject) => {
|
|
70
|
+
const child = spawn(command, args, {
|
|
71
|
+
cwd: options.cwd,
|
|
72
|
+
env: { ...process.env, GIT_TERMINAL_PROMPT: '0', GCM_INTERACTIVE: 'never', ...options.env },
|
|
73
|
+
windowsHide: true,
|
|
74
|
+
})
|
|
75
|
+
let stdout = ''
|
|
76
|
+
let stderr = ''
|
|
77
|
+
child.stdout.setEncoding('utf8')
|
|
78
|
+
child.stderr.setEncoding('utf8')
|
|
79
|
+
child.stdout.on('data', chunk => { stdout += chunk })
|
|
80
|
+
child.stderr.on('data', chunk => { stderr += chunk })
|
|
81
|
+
child.on('error', reject)
|
|
82
|
+
child.on('close', code => { resolve({ code: code ?? 1, stdout, stderr }) })
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Remove the token from text that may reach the browser.
|
|
88
|
+
* @param text - command output.
|
|
89
|
+
* @param token - access token.
|
|
90
|
+
* @returns text with every occurrence replaced.
|
|
91
|
+
*/
|
|
92
|
+
export function scrubToken(text, token) {
|
|
93
|
+
if (token === undefined || token === '') return text
|
|
94
|
+
return String(text).split(token).join('[redacted]')
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Ask the GitHub CLI for the token it already holds.
|
|
99
|
+
*
|
|
100
|
+
* Nothing is stored by this plugin: when `gh` is signed out, the feature says
|
|
101
|
+
* so instead of collecting a credential of its own.
|
|
102
|
+
* @param run - command runner, injected by tests.
|
|
103
|
+
* @returns the token, or undefined when `gh` is missing or signed out.
|
|
104
|
+
*/
|
|
105
|
+
export async function ghToken(run = runCommand) {
|
|
106
|
+
let answer
|
|
107
|
+
try {
|
|
108
|
+
answer = await run('gh', ['auth', 'token'])
|
|
109
|
+
} catch {
|
|
110
|
+
return undefined
|
|
111
|
+
}
|
|
112
|
+
if (answer.code !== 0) return undefined
|
|
113
|
+
const token = answer.stdout.trim()
|
|
114
|
+
return token === '' ? undefined : token
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Read the login that owns a token.
|
|
119
|
+
* @param token - GitHub access token.
|
|
120
|
+
* @param fetchImpl - HTTP implementation.
|
|
121
|
+
* @returns the login name.
|
|
122
|
+
*/
|
|
123
|
+
export async function fetchLogin(token, fetchImpl = fetch) {
|
|
124
|
+
const response = await fetchImpl('https://api.github.com/user', { headers: githubHeaders(token) })
|
|
125
|
+
if (!response.ok) throw new Error(`GitHub sign-in check failed (${String(response.status)})`)
|
|
126
|
+
const body = await response.json()
|
|
127
|
+
const login = body?.login
|
|
128
|
+
if (typeof login !== 'string' || login === '') throw new Error('GitHub did not return a login')
|
|
129
|
+
return login
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* List repositories the token can clone, most recently updated first.
|
|
134
|
+
* @param token - GitHub access token.
|
|
135
|
+
* @param fetchImpl - HTTP implementation.
|
|
136
|
+
* @param pages - most pages of 100 to read.
|
|
137
|
+
* @returns repository rows in list order.
|
|
138
|
+
*/
|
|
139
|
+
export async function fetchRepos(token, fetchImpl = fetch, pages = 3) {
|
|
140
|
+
const repos = []
|
|
141
|
+
let url = 'https://api.github.com/user/repos?per_page=100&sort=updated&affiliation=owner,collaborator,organization_member'
|
|
142
|
+
for (let page = 0; page < pages && url !== undefined; page += 1) {
|
|
143
|
+
const response = await fetchImpl(url, { headers: githubHeaders(token) })
|
|
144
|
+
if (!response.ok) throw new Error(`GitHub repository list failed (${String(response.status)})`)
|
|
145
|
+
const body = await response.json()
|
|
146
|
+
if (!Array.isArray(body)) throw new Error('GitHub repository list was not an array')
|
|
147
|
+
for (const item of body) {
|
|
148
|
+
const row = readRepo(item)
|
|
149
|
+
if (row !== undefined) repos.push(row)
|
|
150
|
+
}
|
|
151
|
+
url = nextLink(response.headers.get('link'))
|
|
152
|
+
}
|
|
153
|
+
return repos
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function githubHeaders(token) {
|
|
157
|
+
return {
|
|
158
|
+
accept: 'application/vnd.github+json',
|
|
159
|
+
authorization: `Bearer ${token}`,
|
|
160
|
+
'user-agent': 'aryacode-polish',
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function readRepo(value) {
|
|
165
|
+
if (value === null || typeof value !== 'object') return undefined
|
|
166
|
+
const name = value.name
|
|
167
|
+
const owner = value.owner !== null && typeof value.owner === 'object' ? value.owner.login : undefined
|
|
168
|
+
if (typeof name !== 'string' || typeof owner !== 'string') return undefined
|
|
169
|
+
return {
|
|
170
|
+
owner,
|
|
171
|
+
name,
|
|
172
|
+
fullName: typeof value.full_name === 'string' ? value.full_name : `${owner}/${name}`,
|
|
173
|
+
isPrivate: value.private === true,
|
|
174
|
+
isFork: value.fork === true,
|
|
175
|
+
description: typeof value.description === 'string' ? value.description : '',
|
|
176
|
+
updatedAt: typeof value.updated_at === 'string' ? value.updated_at : '',
|
|
177
|
+
defaultBranch: typeof value.default_branch === 'string' ? value.default_branch : '',
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function nextLink(header) {
|
|
182
|
+
if (typeof header !== 'string' || header === '') return undefined
|
|
183
|
+
for (const part of header.split(',')) {
|
|
184
|
+
const match = /<([^>]+)>;\s*rel="next"/.exec(part)
|
|
185
|
+
if (match?.[1] !== undefined) return match[1]
|
|
186
|
+
}
|
|
187
|
+
return undefined
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async function directoryExists(path) {
|
|
191
|
+
try {
|
|
192
|
+
return (await stat(path)).isDirectory()
|
|
193
|
+
} catch {
|
|
194
|
+
return false
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
async function existingRealpath(path) {
|
|
199
|
+
try {
|
|
200
|
+
return await realpath(path)
|
|
201
|
+
} catch {
|
|
202
|
+
return undefined
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Clone a repository, or reuse a directory whose origin is already that repo.
|
|
208
|
+
*
|
|
209
|
+
* The token travels in git's environment as an http header, never in the
|
|
210
|
+
* remote URL and never on the command line, so it cannot leak through `ps` or
|
|
211
|
+
* a stored remote.
|
|
212
|
+
* @param owner - repository owner.
|
|
213
|
+
* @param name - repository name.
|
|
214
|
+
* @param token - GitHub access token.
|
|
215
|
+
* @param options - command runner, home directory and optional progress sink.
|
|
216
|
+
* @returns destination path and whether an existing checkout was kept.
|
|
217
|
+
*/
|
|
218
|
+
export async function cloneRepo(owner, name, token, options = {}) {
|
|
219
|
+
const run = options.run ?? runCommand
|
|
220
|
+
const path = cloneDestination(owner, name, options.home)
|
|
221
|
+
const progress = options.progress ?? (() => {})
|
|
222
|
+
if (await directoryExists(path)) {
|
|
223
|
+
const origin = await run('git', ['remote', 'get-url', 'origin'], { cwd: path })
|
|
224
|
+
if (origin.code === 0 && sameOrigin(origin.stdout, owner, name)) {
|
|
225
|
+
progress(`Reusing the existing checkout at ${path}`)
|
|
226
|
+
return { path, reused: true }
|
|
227
|
+
}
|
|
228
|
+
throw new Error(`${path} already exists and is not ${owner}/${name}`)
|
|
229
|
+
}
|
|
230
|
+
await mkdir(dirname(path), { recursive: true })
|
|
231
|
+
progress(`Cloning ${owner}/${name} into ${path}`)
|
|
232
|
+
// GitHub's git server rejects a bearer token, so it is presented as basic auth
|
|
233
|
+
// through an extra http header in git's own environment.
|
|
234
|
+
const header = `Authorization: Basic ${Buffer.from(`x-access-token:${token}`).toString('base64')}`
|
|
235
|
+
const cloned = await run(
|
|
236
|
+
'git',
|
|
237
|
+
['-c', 'credential.helper=', 'clone', '--progress', '--', `https://github.com/${owner}/${name}.git`, path],
|
|
238
|
+
{
|
|
239
|
+
env: {
|
|
240
|
+
GIT_CONFIG_COUNT: '1',
|
|
241
|
+
GIT_CONFIG_KEY_0: 'http.extraHeader',
|
|
242
|
+
GIT_CONFIG_VALUE_0: header,
|
|
243
|
+
},
|
|
244
|
+
},
|
|
245
|
+
)
|
|
246
|
+
if (cloned.code !== 0) {
|
|
247
|
+
throw new Error(scrubToken(cloned.stderr || cloned.stdout || 'git clone failed', token))
|
|
248
|
+
}
|
|
249
|
+
return { path, reused: false }
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Answer one browser request.
|
|
254
|
+
* @param body - parsed request body.
|
|
255
|
+
* @param deps - injected GitHub and git implementations.
|
|
256
|
+
* @returns the JSON value the browser half renders.
|
|
257
|
+
*/
|
|
258
|
+
export async function handleRequest(body, deps = {}) {
|
|
259
|
+
const action = body?.action
|
|
260
|
+
const token = await (deps.token ?? ghToken)()
|
|
261
|
+
if (action === 'status') {
|
|
262
|
+
if (token === undefined) {
|
|
263
|
+
return {
|
|
264
|
+
ok: true,
|
|
265
|
+
value: { signedIn: false, login: '', root: cloneRoot(deps.home), reason: 'The GitHub CLI is missing or not signed in: run `gh auth login` in a terminal.' },
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
const login = await (deps.login ?? fetchLogin)(token)
|
|
269
|
+
return { ok: true, value: { signedIn: true, login, root: cloneRoot(deps.home), reason: '' } }
|
|
270
|
+
}
|
|
271
|
+
if (token === undefined) {
|
|
272
|
+
return { ok: false, error: 'Sign in with the GitHub CLI first: run `gh auth login` in a terminal, then try again.' }
|
|
273
|
+
}
|
|
274
|
+
if (action === 'repos') {
|
|
275
|
+
const repos = await (deps.repos ?? fetchRepos)(token)
|
|
276
|
+
const marked = []
|
|
277
|
+
for (const repo of repos) {
|
|
278
|
+
const real = await existingRealpath(cloneDestination(repo.owner, repo.name, deps.home))
|
|
279
|
+
marked.push({ ...repo, cloned: real !== undefined })
|
|
280
|
+
}
|
|
281
|
+
return { ok: true, value: { root: cloneRoot(deps.home), repos: marked } }
|
|
282
|
+
}
|
|
283
|
+
if (action === 'clone') {
|
|
284
|
+
const owner = assertSegment(body.owner)
|
|
285
|
+
const name = assertSegment(body.name)
|
|
286
|
+
const result = await (deps.clone ?? cloneRepo)(owner, name, token, { home: deps.home })
|
|
287
|
+
return { ok: true, value: { path: result.path, reused: result.reused } }
|
|
288
|
+
}
|
|
289
|
+
return { ok: false, error: `unknown GitHub action: ${String(action)}` }
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* JSON response helper.
|
|
294
|
+
* @param value - payload to serialize.
|
|
295
|
+
* @param status - HTTP status.
|
|
296
|
+
* @returns a JSON response.
|
|
297
|
+
*/
|
|
298
|
+
export function json(value, status = 200) {
|
|
299
|
+
return new Response(JSON.stringify(value), {
|
|
300
|
+
status,
|
|
301
|
+
headers: { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' },
|
|
302
|
+
})
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** Mount the route on a context that provides the `connection` service. */
|
|
306
|
+
export const githubFeature = {
|
|
307
|
+
name: 'polish-github',
|
|
308
|
+
inject: ['connection'],
|
|
309
|
+
apply(ctx, config = {}) {
|
|
310
|
+
ctx.effect(() => ctx.connection.fetch.register({
|
|
311
|
+
path: GITHUB_ROUTE,
|
|
312
|
+
// Without a method list the platform would only route GET, which is why the
|
|
313
|
+
// browser's POST would fall through to the static handler as a 404.
|
|
314
|
+
methods: ['POST'],
|
|
315
|
+
requestBody: 'buffered',
|
|
316
|
+
fetch: async (request) => {
|
|
317
|
+
if (request.method !== 'POST') return json({ ok: false, error: 'POST only' }, 405)
|
|
318
|
+
let body
|
|
319
|
+
try {
|
|
320
|
+
body = await request.json()
|
|
321
|
+
} catch {
|
|
322
|
+
return json({ ok: false, error: 'body must be JSON' }, 400)
|
|
323
|
+
}
|
|
324
|
+
try {
|
|
325
|
+
const deps = {
|
|
326
|
+
home: typeof config.home === 'string' && config.home !== '' ? config.home : undefined,
|
|
327
|
+
}
|
|
328
|
+
return json(await handleRequest(body, deps))
|
|
329
|
+
} catch (error) {
|
|
330
|
+
const message = error instanceof Error ? error.message : 'GitHub request failed'
|
|
331
|
+
return json({ ok: false, error: message })
|
|
332
|
+
}
|
|
333
|
+
},
|
|
334
|
+
}))
|
|
335
|
+
},
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Register one child fiber carrying the GitHub route when a profile mounts the
|
|
340
|
+
* feature. Kept separate from the switch so turning the switch off removes the
|
|
341
|
+
* route together with every other surface.
|
|
342
|
+
* @param ctx - the row's context.
|
|
343
|
+
* @returns the mounted fiber.
|
|
344
|
+
*/
|
|
345
|
+
export function mountGithub(ctx, config = {}) {
|
|
346
|
+
return ctx.plugin({ ...githubFeature, config })
|
|
347
|
+
}
|
package/entries/registry.js
CHANGED
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
*/
|
|
25
25
|
|
|
26
26
|
import { mountCost } from './cost.js'
|
|
27
|
+
import { mountGithub } from './github.js'
|
|
27
28
|
|
|
28
29
|
/**
|
|
29
30
|
* Every feature this bundle ships. Spelling corrections are investigated and
|
|
@@ -46,6 +47,19 @@ export const features = [
|
|
|
46
47
|
description: 'Show the remaining account balance in the sidebar foot, above the account row.',
|
|
47
48
|
defaultOn: true,
|
|
48
49
|
},
|
|
50
|
+
{
|
|
51
|
+
id: 'openWorkspaceFolder',
|
|
52
|
+
label: 'Open workspace folder',
|
|
53
|
+
description: 'Add an "Open workspace folder" row to a Session\u2019s menu that opens its workspace in the file browser.',
|
|
54
|
+
defaultOn: true,
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
id: 'githubWorkspace',
|
|
58
|
+
label: 'Add a workspace from GitHub',
|
|
59
|
+
description: 'Sign in with the GitHub CLI, browse your repositories, and clone one as a workspace.',
|
|
60
|
+
defaultOn: true,
|
|
61
|
+
mount: mountGithub,
|
|
62
|
+
},
|
|
49
63
|
]
|
|
50
64
|
|
|
51
65
|
/**
|