@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 CHANGED
@@ -1,17 +1,19 @@
1
1
  {
2
2
  "id": "git-github",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
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" },
8
- "noldovaDesktop": ">=0.0.17 <0.1.0",
7
+ "dependencies": { "git": "0.0.10" },
8
+ "noldovaDesktop": ">=0.0.19 <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
- "git-github.readCommitComments", "git-github.readPullRequest", "git-github.readPullRequestThreads"],
14
- "events": ["git-github.signInChanged"],
13
+ "git-github.readCommitComments", "git-github.readPullRequest", "git-github.readPullRequestThreads", "git-github.readCommitPullRequest",
14
+ "git-github.readAccounts", "git-github.addAccount", "git-github.readRepositoryAccount", "git-github.pickAccount",
15
+ "git-github.useFolder", "git-github.signInWithToken"],
16
+ "events": ["git-github.signInChanged", "git-github.accountsChanged"],
15
17
  "tools": ["git_github_read", "git_github_pr_checks", "git_github_pr_create"]
16
18
  }
17
19
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noldova/git.github",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
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,16 +35,16 @@
35
35
  "registry": "https://registry.npmjs.org/"
36
36
  },
37
37
  "dependencies": {
38
- "@noldova/git.github.protocol": "0.0.8",
39
- "@noldova/git.protocol": "0.0.8",
40
- "@noldova/git.github.window": "0.0.8",
41
- "@noldova/git.hosting.protocol": "0.0.8"
38
+ "@noldova/git.github.protocol": "0.0.10",
39
+ "@noldova/git.protocol": "0.0.10",
40
+ "@noldova/git.github.window": "0.0.10",
41
+ "@noldova/git.hosting.protocol": "0.0.10"
42
42
  },
43
43
  "peerDependencies": {
44
- "@noldova/desktop.protocol": ">=0.0.17 <0.1.0",
45
- "@noldova/desktop.runtime": ">=0.0.17 <0.1.0",
46
- "@noldova/context.exceptions": ">=0.0.12 <0.1.0",
47
- "@noldova/context.json": ">=0.0.12 <0.1.0"
44
+ "@noldova/desktop.protocol": ">=0.0.19 <0.1.0",
45
+ "@noldova/desktop.runtime": ">=0.0.19 <0.1.0",
46
+ "@noldova/context.exceptions": ">=0.0.18 <0.1.0",
47
+ "@noldova/context.json": ">=0.0.18 <0.1.0"
48
48
  },
49
- "gitHead": "9915474fda79eb6cadb9d336d7ca47f8c28ad4dd"
49
+ "gitHead": "eedb612e4e62dc05d714de998def800b222028a3"
50
50
  }
@@ -0,0 +1,15 @@
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 { Failure } from "@noldova/desktop.protocol";
9
+ import { MethodFailureException } from "@noldova/desktop.runtime";
10
+ import { Resources } from "../resources.js";
11
+ export class GitHubUnreachableException extends MethodFailureException {
12
+ constructor(code) {
13
+ super(new Failure(code, Resources.unreachableMessage));
14
+ }
15
+ }
@@ -0,0 +1,15 @@
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 AccountSearch {
9
+ account;
10
+ answer;
11
+ constructor(account, answer) {
12
+ this.account = account;
13
+ this.answer = answer;
14
+ }
15
+ }
@@ -5,20 +5,40 @@
5
5
  * This source code is licensed under the license found in the
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
+ import { GitHubNames } from "@noldova/git.github.protocol";
9
+ import { HostReference } from "@noldova/git.hosting.protocol";
8
10
  import { Resources } from "../resources.js";
