@noldova/git.github 0.0.26 → 0.0.28
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 +3 -3
- package/package.json +11 -11
- package/src/models/rate-limit-window.js +18 -0
- package/src/resources.js +2 -0
- package/src/services/github-api.js +28 -8
- package/src/services/github-reads.js +15 -15
package/module.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"id": "git-github",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.28",
|
|
4
4
|
"displayName": "GitHub",
|
|
5
5
|
"description": "Signs in to GitHub, shows its issues, pull requests and checks, and opens pull requests, and lets AI agents do the same through permission requests.",
|
|
6
6
|
"parts": ["runtime", "window"],
|
|
7
|
-
"dependencies": { "git": "0.0.
|
|
8
|
-
"noldovaDesktop": ">=0.0.
|
|
7
|
+
"dependencies": { "git": "0.0.28" },
|
|
8
|
+
"noldovaDesktop": ">=0.0.36 <0.1.0",
|
|
9
9
|
"contributes": {
|
|
10
10
|
"notifications": ["git-github.actionFailed", "git-github.actionWarning", "git-github.actionDone"],
|
|
11
11
|
"methods": ["git-github.readSignIn", "git-github.startSignIn", "git-github.cancelSignIn", "git-github.signOut", "git-github.readReference",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@noldova/git.github",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.28",
|
|
4
4
|
"description": "The GitHub module for apps on Noldova Desktop: the sign-in to GitHub, its issues, pull requests and checks, and agent tools that use GitHub through permission requests.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -35,17 +35,17 @@
|
|
|
35
35
|
"registry": "https://registry.npmjs.org/"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@noldova/git.github.protocol": "0.0.
|
|
39
|
-
"@noldova/git.protocol": "0.0.
|
|
40
|
-
"@noldova/git.github.window": "0.0.
|
|
41
|
-
"@noldova/git.hosting.protocol": "0.0.
|
|
38
|
+
"@noldova/git.github.protocol": "0.0.28",
|
|
39
|
+
"@noldova/git.protocol": "0.0.28",
|
|
40
|
+
"@noldova/git.github.window": "0.0.28",
|
|
41
|
+
"@noldova/git.hosting.protocol": "0.0.28"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
|
-
"@noldova/desktop.protocol": ">=0.0.
|
|
45
|
-
"@noldova/desktop.runtime": ">=0.0.
|
|
46
|
-
"@noldova/context.core": ">=0.0.
|
|
47
|
-
"@noldova/context.exceptions": ">=0.0.
|
|
48
|
-
"@noldova/context.json": ">=0.0.
|
|
44
|
+
"@noldova/desktop.protocol": ">=0.0.36 <0.1.0",
|
|
45
|
+
"@noldova/desktop.runtime": ">=0.0.36 <0.1.0",
|
|
46
|
+
"@noldova/context.core": ">=0.0.20 <0.1.0",
|
|
47
|
+
"@noldova/context.exceptions": ">=0.0.20 <0.1.0",
|
|
48
|
+
"@noldova/context.json": ">=0.0.20 <0.1.0"
|
|
49
49
|
},
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "b945c710d0c4beeb3967877631b42fe6776be565"
|
|
51
51
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
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
|
+
export class RateLimitWindow {
|
|
9
|
+
remaining;
|
|
10
|
+
resetAt;
|
|
11
|
+
constructor(remaining, resetAt) {
|
|
12
|
+
this.remaining = remaining;
|
|
13
|
+
this.resetAt = resetAt;
|
|
14
|
+
}
|
|
15
|
+
isNearAt(now, floor) {
|
|
16
|
+
return this.remaining < floor && now < this.resetAt;
|
|
17
|
+
}
|
|
18
|
+
}
|
package/src/resources.js
CHANGED
|
@@ -85,6 +85,8 @@ export class Resources {
|
|
|
85
85
|
static rateLimitRemainingHeader = "x-ratelimit-remaining";
|
|
86
86
|
static rateLimitResetHeader = "x-ratelimit-reset";
|
|
87
87
|
static noneRemaining = "0";
|
|
88
|
+
static retryAfterHeader = "retry-after";
|
|
89
|
+
static nearLimitFloor = 100;
|
|
88
90
|
static rateLimitedMessage = "GitHub's rate limit for this account is used up; try again later.";
|
|
89
91
|
static ifNoneMatchHeader = "If-None-Match";
|
|
90
92
|
static tagHeader = "etag";
|
|
@@ -12,11 +12,13 @@ import { MethodFailureException } from "@noldova/desktop.runtime";
|
|
|
12
12
|
import { HostingNames, HostingProtocolException } from "@noldova/git.hosting.protocol";
|
|
13
13
|
import { GitHubUnreachableException } from "../exceptions/github-unreachable.exception.js";
|
|
14
14
|
import { CachedAnswer } from "../models/cached-answer.js";
|
|
15
|
+
import { RateLimitWindow } from "../models/rate-limit-window.js";
|
|
15
16
|
import { Resources } from "../resources.js";
|
|
16
17
|
import { AnswerText } from "./answer-text.js";
|
|
17
18
|
export class GitHubApi {
|
|
18
19
|
fetcher;
|
|
19
20
|
answers = new Map();
|
|
21
|
+
limits = new Map();
|
|
20
22
|
constructor(fetcher) {
|
|
21
23
|
this.fetcher = fetcher;
|
|
22
24
|
}
|
|
@@ -29,13 +31,15 @@ export class GitHubApi {
|
|
|
29
31
|
const url = Resources.formatApiUrl(host) + path;
|
|
30
32
|
const cached = answers.get(url);
|
|
31
33
|
const now = Date.now();
|
|
32
|
-
|
|
34
|
+
const isNearLimit = this.limits.get(token)?.isNearAt(now, Resources.nearLimitFloor) ?? false;
|
|
35
|
+
if (!Object.isUndefined(cached) && (isNearLimit || !isFresh && now - cached.readAt < Resources.freshFor))
|
|
33
36
|
return this.reuse(url, cached, cached.readAt, token);
|
|
34
37
|
const headers = GitHubApi.headersOf(token);
|
|
35
38
|
const tag = cached?.tag ?? null;
|
|
36
39
|
if (!Object.isNull(tag))
|
|
37
40
|
headers[Resources.ifNoneMatchHeader] = tag;
|
|
38
41
|
const response = await this.followAsync(url, { headers }, signal);
|
|
42
|
+
this.noteLimit(token, response);
|
|
39
43
|
if (response.status === Resources.notModifiedStatus && !Object.isUndefined(cached))
|
|
40
44
|
return this.reuse(url, cached, now, token);
|
|
41
45
|
if (response.status === Resources.notFoundStatus) {
|
|
@@ -68,6 +72,10 @@ export class GitHubApi {
|
|
|
68
72
|
if (!isHeld(token))
|
|
69
73
|
this.answers.delete(token);
|
|
70
74
|
}
|
|
75
|
+
for (const token of [...this.limits.keys()]) {
|
|
76
|
+
if (!isHeld(token))
|
|
77
|
+
this.limits.delete(token);
|
|
78
|
+
}
|
|
71
79
|
}
|
|
72
80
|
static isAnswered(error) {
|
|
73
81
|
return error instanceof MethodFailureException && !(error instanceof GitHubUnreachableException);
|
|
@@ -88,6 +96,12 @@ export class GitHubApi {
|
|
|
88
96
|
throw error;
|
|
89
97
|
}
|
|
90
98
|
}
|
|
99
|
+
noteLimit(token, response) {
|
|
100
|
+
const remaining = Number(response.headers.get(Resources.rateLimitRemainingHeader) ?? "");
|
|
101
|
+
const reset = Number(response.headers.get(Resources.rateLimitResetHeader) ?? "");
|
|
102
|
+
if (Number.isSafeInteger(remaining) && remaining >= 0 && Number.isSafeInteger(reset) && reset > 0)
|
|
103
|
+
this.limits.set(token, new RateLimitWindow(remaining, reset * Resources.millisecondsPerSecond));
|
|
104
|
+
}
|
|
91
105
|
reuse(url, cached, readAt, token) {
|
|
92
106
|
this.keep(url, cached.readAgainAt(readAt), token);
|
|
93
107
|
return GitHubApi.readerOf(cached.body);
|
|
@@ -148,7 +162,7 @@ export class GitHubApi {
|
|
|
148
162
|
static async bodyOf(response, signal) {
|
|
149
163
|
if (GitHubApi.isRateLimited(response)) {
|
|
150
164
|
await GitHubApi.dropBodyAsync(response);
|
|
151
|
-
throw GitHubApi.rateLimited(
|
|
165
|
+
throw GitHubApi.rateLimited(GitHubApi.limitedUntilOf(response));
|
|
152
166
|
}
|
|
153
167
|
if (Resources.redirectStatuses.includes(response.status)) {
|
|
154
168
|
await GitHubApi.dropBodyAsync(response);
|
|
@@ -184,13 +198,19 @@ export class GitHubApi {
|
|
|
184
198
|
}
|
|
185
199
|
static isRateLimited(response) {
|
|
186
200
|
return response.status === Resources.tooManyRequestsStatus
|
|
187
|
-
|| response.status === Resources.forbiddenStatus && response.headers.get(Resources.rateLimitRemainingHeader) === Resources.noneRemaining
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
201
|
+
|| response.status === Resources.forbiddenStatus && (response.headers.get(Resources.rateLimitRemainingHeader) === Resources.noneRemaining
|
|
202
|
+
|| response.headers.has(Resources.retryAfterHeader));
|
|
203
|
+
}
|
|
204
|
+
static limitedUntilOf(response) {
|
|
205
|
+
const after = Number(response.headers.get(Resources.retryAfterHeader) ?? "");
|
|
206
|
+
if (Number.isSafeInteger(after) && after > 0)
|
|
207
|
+
return Date.now() + after * Resources.millisecondsPerSecond;
|
|
208
|
+
const reset = Number(response.headers.get(Resources.rateLimitResetHeader) ?? "");
|
|
209
|
+
return Number.isSafeInteger(reset) && reset > 0 ? reset * Resources.millisecondsPerSecond : null;
|
|
210
|
+
}
|
|
211
|
+
static rateLimited(until) {
|
|
212
|
+
if (Object.isNull(until))
|
|
192
213
|
return GitHubApi.failure(FailureCode.Unavailable, Resources.rateLimitedMessage);
|
|
193
|
-
const until = seconds * Resources.millisecondsPerSecond;
|
|
194
214
|
return new MethodFailureException(new Failure(FailureCode.Unavailable, Resources.formatRateLimited(new Date(until).toISOString()), { [HostingNames.rateLimitedUntilDetail]: until }));
|
|
195
215
|
}
|
|
196
216
|
static readerOf(body) {
|
|
@@ -41,11 +41,11 @@ export class GitHubReads {
|
|
|
41
41
|
return none;
|
|
42
42
|
const answer = await this.askAsync(token, async () => {
|
|
43
43
|
const head = encodeURIComponent(Resources.formatHeadFilter(repository.owner, query.branch));
|
|
44
|
-
const list = await this.api.getAsync(repository.host, repository.pathOf(Resources.formatBranchPullsPath(head)), token, signal);
|
|
44
|
+
const list = await this.api.getAsync(repository.host, repository.pathOf(Resources.formatBranchPullsPath(head)), token, signal, query.isFresh);
|
|
45
45
|
const first = Object.isNull(list) ? undefined : GitHubApi.read(() => list.readObjectArray(Resources.itemsField))[0];
|
|
46
46
|
if (Object.isUndefined(first))
|
|
47
47
|
return none;
|
|
48
|
-
return await this.readPullAnswerAsync(repository, GitHubApi.read(() => first.readInteger(Resources.numberField)), query.headCommit, token, signal);
|
|
48
|
+
return await this.readPullAnswerAsync(repository, GitHubApi.read(() => first.readInteger(Resources.numberField)), query.headCommit, token, signal, query.isFresh);
|
|
49
49
|
});
|
|
50
50
|
return this.tokens.holds(token) ? answer : none;
|
|
51
51
|
}
|
|
@@ -156,7 +156,7 @@ export class GitHubReads {
|
|
|
156
156
|
return null;
|
|
157
157
|
const commit = GitHubApi.read(() => pull.readObject(Resources.headField).readString(Resources.shaField));
|
|
158
158
|
const [avatar, reviews, runs] = await Promise.all([this.avatars.readAsync(GitHubApi.read(() => GitHubReads.avatarUrlOf(pull)), signal),
|
|
159
|
-
this.readReviewsAsync(repository, reference.number, token, signal), this.readCheckRunsAsync(repository, commit, token, signal)]);
|
|
159
|
+
this.readReviewsAsync(repository, reference.number, token, signal), this.readCheckRunsAsync(repository, commit, token, signal, false)]);
|
|
160
160
|
signal.throwIfAborted();
|
|
161
161
|
return GitHubApi.read(() => GitHubReads.pullRequestPageOf(reference, pull, avatar, reviews, runs));
|
|
162
162
|
});
|
|
@@ -217,23 +217,23 @@ export class GitHubReads {
|
|
|
217
217
|
});
|
|
218
218
|
return this.tokens.holds(token) ? item : null;
|
|
219
219
|
}
|
|
220
|
-
async readPullAnswerAsync(repository, number, headCommit, token, signal) {
|
|
221
|
-
const pull = await this.api.getAsync(repository.host, repository.pathOf(Resources.formatPullPath(number)), token, signal);
|
|
220
|
+
async readPullAnswerAsync(repository, number, headCommit, token, signal, isFresh = false) {
|
|
221
|
+
const pull = await this.api.getAsync(repository.host, repository.pathOf(Resources.formatPullPath(number)), token, signal, isFresh);
|
|
222
222
|
if (Object.isNull(pull))
|
|
223
223
|
return new BranchPullRequest(null, []);
|
|
224
224
|
const pullRequest = GitHubApi.read(() => GitHubReads.pullRequestOf(repository.referenceOf(number), pull, null));
|
|
225
225
|
const commit = headCommit ?? GitHubApi.read(() => pull.readObject(Resources.headField).readString(Resources.shaField));
|
|
226
|
-
const checks = await this.readChecksAsync(repository, commit, token, signal);
|
|
227
|
-
return new BranchPullRequest(pullRequest, checks, await this.readReviewDecisionAsync(repository, number, token, signal));
|
|
226
|
+
const checks = await this.readChecksAsync(repository, commit, token, signal, isFresh);
|
|
227
|
+
return new BranchPullRequest(pullRequest, checks, await this.readReviewDecisionAsync(repository, number, token, signal, isFresh));
|
|
228
228
|
}
|
|
229
|
-
async readChecksAsync(repository, commit, token, signal) {
|
|
230
|
-
const runs = await this.readCheckRunsAsync(repository, commit, token, signal);
|
|
229
|
+
async readChecksAsync(repository, commit, token, signal, isFresh = false) {
|
|
230
|
+
const runs = await this.readCheckRunsAsync(repository, commit, token, signal, isFresh);
|
|
231
231
|
return GitHubApi.read(() => runs.map(GitHubReads.checkOf));
|
|
232
232
|
}
|
|
233
|
-
async readCheckRunsAsync(repository, commit, token, signal) {
|
|
233
|
+
async readCheckRunsAsync(repository, commit, token, signal, isFresh) {
|
|
234
234
|
const runs = [];
|
|
235
235
|
for (let page = 1;; page++) {
|
|
236
|
-
const answer = await this.api.getAsync(repository.host, repository.pathOf(Resources.formatCheckRunsPath(commit, page)), token, signal);
|
|
236
|
+
const answer = await this.api.getAsync(repository.host, repository.pathOf(Resources.formatCheckRunsPath(commit, page)), token, signal, isFresh);
|
|
237
237
|
if (Object.isNull(answer))
|
|
238
238
|
return runs;
|
|
239
239
|
const total = GitHubApi.read(() => answer.readInteger(Resources.totalCountField));
|
|
@@ -275,15 +275,15 @@ export class GitHubReads {
|
|
|
275
275
|
const [checks, verdicts] = await Promise.all([this.readChecksAsync(repository, commit, token, signal), this.readVerdictsAsync(repository, number, token, signal)]);
|
|
276
276
|
return GitHubApi.read(() => HostPullRequestRow.of(repository.referenceOf(number), pull.readString(Resources.titleField), pull.readString(Resources.htmlUrlField), pull.readBoolean(Resources.draftField), verdicts.filter(t => t === Resources.approvedReview).length, checks, head.readString(Resources.refField), pull.readObject(Resources.baseField).readString(Resources.refField)));
|
|
277
277
|
}
|
|
278
|
-
async readReviewDecisionAsync(repository, number, token, signal) {
|
|
279
|
-
const verdicts = await this.readVerdictsAsync(repository, number, token, signal);
|
|
278
|
+
async readReviewDecisionAsync(repository, number, token, signal, isFresh) {
|
|
279
|
+
const verdicts = await this.readVerdictsAsync(repository, number, token, signal, isFresh);
|
|
280
280
|
return verdicts.includes(Resources.changesRequestedReview) ? ReviewDecision.ChangesRequested
|
|
281
281
|
: verdicts.includes(Resources.approvedReview) ? ReviewDecision.Approved : ReviewDecision.Waiting;
|
|
282
282
|
}
|
|
283
|
-
async readVerdictsAsync(repository, number, token, signal) {
|
|
283
|
+
async readVerdictsAsync(repository, number, token, signal, isFresh = false) {
|
|
284
284
|
const latest = new Map();
|
|
285
285
|
for (let page = 1;; page++) {
|
|
286
|
-
const reviews = await this.api.getAsync(repository.host, repository.pathOf(Resources.formatReviewsPath(number, page)), token, signal);
|
|
286
|
+
const reviews = await this.api.getAsync(repository.host, repository.pathOf(Resources.formatReviewsPath(number, page)), token, signal, isFresh);
|
|
287
287
|
const read = Object.isNull(reviews) ? []
|
|
288
288
|
: GitHubApi.read(() => reviews.readObjectArray(Resources.itemsField).map(GitHubReads.reviewOf));
|
|
289
289
|
for (const { login, state } of read)
|