@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.
@@ -6,8 +6,9 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  import { FailureCode } from "@noldova/desktop.protocol";
9
+ import { MethodFailureException } from "@noldova/desktop.runtime";
9
10
  import { BranchPullRequest, CheckState, HostBuild, HostChanges, HostCheck, HostComment, HostCommitComments, HostItem, HostPerson, HostPullRequest, HostPullRequestList, HostPullRequestRow, HostPullRequestThreads, HostReference, HostReviewer, HostReviewRequest, HostReviewRequestList, HostThread, ReferenceKind, ReferenceState, ReviewDecision, ThreadStatus } from "@noldova/git.hosting.protocol";
10
- import { GitHubNames } from "@noldova/git.github.protocol";
11
+ import { AccountSearch } from "../models/account-search.js";
11
12
  import { GitHubRepository } from "../models/github-repository.js";
12
13
  import { NewestItems } from "../models/newest-items.js";
13
14
  import { ReviewSearchAnswer } from "../models/review-search-answer.js";
@@ -17,12 +18,12 @@ import { Resources } from "../resources.js";
17
18
  import { GitHubApi } from "./github-api.js";
18
19
  export class GitHubReads {
19
20
  api;
20
- signIn;
21
+ tokens;
21
22
  avatars;
22
- reviewSearch = null;
23
- constructor(api, signIn, avatars) {
23
+ reviewSearches = new Map();
24
+ constructor(api, tokens, avatars) {
24
25
  this.api = api;
25
- this.signIn = signIn;
26
+ this.tokens = tokens;
26
27
  this.avatars = avatars;
27
28
  }
28
29
  readReferenceAsync(reference, signal) {
@@ -31,77 +32,92 @@ export class GitHubReads {
31
32
  readShownReferenceAsync(reference, signal) {
32
33
  return this.readItemAsync(reference, signal, true);
33
34
  }
34
- async readBranchPullRequestAsync(query, signal) {
35
+ async readBranchPullRequestAsync(query, signal, user = null) {
35
36
  const none = new BranchPullRequest(null, []);
36
- const token = this.signIn.token;
37
- const repository = GitHubRepository.parse(query.path);
38
- if (query.host !== Resources.host || token === null || repository === null)
37
+ const token = await this.tokens.tokenForAsync(query.host, query.path, signal, user);
38
+ const repository = GitHubRepository.parse(query.host, query.path);
39
+ if (!this.tokens.serves(query.host) || token === null || repository === null)
39
40
  return none;
40
41
  const answer = await this.askAsync(token, async () => {
41
42
  const head = encodeURIComponent(Resources.formatHeadFilter(repository.owner, query.branch));
42
- const list = await this.api.getAsync(repository.pathOf(Resources.formatBranchPullsPath(head)), token, signal);
43
+ const list = await this.api.getAsync(repository.host, repository.pathOf(Resources.formatBranchPullsPath(head)), token, signal);
43
44
  const first = list === null ? undefined : GitHubApi.read(() => list.readObjectArray(Resources.itemsField))[0];
44
45
  if (first === undefined)
45
46
  return none;
46
- const number = GitHubApi.read(() => first.readInteger(Resources.numberField));
47
- const pull = await this.api.getAsync(repository.pathOf(Resources.formatPullPath(number)), token, signal);
48
- if (pull === null)
49
- return none;
50
- const pullRequest = GitHubApi.read(() => GitHubReads.pullRequestOf(new HostReference(GitHubNames.moduleId, repository.owner, repository.name, number), pull, null));
51
- const commit = query.headCommit ?? GitHubApi.read(() => pull.readObject(Resources.headField).readString(Resources.shaField));
52
- const checks = await this.readChecksAsync(repository, commit, token, signal);
53
- return new BranchPullRequest(pullRequest, checks, await this.readReviewDecisionAsync(repository, number, token, signal));
47
+ return await this.readPullAnswerAsync(repository, GitHubApi.read(() => first.readInteger(Resources.numberField)), query.headCommit, token, signal);
48
+ });
49
+ return this.tokens.holds(token) ? answer : none;
50
+ }
51
+ async readCommitPullRequestAsync(query, signal) {
52
+ const none = new BranchPullRequest(null, []);
53
+ const token = await this.tokens.tokenForAsync(query.host, query.path, signal);
54
+ const repository = GitHubRepository.parse(query.host, query.path);
55
+ if (!this.tokens.serves(query.host) || token === null || repository === null)
56
+ return none;
57
+ const answer = await this.askAsync(token, async () => {
58
+ const list = await this.api.getAsync(repository.host, repository.pathOf(Resources.formatCommitPullRequestsPath(query.commit)), token, signal);
59
+ const pulls = list === null ? [] : GitHubApi.read(() => list.readObjectArray(Resources.itemsField));
60
+ const chosen = GitHubApi.read(() => GitHubReads.bringerOf(pulls));
61
+ return chosen === null ? none : await this.readPullAnswerAsync(repository, chosen, null, token, signal);
54
62
  });
55
- return this.signIn.token === token ? answer : none;
63
+ return this.tokens.holds(token) ? answer : none;
56
64
  }
57
65
  async readPullRequestsAsync(query, signal) {
58
66
  const none = new HostPullRequestList([], false);
59
- const token = this.signIn.token;
60
- const repository = GitHubRepository.parse(query.path);
67
+ const token = await this.tokens.tokenForAsync(query.host, query.path, signal);
68
+ const repository = GitHubRepository.parse(query.host, query.path);
61
69
  if (token === null)
62
70
  return HostPullRequestList.signedOut();
63
- if (query.host !== Resources.host || repository === null)
71
+ if (!this.tokens.serves(query.host) || repository === null)
64
72
  return none;
65
73
  const list = await this.askAsync(token, async () => {
66
- const pulls = await this.api.getAsync(repository.pathOf(Resources.openPullsPath), token, signal);
74
+ const pulls = await this.api.getAsync(repository.host, repository.pathOf(Resources.openPullsPath), token, signal);
67
75
  const read = pulls === null ? [] : GitHubApi.read(() => pulls.readObjectArray(Resources.itemsField));
68
76
  const rows = await Promise.all(read.slice(0, HostPullRequestList.maximumRows).map(t => this.readRowAsync(repository, t, token, signal)));
69
77
  return new HostPullRequestList(rows, read.length > HostPullRequestList.maximumRows);
70
78
  });
71
- return this.signIn.token === token ? list : HostPullRequestList.signedOut();
79
+ return this.tokens.holds(token) ? list : HostPullRequestList.signedOut();
72
80
  }
73
81
  async readReviewRequestsAsync(query, signal) {
74
- const token = this.signIn.token;
75
- const repository = GitHubRepository.parse(query.path);
76
- if (token === null)
82
+ const accounts = this.tokens.serves(query.host) ? this.tokens.signedInOn(query.host) : this.tokens.signedIn;
83
+ const repository = GitHubRepository.parse(query.host, query.path);
84
+ if (accounts.length === 0)
77
85
  return HostReviewRequestList.signedOut();
78
- if (query.host !== Resources.host || repository === null)
86
+ if (!this.tokens.serves(query.host) || repository === null)
79
87
  return new HostReviewRequestList([], false);
80
- const { rows, isCut } = await this.searchReviewRequestsAsync(token);
88
+ const settled = await Promise.allSettled(accounts.map(async (t) => new AccountSearch(t, await this.searchReviewRequestsAsync(repository.host, t.token))));
81
89
  signal.throwIfAborted();
82
- if (this.signIn.token !== token)
90
+ const searches = settled.flatMap(t => t.status === "fulfilled" ? [t.value] : []);
91
+ const failures = settled.flatMap((t) => t.status === "rejected" ? [t.reason] : []);
92
+ const unexpected = failures.filter(t => !(t instanceof MethodFailureException));
93
+ if (unexpected.length > 0 || searches.length === 0)
94
+ throw unexpected[0] ?? failures[0];
95
+ const held = searches.filter(t => this.tokens.holds(t.account.token));
96
+ if (held.length === 0)
83
97
  return HostReviewRequestList.signedOut();
84
98
  const key = GitHubReads.repositoryKeyOf(repository.owner, repository.name);
85
- return new HostReviewRequestList(rows.filter(t => GitHubReads.repositoryKeyOf(t.reference.owner, t.reference.repository) === key)
86
- .toSorted((a, b) => b.createdAt - a.createdAt), isCut);
99
+ const rows = held.flatMap(({ account, answer }) => answer.rows
100
+ .filter(t => GitHubReads.repositoryKeyOf(t.reference.owner, t.reference.repository) === key)
101
+ .map(t => accounts.length === 1 ? t : new HostReviewRequest(t.reference, t.title, t.url, t.isDraft, t.createdAt, account.login)))
102
+ .toSorted((a, b) => b.createdAt - a.createdAt);
103
+ return new HostReviewRequestList(rows.slice(0, HostReviewRequestList.maximumRows), rows.length > HostReviewRequestList.maximumRows || held.some(t => t.answer.isCut) || failures.length > 0);
87
104
  }
88
105
  async readCommitCommentsAsync(query, signal) {
89
106
  const none = HostCommitComments.none();
90
- const token = this.signIn.token;
91
- const repository = GitHubRepository.parse(query.path);
92
- if (query.host !== Resources.host || token === null || repository === null)
107
+ const token = await this.tokens.tokenForAsync(query.host, query.path, signal);
108
+ const repository = GitHubRepository.parse(query.host, query.path);
109
+ if (!this.tokens.serves(query.host) || token === null || repository === null)
93
110
  return none;
94
111
  const answer = await this.askAsync(token, async () => {
95
- const [own, pulls] = await Promise.all([this.api.getAsync(repository.pathOf(Resources.formatCommitCommentsPath(query.commit)), token, signal),
96
- this.api.getAsync(repository.pathOf(Resources.formatCommitPullsPath(query.commit)), token, signal)]);
112
+ const [own, pulls] = await Promise.all([this.api.getAsync(repository.host, repository.pathOf(Resources.formatCommitCommentsPath(query.commit)), token, signal),
113
+ this.api.getAsync(repository.host, repository.pathOf(Resources.formatCommitPullRequestsPath(query.commit)), token, signal)]);
97
114
  const commitComments = own === null ? [] : GitHubApi.read(() => own.readObjectArray(Resources.itemsField));
98
- const pull = pulls === null ? undefined : GitHubApi.read(() => pulls.readObjectArray(Resources.itemsField))[0];
99
- const reviews = pull === undefined ? null
100
- : await this.api.getAsync(repository.pathOf(Resources.formatPullCommentsPath(GitHubApi.read(() => pull.readInteger(Resources.numberField)))), token, signal);
115
+ const number = pulls === null ? null : GitHubApi.read(() => GitHubReads.bringerOf(pulls.readObjectArray(Resources.itemsField)));
116
+ const reviews = number === null ? null : await this.api.getAsync(repository.host, repository.pathOf(Resources.formatPullCommentsPath(number)), token, signal);
101
117
  const reviewComments = reviews === null ? [] : GitHubApi.read(() => reviews.readObjectArray(Resources.itemsField));
102
- return GitHubApi.read(() => GitHubReads.commitCommentsOf(query.commit, commitComments, reviewComments));
118
+ return GitHubApi.read(() => GitHubReads.commitCommentsOf(repository.host, query.commit, commitComments, reviewComments));
103
119
  });
104
- return this.signIn.token === token ? answer : none;
120
+ return this.tokens.holds(token) ? answer : none;
105
121
  }
106
122
  async readAuthorAvatarAsync(query, signal) {
107
123
  if (query.host !== Resources.host)
@@ -109,24 +125,24 @@ export class GitHubReads {
109
125
  const email = query.email.trim().toLowerCase();
110
126
  if (Resources.noReplyPattern.test(email))
111
127
  return this.avatars.readAsync(email.replace(Resources.noReplyPattern, (_email, id, login) => Resources.formatAccountAvatar(id ?? null, login)), signal);
112
- const token = this.signIn.token;
113
- const repository = GitHubRepository.parse(query.path);
128
+ const token = await this.tokens.tokenForAsync(query.host, query.path, signal);
129
+ const repository = GitHubRepository.parse(query.host, query.path);
114
130
  if (token === null || repository === null)
115
131
  return null;
116
132
  const address = await this.askAsync(token, async () => {
117
- const commit = await this.api.getAsync(repository.pathOf(Resources.formatCommitPath(query.commit)), token, signal);
133
+ const commit = await this.api.getAsync(repository.host, repository.pathOf(Resources.formatCommitPath(query.commit)), token, signal);
118
134
  return commit === null ? null : GitHubApi.read(() => commit.readNullableObject(Resources.authorField)?.readNullableString(Resources.avatarUrlField) ?? null);
119
135
  });
120
136
  const avatar = await this.avatars.readAsync(address, signal);
121
- return this.signIn.token === token ? avatar : null;
137
+ return this.tokens.holds(token) ? avatar : null;
122
138
  }
123
139
  async readPullRequestAsync(reference, signal) {
124
- const token = this.signIn.token;
125
- const repository = GitHubRepository.parse(Resources.formatRepositoryName(reference.owner, reference.repository));
126
- if (reference.host !== GitHubNames.moduleId || token === null || repository === null)
140
+ const repository = GitHubRepository.ofReference(reference);
141
+ const token = repository === null ? null : await this.tokens.tokenForAsync(repository.host, repository.repositoryPath, signal);
142
+ if (repository === null || token === null)
127
143
  return null;
128
144
  const pullRequest = await this.askAsync(token, async () => {
129
- const pull = await this.api.getAsync(repository.pathOf(Resources.formatPullPath(reference.number)), token, signal);
145
+ const pull = await this.api.getAsync(repository.host, repository.pathOf(Resources.formatPullPath(reference.number)), token, signal);
130
146
  if (pull === null)
131
147
  return null;
132
148
  const commit = GitHubApi.read(() => pull.readObject(Resources.headField).readString(Resources.shaField));
@@ -135,62 +151,71 @@ export class GitHubReads {
135
151
  signal.throwIfAborted();
136
152
  return GitHubApi.read(() => GitHubReads.pullRequestPageOf(reference, pull, avatar, reviews, runs));
137
153
  });
138
- return this.signIn.token === token ? pullRequest : null;
154
+ return this.tokens.holds(token) ? pullRequest : null;
139
155
  }
140
156
  async readPullRequestThreadsAsync(reference, signal) {
141
157
  const none = new HostPullRequestThreads([], false);
142
- const token = this.signIn.token;
143
- const repository = GitHubRepository.parse(Resources.formatRepositoryName(reference.owner, reference.repository));
144
- if (reference.host !== GitHubNames.moduleId || token === null || repository === null)
158
+ const repository = GitHubRepository.ofReference(reference);
159
+ const token = repository === null ? null : await this.tokens.tokenForAsync(repository.host, repository.repositoryPath, signal);
160
+ if (repository === null || token === null)
145
161
  return none;
146
162
  const threads = await this.askAsync(token, async () => {
147
- const pull = await this.api.getAsync(repository.pathOf(Resources.formatPullPath(reference.number)), token, signal);
163
+ const pull = await this.api.getAsync(repository.host, repository.pathOf(Resources.formatPullPath(reference.number)), token, signal);
148
164
  if (pull === null)
149
165
  return none;
150
166
  const conversationCount = GitHubApi.read(() => pull.readInteger(Resources.commentsField));
151
167
  const reviewCount = GitHubApi.read(() => pull.readInteger(Resources.reviewCommentsField));
152
168
  const [conversation, reviews] = await Promise.all([
153
- this.readNewestAsync(page => repository.pathOf(Resources.formatIssueCommentsPath(reference.number, page)), conversationCount, token, signal),
154
- this.readNewestAsync(page => repository.pathOf(Resources.formatPullCommentsPagePath(reference.number, page)), reviewCount, token, signal)
169
+ this.readNewestAsync(repository.host, page => repository.pathOf(Resources.formatIssueCommentsPath(reference.number, page)), conversationCount, token, signal),
170
+ this.readNewestAsync(repository.host, page => repository.pathOf(Resources.formatPullCommentsPagePath(reference.number, page)), reviewCount, token, signal)
155
171
  ]);
156
172
  return GitHubApi.read(() => new HostPullRequestThreads(GitHubReads.threadsOf(conversation.items, reviews.items), conversation.hasMore || reviews.hasMore));
157
173
  });
158
- return this.signIn.token === token ? threads : none;
174
+ return this.tokens.holds(token) ? threads : none;
159
175
  }
160
- async createPullRequestAsync(draft, signal) {
161
- const token = this.signIn.token;
162
- if (token === null)
176
+ async createPullRequestAsync(draft, signal, chosen = null) {
177
+ const token = chosen ?? await this.tokens.tokenForAsync(draft.host, draft.path, signal);
178
+ if (token === null || !this.tokens.holds(token))
163
179
  throw GitHubApi.failure(FailureCode.Unauthorized, Resources.signedOutMessage);
164
- const repository = GitHubRepository.parse(draft.path);
165
- if (draft.host !== Resources.host || repository === null)
180
+ const repository = GitHubRepository.parse(draft.host, draft.path);
181
+ if (!this.tokens.serves(draft.host) || repository === null)
166
182
  throw GitHubApi.failure(FailureCode.InvalidParams, Resources.otherHostMessage);
167
183
  return this.askAsync(token, async () => {
168
- const created = await this.api.postAsync(repository.pathOf(Resources.pullsPath), token, {
184
+ const created = await this.api.postAsync(repository.host, repository.pathOf(Resources.pullsPath), token, {
169
185
  [Resources.titleField]: draft.title, [Resources.bodyField]: draft.body, [Resources.headField]: draft.headBranch, [Resources.baseField]: draft.baseBranch,
170
186
  [Resources.draftField]: draft.isDraft
171
187
  }, signal);
172
- return GitHubApi.read(() => GitHubReads.pullRequestOf(new HostReference(GitHubNames.moduleId, repository.owner, repository.name, created.readInteger(Resources.numberField)), created, null));
188
+ return GitHubApi.read(() => GitHubReads.pullRequestOf(repository.referenceOf(created.readInteger(Resources.numberField)), created, null));
173
189
  });
174
190
  }
175
191
  async readItemAsync(reference, signal, withAvatar) {
176
- const token = this.signIn.token;
177
- const repository = GitHubRepository.parse(Resources.formatRepositoryName(reference.owner, reference.repository));
178
- if (reference.host !== GitHubNames.moduleId || token === null || repository === null)
192
+ const repository = GitHubRepository.ofReference(reference);
193
+ const token = repository === null ? null : await this.tokens.tokenForAsync(repository.host, repository.repositoryPath, signal);
194
+ if (repository === null || token === null)
179
195
  return null;
180
196
  const item = await this.askAsync(token, async () => {
181
- const issue = await this.api.getAsync(repository.pathOf(Resources.formatIssuePath(reference.number)), token, signal);
197
+ const issue = await this.api.getAsync(repository.host, repository.pathOf(Resources.formatIssuePath(reference.number)), token, signal);
182
198
  if (issue === null)
183
199
  return null;
184
200
  const reading = withAvatar ? this.avatars.readAsync(GitHubApi.read(() => GitHubReads.avatarUrlOf(issue)), signal) : Promise.resolve(null);
185
201
  const asking = issue.hasField(Resources.pullRequestField)
186
- ? this.api.getAsync(repository.pathOf(Resources.formatPullPath(reference.number)), token, signal) : Promise.resolve(issue);
202
+ ? this.api.getAsync(repository.host, repository.pathOf(Resources.formatPullPath(reference.number)), token, signal) : Promise.resolve(issue);
187
203
  const [pull, avatar] = await Promise.all([asking, reading]);
188
204
  signal.throwIfAborted();
189
205
  if (pull === issue)
190
206
  return GitHubApi.read(() => GitHubReads.issueOf(reference, issue, avatar));
191
207
  return pull === null ? null : GitHubApi.read(() => GitHubReads.pullRequestOf(reference, pull, avatar));
192
208
  });
193
- return this.signIn.token === token ? item : null;
209
+ return this.tokens.holds(token) ? item : null;
210
+ }
211
+ async readPullAnswerAsync(repository, number, headCommit, token, signal) {
212
+ const pull = await this.api.getAsync(repository.host, repository.pathOf(Resources.formatPullPath(number)), token, signal);
213
+ if (pull === null)
214
+ return new BranchPullRequest(null, []);
215
+ const pullRequest = GitHubApi.read(() => GitHubReads.pullRequestOf(repository.referenceOf(number), pull, null));
216
+ const commit = headCommit ?? GitHubApi.read(() => pull.readObject(Resources.headField).readString(Resources.shaField));
217
+ const checks = await this.readChecksAsync(repository, commit, token, signal);
218
+ return new BranchPullRequest(pullRequest, checks, await this.readReviewDecisionAsync(repository, number, token, signal));
194
219
  }
195
220
  async readChecksAsync(repository, commit, token, signal) {
196
221
  const runs = await this.readCheckRunsAsync(repository, commit, token, signal);
@@ -199,7 +224,7 @@ export class GitHubReads {
199
224
  async readCheckRunsAsync(repository, commit, token, signal) {
200
225
  const runs = [];
201
226
  for (let page = 1;; page++) {
202
- const answer = await this.api.getAsync(repository.pathOf(Resources.formatCheckRunsPath(commit, page)), token, signal);
227
+ const answer = await this.api.getAsync(repository.host, repository.pathOf(Resources.formatCheckRunsPath(commit, page)), token, signal);
203
228
  if (answer === null)
204
229
  return runs;
205
230
  const total = GitHubApi.read(() => answer.readInteger(Resources.totalCountField));
@@ -210,24 +235,24 @@ export class GitHubReads {
210
235
  }
211
236
  }
212
237
  readReviewsAsync(repository, number, token, signal) {
213
- return this.readPagesAsync(page => repository.pathOf(Resources.formatReviewsPath(number, page)), Resources.reviewsPageSize, token, signal);
238
+ return this.readPagesAsync(repository.host, page => repository.pathOf(Resources.formatReviewsPath(number, page)), Resources.reviewsPageSize, token, signal);
214
239
  }
215
- async readNewestAsync(pathOf, count, token, signal) {
240
+ async readNewestAsync(host, pathOf, count, token, signal) {
216
241
  const kept = Resources.newestCommentsCount;
217
242
  const first = count <= kept ? 1 : Math.floor((count - kept) / Resources.commentsPageSize) + 1;
218
243
  const items = [];
219
244
  for (let page = first;; page++) {
220
- const answer = await this.api.getAsync(pathOf(page), token, signal);
245
+ const answer = await this.api.getAsync(host, pathOf(page), token, signal);
221
246
  const read = answer === null ? [] : GitHubApi.read(() => answer.readObjectArray(Resources.itemsField));
222
247
  items.push(...read);
223
248
  if (read.length < Resources.commentsPageSize)
224
249
  return new NewestItems(items.slice(-kept), first > 1 || items.length > kept);
225
250
  }
226
251
  }
227
- async readPagesAsync(pathOf, pageSize, token, signal) {
252
+ async readPagesAsync(host, pathOf, pageSize, token, signal) {
228
253
  const items = [];
229
254
  for (let page = 1;; page++) {
230
- const answer = await this.api.getAsync(pathOf(page), token, signal);
255
+ const answer = await this.api.getAsync(host, pathOf(page), token, signal);
231
256
  const read = answer === null ? [] : GitHubApi.read(() => answer.readObjectArray(Resources.itemsField));
232
257
  items.push(...read);
233
258
  if (read.length < pageSize)
@@ -239,7 +264,7 @@ export class GitHubReads {
239
264
  const head = GitHubApi.read(() => pull.readObject(Resources.headField));
240
265
  const commit = GitHubApi.read(() => head.readString(Resources.shaField));
241
266
  const [checks, verdicts] = await Promise.all([this.readChecksAsync(repository, commit, token, signal), this.readVerdictsAsync(repository, number, token, signal)]);
242
- return GitHubApi.read(() => HostPullRequestRow.of(new HostReference(GitHubNames.moduleId, repository.owner, repository.name, 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)));
267
+ 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)));
243
268
  }
244
269
  async readReviewDecisionAsync(repository, number, token, signal) {
245
270
  const verdicts = await this.readVerdictsAsync(repository, number, token, signal);
@@ -249,7 +274,7 @@ export class GitHubReads {
249
274
  async readVerdictsAsync(repository, number, token, signal) {
250
275
  const latest = new Map();
251
276
  for (let page = 1;; page++) {
252
- const reviews = await this.api.getAsync(repository.pathOf(Resources.formatReviewsPath(number, page)), token, signal);
277
+ const reviews = await this.api.getAsync(repository.host, repository.pathOf(Resources.formatReviewsPath(number, page)), token, signal);
253
278
  const read = reviews === null ? []
254
279
  : GitHubApi.read(() => reviews.readObjectArray(Resources.itemsField).map(GitHubReads.reviewOf));
255
280
  for (const { login, state } of read)
@@ -260,16 +285,20 @@ export class GitHubReads {
260
285
  }
261
286
  return [...latest.values()];
262
287
  }
263
- searchReviewRequestsAsync(token) {
288
+ searchReviewRequestsAsync(host, token) {
264
289
  const now = Date.now();
265
- const held = this.reviewSearch;
266
- if (held !== null && held.token === token && now - held.startedAt < Resources.reviewRequestsHeldFor)
290
+ for (const kept of this.reviewSearches.keys()) {
291
+ if (!this.tokens.holds(kept))
292
+ this.reviewSearches.delete(kept);
293
+ }
294
+ const held = this.reviewSearches.get(token);
295
+ if (held !== undefined && now - held.startedAt < Resources.reviewRequestsHeldFor)
267
296
  return held.answer;
268
297
  const answer = this.askAsync(token, async () => {
269
- const found = await this.api.getAsync(Resources.reviewRequestsPath, token, new AbortController().signal);
270
- return found === null ? new ReviewSearchAnswer([], false) : GitHubApi.read(() => GitHubReads.reviewSearchOf(found));
298
+ const found = await this.api.getAsync(host, Resources.reviewRequestsPath, token, new AbortController().signal);
299
+ return found === null ? new ReviewSearchAnswer([], false) : GitHubApi.read(() => GitHubReads.reviewSearchOf(host, found));
271
300
  });
272
- this.reviewSearch = new ReviewSearch(token, now, answer);
301
+ this.reviewSearches.set(token, new ReviewSearch(token, now, answer));
273
302
  return answer;
274
303
  }
275
304
  async askAsync(token, ask) {
@@ -278,28 +307,37 @@ export class GitHubReads {
278
307
  }
279
308
  catch (error) {
280
309
  if (GitHubApi.isUnauthorized(error))
281
- await this.signIn.refusedAsync(token);
310
+ await this.tokens.refusedAsync(token);
282
311
  throw error;
283
312
  }
284
313
  }
285
- static reviewSearchOf(found) {
314
+ static reviewSearchOf(host, found) {
286
315
  const items = found.readObjectArray(Resources.itemsField);
287
316
  const rows = [];
288
317
  for (const item of items) {
289
318
  const address = item.readString(Resources.repositoryUrlField);
290
- const repository = address.startsWith(Resources.repositoryApiPrefix)
291
- ? GitHubRepository.parse(address.slice(Resources.repositoryApiPrefix.length)) : null;
319
+ const prefix = Resources.formatRepositoryApiPrefix(host);
320
+ const repository = address.startsWith(prefix) ? GitHubRepository.parse(host, address.slice(prefix.length)) : null;
292
321
  if (repository !== null)
293
- rows.push(new HostReviewRequest(new HostReference(GitHubNames.moduleId, repository.owner, repository.name, item.readInteger(Resources.numberField)), item.readString(Resources.titleField), item.readString(Resources.htmlUrlField), item.hasField(Resources.draftField) && item.readBoolean(Resources.draftField), Date.parse(item.readString(Resources.createdAtField))));
322
+ rows.push(new HostReviewRequest(repository.referenceOf(item.readInteger(Resources.numberField)), item.readString(Resources.titleField), item.readString(Resources.htmlUrlField), item.hasField(Resources.draftField) && item.readBoolean(Resources.draftField), Date.parse(item.readString(Resources.createdAtField))));
294
323
  }
295
324
  return new ReviewSearchAnswer(rows, found.readInteger(Resources.totalCountField) > items.length || found.readBoolean(Resources.incompleteResultsField));
296
325
  }
297
- static commitCommentsOf(commit, commitComments, reviewComments) {
326
+ static bringerOf(pulls) {
327
+ const merged = pulls.find(t => t.readNullableString(Resources.mergedAtField) !== null && GitHubReads.isIntoDefault(t));
328
+ const chosen = merged ?? pulls[0];
329
+ return chosen === undefined ? null : chosen.readInteger(Resources.numberField);
330
+ }
331
+ static isIntoDefault(pull) {
332
+ const base = pull.readObject(Resources.baseField);
333
+ return base.readString(Resources.refField) === base.readObject(Resources.repoField).readString(Resources.defaultBranchField);
334
+ }
335
+ static commitCommentsOf(host, commit, commitComments, reviewComments) {
298
336
  const onCommit = reviewComments.filter(t => [Resources.commitIdField, Resources.originalCommitIdField]
299
337
  .some(field => t.hasField(field) && t.readNullableString(field) === commit));
300
338
  const first = commitComments[0] ?? onCommit[0];
301
339
  const url = first === undefined ? null : URL.parse(first.readString(Resources.htmlUrlField));
302
- if (url === null || url.protocol !== Resources.httpsProtocol || url.hostname !== Resources.host || url.username.length > 0 || url.password.length > 0)
340
+ if (url === null || url.protocol !== Resources.httpsProtocol || url.host !== host || url.username.length > 0 || url.password.length > 0)
303
341
  return HostCommitComments.none();
304
342
  const count = commitComments.length + onCommit.length;
305
343
  return new HostCommitComments(Math.min(count, HostCommitComments.maximumCount), count > HostCommitComments.maximumCount
@@ -24,6 +24,7 @@ export class GitHubSignIn {
24
24
  appWindows;
25
25
  log;
26
26
  changed;
27
+ secret;
27
28
  stopped = new AbortController();
28
29
  signIn = HostSignIn.signedOut();
29
30
  waiting = null;
@@ -32,12 +33,15 @@ export class GitHubSignIn {
32
33
  saves = 0;
33
34
  heldToken = null;
34
35
  unreadable = false;
35
- constructor(flow, secrets, appWindows, log, changed) {
36
+ host;
37
+ constructor(flow, secrets, appWindows, log, changed, secret = Resources.signInSecret, host = Resources.host) {
36
38
  this.flow = flow;
37
39
  this.secrets = secrets;
38
40
  this.appWindows = appWindows;
39
41
  this.log = log;
40
42
  this.changed = changed;
43
+ this.secret = secret;
44
+ this.host = host;
41
45
  }
42
46
  get current() {
43
47
  return this.signIn;
@@ -53,7 +57,7 @@ export class GitHubSignIn {
53
57
  return;
54
58
  let kept;
55
59
  try {
56
- kept = await this.secrets.readAsync(Resources.signInSecret);
60
+ kept = await this.secrets.readAsync(this.secret);
57
61
  }
58
62
  catch (error) {
59
63
  if (this.stopped.signal.aborted)
@@ -89,7 +93,7 @@ export class GitHubSignIn {
89
93
  this.background(this.checkAsync(restored.token, this.generation));
90
94
  }
91
95
  async startAsync() {
92
- if (this.signIn.state !== SignInState.SignedOut || this.waiting !== null || this.stopped.signal.aborted)
96
+ if (this.host !== Resources.host || this.signIn.state !== SignInState.SignedOut || this.waiting !== null || this.stopped.signal.aborted)
93
97
  return this.signIn;
94
98
  const waiting = new AbortController();
95
99
  this.waiting = waiting;
@@ -104,6 +108,16 @@ export class GitHubSignIn {
104
108
  }
105
109
  return this.signIn;
106
110
  }
111
+ async adoptAsync(token, read, signal) {
112
+ const signed = AbortSignal.any([signal, this.stopped.signal]);
113
+ try {
114
+ await this.keepReadAsync(token, read, signed);
115
+ }
116
+ catch (error) {
117
+ this.end(error, signed);
118
+ }
119
+ return this.signIn;
120
+ }
107
121
  cancel() {
108
122
  if (this.waiting === null)
109
123
  return this.signIn;
@@ -118,6 +132,10 @@ export class GitHubSignIn {
118
132
  await this.forgetAsync(null);
119
133
  return this.signIn;
120
134
  }
135
+ async alreadyKeptAsync() {
136
+ if (this.signIn.state === SignInState.SignedIn)
137
+ await this.forgetAsync(SignInFailure.AlreadyKept);
138
+ }
121
139
  async refusedAsync(token) {
122
140
  if (this.heldToken === token)
123
141
  await this.forgetAsync(SignInFailure.Revoked);
@@ -163,10 +181,13 @@ export class GitHubSignIn {
163
181
  const read = await this.flow.readAccountAsync(token, signal);
164
182
  if (read === null)
165
183
  throw new SignInTokenRefusedException();
184
+ await this.keepReadAsync(token, read, signal);
185
+ }
186
+ async keepReadAsync(token, read, signal) {
166
187
  const { account } = read;
167
188
  const save = ++this.saves;
168
189
  try {
169
- await this.secrets.saveAsync(Resources.signInSecret, JSON.stringify(new KeptSignIn(token, account).toJson()));
190
+ await this.secrets.saveAsync(this.secret, JSON.stringify(new KeptSignIn(token, account).toJson()));
170
191
  }
171
192
  catch (error) {
172
193
  if (!(error instanceof MethodFailureException))
@@ -189,7 +210,7 @@ export class GitHubSignIn {
189
210
  async checkAsync(token, generation) {
190
211
  let read;
191
212
  try {
192
- read = await this.flow.readAccountAsync(token, this.stopped.signal);
213
+ read = await this.flow.readAccountAsync(token, this.stopped.signal, this.host);
193
214
  }
194
215
  catch (error) {
195
216
  if (this.stopped.signal.aborted)
@@ -234,7 +255,7 @@ export class GitHubSignIn {
234
255
  }
235
256
  async removeKeptAsync() {
236
257
  try {
237
- await this.secrets.removeAsync(Resources.signInSecret);
258
+ await this.secrets.removeAsync(this.secret);
238
259
  return true;
239
260
  }
240
261
  catch (error) {
@@ -9,6 +9,7 @@ import { JsonException } from "@noldova/context.json";
9
9
  import { ServiceAccessException, ToolFailureCode } from "@noldova/desktop.runtime";
10
10
  import { GitFolderBinding, ProtocolException } from "@noldova/git.protocol";
11
11
  import { GitHubRepository } from "../models/github-repository.js";
12
+ import { RemoteUser } from "../models/remote-user.js";
12
13
  import { Resources } from "../resources.js";
13
14
  import { GitHubToolFailures } from "./github-tool-failures.js";
14
15
  export class BoundRepository {
@@ -16,20 +17,24 @@ export class BoundRepository {
16
17
  path;
17
18
  branch;
18
19
  headCommit;
19
- constructor(host, path, branch, headCommit) {
20
+ user;
21
+ constructor(host, path, branch, headCommit, user) {
20
22
  this.host = host;
21
23
  this.path = path;
22
24
  this.branch = branch;
23
25
  this.headCommit = headCommit;
26
+ this.user = user;
24
27
  }
25
28
  static async readAsync(call, repositories) {
26
29
  const folder = BoundRepository.folderOf(call);
27
30
  let status;
28
31
  let origin;
32
+ let user;
29
33
  try {
30
34
  const reads = repositories();
31
35
  status = await reads.readStatusAsync(folder, call.signal);
32
36
  origin = await reads.readRemoteAsync(folder, Resources.originRemote, call.signal);
37
+ user = await reads.readRemoteUserAsync(folder, Resources.originRemote, call.signal);
33
38
  }
34
39
  catch (error) {
35
40
  throw error instanceof ServiceAccessException
@@ -39,11 +44,11 @@ export class BoundRepository {
39
44
  const host = origin?.host ?? null;
40
45
  const remotePath = origin?.path ?? null;
41
46
  const path = remotePath?.endsWith(Resources.gitSuffix) === true ? remotePath.slice(0, -Resources.gitSuffix.length) : remotePath;
42
- if (host !== Resources.host || path === null || GitHubRepository.parse(path) === null)
47
+ if (host !== Resources.host || path === null || GitHubRepository.parse(host, path) === null)
43
48
  throw GitHubToolFailures.fail(ToolFailureCode.Refused, Resources.notGitHubToolMessage, Resources.notGitHubToolCode);
44
49
  if (status.branch === null)
45
50
  throw GitHubToolFailures.fail(ToolFailureCode.Refused, Resources.detachedHeadToolMessage, Resources.detachedHeadToolCode);
46
- return new BoundRepository(host, path, status.branch, status.head);
51
+ return new BoundRepository(host, path, status.branch, status.head, new RemoteUser(user));
47
52
  }
48
53
  static folderOf(call) {
49
54
  try {
@@ -13,7 +13,7 @@ import { BoundRepository } from "./bound-repository.js";
13
13
  import { GitHubToolFailures } from "./github-tool-failures.js";
14
14
  export class GitHubPrChecksTool {
15
15
  reads;
16
- token;
16
+ tokens;
17
17
  repositories;
18
18
  descriptor = {
19
19
  name: GitHubNames.prChecksTool,
@@ -24,23 +24,23 @@ export class GitHubPrChecksTool {
24
24
  risk: { default: ToolRisk.Low, maximum: ToolRisk.Low },
25
25
  deadline: { default: Resources.readToolDeadline, maximum: Resources.readToolMaximumDeadline }
26
26
  };
27
- constructor(reads, token, repositories) {
27
+ constructor(reads, tokens, repositories) {
28
28
  this.reads = reads;
29
- this.token = token;
29
+ this.tokens = tokens;
30
30
  this.repositories = repositories;
31
31
  }
32
32
  async handleAsync(call) {
33
33
  const repository = await BoundRepository.readAsync(call, this.repositories);
34
- if (this.token.token === null)
34
+ if (this.tokens.signedIn.length === 0)
35
35
  throw GitHubToolFailures.signedOut();
36
36
  let answer;
37
37
  try {
38
- answer = await this.reads.readBranchPullRequestAsync(new BranchQuery(repository.host, repository.path, repository.branch, repository.headCommit), call.signal);
38
+ answer = await this.reads.readBranchPullRequestAsync(new BranchQuery(repository.host, repository.path, repository.branch, repository.headCommit), call.signal, repository.user);
39
39
  }
40
40
  catch (error) {
41
41
  throw GitHubToolFailures.fromGitHub(error);
42
42
  }
43
- if (this.token.token === null)
43
+ if (this.tokens.signedIn.length === 0)
44
44
  throw GitHubToolFailures.signedOut();
45
45
  return new ToolResult([ToolContent.fromText(JSON.stringify(answer.toJson()), Resources.gitHubOrigin)]);
46
46
  }