9
11
  export class GitHubRepository {
12
+ host;
10
13
  owner;
11
14
  name;
12
- constructor(owner, name) {
15
+ constructor(host, owner, name) {
16
+ this.host = host;
13
17
  this.owner = owner;
14
18
  this.name = name;
15
19
  }
16
- static parse(path) {
20
+ static parse(host, path) {
17
21
  const trimmed = path.endsWith(Resources.gitSuffix) ? path.slice(0, -Resources.gitSuffix.length) : path;
18
22
  if (!Resources.repositoryPathPattern.test(trimmed))
19
23
  return null;
20
24
  const slash = trimmed.indexOf(Resources.repositoryPathSeparator);
21
- return new GitHubRepository(trimmed.slice(0, slash), trimmed.slice(slash + 1));
25
+ return new GitHubRepository(host.toLowerCase(), trimmed.slice(0, slash), trimmed.slice(slash + 1));
26
+ }
27
+ static ofReference(reference) {
28
+ return reference.host === GitHubNames.moduleId
29
+ ? GitHubRepository.parse(reference.place ?? Resources.host, Resources.formatRepositoryName(reference.owner, reference.repository)) : null;
30
+ }
31
+ get place() {
32
+ return this.host === Resources.host ? null : this.host;
33
+ }
34
+ get repositoryPath() {
35
+ return Resources.formatRepositoryName(this.owner, this.name);
36
+ }
37
+ get path() {
38
+ return Resources.formatRepositoryReadPath(encodeURIComponent(this.owner), encodeURIComponent(this.name));
39
+ }
40
+ referenceOf(number) {
41
+ return new HostReference(GitHubNames.moduleId, this.owner, this.name, number, undefined, this.place);
22
42
  }
23
43
  pathOf(rest) {
24
44
  return Resources.formatRepositoryPath(encodeURIComponent(this.owner), encodeURIComponent(this.name), rest);
@@ -0,0 +1,26 @@
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 { JsonReader } from "@noldova/context.json";
9
+ import { Resources } from "../resources.js";
10
+ export class KeptAccount {
11
+ id;
12
+ host;
13
+ login;
14
+ constructor(id, host, login) {
15
+ this.id = id;
16
+ this.host = host;
17
+ this.login = login;
18
+ }
19
+ static fromJson(value) {
20
+ const reader = JsonReader.fromValue(value);
21
+ return new KeptAccount(reader.readInteger(Resources.idField), reader.readNonBlankString(Resources.hostField), reader.readNullableString(Resources.loginField));
22
+ }
23
+ toJson() {
24
+ return { [Resources.idField]: this.id, [Resources.hostField]: this.host, [Resources.loginField]: this.login };
25
+ }
26
+ }
@@ -0,0 +1,26 @@
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 { JsonReader } from "@noldova/context.json";
9
+ import { AccountPick } from "@noldova/git.hosting.protocol";
10
+ import { Resources } from "../resources.js";
11
+ import { KeptAccount } from "./kept-account.js";
12
+ export class KeptAccounts {
13
+ accounts;
14
+ picks;
15
+ constructor(accounts, picks) {
16
+ this.accounts = accounts;
17
+ this.picks = picks;
18
+ }
19
+ static fromJson(value) {
20
+ const reader = JsonReader.fromValue(value);
21
+ return new KeptAccounts(reader.readObjectArray(Resources.accountsField).map(t => KeptAccount.fromJson(t.toJson())), reader.readObjectArray(Resources.picksField).map(t => AccountPick.fromJson(t.toJson(), t.path)));
22
+ }
23
+ toJson() {
24
+ return { [Resources.accountsField]: this.accounts.map(t => t.toJson()), [Resources.picksField]: this.picks.map(t => t.toJson()) };
25
+ }
26
+ }
@@ -0,0 +1,13 @@
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 RemoteUser {
9
+ name;
10
+ constructor(name) {
11
+ this.name = name;
12
+ }
13
+ }
@@ -0,0 +1,17 @@
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 SignedInAccount {
9
+ id;
10
+ login;
11
+ token;
12
+ constructor(id, login, token) {
13
+ this.id = id;
14
+ this.login = login;
15
+ this.token = token;
16
+ }
17
+ }
@@ -0,0 +1,21 @@
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 { JsonReader } from "@noldova/context.json";
9
+ import { Resources } from "../resources.js";
10
+ export class TokenConnect {
11
+ host;
12
+ token;
13
+ constructor(host, token) {
14
+ this.host = host;
15
+ this.token = token;
16
+ }
17
+ static fromJson(value) {
18
+ const reader = JsonReader.fromValue(value);
19
+ return new TokenConnect(reader.readNonBlankString(Resources.hostField), reader.readNonBlankString(Resources.tokenField));
20
+ }
21
+ }
package/src/resources.js CHANGED
@@ -9,9 +9,20 @@ export class Resources {
9
9
  static deviceCodeUrl = "https://github.com/login/device/code";
10
10
  static accessTokenUrl = "https://github.com/login/oauth/access_token";
11
11
  static userUrl = "https://api.github.com/user";
12
+ static enterpriseHostMessage = "Type the GitHub Enterprise server's name, such as github.example.com; github.com signs in with Sign in to GitHub.";
13
+ static tokenNotFromWindowMessage = "Only a window can sign in with a token.";
12
14
  static scope = "repo";
13
15
  static grantType = "urn:ietf:params:oauth:grant-type:device_code";
14
16
  static signInSecret = "git-github.signIn";
17
+ static accountsSecret = "git-github.accounts";
18
+ static picksCap = 200;
19
+ static usersCap = 200;
20
+ static unknownAccountMessage = "This GitHub account isn't kept on this computer any more.";
21
+ static keptAccountsUnreadable = "The GitHub accounts kept on this computer couldn't be read; only the first account was restored.";
22
+ static accountAlreadyKept = "An added GitHub account signed in as an account already kept here, so its token was forgotten and it ended signed out as already kept.";
23
+ static accountsUnreadMessage = "The GitHub accounts kept on this computer aren't read, so no account can be added until they are; the ones kept stay as they are.";
24
+ static accountsNotKept = "The GitHub accounts couldn't be kept on this computer; added accounts and chosen accounts last until the app closes.";
25
+ static accessOtherHostReason = "The GitHub module gives other modules access to github.com only; other GitHub servers aren't served yet.";
15
26
  static accessSignedOutReason = "Sign in to GitHub in Settings, GitHub.";
16
27
  static accessUnreadableReason = "The GitHub sign-in kept on this computer couldn't be read; sign in again in Settings, GitHub.";
17
28
  static accessWaitingReason = "The sign-in to GitHub isn't finished yet: enter its code at GitHub, or sign in again in Settings, GitHub.";
@@ -88,6 +99,8 @@ export class Resources {
88
99
  static headField = "head";
89
100
  static baseField = "base";
90
101
  static refField = "ref";
102
+ static repoField = "repo";
103
+ static defaultBranchField = "default_branch";
91
104
  static shaField = "sha";
92
105
  static additionsField = "additions";
93
106
  static deletionsField = "deletions";
@@ -97,6 +110,10 @@ export class Resources {
97
110
  static userField = "user";
98
111
  static authorField = "author";
99
112
  static loginField = "login";
113
+ static hostField = "host";
114
+ static enterpriseHostPattern = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)+(?::[1-9][0-9]{0,4})?$/;
115
+ static accountsField = "accounts";
116
+ static picksField = "picks";
100
117
  static avatarUrlField = "avatar_url";
101
118
  static titleField = "title";
102
119
  static htmlUrlField = "html_url";
@@ -128,6 +145,7 @@ export class Resources {
128
145
  static refusedPullRequestMessage = "GitHub refused the pull request: the branch may already have one, or have no commits the base lacks.";
129
146
  static otherHostMessage = "The repository isn't on github.com.";
130
147
  static notFromWindowMessage = "Only a person's click in a window opens a pull request.";
148
+ static folderNotFromWindowMessage = "Only a window reads a folder's remote.";
131
149
  static readToolTitle = "Read a GitHub issue or pull request";
132
150
  static readToolDescription = "Reads an issue or pull request on github.com by its owner, repository and number, with the account the person "
133
151
  + "signed in to GitHub with: its kind, state, title, address, author, created time and comment count, and for a pull request its branches, "
@@ -169,6 +187,7 @@ export class Resources {
169
187
  static createToolDeadline = 60_000;
170
188
  static createToolMaximumDeadline = 120_000;
171
189
  static branchLabel = "Branch";
190
+ static accountLabel = "GitHub account";
172
191
  static titleLabel = "Title";
173
192
  static draftLabel = "Draft";
174
193
  static bodyLabel = "Body";
@@ -182,6 +201,7 @@ export class Resources {
182
201
  static detachedHeadToolCode = "git-github.detachedHead";
183
202
  static noFolderToolCode = "git-github.noFolder";
184
203
  static gitMissingToolMessage = "The Git module isn't active, so the session's repository can't be read.";
204
+ static gitMissingMessage = "The Git module isn't active, so the folder's remote can't be read.";
185
205
  static notGitHubToolMessage = "The session's repository has no origin remote on github.com.";
186
206
  static detachedHeadToolMessage = "The session's repository has no branch checked out.";
187
207
  static itemNotFoundMessage = "GitHub has no such issue or pull request, or the signed-in account can't see it.";
@@ -220,6 +240,7 @@ export class Resources {
220
240
  static checkRunsPageSize = 100;
221
241
  static reviewsPageSize = 100;
222
242
  static commentsPageSize = 100;
243
+ static commitPullsPageSize = 100;
223
244
  static newestCommentsCount = 100;
224
245
  static approvedReview = "APPROVED";
225
246
  static changesRequestedReview = "CHANGES_REQUESTED";
@@ -239,7 +260,6 @@ export class Resources {
239
260
  static incompleteResultsField = "incomplete_results";
240
261
  static repositoryUrlField = "repository_url";
241
262
  static reviewRequestsPath = "/search/issues?q=is%3Apr+is%3Aopen+review-requested%3A%40me&sort=updated&order=desc&per_page=100";
242
- static repositoryApiPrefix = "https://api.github.com/repos/";
243
263
  static reviewRequestsHeldFor = 60_000;
244
264
  static objectType = "object";
245
265
  static stringType = "string";
@@ -322,8 +342,8 @@ export class Resources {
322
342
  static formatCommitCommentsPath(commit) {
323
343
  return `commits/${commit}/comments?per_page=${Resources.commentsPageSize}`;
324
344
  }
325
- static formatCommitPullsPath(commit) {
326
- return `commits/${commit}/${Resources.pullsPath}?per_page=1`;
345
+ static formatCommitPullRequestsPath(commit) {
346
+ return `commits/${commit}/${Resources.pullsPath}?per_page=${Resources.commitPullsPageSize}`;
327
347
  }
328
348
  static formatPullCommentsPath(number) {
329
349
  return `${Resources.pullsPath}/${number}/comments?per_page=${Resources.commentsPageSize}`;
@@ -359,6 +379,15 @@ export class Resources {
359
379
  static formatBody(body) {
360
380
  return `${Resources.bodyStart}${body}`;
361
381
  }
382
+ static formatRepositoryReadPath(owner, repository) {
383
+ return `/repos/${owner}/${repository}`;
384
+ }
385
+ static formatAccountSecret(id) {
386
+ return `git-github.account${id}`;
387
+ }
388
+ static formatKeptAccountsUnread(reason) {
389
+ return `The GitHub accounts kept on this computer couldn't be read: ${reason}`;
390
+ }
362
391
  static formatRepositoryPath(owner, repository, rest) {
363
392
  return `/repos/${owner}/${repository}/${rest}`;
364
393
  }
@@ -368,4 +397,22 @@ export class Resources {
368
397
  static formatDraftSubject(host, path) {
369
398
  return `${host}/${path}`;
370
399
  }
400
+ static formatUserUrl(host) {
401
+ return host === Resources.host ? Resources.userUrl : `${Resources.formatApiUrl(host)}/user`;
402
+ }
403
+ static formatApiUrl(host) {
404
+ return host === Resources.host ? Resources.apiUrl : `https://${host}/api/v3`;
405
+ }
406
+ static formatRepositoryApiPrefix(host) {
407
+ return `${Resources.formatApiUrl(host)}/repos/`;
408
+ }
409
+ static formatTokenRefused(host) {
410
+ return `${host} didn't accept the token. Check that it was copied whole, hasn't expired and has the ${Resources.scope} scope.`;
411
+ }
412
+ static formatScopeMissing(host) {
413
+ return `The token for ${host} doesn't have the ${Resources.scope} scope, which reading and opening pull requests needs. Create one with ${Resources.scope} ticked.`;
414
+ }
415
+ static formatServerUnreachable(host) {
416
+ return `${host} couldn't be reached. Check the server's name and the network, then connect again.`;
417
+ }
371
418
  }
@@ -7,45 +7,53 @@
7
7
  */
8
8
  import { JsonException } from "@noldova/context.json";
9
9
  import { Failure, FailureCode, ShellClients } from "@noldova/desktop.protocol";
10
- import { MethodFailureException } from "@noldova/desktop.runtime";
11
- import { AuthorQuery, BranchQuery, CommitQuery, HostAvatar, HostingProtocolException, HostReference, PullRequestDraft, RepositoryQuery, SignInState } from "@noldova/git.hosting.protocol";
10
+ import { MethodFailureException, ServiceAccessException } from "@noldova/desktop.runtime";
11
+ import { AccountPick, AccountQuery, AuthorQuery, BranchQuery, CommitQuery, FolderQuery, HostAvatar, HostingProtocolException, HostReference, PullRequestDraft, RepositoryAccount, RepositoryQuery } from "@noldova/git.hosting.protocol";
12
12
  import { GitHubNames } from "@noldova/git.github.protocol";
13
13
  import { GitNames, GitRepositories } from "@noldova/git.protocol";
14
14
  import { SignInMethod } from "./models/sign-in-method.js";
15
+ import { TokenConnect } from "./models/token-connect.js";
15
16
  import { Resources } from "./resources.js";
16
17
  import { GitHubAccess } from "./services/github-access.js";
18
+ import { GitHubAccounts } from "./services/github-accounts.js";
17
19
  import { GitHubApi } from "./services/github-api.js";
18
20
  import { GitHubAvatars } from "./services/github-avatars.js";
19
21
  import { GitHubDeviceFlow } from "./services/github-device-flow.js";
22
+ import { GitHubFolderUsers } from "./services/github-folder-users.js";
20
23
  import { GitHubReads } from "./services/github-reads.js";
21
- import { GitHubSignIn } from "./services/github-sign-in.js";
22
24
  import { GitHubTools } from "./tools/github-tools.js";
23
25
  export class RuntimePart {
26
+ static firstAccount = 1;
24
27
  context = null;
25
- signIn = null;
28
+ accounts = null;
26
29
  tools = null;
27
30
  get isActive() {
28
31
  return this.context !== null;
29
32
  }
30
33
  async activateAsync(context) {
31
34
  const changed = context.declareEvent(GitHubNames.signInChangedEvent);
35
+ const accountsChanged = context.declareEvent(GitHubNames.accountsChangedEvent);
32
36
  const fetcher = (url, init) => fetch(url, init);
33
37
  const api = new GitHubApi(fetcher);
34
38
  const avatars = new GitHubAvatars(fetcher, context.log);
35
- const signIn = new GitHubSignIn(new GitHubDeviceFlow(fetcher, avatars), context.secrets, context.appWindows, context.log, t => {
36
- if (t.state !== SignInState.SignedIn)
37
- api.clear();
38
- changed.publish(t.toJson());
39
+ const accounts = new GitHubAccounts(new GitHubDeviceFlow(fetcher, avatars), context.secrets, context.appWindows, context.log, api, (id, t) => {
40
+ api.keepOnly(u => accounts.holds(u));
41
+ if (id === RuntimePart.firstAccount)
42
+ changed.publish(t.toJson());
43
+ accountsChanged.publish(accounts.list.toJson());
39
44
  });
40
- const reads = new GitHubReads(api, signIn, avatars);
45
+ const reads = new GitHubReads(api, accounts, avatars);
41
46
  const methods = [
42
- new SignInMethod(GitHubNames.readSignInMethod, () => signIn.current),
43
- new SignInMethod(GitHubNames.startSignInMethod, () => signIn.startAsync()),
44
- new SignInMethod(GitHubNames.cancelSignInMethod, () => signIn.cancel()),
45
- new SignInMethod(GitHubNames.signOutMethod, () => signIn.signOutAsync())
47
+ new SignInMethod(GitHubNames.readSignInMethod, t => accounts.signInOf(t).current),
48
+ new SignInMethod(GitHubNames.startSignInMethod, t => accounts.signInOf(t).startAsync()),
49
+ new SignInMethod(GitHubNames.cancelSignInMethod, t => accounts.signInOf(t).cancel()),
50
+ new SignInMethod(GitHubNames.signOutMethod, t => accounts.signOutAsync(t))
46
51
  ];
47
- for (const method of methods)
48
- context.registerMethod(method.name, { handleAsync: async () => (await method.run()).toJson() });
52
+ for (const method of methods) {
53
+ context.registerMethod(method.name, {
54
+ handleAsync: async (request) => (await method.run(request.payload === null ? RuntimePart.firstAccount : RuntimePart.read(request, AccountQuery.fromJson).account)).toJson()
55
+ });
56
+ }
49
57
  context.registerMethod(GitHubNames.readReferenceMethod, {
50
58
  handleAsync: async (request) => (await reads.readShownReferenceAsync(RuntimePart.read(request, HostReference.fromJson), request.signal))?.toJson() ?? null
51
59
  });
@@ -70,29 +78,67 @@ export class RuntimePart {
70
78
  context.registerMethod(GitHubNames.readPullRequestThreadsMethod, {
71
79
  handleAsync: async (request) => (await reads.readPullRequestThreadsAsync(RuntimePart.read(request, HostReference.fromJson), request.signal)).toJson()
72
80
  });
81
+ context.registerMethod(GitHubNames.readCommitPullRequestMethod, {
82
+ handleAsync: async (request) => (await reads.readCommitPullRequestAsync(RuntimePart.read(request, CommitQuery.fromJson), request.signal)).toJson()
83
+ });
73
84
  context.registerMethod(GitHubNames.createPullRequestMethod, {
74
- handleAsync: async (request) => (await reads.createPullRequestAsync(RuntimePart.read(RuntimePart.fromWindow(request), PullRequestDraft.fromJson), request.signal)).toJson()
85
+ handleAsync: async (request) => (await reads.createPullRequestAsync(RuntimePart.read(RuntimePart.fromWindow(request, Resources.notFromWindowMessage), PullRequestDraft.fromJson), request.signal)).toJson()
86
+ });
87
+ context.registerMethod(GitHubNames.readAccountsMethod, { handleAsync: () => Promise.resolve(accounts.list.toJson()) });
88
+ context.registerMethod(GitHubNames.addAccountMethod, { handleAsync: async () => (await accounts.addAsync()).toJson() });
89
+ context.registerMethod(GitHubNames.readRepositoryAccountMethod, {
90
+ handleAsync: async (request) => {
91
+ const query = RuntimePart.read(request, RepositoryQuery.fromJson);
92
+ return (await accounts.repositoryAccountAsync(query.host, query.path, request.signal)).toJson();
93
+ }
94
+ });
95
+ context.registerMethod(GitHubNames.pickAccountMethod, {
96
+ handleAsync: async (request) => (await accounts.pickAsync(RuntimePart.read(request, AccountPick.fromJson), request.signal)).toJson()
97
+ });
98
+ context.registerMethod(GitHubNames.useFolderMethod, {
99
+ handleAsync: async (request) => {
100
+ const query = RuntimePart.read(RuntimePart.fromWindow(request, Resources.folderNotFromWindowMessage), FolderQuery.fromJson);
101
+ const repositories = RuntimePart.gitOf(context);
102
+ const remote = await repositories.readRemoteAsync(query.folder, query.remote, request.signal);
103
+ if (remote === null || remote.host === null || remote.path === null)
104
+ return new RepositoryAccount(null).toJson();
105
+ accounts.useUser(remote.host, remote.path, await repositories.readRemoteUserAsync(query.folder, query.remote, request.signal));
106
+ return (await accounts.repositoryAccountAsync(remote.host, remote.path, request.signal)).toJson();
107
+ }
108
+ });
109
+ context.registerMethod(GitHubNames.signInWithTokenMethod, {
110
+ handleAsync: async (request) => (await accounts.connectAsync(RuntimePart.read(RuntimePart.fromWindow(request, Resources.tokenNotFromWindowMessage), TokenConnect.fromJson), request.signal)).toJson()
75
111
  });
76
- context.publishService(GitHubNames.accessService, new GitHubAccess(signIn));
77
- const tools = new GitHubTools(reads, signIn, () => context.getService(GitNames.repositoriesService, GitRepositories));
112
+ context.publishService(GitHubNames.accessService, new GitHubAccess(accounts, new GitHubFolderUsers(() => context.getService(GitNames.repositoriesService, GitRepositories))));
113
+ const tools = new GitHubTools(reads, accounts, () => context.getService(GitNames.repositoriesService, GitRepositories));
78
114
  this.context = context;
79
- this.signIn = signIn;
115
+ this.accounts = accounts;
80
116
  this.tools = tools;
81
- await signIn.restoreAsync();
117
+ await accounts.restoreAsync();
82
118
  tools.publish(context.tools);
83
119
  }
84
120
  async deactivateAsync() {
85
121
  this.tools?.dispose();
86
122
  this.tools = null;
87
- this.signIn?.dispose();
88
- this.signIn = null;
123
+ this.accounts?.dispose();
124
+ this.accounts = null;
89
125
  this.context = null;
90
126
  }
91
- static fromWindow(request) {
127
+ static fromWindow(request, message) {
92
128
  if (request.window === null || request.client !== ShellClients.mainProcess)
93
- throw GitHubApi.failure(FailureCode.Unauthorized, Resources.notFromWindowMessage);
129
+ throw GitHubApi.failure(FailureCode.Unauthorized, message);
94
130
  return request;
95
131
  }
132
+ static gitOf(context) {
133
+ try {
134
+ return context.getService(GitNames.repositoriesService, GitRepositories);
135
+ }
136
+ catch (error) {
137
+ if (!(error instanceof ServiceAccessException))
138
+ throw error;
139
+ throw GitHubApi.failure(FailureCode.Unavailable, Resources.gitMissingMessage);
140
+ }
141
+ }
96
142
  static read(request, read) {
97
143
  try {
98
144
  return read(request.payload);
@@ -9,20 +9,31 @@ import { SignInState } from "@noldova/git.hosting.protocol";
9
9
  import { GitHubAccessGrant, GitHubAccessRefusal } from "@noldova/git.github.protocol";
10
10
  import { Resources } from "../resources.js";
11
11
  export class GitHubAccess {
12
- signIn;
13
- constructor(signIn) {
14
- this.signIn = signIn;
12
+ accounts;
13
+ folders;
14
+ constructor(accounts, folders) {
15
+ this.accounts = accounts;
16
+ this.folders = folders;
15
17
  }
16
18
  accessAsync(signal) {
17
- return signal.aborted ? Promise.reject(signal.reason) : Promise.resolve(this.answer());
19
+ return signal.aborted ? Promise.reject(signal.reason) : Promise.resolve(GitHubAccess.answerOf(this.accounts.first));
18
20
  }
19
- answer() {
20
- const current = this.signIn.current;
21
- const token = this.signIn.token;
21
+ async accessForAsync(repository, signal) {
22
+ signal.throwIfAborted();
23
+ if (repository.host.toLowerCase() !== Resources.host)
24
+ return new GitHubAccessRefusal(Resources.accessOtherHostReason);
25
+ const user = Object.isNull(repository.folder) ? null
26
+ : await this.folders.userOfAsync(repository.folder, repository.remote ?? Resources.originRemote, repository.host, repository.path, signal);
27
+ const id = await this.accounts.accountForAsync(repository.host, repository.path, signal, user);
28
+ return GitHubAccess.answerOf(id === null ? this.accounts.first : this.accounts.signInOf(id));
29
+ }
30
+ static answerOf(signIn) {
31
+ const current = signIn.current;
32
+ const token = signIn.token;
22
33
  if (current.account !== null && token !== null)
23
- return new GitHubAccessGrant(token, current.account.login);
34
+ return new GitHubAccessGrant(token, current.account.login, Resources.host);
24
35
  if (current.state === SignInState.Waiting)
25
36
  return new GitHubAccessRefusal(Resources.accessWaitingReason);
26
- return new GitHubAccessRefusal(this.signIn.keptUnreadable ? Resources.accessUnreadableReason : Resources.accessSignedOutReason);
37
+ return new GitHubAccessRefusal(signIn.keptUnreadable ? Resources.accessUnreadableReason : Resources.accessSignedOutReason);
27
38
  }
28
39
  }