@noldova/git.github 0.0.12 → 0.0.14
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 +5 -4
- package/package.json +8 -8
- package/src/resources.js +4 -0
- package/src/runtime-part.js +5 -2
- package/src/services/github-api.js +2 -2
- package/src/services/github-avatars.js +22 -12
- package/src/services/github-reads.js +36 -26
package/module.json
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"id": "git-github",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.14",
|
|
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.14" },
|
|
8
|
+
"noldovaDesktop": ">=0.0.21 <0.1.0",
|
|
9
9
|
"contributes": {
|
|
10
10
|
"methods": ["git-github.readSignIn", "git-github.startSignIn", "git-github.cancelSignIn", "git-github.signOut", "git-github.readReference",
|
|
11
11
|
"git-github.readBranchPullRequest", "git-github.createPullRequest",
|
|
12
12
|
"git-github.readAuthorAvatar", "git-github.readPullRequests", "git-github.readReviewRequests",
|
|
13
13
|
"git-github.readCommitComments", "git-github.readPullRequest", "git-github.readPullRequestThreads", "git-github.readCommitPullRequest",
|
|
14
14
|
"git-github.readAccounts", "git-github.addAccount", "git-github.readRepositoryAccount", "git-github.pickAccount",
|
|
15
|
-
"git-github.useFolder", "git-github.signInWithToken", "git-github.readRepositories"
|
|
15
|
+
"git-github.useFolder", "git-github.signInWithToken", "git-github.readRepositories",
|
|
16
|
+
"git-github.searchRepositories"],
|
|
16
17
|
"events": ["git-github.signInChanged", "git-github.accountsChanged"],
|
|
17
18
|
"tools": ["git_github_read", "git_github_pr_checks", "git_github_pr_create"]
|
|
18
19
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@noldova/git.github",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.14",
|
|
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.14",
|
|
39
|
+
"@noldova/git.protocol": "0.0.14",
|
|
40
|
+
"@noldova/git.github.window": "0.0.14",
|
|
41
|
+
"@noldova/git.hosting.protocol": "0.0.14"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
|
-
"@noldova/desktop.protocol": ">=0.0.
|
|
45
|
-
"@noldova/desktop.runtime": ">=0.0.
|
|
44
|
+
"@noldova/desktop.protocol": ">=0.0.21 <0.1.0",
|
|
45
|
+
"@noldova/desktop.runtime": ">=0.0.21 <0.1.0",
|
|
46
46
|
"@noldova/context.core": ">=0.0.18 <0.1.0",
|
|
47
47
|
"@noldova/context.exceptions": ">=0.0.18 <0.1.0",
|
|
48
48
|
"@noldova/context.json": ">=0.0.18 <0.1.0"
|
|
49
49
|
},
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "a900e302eac2bbdfd14e6727962556c66b156e7a"
|
|
51
51
|
}
|
package/src/resources.js
CHANGED
|
@@ -35,6 +35,7 @@ export class Resources {
|
|
|
35
35
|
static scopeSeparator = ",";
|
|
36
36
|
static requestTimeout = 30_000;
|
|
37
37
|
static avatarHost = "avatars.githubusercontent.com";
|
|
38
|
+
static avatarSubdomain = "avatars.";
|
|
38
39
|
static noReplyPattern = /^(?:(\d{1,12})\+)?([a-z\d](?:[a-z\d-]{0,38}))@users\.noreply\.github\.com$/u;
|
|
39
40
|
static httpsProtocol = "https:";
|
|
40
41
|
static avatarSizeParameter = "s";
|
|
@@ -120,6 +121,8 @@ export class Resources {
|
|
|
120
121
|
static htmlUrlField = "html_url";
|
|
121
122
|
static fullNameField = "full_name";
|
|
122
123
|
static cloneUrlField = "clone_url";
|
|
124
|
+
static ownerField = "owner";
|
|
125
|
+
static forkField = "fork";
|
|
123
126
|
static privateField = "private";
|
|
124
127
|
static pushedAtField = "pushed_at";
|
|
125
128
|
static createdAtField = "created_at";
|
|
@@ -245,6 +248,7 @@ export class Resources {
|
|
|
245
248
|
static repositoriesPath = "/user/repos?affiliation=owner,collaborator,organization_member&sort=pushed&per_page=100";
|
|
246
249
|
static repositoriesPageSize = 100;
|
|
247
250
|
static repositoryPages = 3;
|
|
251
|
+
static repositorySearchPages = 10;
|
|
248
252
|
static checkRunsPageSize = 100;
|
|
249
253
|
static reviewsPageSize = 100;
|
|
250
254
|
static commentsPageSize = 100;
|
package/src/runtime-part.js
CHANGED
|
@@ -9,7 +9,7 @@ import "@noldova/context.core";
|
|
|
9
9
|
import { JsonException } from "@noldova/context.json";
|
|
10
10
|
import { Failure, FailureCode, ShellClients } from "@noldova/desktop.protocol";
|
|
11
11
|
import { MethodFailureException, ServiceAccessException } from "@noldova/desktop.runtime";
|
|
12
|
-
import { AccountPick, AccountQuery, AuthorQuery, BranchQuery, CommitQuery, FolderQuery, HostAvatar, HostingProtocolException, HostReference, PullRequestDraft, RepositoryAccount, RepositoryQuery } from "@noldova/git.hosting.protocol";
|
|
12
|
+
import { AccountPick, AccountQuery, AuthorQuery, BranchQuery, CommitQuery, FolderQuery, HostAvatar, HostingProtocolException, HostReference, PullRequestDraft, RepositoryAccount, RepositoryListQuery, RepositoryQuery, RepositorySearch } from "@noldova/git.hosting.protocol";
|
|
13
13
|
import { GitHubNames } from "@noldova/git.github.protocol";
|
|
14
14
|
import { GitNames, GitRepositories } from "@noldova/git.protocol";
|
|
15
15
|
import { SignInMethod } from "./models/sign-in-method.js";
|
|
@@ -68,7 +68,10 @@ export class RuntimePart {
|
|
|
68
68
|
handleAsync: async (request) => (await reads.readPullRequestsAsync(RuntimePart.read(request, RepositoryQuery.fromJson), request.signal)).toJson()
|
|
69
69
|
});
|
|
70
70
|
context.registerMethod(GitHubNames.readRepositoriesMethod, {
|
|
71
|
-
handleAsync: async (request) => (await reads.readRepositoriesAsync(RuntimePart.read(request,
|
|
71
|
+
handleAsync: async (request) => (await reads.readRepositoriesAsync(RuntimePart.read(request, RepositoryListQuery.fromJson), request.signal)).toJson()
|
|
72
|
+
});
|
|
73
|
+
context.registerMethod(GitHubNames.searchRepositoriesMethod, {
|
|
74
|
+
handleAsync: async (request) => (await reads.searchRepositoriesAsync(RuntimePart.read(request, RepositorySearch.fromJson), request.signal)).toJson()
|
|
72
75
|
});
|
|
73
76
|
context.registerMethod(GitHubNames.readReviewRequestsMethod, {
|
|
74
77
|
handleAsync: async (request) => (await reads.readReviewRequestsAsync(RuntimePart.read(request, RepositoryQuery.fromJson), request.signal)).toJson()
|
|
@@ -20,7 +20,7 @@ export class GitHubApi {
|
|
|
20
20
|
constructor(fetcher) {
|
|
21
21
|
this.fetcher = fetcher;
|
|
22
22
|
}
|
|
23
|
-
async getAsync(host, path, token, signal) {
|
|
23
|
+
async getAsync(host, path, token, signal, isFresh = false) {
|
|
24
24
|
let answers = this.answers.get(token);
|
|
25
25
|
if (Object.isUndefined(answers)) {
|
|
26
26
|
answers = new Map();
|
|
@@ -29,7 +29,7 @@ export class GitHubApi {
|
|
|
29
29
|
const url = Resources.formatApiUrl(host) + path;
|
|
30
30
|
const cached = answers.get(url);
|
|
31
31
|
const now = Date.now();
|
|
32
|
-
if (!Object.isUndefined(cached) && now - cached.readAt < Resources.freshFor)
|
|
32
|
+
if (!isFresh && !Object.isUndefined(cached) && now - cached.readAt < Resources.freshFor)
|
|
33
33
|
return this.reuse(url, cached, cached.readAt, token);
|
|
34
34
|
const headers = GitHubApi.headersOf(token);
|
|
35
35
|
const tag = cached?.tag ?? null;
|
|
@@ -15,41 +15,45 @@ export class GitHubAvatars {
|
|
|
15
15
|
this.fetcher = fetcher;
|
|
16
16
|
this.log = log;
|
|
17
17
|
}
|
|
18
|
-
async readAsync(address, signal) {
|
|
19
|
-
const
|
|
18
|
+
async readAsync(address, signal, server = null, token = null) {
|
|
19
|
+
const hosts = GitHubAvatars.hostsOf(server);
|
|
20
|
+
const url = GitHubAvatars.avatarUrlOf(address, undefined, hosts);
|
|
20
21
|
if (Object.isNull(url))
|
|
21
22
|
return null;
|
|
22
23
|
url.searchParams.set(Resources.avatarSizeParameter, Resources.avatarSize);
|
|
23
|
-
const avatar = this.kept.get(url.href) ?? await this.fetchWithinAsync(url, signal);
|
|
24
|
+
const avatar = this.kept.get(url.href) ?? await this.fetchWithinAsync(url, hosts, token, signal);
|
|
24
25
|
if (!Object.isNull(avatar))
|
|
25
26
|
this.keep(url.href, avatar);
|
|
26
27
|
return avatar;
|
|
27
28
|
}
|
|
28
|
-
async fetchWithinAsync(first, signal) {
|
|
29
|
+
async fetchWithinAsync(first, hosts, token, signal) {
|
|
29
30
|
const deadline = new AbortController();
|
|
30
31
|
const timer = setTimeout(() => deadline.abort(), Resources.avatarDeadline);
|
|
31
32
|
try {
|
|
32
|
-
return await this.fetchAsync(first, signal, AbortSignal.any([signal, deadline.signal]));
|
|
33
|
+
return await this.fetchAsync(first, hosts, token, signal, AbortSignal.any([signal, deadline.signal]));
|
|
33
34
|
}
|
|
34
35
|
finally {
|
|
35
36
|
clearTimeout(timer);
|
|
36
37
|
}
|
|
37
38
|
}
|
|
38
|
-
async fetchAsync(first, signal, within) {
|
|
39
|
+
async fetchAsync(first, hosts, token, signal, within) {
|
|
39
40
|
let url = first;
|
|
40
41
|
for (let hop = 0; !Object.isNull(url) && hop <= Resources.avatarRedirects; hop++) {
|
|
41
|
-
const response = await this.askAsync(url, signal, within);
|
|
42
|
+
const response = await this.askAsync(url, url.host === hosts[0] ? token : null, signal, within);
|
|
42
43
|
if (Object.isNull(response) || !Resources.redirectStatuses.includes(response.status))
|
|
43
44
|
return Object.isNull(response) ? null : this.dataOfAsync(response, signal, within);
|
|
44
45
|
await response.body?.cancel();
|
|
45
|
-
url = GitHubAvatars.avatarUrlOf(response.headers.get(Resources.locationHeader), url.href);
|
|
46
|
+
url = GitHubAvatars.avatarUrlOf(response.headers.get(Resources.locationHeader), url.href, hosts);
|
|
46
47
|
}
|
|
47
48
|
return this.drop(Resources.avatarRedirectedAway);
|
|
48
49
|
}
|
|
49
|
-
async askAsync(url, signal, within) {
|
|
50
|
+
async askAsync(url, token, signal, within) {
|
|
51
|
+
const headers = { [Resources.acceptHeader]: Resources.avatarAccept, [Resources.userAgentHeader]: Resources.userAgent };
|
|
52
|
+
if (!Object.isNull(token))
|
|
53
|
+
headers[Resources.authorizationHeader] = Resources.formatBearer(token);
|
|
50
54
|
try {
|
|
51
55
|
return await this.fetcher(url.href, {
|
|
52
|
-
headers
|
|
56
|
+
headers,
|
|
53
57
|
redirect: Resources.manualRedirect,
|
|
54
58
|
signal: within
|
|
55
59
|
});
|
|
@@ -79,9 +83,15 @@ export class GitHubAvatars {
|
|
|
79
83
|
this.kept.delete(oldest);
|
|
80
84
|
}
|
|
81
85
|
}
|
|
82
|
-
static
|
|
86
|
+
static hostsOf(server) {
|
|
87
|
+
if (Object.isNull(server))
|
|
88
|
+
return [Resources.avatarHost];
|
|
89
|
+
const host = URL.parse(`${Resources.httpsProtocol}//${server}`)?.host ?? server;
|
|
90
|
+
return [host, `${Resources.avatarSubdomain}${host}`];
|
|
91
|
+
}
|
|
92
|
+
static avatarUrlOf(address, base, hosts) {
|
|
83
93
|
const url = Object.isNull(address) ? null : URL.parse(address, base);
|
|
84
|
-
return !Object.isNull(url) && url.protocol === Resources.httpsProtocol && url.host
|
|
94
|
+
return !Object.isNull(url) && url.protocol === Resources.httpsProtocol && hosts.includes(url.host) && url.username.length === 0
|
|
85
95
|
&& url.password.length === 0 ? url : null;
|
|
86
96
|
}
|
|
87
97
|
async dataOfAsync(response, signal, within) {
|
|
@@ -80,28 +80,10 @@ export class GitHubReads {
|
|
|
80
80
|
return this.tokens.holds(token) ? list : HostPullRequestList.signedOut();
|
|
81
81
|
}
|
|
82
82
|
async readRepositoriesAsync(query, signal) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const list = await this.askAsync(account.token, async () => {
|
|
88
|
-
const rows = [];
|
|
89
|
-
const names = new Set();
|
|
90
|
-
for (let page = 1; page <= Resources.repositoryPages; page++) {
|
|
91
|
-
const answer = await this.api.getAsync(host, Resources.formatPagePath(Resources.repositoriesPath, page), account.token, signal);
|
|
92
|
-
const read = Object.isNull(answer) ? [] : GitHubApi.read(() => answer.readObjectArray(Resources.itemsField));
|
|
93
|
-
for (const row of read.map(t => GitHubReads.repositoryOf(t))) {
|
|
94
|
-
if (names.has(row.name))
|
|
95
|
-
continue;
|
|
96
|
-
names.add(row.name);
|
|
97
|
-
rows.push(row);
|
|
98
|
-
}
|
|
99
|
-
if (read.length < Resources.repositoriesPageSize)
|
|
100
|
-
return new HostRepositoryList(rows, false);
|
|
101
|
-
}
|
|
102
|
-
return new HostRepositoryList(rows, true);
|
|
103
|
-
});
|
|
104
|
-
return this.tokens.holds(account.token) ? list : HostRepositoryList.signedOut();
|
|
83
|
+
return await this.listRepositoriesAsync(query.account, Resources.repositoryPages, query.isFresh, () => true, signal);
|
|
84
|
+
}
|
|
85
|
+
async searchRepositoriesAsync(search, signal) {
|
|
86
|
+
return await this.listRepositoriesAsync(search.account, Resources.repositorySearchPages, false, t => search.matches(t.name), signal);
|
|
105
87
|
}
|
|
106
88
|
async readReviewRequestsAsync(query, signal) {
|
|
107
89
|
const accounts = this.tokens.serves(query.host) ? this.tokens.signedInOn(query.host) : this.tokens.signedIn;
|
|
@@ -145,10 +127,11 @@ export class GitHubReads {
|
|
|
145
127
|
return this.tokens.holds(token) ? answer : none;
|
|
146
128
|
}
|
|
147
129
|
async readAuthorAvatarAsync(query, signal) {
|
|
148
|
-
if (query.host
|
|
130
|
+
if (!this.tokens.serves(query.host))
|
|
149
131
|
return null;
|
|
132
|
+
const isGitHub = query.host === Resources.host;
|
|
150
133
|
const email = query.email.trim().toLowerCase();
|
|
151
|
-
if (Resources.noReplyPattern.test(email))
|
|
134
|
+
if (isGitHub && Resources.noReplyPattern.test(email))
|
|
152
135
|
return this.avatars.readAsync(email.replace(Resources.noReplyPattern, (_email, id, login) => Resources.formatAccountAvatar(id ?? null, login)), signal);
|
|
153
136
|
const token = await this.tokens.tokenForAsync(query.host, query.path, signal);
|
|
154
137
|
const repository = GitHubRepository.parse(query.host, query.path);
|
|
@@ -158,7 +141,8 @@ export class GitHubReads {
|
|
|
158
141
|
const commit = await this.api.getAsync(repository.host, repository.pathOf(Resources.formatCommitPath(query.commit)), token, signal);
|
|
159
142
|
return Object.isNull(commit) ? null : GitHubApi.read(() => commit.readNullableObject(Resources.authorField)?.readNullableString(Resources.avatarUrlField) ?? null);
|
|
160
143
|
});
|
|
161
|
-
const avatar = await this.avatars.readAsync(address, signal)
|
|
144
|
+
const avatar = isGitHub ? await this.avatars.readAsync(address, signal)
|
|
145
|
+
: await this.avatars.readAsync(address, signal, repository.host, token);
|
|
162
146
|
return this.tokens.holds(token) ? avatar : null;
|
|
163
147
|
}
|
|
164
148
|
async readPullRequestAsync(reference, signal) {
|
|
@@ -336,10 +320,36 @@ export class GitHubReads {
|
|
|
336
320
|
throw error;
|
|
337
321
|
}
|
|
338
322
|
}
|
|
323
|
+
async listRepositoriesAsync(id, pages, isFresh, keeps, signal) {
|
|
324
|
+
const host = this.tokens.hostOf(id);
|
|
325
|
+
const account = Object.isNull(host) ? undefined : this.tokens.signedInOn(host).find(t => t.id === id);
|
|
326
|
+
if (Object.isNull(host) || Object.isUndefined(account))
|
|
327
|
+
return HostRepositoryList.signedOut();
|
|
328
|
+
const list = await this.askAsync(account.token, async () => {
|
|
329
|
+
const rows = [];
|
|
330
|
+
const names = new Set();
|
|
331
|
+
for (let page = 1; page <= pages; page++) {
|
|
332
|
+
const answer = await this.api.getAsync(host, Resources.formatPagePath(Resources.repositoriesPath, page), account.token, signal, isFresh);
|
|
333
|
+
const read = Object.isNull(answer) ? [] : GitHubApi.read(() => answer.readObjectArray(Resources.itemsField));
|
|
334
|
+
for (const row of read.map(t => GitHubReads.repositoryOf(t))) {
|
|
335
|
+
if (names.has(row.name) || !keeps(row))
|
|
336
|
+
continue;
|
|
337
|
+
if (rows.length === HostRepositoryList.maximumRows)
|
|
338
|
+
return new HostRepositoryList(rows, true);
|
|
339
|
+
names.add(row.name);
|
|
340
|
+
rows.push(row);
|
|
341
|
+
}
|
|
342
|
+
if (read.length < Resources.repositoriesPageSize)
|
|
343
|
+
return new HostRepositoryList(rows, false);
|
|
344
|
+
}
|
|
345
|
+
return new HostRepositoryList(rows, true);
|
|
346
|
+
});
|
|
347
|
+
return this.tokens.holds(account.token) ? list : HostRepositoryList.signedOut();
|
|
348
|
+
}
|
|
339
349
|
static repositoryOf(item) {
|
|
340
350
|
return GitHubApi.read(() => {
|
|
341
351
|
const pushedAt = item.readNullableString(Resources.pushedAtField);
|
|
342
|
-
return new HostRepository(item.readString(Resources.fullNameField), item.readString(Resources.cloneUrlField), item.readBoolean(Resources.privateField), Object.isNull(pushedAt) ? null : Date.parse(pushedAt));
|
|
352
|
+
return new HostRepository(item.readString(Resources.fullNameField), item.readString(Resources.cloneUrlField), item.readBoolean(Resources.privateField), Object.isNull(pushedAt) ? null : Date.parse(pushedAt), item.readObject(Resources.ownerField).readString(Resources.loginField), item.readBoolean(Resources.forkField));
|
|
343
353
|
});
|
|
344
354
|
}
|
|
345
355
|
static reviewSearchOf(host, found) {
|