@bridgebrew/project-github 0.0.0-stage → 0.0.2
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/LICENSE +21 -0
- package/README.md +32 -2
- package/browser/index.tsx +18 -0
- package/browser/mark.tsx +8 -0
- package/browser/step.tsx +320 -0
- package/browser/tsconfig.json +3 -0
- package/names.ts +79 -0
- package/package.json +40 -4
- package/server.ts +108 -0
- package/tsconfig.json +3 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 bridgebrew
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,33 @@
|
|
|
1
|
-
#
|
|
1
|
+
# GitHub projects (`@bridgebrew/project-github`)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Adds GitHub as a source in **New project**: clone one of your repositories, or any repository you can read, through
|
|
4
|
+
the GitHub CLI signed in on this machine.
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
From the Marketplace (the Plugins screen), or:
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
bridge install @bridgebrew/project-github --workspace <slug>
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
It needs `@bridgebrew/github` in the same workspace, and the GitHub CLI (`gh`) installed and signed in on the machine
|
|
15
|
+
that hosts Bridge. Bridge keeps no GitHub token.
|
|
16
|
+
|
|
17
|
+
## Use
|
|
18
|
+
|
|
19
|
+
In **New project**, choose **GitHub repository**. The step lists your repositories (yours, those you collaborate on,
|
|
20
|
+
and your organizations', most recently pushed first) with a search field. You can also type or paste `owner/repo`, a
|
|
21
|
+
github.com page or an SSH remote (`git@github.com:owner/repo.git`). Pick one, optionally name the project, and select **Clone**:
|
|
22
|
+
Bridge clones it as a new project, over HTTPS or SSH as `gh` is set up. To clone a private repository over HTTPS, git
|
|
23
|
+
may need `gh auth setup-git` run once.
|
|
24
|
+
|
|
25
|
+
While the GitHub CLI is missing, signed out or outdated, the step says so and opens GitHub's settings.
|
|
26
|
+
|
|
27
|
+
## Contracts
|
|
28
|
+
|
|
29
|
+
Requires `github`.
|
|
30
|
+
|
|
31
|
+
## Limits
|
|
32
|
+
|
|
33
|
+
- The list shows one page of 100 repositories; type a repository's name to reach one beyond it.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// project-github's page half: the "GitHub repository" source of New project (./step.tsx), added to the project
|
|
2
|
+
// plugin's `projectSources` once it is there and removed with this plugin. Without the project plugin it adds nothing.
|
|
3
|
+
import type { Context } from 'cordis';
|
|
4
|
+
import { githubSource, type Source } from './step.tsx';
|
|
5
|
+
|
|
6
|
+
export const name = 'project-github';
|
|
7
|
+
export const inject = ['workspace', 'shell'];
|
|
8
|
+
|
|
9
|
+
/** The project plugin's `projectSources`, the part used (project.d.ts), checked: no plugin imports another. */
|
|
10
|
+
const isSources = (value: unknown): value is { register(source: Source): () => void } =>
|
|
11
|
+
typeof value === 'object' && value !== null && typeof Reflect.get(value, 'register') === 'function';
|
|
12
|
+
|
|
13
|
+
export function apply(ctx: Context) {
|
|
14
|
+
ctx.inject(['projectSources'], (sub) => {
|
|
15
|
+
const sources: unknown = sub.get('projectSources');
|
|
16
|
+
if (isSources(sources)) sub.effect(() => sources.register(githubSource(ctx)));
|
|
17
|
+
});
|
|
18
|
+
}
|
package/browser/mark.tsx
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// The GitHub mark (Octicons `mark-github`, 16px, MIT), the source's icon in New project.
|
|
2
|
+
export function GitHubMark({ size = 16 }: { size?: number }) {
|
|
3
|
+
return (
|
|
4
|
+
<svg width={size} height={size} viewBox="0 0 16 16" fill="currentColor" aria-hidden="true">
|
|
5
|
+
<path d="M8 0c4.42 0 8 3.58 8 8a8.013 8.013 0 0 1-5.45 7.59c-.4.08-.55-.17-.55-.38 0-.27.01-1.13.01-2.2 0-.75-.25-1.23-.54-1.48 1.78-.2 3.65-.88 3.65-3.95 0-.88-.31-1.59-.82-2.15.08-.2.36-1.02-.08-2.12 0 0-.67-.22-2.2.82-.64-.18-1.32-.27-2-.27-.68 0-1.36.09-2 .27-1.53-1.03-2.2-.82-2.2-.82-.44 1.1-.16 1.92-.08 2.12-.51.56-.82 1.28-.82 2.15 0 3.06 1.86 3.75 3.64 3.95-.23.2-.44.55-.51 1.07-.46.21-1.61.55-2.33-.66-.15-.24-.6-.83-1.23-.82-.67.01-.27.38.01.53.34.19.73.9.82 1.13.16.45.68 1.31 2.69.94 0 .67.01 1.3.01 1.49 0 .21-.15.45-.55.38A7.995 7.995 0 0 1 0 8c0-4.42 3.58-8 8-8Z" />
|
|
6
|
+
</svg>
|
|
7
|
+
);
|
|
8
|
+
}
|
package/browser/step.tsx
ADDED
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
// The "GitHub repository" source of New project (bridge#126's `projectSources`): its step lists the person's
|
|
2
|
+
// repositories (search as one types), or reads one typed or pasted (`owner/repo`, its github.com page), then hands
|
|
3
|
+
// its clone URL to the project plugin, in the protocol of gh's sign-in. While gh is not ready, the step only says so
|
|
4
|
+
// and opens GitHub's settings: GitHub (@bridgebrew/github) explains it there (its Test) and in its attention.
|
|
5
|
+
import * as React from 'react';
|
|
6
|
+
import {
|
|
7
|
+
Button,
|
|
8
|
+
Card,
|
|
9
|
+
EmptyState,
|
|
10
|
+
Field,
|
|
11
|
+
FieldDescription,
|
|
12
|
+
FieldLabel,
|
|
13
|
+
Icon,
|
|
14
|
+
Input,
|
|
15
|
+
List,
|
|
16
|
+
ListItem,
|
|
17
|
+
ScrollArea,
|
|
18
|
+
WizardStep,
|
|
19
|
+
useWizard,
|
|
20
|
+
} from '@bridge/ui';
|
|
21
|
+
import type { Context } from 'cordis';
|
|
22
|
+
import { cloneUrlOf, folderOf, pick, type Looked, type Repo } from '../names.ts';
|
|
23
|
+
import { GitHubMark } from './mark.tsx';
|
|
24
|
+
|
|
25
|
+
/** What the project plugin hands a source's step (project.d.ts's `ProjectSourceStepProps`, the part used). */
|
|
26
|
+
export interface StepProps {
|
|
27
|
+
step: string;
|
|
28
|
+
folder: string | undefined;
|
|
29
|
+
make(input: { name: string; title?: string; clone?: string }): Promise<unknown>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** A source of New project (project.d.ts's `ProjectSource`). */
|
|
33
|
+
export interface Source {
|
|
34
|
+
id: string;
|
|
35
|
+
title: string;
|
|
36
|
+
description: string;
|
|
37
|
+
icon: React.ReactNode;
|
|
38
|
+
order?: number;
|
|
39
|
+
Step: React.ComponentType<StepProps>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** GET /state: the service's status, or why it could not be read. */
|
|
43
|
+
type State =
|
|
44
|
+
| { state: 'ready'; login: string; gitProtocol: 'https' | 'ssh' }
|
|
45
|
+
| { state: 'missing' }
|
|
46
|
+
| { state: 'signed_out'; detail?: string }
|
|
47
|
+
| { state: 'outdated'; version: string }
|
|
48
|
+
| { state: 'unavailable'; message: string };
|
|
49
|
+
|
|
50
|
+
/** GitHub's key (`@bridgebrew/github`), for its settings. */
|
|
51
|
+
const SERVICE = 'github';
|
|
52
|
+
|
|
53
|
+
/** The list's height: six rows of 36 px. */
|
|
54
|
+
const BOX = 'h-[216px]';
|
|
55
|
+
|
|
56
|
+
/** What the step says while gh is not ready. */
|
|
57
|
+
const notReady = (state: Exclude<State, { state: 'ready' }>) =>
|
|
58
|
+
state.state === 'missing'
|
|
59
|
+
? 'The GitHub CLI (gh) is not found on this machine.'
|
|
60
|
+
: state.state === 'signed_out'
|
|
61
|
+
? 'The GitHub CLI is not signed in.'
|
|
62
|
+
: state.state === 'outdated'
|
|
63
|
+
? `The GitHub CLI ${state.version} is too old.`
|
|
64
|
+
: `The GitHub CLI could not be checked: ${state.message}`;
|
|
65
|
+
|
|
66
|
+
/** Where a project in folder `name` goes under `folder`, as the project plugin's own steps show it: one line, cut
|
|
67
|
+
* short (the step keeps its height), the whole path in its tooltip. */
|
|
68
|
+
function Where({ folder, name }: { folder: string | undefined; name: string }) {
|
|
69
|
+
const path = `${folder ?? '…'}/projects/${name || '…'}`;
|
|
70
|
+
return (
|
|
71
|
+
<FieldDescription data-folder className="flex items-center gap-1.5">
|
|
72
|
+
<Icon name="folder" size={12} className="shrink-0" />
|
|
73
|
+
<span className="sr-only">Its folder:</span>
|
|
74
|
+
<span className="min-w-0 truncate" title={path}>
|
|
75
|
+
{path}
|
|
76
|
+
</span>
|
|
77
|
+
</FieldDescription>
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** An answer of this plugin's own routes: its body, or a throw with its message. */
|
|
82
|
+
async function read<T>(res: Response): Promise<T> {
|
|
83
|
+
const body: unknown = await res.json().catch(() => undefined);
|
|
84
|
+
if (!res.ok) {
|
|
85
|
+
const error: unknown =
|
|
86
|
+
typeof body === 'object' && body !== null ? Reflect.get(body, 'error') : undefined;
|
|
87
|
+
const message: unknown =
|
|
88
|
+
typeof error === 'object' && error !== null ? Reflect.get(error, 'message') : undefined;
|
|
89
|
+
throw new Error(typeof message === 'string' ? message : `HTTP ${res.status}`);
|
|
90
|
+
}
|
|
91
|
+
// This plugin's own answer.
|
|
92
|
+
return body as T;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function githubSource(ctx: Context): Source {
|
|
96
|
+
const api = `/${ctx.workspace.slug}/api/project-github`;
|
|
97
|
+
|
|
98
|
+
function Step({ step, folder, make }: StepProps) {
|
|
99
|
+
const wizard = useWizard();
|
|
100
|
+
const [state, setState] = React.useState<State>();
|
|
101
|
+
const [check, setCheck] = React.useState(0);
|
|
102
|
+
const [repos, setRepos] = React.useState<Repo[]>();
|
|
103
|
+
const [listFailed, setListFailed] = React.useState<string>();
|
|
104
|
+
const [typed, setTyped] = React.useState('');
|
|
105
|
+
const [chosen, setChosen] = React.useState<string>();
|
|
106
|
+
/** The repository typed or pasted that is not in the list: found, or why not. */
|
|
107
|
+
const [looked, setLooked] = React.useState<Looked>();
|
|
108
|
+
const [typedName, setTypedName] = React.useState('');
|
|
109
|
+
|
|
110
|
+
React.useEffect(() => {
|
|
111
|
+
let live = true;
|
|
112
|
+
setState(undefined);
|
|
113
|
+
ctx.workspace
|
|
114
|
+
.fetch(`${api}/state`)
|
|
115
|
+
.then((res) => read<State>(res))
|
|
116
|
+
.then(
|
|
117
|
+
(answer) => live && setState(answer),
|
|
118
|
+
(error: unknown) =>
|
|
119
|
+
live &&
|
|
120
|
+
setState({
|
|
121
|
+
state: 'unavailable',
|
|
122
|
+
message: error instanceof Error ? error.message : String(error),
|
|
123
|
+
}),
|
|
124
|
+
);
|
|
125
|
+
return () => {
|
|
126
|
+
live = false;
|
|
127
|
+
};
|
|
128
|
+
}, [check]);
|
|
129
|
+
|
|
130
|
+
const ready = state?.state === 'ready';
|
|
131
|
+
React.useEffect(() => {
|
|
132
|
+
if (!ready) return;
|
|
133
|
+
let live = true;
|
|
134
|
+
setListFailed(undefined);
|
|
135
|
+
ctx.workspace
|
|
136
|
+
.fetch(`${api}/repos`)
|
|
137
|
+
.then((res) => read<{ repos: Repo[] }>(res))
|
|
138
|
+
.then(
|
|
139
|
+
(answer) => live && setRepos(answer.repos),
|
|
140
|
+
(error: unknown) =>
|
|
141
|
+
live && setListFailed(error instanceof Error ? error.message : String(error)),
|
|
142
|
+
);
|
|
143
|
+
return () => {
|
|
144
|
+
live = false;
|
|
145
|
+
};
|
|
146
|
+
}, [ready, check]);
|
|
147
|
+
|
|
148
|
+
const { wanted, listed, lookup, shown, repo } = pick({ typed, repos, looked, chosen });
|
|
149
|
+
// Typed or pasted, a repository the list does not hold is read on its own, once typing pauses.
|
|
150
|
+
React.useEffect(() => {
|
|
151
|
+
if (!ready || !wanted || listed) {
|
|
152
|
+
setLooked(undefined);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
const controller = new AbortController();
|
|
156
|
+
const timer = setTimeout(() => {
|
|
157
|
+
ctx.workspace
|
|
158
|
+
.fetch(`${api}/repo?${new URLSearchParams({ name: wanted })}`, {
|
|
159
|
+
signal: controller.signal,
|
|
160
|
+
})
|
|
161
|
+
.then((res) => read<{ repo: Repo }>(res))
|
|
162
|
+
.then(
|
|
163
|
+
(answer) => setLooked({ name: wanted, repo: answer.repo }),
|
|
164
|
+
(error: unknown) => {
|
|
165
|
+
if (controller.signal.aborted) return;
|
|
166
|
+
setLooked({
|
|
167
|
+
name: wanted,
|
|
168
|
+
error: error instanceof Error ? error.message : String(error),
|
|
169
|
+
});
|
|
170
|
+
},
|
|
171
|
+
);
|
|
172
|
+
}, 400);
|
|
173
|
+
return () => {
|
|
174
|
+
clearTimeout(timer);
|
|
175
|
+
controller.abort();
|
|
176
|
+
};
|
|
177
|
+
}, [ready, wanted, listed]);
|
|
178
|
+
|
|
179
|
+
const fromRepo = repo ? folderOf(repo.name.split('/')[1] ?? '') : '';
|
|
180
|
+
const own = (typedName.trim() ? folderOf(typedName) || 'project' : '') || fromRepo;
|
|
181
|
+
const title = typedName.trim() || repo?.name.split('/')[1] || own;
|
|
182
|
+
|
|
183
|
+
// One layout whatever the state, so the dialog keeps its height: the box under the search field holds the list,
|
|
184
|
+
// or what stands in for it (checking gh, gh not ready, loading, no match, a failure).
|
|
185
|
+
let note: React.ReactNode;
|
|
186
|
+
if (state === undefined) note = <EmptyState fill title="Checking the GitHub CLI…" />;
|
|
187
|
+
else if (state.state !== 'ready')
|
|
188
|
+
note = (
|
|
189
|
+
<EmptyState fill icon={<Icon name="triangle-alert" />} title={notReady(state)}>
|
|
190
|
+
<div className="flex items-center gap-2">
|
|
191
|
+
<Button type="button" variant="ghost" onClick={() => setCheck((was) => was + 1)}>
|
|
192
|
+
Check again
|
|
193
|
+
</Button>
|
|
194
|
+
<Button
|
|
195
|
+
type="button"
|
|
196
|
+
variant="primary"
|
|
197
|
+
onClick={(event) => {
|
|
198
|
+
wizard.close();
|
|
199
|
+
ctx.shell.openSetting(ctx.workspace.slug, SERVICE, 'cli', event);
|
|
200
|
+
}}
|
|
201
|
+
>
|
|
202
|
+
Open GitHub settings
|
|
203
|
+
</Button>
|
|
204
|
+
</div>
|
|
205
|
+
</EmptyState>
|
|
206
|
+
);
|
|
207
|
+
else if (listFailed)
|
|
208
|
+
note = (
|
|
209
|
+
<EmptyState fill title="Your repositories could not be read">
|
|
210
|
+
<p>{listFailed}</p>
|
|
211
|
+
<Button type="button" onClick={() => setCheck((was) => was + 1)}>
|
|
212
|
+
Retry
|
|
213
|
+
</Button>
|
|
214
|
+
</EmptyState>
|
|
215
|
+
);
|
|
216
|
+
else if (lookup?.error) note = <EmptyState fill title={lookup.error} />;
|
|
217
|
+
else if (lookup && !lookup.repo) note = <EmptyState fill title={`Looking for ${lookup.name}…`} />;
|
|
218
|
+
else if (repos === undefined) note = <EmptyState fill title="Loading your repositories…" />;
|
|
219
|
+
else if (shown.length === 0)
|
|
220
|
+
note = (
|
|
221
|
+
<EmptyState fill title="No repository matches">
|
|
222
|
+
<p>Type owner/repo, or paste its address on github.com.</p>
|
|
223
|
+
</EmptyState>
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
return (
|
|
227
|
+
<WizardStep
|
|
228
|
+
id={step}
|
|
229
|
+
title="Clone from GitHub"
|
|
230
|
+
description={ready ? `Signed in as ${state.login}` : 'With the GitHub CLI of this machine'}
|
|
231
|
+
submitLabel="Clone"
|
|
232
|
+
busyLabel="Cloning…"
|
|
233
|
+
canSubmit={ready && repo !== undefined && own !== ''}
|
|
234
|
+
onSubmit={async () => {
|
|
235
|
+
if (!ready || !repo) return;
|
|
236
|
+
try {
|
|
237
|
+
await make({ name: own, title, clone: cloneUrlOf(repo, state.gitProtocol) });
|
|
238
|
+
} catch (error) {
|
|
239
|
+
// git, not gh, clones: over https a private repository needs gh as git's credential helper.
|
|
240
|
+
if (!repo.private || state.gitProtocol !== 'https') throw error;
|
|
241
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
242
|
+
throw new Error(
|
|
243
|
+
`${message} If git could not sign in, run gh auth setup-git once in a terminal, then Clone again.`,
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
}}
|
|
247
|
+
>
|
|
248
|
+
<Field>
|
|
249
|
+
<FieldLabel>Repository</FieldLabel>
|
|
250
|
+
<Input
|
|
251
|
+
variant="outline"
|
|
252
|
+
value={typed}
|
|
253
|
+
disabled={!ready}
|
|
254
|
+
icon={<Icon name="search" />}
|
|
255
|
+
placeholder="Search your repositories, or owner/repo"
|
|
256
|
+
onChange={(event) => {
|
|
257
|
+
setTyped(event.currentTarget.value);
|
|
258
|
+
setChosen(undefined);
|
|
259
|
+
}}
|
|
260
|
+
/>
|
|
261
|
+
</Field>
|
|
262
|
+
{/* The list's frame: one height whatever it holds, six rows, the rest scrolled. */}
|
|
263
|
+
<Card variant="outline" className="overflow-hidden p-0">
|
|
264
|
+
{note ? (
|
|
265
|
+
<div className={BOX}>{note}</div>
|
|
266
|
+
) : (
|
|
267
|
+
<ScrollArea className={BOX}>
|
|
268
|
+
<List
|
|
269
|
+
aria-label="Your repositories"
|
|
270
|
+
selectionMode="single"
|
|
271
|
+
selectedKey={repo?.name ?? ''}
|
|
272
|
+
onSelectionChange={(keys) => {
|
|
273
|
+
const key = keys[0];
|
|
274
|
+
if (key !== undefined) setChosen(key);
|
|
275
|
+
}}
|
|
276
|
+
>
|
|
277
|
+
{shown.map((each) => (
|
|
278
|
+
<ListItem
|
|
279
|
+
key={each.name}
|
|
280
|
+
value={each.name}
|
|
281
|
+
textValue={each.name}
|
|
282
|
+
size="sm"
|
|
283
|
+
leading={
|
|
284
|
+
<Icon
|
|
285
|
+
name={each.private ? 'lock' : 'globe'}
|
|
286
|
+
aria-label={each.private ? 'Private' : 'Public'}
|
|
287
|
+
/>
|
|
288
|
+
}
|
|
289
|
+
>
|
|
290
|
+
{each.name}
|
|
291
|
+
</ListItem>
|
|
292
|
+
))}
|
|
293
|
+
</List>
|
|
294
|
+
</ScrollArea>
|
|
295
|
+
)}
|
|
296
|
+
</Card>
|
|
297
|
+
<Field>
|
|
298
|
+
<FieldLabel>Name (optional)</FieldLabel>
|
|
299
|
+
<Input
|
|
300
|
+
variant="outline"
|
|
301
|
+
value={typedName}
|
|
302
|
+
disabled={!ready}
|
|
303
|
+
placeholder={fromRepo}
|
|
304
|
+
onChange={(event) => setTypedName(event.currentTarget.value)}
|
|
305
|
+
/>
|
|
306
|
+
<Where folder={folder} name={own} />
|
|
307
|
+
</Field>
|
|
308
|
+
</WizardStep>
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
return {
|
|
313
|
+
id: 'project-github',
|
|
314
|
+
title: 'GitHub repository',
|
|
315
|
+
description: 'Clone one of your repositories, or any you can read',
|
|
316
|
+
icon: <GitHubMark />,
|
|
317
|
+
order: 25,
|
|
318
|
+
Step,
|
|
319
|
+
};
|
|
320
|
+
}
|
package/names.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// What the GitHub source reads of what one types, and the names it makes; shared by its step and its server.
|
|
2
|
+
|
|
3
|
+
/** A repository as the step shows it (GET /repos, GET /repo). */
|
|
4
|
+
export interface Repo {
|
|
5
|
+
name: string;
|
|
6
|
+
description: string;
|
|
7
|
+
private: boolean;
|
|
8
|
+
defaultBranch: string;
|
|
9
|
+
https: string;
|
|
10
|
+
ssh: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** The last lookup of a repository typed or pasted that the list does not hold: found, or why not. */
|
|
14
|
+
export interface Looked {
|
|
15
|
+
name: string;
|
|
16
|
+
repo?: Repo;
|
|
17
|
+
error?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const OWNER = /^[A-Za-z0-9](?:[A-Za-z0-9-]{0,38})$/;
|
|
21
|
+
const NAME = /^[A-Za-z0-9._-]{1,100}$/;
|
|
22
|
+
|
|
23
|
+
/** `owner/repo` from what one types or pastes: `owner/repo`, a github.com page (https or not, `.git`, a deeper page),
|
|
24
|
+
* or a git remote (`git@github.com:owner/repo.git`, `ssh://git@github.com/owner/repo.git`), every remote GitHub's
|
|
25
|
+
* `repositoryOf` (@bridgebrew/github, ./pull-requests.ts) reads; `undefined` for anything else. */
|
|
26
|
+
export function repoOf(text: string): string | undefined {
|
|
27
|
+
const typed = text.trim();
|
|
28
|
+
const found =
|
|
29
|
+
/^(?:(?:https?|ssh|git):\/\/)?(?:[^@/?#\\\s]+@)?(?:www\.|ssh\.)?github\.com(?::\d+)?\/([^/\s]+)\/([^/\s?#]+)/i.exec(typed) ??
|
|
30
|
+
/^(?:[^@/?#\\\s]+@)?(?:ssh\.)?github\.com:([^/\s]+)\/([^/\s]+?)\/?$/i.exec(typed) ??
|
|
31
|
+
/^([^/\s:]+)\/([^/\s]+)$/.exec(typed);
|
|
32
|
+
if (!found) return undefined;
|
|
33
|
+
const owner = found[1]!;
|
|
34
|
+
const name = found[2]!.replace(/\.git$/i, '');
|
|
35
|
+
if (!OWNER.test(owner) || !NAME.test(name) || name === '.' || name === '..') return undefined;
|
|
36
|
+
return `${owner}/${name}`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The URL `git clone` takes for a repository, in the protocol gh's sign-in uses. */
|
|
40
|
+
export const cloneUrlOf = (repo: { https: string; ssh: string }, protocol: 'https' | 'ssh') =>
|
|
41
|
+
protocol === 'ssh' ? repo.ssh : repo.https;
|
|
42
|
+
|
|
43
|
+
/** A project's folder name from its title, as the project plugin makes it: `Test git` → `test-git`, `Café` → `cafe`.
|
|
44
|
+
* Empty when nothing of it fits a folder name (letters, digits, `_ - .`, at most 100). */
|
|
45
|
+
export const folderOf = (title: string) =>
|
|
46
|
+
title
|
|
47
|
+
.normalize('NFKD')
|
|
48
|
+
.replace(/[̀-ͯ]/g, '')
|
|
49
|
+
.toLowerCase()
|
|
50
|
+
.replace(/[^\w.-]+/g, '-')
|
|
51
|
+
.replace(/-{2,}/g, '-')
|
|
52
|
+
.replace(/^[.-]+|-+$/g, '')
|
|
53
|
+
.slice(0, 100)
|
|
54
|
+
.replace(/-+$/, '');
|
|
55
|
+
|
|
56
|
+
/** What the step shows for what is typed: the repository typed (`wanted`), the list's own one (`listed`), the lookup of
|
|
57
|
+
* one the list does not hold, the rows, and the repository chosen: the row clicked, else the one typed. */
|
|
58
|
+
export function pick(input: {
|
|
59
|
+
typed: string;
|
|
60
|
+
repos: Repo[] | undefined;
|
|
61
|
+
looked: Looked | undefined;
|
|
62
|
+
chosen: string | undefined;
|
|
63
|
+
}) {
|
|
64
|
+
const { typed, repos, looked, chosen } = input;
|
|
65
|
+
const wanted = repoOf(typed);
|
|
66
|
+
const listed = wanted
|
|
67
|
+
? repos?.find((each) => each.name.toLowerCase() === wanted.toLowerCase())
|
|
68
|
+
: undefined;
|
|
69
|
+
const lookup: Looked | undefined =
|
|
70
|
+
wanted && !listed ? (looked?.name === wanted ? looked : { name: wanted }) : undefined;
|
|
71
|
+
const search = typed.trim().toLowerCase();
|
|
72
|
+
const shown = lookup
|
|
73
|
+
? lookup.repo
|
|
74
|
+
? [lookup.repo]
|
|
75
|
+
: []
|
|
76
|
+
: (repos ?? []).filter((each) => !search || each.name.toLowerCase().includes(search));
|
|
77
|
+
const repo = shown.find((each) => each.name === chosen) ?? listed ?? lookup?.repo;
|
|
78
|
+
return { wanted, listed, lookup, shown, repo };
|
|
79
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bridgebrew/project-github",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"files": [
|
|
7
|
+
"*.json",
|
|
8
|
+
"*.ts",
|
|
9
|
+
"browser/",
|
|
10
|
+
"!**/*.test.ts",
|
|
11
|
+
"!**/*.test.tsx"
|
|
12
|
+
],
|
|
13
|
+
"keywords": [
|
|
14
|
+
"bridge-plugin"
|
|
15
|
+
],
|
|
16
|
+
"description": "A GitHub source for New project: clone one of your repositories, or any you can read, through the GitHub CLI signed in on this machine.",
|
|
17
|
+
"bridge": {
|
|
18
|
+
"kind": "extension",
|
|
19
|
+
"title": "GitHub projects",
|
|
20
|
+
"icon": "git-branch",
|
|
21
|
+
"domain": "development",
|
|
22
|
+
"server": "./server.ts",
|
|
23
|
+
"browser": "./browser/index.tsx",
|
|
24
|
+
"requires": {
|
|
25
|
+
"github": {}
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"test": "node --test test/*.test.ts",
|
|
30
|
+
"e2e": "node ../scripts/e2e.mjs .",
|
|
31
|
+
"e2e:run": "node --test --test-concurrency=1 e2e.test.ts",
|
|
32
|
+
"typecheck": "bridge plugin check ."
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"cordis": "^4.0.0-rc.11",
|
|
36
|
+
"react": "^19.3.0",
|
|
37
|
+
"@bridge/ui": ">=0.0.1"
|
|
38
|
+
},
|
|
39
|
+
"engines": {
|
|
40
|
+
"bridge": ">=0.0.1-nightly.20261117.1"
|
|
41
|
+
}
|
|
42
|
+
}
|
package/server.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// project-github's server half: what its New project step reads, all through the `github` service (@bridgebrew/github):
|
|
2
|
+
// gh's state (GET /state), the person's repositories (GET /repos: theirs, those they collaborate on, their
|
|
3
|
+
// organizations', most recently pushed first, one page of 100) and one repository typed or pasted (GET /repo?name=).
|
|
4
|
+
// The clone itself is the project plugin's: the step hands it the repository's URL.
|
|
5
|
+
import type { Context } from 'cordis';
|
|
6
|
+
import type {} from '@bridgebrew/github/server.ts';
|
|
7
|
+
import { repoOf, type Repo } from './names.ts';
|
|
8
|
+
|
|
9
|
+
export const name = 'project-github';
|
|
10
|
+
export const inject = ['router', 'github'];
|
|
11
|
+
|
|
12
|
+
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
13
|
+
typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
14
|
+
|
|
15
|
+
/** A repository from GitHub's answer; `undefined` when it lacks what the step needs. */
|
|
16
|
+
const repoFrom = (value: unknown): Repo | undefined =>
|
|
17
|
+
isRecord(value) &&
|
|
18
|
+
typeof value.full_name === 'string' &&
|
|
19
|
+
typeof value.clone_url === 'string' &&
|
|
20
|
+
typeof value.ssh_url === 'string'
|
|
21
|
+
? {
|
|
22
|
+
name: value.full_name,
|
|
23
|
+
description: typeof value.description === 'string' ? value.description : '',
|
|
24
|
+
private: value.private === true,
|
|
25
|
+
defaultBranch: typeof value.default_branch === 'string' ? value.default_branch : '',
|
|
26
|
+
https: value.clone_url,
|
|
27
|
+
ssh: value.ssh_url,
|
|
28
|
+
}
|
|
29
|
+
: undefined;
|
|
30
|
+
|
|
31
|
+
/** An answer of 4xx or 5xx to the step, `{ error: { code, message } }`. */
|
|
32
|
+
const refusal = (status: number, code: string, message: string) =>
|
|
33
|
+
Object.assign(new Error(message), { code, status, [Symbol.for('bridge.clientError')]: true });
|
|
34
|
+
|
|
35
|
+
/** The `code` of a failure of the `github` service (a `GitHubError`, read without importing the other plugin). */
|
|
36
|
+
const codeOf = (error: unknown) =>
|
|
37
|
+
isRecord(error) && typeof error.code === 'string' ? error.code : undefined;
|
|
38
|
+
const messageOf = (error: unknown) => (error instanceof Error ? error.message : String(error));
|
|
39
|
+
|
|
40
|
+
/** gh not ready: the step shows the service's state again (GET /state). */
|
|
41
|
+
const NOT_READY = new Set(['gh_missing', 'signed_out', 'outdated']);
|
|
42
|
+
|
|
43
|
+
/** A failure of the service as the step's answer. */
|
|
44
|
+
const failed = (error: unknown) => {
|
|
45
|
+
const code = codeOf(error);
|
|
46
|
+
if (code !== undefined && NOT_READY.has(code))
|
|
47
|
+
return refusal(503, code, 'The GitHub CLI is not ready: see GitHub in Settings.');
|
|
48
|
+
if (code === 'rate_limited') return refusal(429, code, messageOf(error));
|
|
49
|
+
return refusal(502, code ?? 'github_failed', `GitHub could not be read: ${messageOf(error)}`);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export function apply(ctx: Context) {
|
|
53
|
+
ctx.router.route(ctx, {
|
|
54
|
+
method: 'GET',
|
|
55
|
+
path: '/state',
|
|
56
|
+
agent: false,
|
|
57
|
+
description: "The GitHub CLI's state on this machine, as GitHub (@bridgebrew/github) reads it.",
|
|
58
|
+
handle: async () => {
|
|
59
|
+
try {
|
|
60
|
+
return await ctx.github.status();
|
|
61
|
+
} catch (error) {
|
|
62
|
+
return { state: 'unavailable' as const, message: messageOf(error) };
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
ctx.router.route(ctx, {
|
|
68
|
+
method: 'GET',
|
|
69
|
+
path: '/repos',
|
|
70
|
+
agent: false,
|
|
71
|
+
description: "The signed-in account's repositories, most recently pushed first (100 at most).",
|
|
72
|
+
handle: async () => {
|
|
73
|
+
try {
|
|
74
|
+
const answer = await ctx.github.rest({
|
|
75
|
+
path: 'user/repos',
|
|
76
|
+
query: { affiliation: 'owner,collaborator,organization_member', sort: 'pushed', per_page: 100 },
|
|
77
|
+
});
|
|
78
|
+
const list = Array.isArray(answer.body) ? answer.body : [];
|
|
79
|
+
return { repos: list.flatMap((each) => repoFrom(each) ?? []) };
|
|
80
|
+
} catch (error) {
|
|
81
|
+
throw failed(error);
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
ctx.router.route(ctx, {
|
|
87
|
+
method: 'GET',
|
|
88
|
+
path: '/repo',
|
|
89
|
+
agent: false,
|
|
90
|
+
description: 'One repository the account can read, by owner/repo or its github.com address.',
|
|
91
|
+
handle: async ({ query }) => {
|
|
92
|
+
const repo = repoOf(query.get('name') ?? '');
|
|
93
|
+
if (!repo)
|
|
94
|
+
throw refusal(400, 'invalid_name', 'Give owner/repo, or the address of its page on github.com.');
|
|
95
|
+
let body: unknown;
|
|
96
|
+
try {
|
|
97
|
+
body = (await ctx.github.rest({ path: `repos/${repo}` })).body;
|
|
98
|
+
} catch (error) {
|
|
99
|
+
if (codeOf(error) === 'not_found')
|
|
100
|
+
throw refusal(404, 'not_found', `No repository ${repo}, or this GitHub account cannot read it.`);
|
|
101
|
+
throw failed(error);
|
|
102
|
+
}
|
|
103
|
+
const found = repoFrom(body);
|
|
104
|
+
if (!found) throw refusal(502, 'invalid_response', `GitHub's answer for ${repo} could not be read.`);
|
|
105
|
+
return { repo: found };
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
}
|
package/tsconfig.json
ADDED