@noldova/git.github 0.0.8 → 0.0.10
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/module.json +7 -5
- package/package.json +10 -10
- package/src/exceptions/github-unreachable.exception.js +15 -0
- package/src/models/account-search.js +15 -0
- package/src/models/github-repository.js +23 -3
- package/src/models/kept-account.js +26 -0
- package/src/models/kept-accounts.js +26 -0
- package/src/models/remote-user.js +13 -0
- package/src/models/signed-in-account.js +17 -0
- package/src/models/token-connect.js +21 -0
- package/src/resources.js +50 -3
- package/src/runtime-part.js +70 -24
- package/src/services/github-access.js +20 -9
- package/src/services/github-accounts.js +350 -0
- package/src/services/github-api.js +35 -26
- package/src/services/github-device-flow.js +4 -3
- package/src/services/github-folder-users.js +38 -0
- package/src/services/github-reads.js +130 -92
- package/src/services/github-sign-in.js +27 -6
- package/src/tools/bound-repository.js +8 -3
- package/src/tools/github-pr-checks-tool.js +6 -6
- package/src/tools/github-pr-create-tool.js +12 -7
- package/src/tools/github-read-tool.js +5 -5
- package/src/tools/github-tools.js +2 -2
- /package/src/interfaces/{i-github-token.js → i-github-tokens.js} +0 -0
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import { JsonException, JsonReader } from "@noldova/context.json";
|
|
9
|
+
import { FailureCode, SecretFailureReason, SecretFailures } from "@noldova/desktop.protocol";
|
|
10
|
+
import { MethodFailureException } from "@noldova/desktop.runtime";
|
|
11
|
+
import { AccountPick, HostAccountEntry, HostAccountList, HostingProtocolException, RepositoryAccount, SignInState } from "@noldova/git.hosting.protocol";
|
|
12
|
+
import { GitHubException } from "../exceptions/github.exception.js";
|
|
13
|
+
import { GitHubRepository } from "../models/github-repository.js";
|
|
14
|
+
import { KeptAccount } from "../models/kept-account.js";
|
|
15
|
+
import { KeptAccounts } from "../models/kept-accounts.js";
|
|
16
|
+
import { SignedInAccount } from "../models/signed-in-account.js";
|
|
17
|
+
import { Resources } from "../resources.js";
|
|
18
|
+
import { GitHubApi } from "./github-api.js";
|
|
19
|
+
import { GitHubSignIn } from "./github-sign-in.js";
|
|
20
|
+
export class GitHubAccounts {
|
|
21
|
+
static firstId = 1;
|
|
22
|
+
flow;
|
|
23
|
+
secrets;
|
|
24
|
+
appWindows;
|
|
25
|
+
log;
|
|
26
|
+
api;
|
|
27
|
+
changed;
|
|
28
|
+
entries = new Map();
|
|
29
|
+
found = new Map();
|
|
30
|
+
users = new Map();
|
|
31
|
+
dropping = new Map();
|
|
32
|
+
picks = [];
|
|
33
|
+
kept = null;
|
|
34
|
+
isIndexRead = false;
|
|
35
|
+
reading = Promise.resolve(null);
|
|
36
|
+
saving = Promise.resolve();
|
|
37
|
+
unfollowWindows = null;
|
|
38
|
+
stopped = false;
|
|
39
|
+
constructor(flow, secrets, appWindows, log, api, changed) {
|
|
40
|
+
this.flow = flow;
|
|
41
|
+
this.secrets = secrets;
|
|
42
|
+
this.appWindows = appWindows;
|
|
43
|
+
this.log = log;
|
|
44
|
+
this.api = api;
|
|
45
|
+
this.changed = changed;
|
|
46
|
+
this.entries.set(GitHubAccounts.firstId, this.make(GitHubAccounts.firstId));
|
|
47
|
+
}
|
|
48
|
+
get first() {
|
|
49
|
+
return this.signInOf(GitHubAccounts.firstId);
|
|
50
|
+
}
|
|
51
|
+
get list() {
|
|
52
|
+
return new HostAccountList([...this.entries].map(([id, signIn]) => new HostAccountEntry(id, signIn.current, signIn.host === Resources.host ? null : signIn.host)));
|
|
53
|
+
}
|
|
54
|
+
get signedIn() {
|
|
55
|
+
return this.signedInOn(Resources.host);
|
|
56
|
+
}
|
|
57
|
+
signedInOn(host) {
|
|
58
|
+
const server = host.toLowerCase();
|
|
59
|
+
const accounts = [];
|
|
60
|
+
for (const [id, signIn] of this.entries) {
|
|
61
|
+
const token = signIn.token;
|
|
62
|
+
const login = signIn.current.account?.login;
|
|
63
|
+
if (token !== null && login !== undefined && signIn.host === server)
|
|
64
|
+
accounts.push(new SignedInAccount(id, login, token));
|
|
65
|
+
}
|
|
66
|
+
return accounts;
|
|
67
|
+
}
|
|
68
|
+
serves(host) {
|
|
69
|
+
const server = host.toLowerCase();
|
|
70
|
+
return server === Resources.host || [...this.entries.values()].some(t => t.host === server);
|
|
71
|
+
}
|
|
72
|
+
async restoreAsync() {
|
|
73
|
+
if (this.stopped)
|
|
74
|
+
return;
|
|
75
|
+
this.reading = this.readKeptAsync();
|
|
76
|
+
const kept = await this.reading;
|
|
77
|
+
if (this.stopped)
|
|
78
|
+
return;
|
|
79
|
+
const named = (kept?.accounts ?? []).map(t => t.id);
|
|
80
|
+
for (const account of kept?.accounts ?? []) {
|
|
81
|
+
if (!this.entries.has(account.id))
|
|
82
|
+
this.entries.set(account.id, this.make(account.id, account.host));
|
|
83
|
+
}
|
|
84
|
+
this.picks = kept?.picks ?? [];
|
|
85
|
+
const restoring = [...this.entries.keys()].filter(id => id === GitHubAccounts.firstId || named.includes(id)).map(id => this.signInOf(id));
|
|
86
|
+
await Promise.all(restoring.map(t => t.restoreAsync()));
|
|
87
|
+
for (const [id, signIn] of this.entries) {
|
|
88
|
+
if (id !== GitHubAccounts.firstId && named.includes(id) && signIn.current.state === SignInState.SignedOut && signIn.current.failure === null
|
|
89
|
+
&& !signIn.keptUnreadable)
|
|
90
|
+
this.leave(id);
|
|
91
|
+
}
|
|
92
|
+
this.save();
|
|
93
|
+
}
|
|
94
|
+
signInOf(id) {
|
|
95
|
+
const signIn = this.entries.get(id);
|
|
96
|
+
if (signIn === undefined)
|
|
97
|
+
throw GitHubApi.failure(FailureCode.NotFound, Resources.unknownAccountMessage);
|
|
98
|
+
return signIn;
|
|
99
|
+
}
|
|
100
|
+
async addAsync() {
|
|
101
|
+
await this.reading;
|
|
102
|
+
if (!this.isIndexRead)
|
|
103
|
+
throw GitHubApi.failure(FailureCode.Unavailable, Resources.accountsUnreadMessage);
|
|
104
|
+
const id = Math.max(...this.entries.keys()) + 1;
|
|
105
|
+
const signIn = this.make(id);
|
|
106
|
+
this.entries.set(id, signIn);
|
|
107
|
+
return new HostAccountEntry(id, await signIn.startAsync());
|
|
108
|
+
}
|
|
109
|
+
async connectAsync(connect, signal) {
|
|
110
|
+
const host = connect.host.toLowerCase();
|
|
111
|
+
if (!Resources.enterpriseHostPattern.test(host) || host === Resources.host)
|
|
112
|
+
throw GitHubApi.failure(FailureCode.InvalidParams, Resources.enterpriseHostMessage);
|
|
113
|
+
await this.reading;
|
|
114
|
+
if (!this.isIndexRead)
|
|
115
|
+
throw GitHubApi.failure(FailureCode.Unavailable, Resources.accountsUnreadMessage);
|
|
116
|
+
const read = await this.readTokenAsync(host, connect.token, signal);
|
|
117
|
+
if (read.account.scopes.length > 0 && !read.account.scopes.includes(Resources.scope))
|
|
118
|
+
throw GitHubApi.failure(FailureCode.Unauthorized, Resources.formatScopeMissing(host));
|
|
119
|
+
const id = Math.max(...this.entries.keys()) + 1;
|
|
120
|
+
const signIn = this.make(id, host);
|
|
121
|
+
this.entries.set(id, signIn);
|
|
122
|
+
await signIn.adoptAsync(connect.token, read, signal);
|
|
123
|
+
await this.dropping.get(id);
|
|
124
|
+
const answer = signIn.current;
|
|
125
|
+
if (answer.state !== SignInState.SignedIn && this.entries.has(id)) {
|
|
126
|
+
this.leave(id);
|
|
127
|
+
this.publish(id, answer);
|
|
128
|
+
}
|
|
129
|
+
return new HostAccountEntry(id, answer, host);
|
|
130
|
+
}
|
|
131
|
+
async signOutAsync(id) {
|
|
132
|
+
const signIn = this.signInOf(id);
|
|
133
|
+
const answer = await signIn.signOutAsync();
|
|
134
|
+
if (id !== GitHubAccounts.firstId && this.entries.has(id)) {
|
|
135
|
+
this.leave(id);
|
|
136
|
+
this.publish(id, answer);
|
|
137
|
+
}
|
|
138
|
+
return answer;
|
|
139
|
+
}
|
|
140
|
+
async pickAsync(pick, signal) {
|
|
141
|
+
this.signInOf(pick.account);
|
|
142
|
+
const others = this.picks.filter(t => !GitHubAccounts.isSameRepository(t, pick.host, pick.path));
|
|
143
|
+
this.picks = [...others, pick].slice(-Resources.picksCap);
|
|
144
|
+
this.found.delete(GitHubAccounts.keyOf(pick.host, pick.path));
|
|
145
|
+
this.save();
|
|
146
|
+
return this.repositoryAccountAsync(pick.host, pick.path, signal);
|
|
147
|
+
}
|
|
148
|
+
async repositoryAccountAsync(host, path, signal) {
|
|
149
|
+
const id = await this.accountForAsync(host, path, signal);
|
|
150
|
+
return id === null ? new RepositoryAccount(null) : new RepositoryAccount(id, undefined, this.namedFor(host, path) === id);
|
|
151
|
+
}
|
|
152
|
+
useUser(host, path, user) {
|
|
153
|
+
const key = GitHubAccounts.keyOf(host, path);
|
|
154
|
+
this.users.delete(key);
|
|
155
|
+
if (user === null)
|
|
156
|
+
return;
|
|
157
|
+
this.users.set(key, user.toLowerCase());
|
|
158
|
+
for (const old of [...this.users.keys()].slice(0, -Resources.usersCap))
|
|
159
|
+
this.users.delete(old);
|
|
160
|
+
}
|
|
161
|
+
async accountForAsync(host, path, signal, user = null) {
|
|
162
|
+
const signedIn = this.signedInOn(host);
|
|
163
|
+
const first = signedIn[0];
|
|
164
|
+
if (first === undefined)
|
|
165
|
+
return null;
|
|
166
|
+
const named = Object.isNull(user) ? this.namedFor(host, path) : this.loginOf(host, user.name);
|
|
167
|
+
if (named !== null)
|
|
168
|
+
return named;
|
|
169
|
+
const pick = this.picks.find(t => GitHubAccounts.isSameRepository(t, host, path))?.account;
|
|
170
|
+
if (pick !== undefined && signedIn.some(t => t.id === pick))
|
|
171
|
+
return pick;
|
|
172
|
+
if (signedIn.length === 1)
|
|
173
|
+
return first.id;
|
|
174
|
+
const key = GitHubAccounts.keyOf(host, path);
|
|
175
|
+
let found = this.found.get(key);
|
|
176
|
+
if (found === undefined) {
|
|
177
|
+
found = this.probeAsync(key, host, path, signedIn, first.id);
|
|
178
|
+
this.found.set(key, found);
|
|
179
|
+
}
|
|
180
|
+
const id = await found;
|
|
181
|
+
signal.throwIfAborted();
|
|
182
|
+
const now = this.signedInOn(host);
|
|
183
|
+
return now.some(t => t.id === id) ? id : now[0]?.id ?? null;
|
|
184
|
+
}
|
|
185
|
+
async tokenForAsync(host, path, signal, user = null) {
|
|
186
|
+
const id = await this.accountForAsync(host, path, signal, user);
|
|
187
|
+
return id === null ? null : this.signInOf(id).token;
|
|
188
|
+
}
|
|
189
|
+
namedFor(host, path) {
|
|
190
|
+
return this.loginOf(host, this.users.get(GitHubAccounts.keyOf(host, path)) ?? null);
|
|
191
|
+
}
|
|
192
|
+
loginOf(host, user) {
|
|
193
|
+
const name = user?.toLowerCase();
|
|
194
|
+
return this.signedInOn(host).find(t => t.login.toLowerCase() === name)?.id ?? null;
|
|
195
|
+
}
|
|
196
|
+
holds(token) {
|
|
197
|
+
return [...this.entries.values()].some(t => t.token === token);
|
|
198
|
+
}
|
|
199
|
+
async refusedAsync(token) {
|
|
200
|
+
await Promise.all([...this.entries.values()].map(t => t.refusedAsync(token)));
|
|
201
|
+
}
|
|
202
|
+
dispose() {
|
|
203
|
+
this.stopped = true;
|
|
204
|
+
this.unfollowWindows?.();
|
|
205
|
+
this.unfollowWindows = null;
|
|
206
|
+
for (const signIn of this.entries.values())
|
|
207
|
+
signIn.dispose();
|
|
208
|
+
}
|
|
209
|
+
make(id, host = Resources.host) {
|
|
210
|
+
const secret = id === GitHubAccounts.firstId ? Resources.signInSecret : Resources.formatAccountSecret(id);
|
|
211
|
+
return new GitHubSignIn(this.flow, this.secrets, this.appWindows, this.log, t => this.onChanged(id, t), secret, host);
|
|
212
|
+
}
|
|
213
|
+
async readTokenAsync(host, token, signal) {
|
|
214
|
+
let read;
|
|
215
|
+
try {
|
|
216
|
+
read = await this.flow.readAccountAsync(token, signal, host);
|
|
217
|
+
}
|
|
218
|
+
catch (error) {
|
|
219
|
+
if (!(error instanceof GitHubException))
|
|
220
|
+
throw error;
|
|
221
|
+
throw GitHubApi.failure(FailureCode.Unavailable, Resources.formatServerUnreachable(host));
|
|
222
|
+
}
|
|
223
|
+
if (read === null)
|
|
224
|
+
throw GitHubApi.failure(FailureCode.Unauthorized, Resources.formatTokenRefused(host));
|
|
225
|
+
return read;
|
|
226
|
+
}
|
|
227
|
+
onChanged(id, signIn) {
|
|
228
|
+
const twin = this.twinOf(id, signIn);
|
|
229
|
+
if (twin !== undefined) {
|
|
230
|
+
this.drop(twin);
|
|
231
|
+
if (twin === id)
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
if (id !== GitHubAccounts.firstId && signIn.state === SignInState.SignedOut && signIn.failure === null)
|
|
235
|
+
this.leave(id);
|
|
236
|
+
this.publish(id, signIn);
|
|
237
|
+
}
|
|
238
|
+
publish(id, signIn) {
|
|
239
|
+
this.found.clear();
|
|
240
|
+
this.save();
|
|
241
|
+
this.changed(id, signIn);
|
|
242
|
+
}
|
|
243
|
+
twinOf(id, signIn) {
|
|
244
|
+
const host = this.signInOf(id).host;
|
|
245
|
+
const login = GitHubAccounts.loginOf(signIn);
|
|
246
|
+
const other = login === null ? undefined : [...this.entries].find(([t, entry]) => t !== id && entry.host === host
|
|
247
|
+
&& GitHubAccounts.loginOf(entry.current) === login)?.[0];
|
|
248
|
+
if (other === undefined)
|
|
249
|
+
return undefined;
|
|
250
|
+
return id === GitHubAccounts.firstId ? other : id;
|
|
251
|
+
}
|
|
252
|
+
drop(id) {
|
|
253
|
+
const signIn = this.signInOf(id);
|
|
254
|
+
this.log.write(Resources.accountAlreadyKept);
|
|
255
|
+
this.dropping.set(id, Promise.resolve().then(() => signIn.alreadyKeptAsync())
|
|
256
|
+
.catch((error) => this.log.write(Resources.formatSignInBroken(String(error)))));
|
|
257
|
+
}
|
|
258
|
+
leave(id) {
|
|
259
|
+
this.signInOf(id).dispose();
|
|
260
|
+
this.entries.delete(id);
|
|
261
|
+
this.dropping.delete(id);
|
|
262
|
+
this.picks = this.picks.filter(t => t.account !== id);
|
|
263
|
+
}
|
|
264
|
+
async probeAsync(key, host, path, signedIn, first) {
|
|
265
|
+
const repository = GitHubRepository.parse(host, path);
|
|
266
|
+
if (repository === null)
|
|
267
|
+
return first;
|
|
268
|
+
let isSure = true;
|
|
269
|
+
for (const account of signedIn) {
|
|
270
|
+
try {
|
|
271
|
+
if (await this.api.getAsync(repository.host, repository.path, account.token, new AbortController().signal) !== null)
|
|
272
|
+
return account.id;
|
|
273
|
+
}
|
|
274
|
+
catch (error) {
|
|
275
|
+
if (!GitHubApi.isAnswered(error)) {
|
|
276
|
+
this.found.delete(key);
|
|
277
|
+
return first;
|
|
278
|
+
}
|
|
279
|
+
isSure = false;
|
|
280
|
+
if (GitHubApi.isUnauthorized(error))
|
|
281
|
+
await this.refusedAsync(account.token);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
if (!isSure)
|
|
285
|
+
this.found.delete(key);
|
|
286
|
+
return first;
|
|
287
|
+
}
|
|
288
|
+
async readKeptAsync() {
|
|
289
|
+
let kept;
|
|
290
|
+
try {
|
|
291
|
+
kept = await this.secrets.readAsync(Resources.accountsSecret);
|
|
292
|
+
}
|
|
293
|
+
catch (error) {
|
|
294
|
+
if (!(error instanceof MethodFailureException))
|
|
295
|
+
throw error;
|
|
296
|
+
if (SecretFailures.reasonOf(error.failure) === SecretFailureReason.NotConnected && this.unfollowWindows === null)
|
|
297
|
+
this.unfollowWindows = this.appWindows.onChanged(() => this.restoreOnceConnected());
|
|
298
|
+
else
|
|
299
|
+
this.log.write(Resources.formatKeptAccountsUnread(error.failure.message));
|
|
300
|
+
return null;
|
|
301
|
+
}
|
|
302
|
+
this.kept = kept;
|
|
303
|
+
if (kept === null) {
|
|
304
|
+
this.isIndexRead = true;
|
|
305
|
+
return null;
|
|
306
|
+
}
|
|
307
|
+
try {
|
|
308
|
+
const read = KeptAccounts.fromJson(JsonReader.parseValue(kept));
|
|
309
|
+
this.isIndexRead = true;
|
|
310
|
+
return read;
|
|
311
|
+
}
|
|
312
|
+
catch (error) {
|
|
313
|
+
if (!(error instanceof JsonException || error instanceof HostingProtocolException))
|
|
314
|
+
throw error;
|
|
315
|
+
this.log.write(Resources.keptAccountsUnreadable);
|
|
316
|
+
return null;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
restoreOnceConnected() {
|
|
320
|
+
this.unfollowWindows?.();
|
|
321
|
+
this.unfollowWindows = null;
|
|
322
|
+
this.restoreAsync().catch((error) => this.log.write(Resources.formatSignInBroken(String(error))));
|
|
323
|
+
}
|
|
324
|
+
save() {
|
|
325
|
+
const accounts = [];
|
|
326
|
+
for (const [id, signIn] of this.entries) {
|
|
327
|
+
const login = signIn.current.account?.login ?? null;
|
|
328
|
+
if (id !== GitHubAccounts.firstId && (login !== null || signIn.keptUnreadable))
|
|
329
|
+
accounts.push(new KeptAccount(id, signIn.host, login));
|
|
330
|
+
}
|
|
331
|
+
const value = JSON.stringify(new KeptAccounts(accounts, this.picks).toJson());
|
|
332
|
+
if (this.stopped || !this.isIndexRead || value === this.kept || (this.kept === null && accounts.length === 0 && this.picks.length === 0))
|
|
333
|
+
return;
|
|
334
|
+
this.kept = value;
|
|
335
|
+
this.saving = this.saving.then(() => this.secrets.saveAsync(Resources.accountsSecret, value)).catch((error) => this.log.write(error instanceof MethodFailureException ? Resources.accountsNotKept : Resources.formatSignInBroken(String(error))));
|
|
336
|
+
}
|
|
337
|
+
static loginOf(signIn) {
|
|
338
|
+
return signIn.account === null ? null : signIn.account.login.toLowerCase();
|
|
339
|
+
}
|
|
340
|
+
static isSameKey(host, path, otherHost, otherPath) {
|
|
341
|
+
return GitHubAccounts.keyOf(host, path) === GitHubAccounts.keyOf(otherHost, otherPath);
|
|
342
|
+
}
|
|
343
|
+
static keyOf(host, path) {
|
|
344
|
+
const key = Resources.formatDraftSubject(host, path).toLowerCase();
|
|
345
|
+
return key.endsWith(Resources.gitSuffix) ? key.slice(0, -Resources.gitSuffix.length) : key;
|
|
346
|
+
}
|
|
347
|
+
static isSameRepository(pick, host, path) {
|
|
348
|
+
return pick.host.toLowerCase() === host.toLowerCase() && pick.path.toLowerCase() === path.toLowerCase();
|
|
349
|
+
}
|
|
350
|
+
}
|
|
@@ -8,24 +8,25 @@
|
|
|
8
8
|
import { JsonException, JsonReader } from "@noldova/context.json";
|
|
9
9
|
import { Failure, FailureCode } from "@noldova/desktop.protocol";
|
|
10
10
|
import { MethodFailureException } from "@noldova/desktop.runtime";
|
|
11
|
-
import { HostingProtocolException } from "@noldova/git.hosting.protocol";
|
|
11
|
+
import { HostingNames, HostingProtocolException } from "@noldova/git.hosting.protocol";
|
|
12
|
+
import { GitHubUnreachableException } from "../exceptions/github-unreachable.exception.js";
|
|
12
13
|
import { CachedAnswer } from "../models/cached-answer.js";
|
|
13
14
|
import { Resources } from "../resources.js";
|
|
14
15
|
import { AnswerText } from "./answer-text.js";
|
|
15
16
|
export class GitHubApi {
|
|
16
17
|
fetcher;
|
|
17
18
|
answers = new Map();
|
|
18
|
-
answersToken = null;
|
|
19
19
|
constructor(fetcher) {
|
|
20
20
|
this.fetcher = fetcher;
|
|
21
21
|
}
|
|
22
|
-
async getAsync(path, token, signal) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
22
|
+
async getAsync(host, path, token, signal) {
|
|
23
|
+
let answers = this.answers.get(token);
|
|
24
|
+
if (answers === undefined) {
|
|
25
|
+
answers = new Map();
|
|
26
|
+
this.answers.set(token, answers);
|
|
26
27
|
}
|
|
27
|
-
const url = Resources.
|
|
28
|
-
const cached =
|
|
28
|
+
const url = Resources.formatApiUrl(host) + path;
|
|
29
|
+
const cached = answers.get(url);
|
|
29
30
|
const now = Date.now();
|
|
30
31
|
if (cached !== undefined && now - cached.readAt < Resources.freshFor)
|
|
31
32
|
return this.reuse(url, cached, cached.readAt, token);
|
|
@@ -45,8 +46,8 @@ export class GitHubApi {
|
|
|
45
46
|
this.keep(url, new CachedAnswer(response.headers.get(Resources.tagHeader), body, now), token);
|
|
46
47
|
return reader;
|
|
47
48
|
}
|
|
48
|
-
async postAsync(path, token, body, signal) {
|
|
49
|
-
const response = await this.fetchAsync(Resources.
|
|
49
|
+
async postAsync(host, path, token, body, signal) {
|
|
50
|
+
const response = await this.fetchAsync(Resources.formatApiUrl(host) + path, {
|
|
50
51
|
method: Resources.postMethod,
|
|
51
52
|
headers: { ...GitHubApi.headersOf(token), [Resources.contentTypeHeader]: Resources.jsonType },
|
|
52
53
|
body: JSON.stringify(body)
|
|
@@ -61,9 +62,14 @@ export class GitHubApi {
|
|
|
61
62
|
}
|
|
62
63
|
return GitHubApi.readerOf(await GitHubApi.bodyOf(response, signal));
|
|
63
64
|
}
|
|
64
|
-
|
|
65
|
-
this.answers.
|
|
66
|
-
|
|
65
|
+
keepOnly(isHeld) {
|
|
66
|
+
for (const token of [...this.answers.keys()]) {
|
|
67
|
+
if (!isHeld(token))
|
|
68
|
+
this.answers.delete(token);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
static isAnswered(error) {
|
|
72
|
+
return error instanceof MethodFailureException && !(error instanceof GitHubUnreachableException);
|
|
67
73
|
}
|
|
68
74
|
static isUnauthorized(error) {
|
|
69
75
|
return error instanceof MethodFailureException && error.failure.code === FailureCode.Unauthorized;
|
|
@@ -86,16 +92,17 @@ export class GitHubApi {
|
|
|
86
92
|
return GitHubApi.readerOf(cached.body);
|
|
87
93
|
}
|
|
88
94
|
keep(url, answer, token) {
|
|
89
|
-
|
|
95
|
+
const answers = this.answers.get(token);
|
|
96
|
+
if (answers === undefined)
|
|
90
97
|
return;
|
|
91
|
-
|
|
98
|
+
answers.delete(url);
|
|
92
99
|
if (answer.body.length > Resources.maximumKeptLength)
|
|
93
100
|
return;
|
|
94
|
-
|
|
95
|
-
for (const oldest of
|
|
96
|
-
if (
|
|
101
|
+
answers.set(url, answer);
|
|
102
|
+
for (const oldest of answers.keys()) {
|
|
103
|
+
if (answers.size <= Resources.cacheSize)
|
|
97
104
|
break;
|
|
98
|
-
|
|
105
|
+
answers.delete(oldest);
|
|
99
106
|
}
|
|
100
107
|
}
|
|
101
108
|
async fetchAsync(url, init, signal) {
|
|
@@ -129,7 +136,7 @@ export class GitHubApi {
|
|
|
129
136
|
}
|
|
130
137
|
static apiUrlOf(address, base) {
|
|
131
138
|
const url = address === null ? null : URL.parse(address, base);
|
|
132
|
-
return url !== null && url.origin ===
|
|
139
|
+
return url !== null && url.origin === new URL(base).origin && url.username.length === 0 && url.password.length === 0 ? url : null;
|
|
133
140
|
}
|
|
134
141
|
static headersOf(token) {
|
|
135
142
|
return {
|
|
@@ -140,7 +147,7 @@ export class GitHubApi {
|
|
|
140
147
|
static async bodyOf(response, signal) {
|
|
141
148
|
if (GitHubApi.isRateLimited(response)) {
|
|
142
149
|
await GitHubApi.dropBodyAsync(response);
|
|
143
|
-
throw GitHubApi.
|
|
150
|
+
throw GitHubApi.rateLimited(response.headers.get(Resources.rateLimitResetHeader));
|
|
144
151
|
}
|
|
145
152
|
if (Resources.redirectStatuses.includes(response.status)) {
|
|
146
153
|
await GitHubApi.dropBodyAsync(response);
|
|
@@ -169,19 +176,21 @@ export class GitHubApi {
|
|
|
169
176
|
if (signal.aborted)
|
|
170
177
|
return signal.reason;
|
|
171
178
|
if (error instanceof DOMException && error.name === Resources.timeoutErrorName)
|
|
172
|
-
return
|
|
179
|
+
return new GitHubUnreachableException(FailureCode.DeadlineExceeded);
|
|
173
180
|
if (error instanceof TypeError)
|
|
174
|
-
return
|
|
181
|
+
return new GitHubUnreachableException(FailureCode.Unavailable);
|
|
175
182
|
return error;
|
|
176
183
|
}
|
|
177
184
|
static isRateLimited(response) {
|
|
178
185
|
return response.status === Resources.tooManyRequestsStatus
|
|
179
186
|
|| response.status === Resources.forbiddenStatus && response.headers.get(Resources.rateLimitRemainingHeader) === Resources.noneRemaining;
|
|
180
187
|
}
|
|
181
|
-
static
|
|
188
|
+
static rateLimited(reset) {
|
|
182
189
|
const seconds = Number(reset ?? "");
|
|
183
|
-
|
|
184
|
-
|
|
190
|
+
if (!Number.isSafeInteger(seconds) || seconds <= 0)
|
|
191
|
+
return GitHubApi.failure(FailureCode.Unavailable, Resources.rateLimitedMessage);
|
|
192
|
+
const until = seconds * Resources.millisecondsPerSecond;
|
|
193
|
+
return new MethodFailureException(new Failure(FailureCode.Unavailable, Resources.formatRateLimited(new Date(until).toISOString()), { [HostingNames.rateLimitedUntilDetail]: until }));
|
|
185
194
|
}
|
|
186
195
|
static readerOf(body) {
|
|
187
196
|
return GitHubApi.read(() => {
|
|
@@ -55,8 +55,9 @@ export class GitHubDeviceFlow {
|
|
|
55
55
|
throw new SignInErrorException(error);
|
|
56
56
|
});
|
|
57
57
|
}
|
|
58
|
-
async readAccountAsync(token, signal) {
|
|
59
|
-
const
|
|
58
|
+
async readAccountAsync(token, signal, host = Resources.host) {
|
|
59
|
+
const url = Resources.formatUserUrl(host);
|
|
60
|
+
const response = await this.fetchAsync(url, {
|
|
60
61
|
headers: {
|
|
61
62
|
[Resources.acceptHeader]: Resources.gitHubJsonType, [Resources.authorizationHeader]: Resources.formatBearer(token),
|
|
62
63
|
[Resources.userAgentHeader]: Resources.userAgent, [Resources.apiVersionHeader]: Resources.apiVersion
|
|
@@ -66,7 +67,7 @@ export class GitHubDeviceFlow {
|
|
|
66
67
|
await GitHubDeviceFlow.dropBodyAsync(response);
|
|
67
68
|
return null;
|
|
68
69
|
}
|
|
69
|
-
const reader = await GitHubDeviceFlow.readBodyAsync(
|
|
70
|
+
const reader = await GitHubDeviceFlow.readBodyAsync(url, response, signal);
|
|
70
71
|
const scopes = (response.headers.get(Resources.scopesHeader) ?? "").split(Resources.scopeSeparator).map(t => t.trim()).filter(t => t.length > 0);
|
|
71
72
|
return GitHubDeviceFlow.read(() => new AccountRead(new HostAccount(reader.readNonBlankString(Resources.loginField), reader.readNullableString(Resources.nameField), null, scopes), reader.readNullableString(Resources.avatarUrlField)));
|
|
72
73
|
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import { MethodFailureException, ServiceAccessException } from "@noldova/desktop.runtime";
|
|
9
|
+
import { RemoteUser } from "../models/remote-user.js";
|
|
10
|
+
import { GitHubAccounts } from "./github-accounts.js";
|
|
11
|
+
export class GitHubFolderUsers {
|
|
12
|
+
repositories;
|
|
13
|
+
constructor(repositories) {
|
|
14
|
+
this.repositories = repositories;
|
|
15
|
+
}
|
|
16
|
+
async userOfAsync(folder, remote, host, path, signal) {
|
|
17
|
+
let repositories;
|
|
18
|
+
try {
|
|
19
|
+
repositories = this.repositories();
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
if (!(error instanceof ServiceAccessException))
|
|
23
|
+
throw error;
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
const found = await repositories.readRemoteAsync(folder, remote, signal);
|
|
28
|
+
if (found === null || found.host === null || found.path === null || !GitHubAccounts.isSameKey(found.host, found.path, host, path))
|
|
29
|
+
return null;
|
|
30
|
+
return new RemoteUser(await repositories.readRemoteUserAsync(folder, remote, signal));
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
if (!(error instanceof MethodFailureException))
|
|
34
|
+
throw error;
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|