@noldova/git.github 0.0.0-stage → 0.0.2

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.
Files changed (42) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +10 -2
  3. package/module.json +16 -0
  4. package/package.json +48 -4
  5. package/settings.json +4 -0
  6. package/src/api/index.d.ts +73 -0
  7. package/src/api/index.js +8 -0
  8. package/src/api/window.d.ts +19 -0
  9. package/src/api/window.js +8 -0
  10. package/src/exceptions/github.exception.js +13 -0
  11. package/src/exceptions/sign-in-answer-unreadable.exception.js +15 -0
  12. package/src/exceptions/sign-in-denied.exception.js +14 -0
  13. package/src/exceptions/sign-in-error.exception.js +16 -0
  14. package/src/exceptions/sign-in-expired.exception.js +14 -0
  15. package/src/exceptions/sign-in-expiry-invalid.exception.js +16 -0
  16. package/src/exceptions/sign-in-not-kept.exception.js +15 -0
  17. package/src/exceptions/sign-in-refused.exception.js +13 -0
  18. package/src/exceptions/sign-in-request-refused.exception.js +18 -0
  19. package/src/exceptions/sign-in-token-refused.exception.js +14 -0
  20. package/src/exceptions/sign-in-unreachable.exception.js +17 -0
  21. package/src/interfaces/i-github-token.js +8 -0
  22. package/src/models/account-read.js +15 -0
  23. package/src/models/cached-answer.js +20 -0
  24. package/src/models/device-grant.js +19 -0
  25. package/src/models/device-poll.js +26 -0
  26. package/src/models/github-repository.js +26 -0
  27. package/src/models/kept-sign-in.js +26 -0
  28. package/src/resources.js +294 -0
  29. package/src/runtime-part.js +87 -0
  30. package/src/services/github-access.js +28 -0
  31. package/src/services/github-api.js +167 -0
  32. package/src/services/github-avatars.js +115 -0
  33. package/src/services/github-device-flow.js +144 -0
  34. package/src/services/github-reads.js +149 -0
  35. package/src/services/github-sign-in.js +280 -0
  36. package/src/tools/bound-repository.js +58 -0
  37. package/src/tools/github-pr-checks-tool.js +47 -0
  38. package/src/tools/github-pr-create-tool.js +97 -0
  39. package/src/tools/github-read-tool.js +62 -0
  40. package/src/tools/github-tool-failures.js +46 -0
  41. package/src/tools/github-tools.js +25 -0
  42. package/src/types/fetcher.js +8 -0
@@ -0,0 +1,58 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) Noldova.
4
+ *
5
+ * This source code is licensed under the license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ import { JsonException } from "@noldova/context.json";
9
+ import { ServiceAccessException, ToolFailureCode } from "@noldova/desktop.runtime";
10
+ import { GitFolderBinding, ProtocolException } from "@noldova/git.protocol";
11
+ import { GitHubRepository } from "../models/github-repository.js";
12
+ import { Resources } from "../resources.js";
13
+ import { GitHubToolFailures } from "./github-tool-failures.js";
14
+ export class BoundRepository {
15
+ host;
16
+ path;
17
+ branch;
18
+ headCommit;
19
+ constructor(host, path, branch, headCommit) {
20
+ this.host = host;
21
+ this.path = path;
22
+ this.branch = branch;
23
+ this.headCommit = headCommit;
24
+ }
25
+ static async readAsync(call, repositories) {
26
+ const folder = BoundRepository.folderOf(call);
27
+ let status;
28
+ let origin;
29
+ try {
30
+ const reads = repositories();
31
+ status = await reads.readStatusAsync(folder, call.signal);
32
+ origin = await reads.readRemoteAsync(folder, Resources.originRemote, call.signal);
33
+ }
34
+ catch (error) {
35
+ throw error instanceof ServiceAccessException
36
+ ? GitHubToolFailures.fail(ToolFailureCode.Unavailable, Resources.gitMissingToolMessage, Resources.gitUnavailableToolCode)
37
+ : GitHubToolFailures.fromGit(error);
38
+ }
39
+ const host = origin?.host ?? null;
40
+ const remotePath = origin?.path ?? null;
41
+ 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)
43
+ throw GitHubToolFailures.fail(ToolFailureCode.Refused, Resources.notGitHubToolMessage, Resources.notGitHubToolCode);
44
+ if (status.branch === null)
45
+ throw GitHubToolFailures.fail(ToolFailureCode.Refused, Resources.detachedHeadToolMessage, Resources.detachedHeadToolCode);
46
+ return new BoundRepository(host, path, status.branch, status.head);
47
+ }
48
+ static folderOf(call) {
49
+ try {
50
+ return GitFolderBinding.fromBindings(call.bindings).folder;
51
+ }
52
+ catch (error) {
53
+ if (!(error instanceof JsonException || error instanceof ProtocolException))
54
+ throw error;
55
+ throw GitHubToolFailures.fail(ToolFailureCode.Refused, Resources.formatNoFolder(String(error)), Resources.noFolderToolCode);
56
+ }
57
+ }
58
+ }
@@ -0,0 +1,47 @@
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 { ToolContent, ToolResult, ToolRisk } from "@noldova/desktop.runtime";
9
+ import { BranchPullRequest, BranchQuery } from "@noldova/git.hosting.protocol";
10
+ import { GitHubNames } from "@noldova/git.github.protocol";
11
+ import { Resources } from "../resources.js";
12
+ import { BoundRepository } from "./bound-repository.js";
13
+ import { GitHubToolFailures } from "./github-tool-failures.js";
14
+ export class GitHubPrChecksTool {
15
+ reads;
16
+ token;
17
+ repositories;
18
+ descriptor = {
19
+ name: GitHubNames.prChecksTool,
20
+ title: Resources.prChecksToolTitle,
21
+ description: Resources.prChecksToolDescription,
22
+ inputSchema: { type: Resources.objectType, properties: {}, additionalProperties: false },
23
+ hints: { isReadOnly: true, isDestructive: false, isOpenWorld: true },
24
+ risk: { default: ToolRisk.Low, maximum: ToolRisk.Low },
25
+ deadline: { default: Resources.readToolDeadline, maximum: Resources.readToolMaximumDeadline }
26
+ };
27
+ constructor(reads, token, repositories) {
28
+ this.reads = reads;
29
+ this.token = token;
30
+ this.repositories = repositories;
31
+ }
32
+ async handleAsync(call) {
33
+ const repository = await BoundRepository.readAsync(call, this.repositories);
34
+ if (this.token.token === null)
35
+ throw GitHubToolFailures.signedOut();
36
+ let answer;
37
+ try {
38
+ answer = await this.reads.readBranchPullRequestAsync(new BranchQuery(repository.host, repository.path, repository.branch, repository.headCommit), call.signal);
39
+ }
40
+ catch (error) {
41
+ throw GitHubToolFailures.fromGitHub(error);
42
+ }
43
+ if (this.token.token === null)
44
+ throw GitHubToolFailures.signedOut();
45
+ return new ToolResult([ToolContent.fromText(JSON.stringify(answer.toJson()), Resources.gitHubOrigin)]);
46
+ }
47
+ }
@@ -0,0 +1,97 @@
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 { PermissionDetailKind, PermissionOption, ToolContent, ToolFailureCode, ToolResult, ToolRisk } from "@noldova/desktop.runtime";
10
+ import { HostingProtocolException, PullRequestDraft } from "@noldova/git.hosting.protocol";
11
+ import { GitHubNames } from "@noldova/git.github.protocol";
12
+ import { GitNames } from "@noldova/git.protocol";
13
+ import { Resources } from "../resources.js";
14
+ import { BoundRepository } from "./bound-repository.js";
15
+ import { GitHubToolFailures } from "./github-tool-failures.js";
16
+ export class GitHubPrCreateTool {
17
+ reads;
18
+ token;
19
+ repositories;
20
+ descriptor = {
21
+ name: GitHubNames.prCreateTool,
22
+ title: Resources.prCreateToolTitle,
23
+ description: Resources.prCreateToolDescription,
24
+ inputSchema: {
25
+ type: Resources.objectType,
26
+ properties: {
27
+ [Resources.titleArgument]: { type: Resources.stringType, minLength: Resources.minimumArgumentLength, maxLength: Resources.maximumTitleLength,
28
+ pattern: Resources.oneLinePattern },
29
+ [Resources.baseArgument]: { type: Resources.stringType, minLength: Resources.minimumArgumentLength, maxLength: Resources.maximumBranchLength,
30
+ pattern: Resources.notBlankPattern },
31
+ [Resources.bodyArgument]: { type: Resources.stringType, maxLength: Resources.maximumBodyLength },
32
+ [Resources.draftArgument]: { type: Resources.booleanType }
33
+ },
34
+ required: [Resources.titleArgument, Resources.baseArgument],
35
+ additionalProperties: false
36
+ },
37
+ hints: { isReadOnly: false, isDestructive: false, isOpenWorld: true },
38
+ risk: { default: ToolRisk.High, maximum: ToolRisk.High },
39
+ marksSending: true,
40
+ deadline: { default: Resources.createToolDeadline, maximum: Resources.createToolMaximumDeadline },
41
+ resource: Resources.formatBindingResource(GitNames.folderBinding),
42
+ nextTool: GitHubNames.prChecksTool
43
+ };
44
+ constructor(reads, token, repositories) {
45
+ this.reads = reads;
46
+ this.token = token;
47
+ this.repositories = repositories;
48
+ }
49
+ async handleAsync(call) {
50
+ const repository = await BoundRepository.readAsync(call, this.repositories);
51
+ const draft = GitHubPrCreateTool.draftOf(call, repository);
52
+ if (this.token.token === null)
53
+ throw GitHubToolFailures.signedOut();
54
+ const request = {
55
+ action: GitHubNames.createPullRequestMethod,
56
+ subject: Resources.formatDraftSubject(draft.host, draft.path),
57
+ title: Resources.formatCreateRequestTitle(draft.path, draft.isDraft),
58
+ details: GitHubPrCreateTool.detailsOf(draft),
59
+ risk: ToolRisk.High,
60
+ options: [PermissionOption.AllowOnce, PermissionOption.Deny]
61
+ };
62
+ const outcome = await call.requestPermissionAsync(request);
63
+ if (!outcome.isAllowed)
64
+ throw GitHubToolFailures.notGranted(outcome.decision);
65
+ call.markSending();
66
+ let created;
67
+ try {
68
+ created = await this.reads.createPullRequestAsync(draft, call.signal);
69
+ }
70
+ catch (error) {
71
+ throw GitHubToolFailures.fromGitHub(error);
72
+ }
73
+ return new ToolResult([ToolContent.fromText(JSON.stringify(created.toJson()), Resources.gitHubOrigin)]);
74
+ }
75
+ static draftOf(call, repository) {
76
+ const input = JsonReader.fromValue(call.arguments);
77
+ try {
78
+ return new PullRequestDraft(repository.host, repository.path, repository.branch, input.readString(Resources.baseArgument), input.readString(Resources.titleArgument), input.hasField(Resources.bodyArgument) ? input.readString(Resources.bodyArgument) : "", input.hasField(Resources.draftArgument) && input.readBoolean(Resources.draftArgument));
79
+ }
80
+ catch (error) {
81
+ if (!(error instanceof HostingProtocolException))
82
+ throw error;
83
+ throw GitHubToolFailures.fail(ToolFailureCode.Refused, String(error), null);
84
+ }
85
+ }
86
+ static detailsOf(draft) {
87
+ const details = [
88
+ { label: Resources.branchLabel, kind: PermissionDetailKind.Code, value: Resources.formatBranches(draft.headBranch, draft.baseBranch) },
89
+ { label: Resources.titleLabel, kind: PermissionDetailKind.AgentText, value: draft.title },
90
+ { label: Resources.draftLabel, kind: PermissionDetailKind.Text, value: draft.isDraft ? Resources.yes : Resources.no }
91
+ ];
92
+ const body = draft.body.trimEnd();
93
+ if (body.trim().length > 0)
94
+ details.push({ label: Resources.bodyLabel, kind: PermissionDetailKind.AgentText, value: Resources.formatBody(body) });
95
+ return details;
96
+ }
97
+ }
@@ -0,0 +1,62 @@
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 { ToolContent, ToolFailureCode, ToolResult, ToolRisk } from "@noldova/desktop.runtime";
10
+ import { HostReference } from "@noldova/git.hosting.protocol";
11
+ import { GitHubNames } from "@noldova/git.github.protocol";
12
+ import { Resources } from "../resources.js";
13
+ import { GitHubToolFailures } from "./github-tool-failures.js";
14
+ export class GitHubReadTool {
15
+ reads;
16
+ token;
17
+ descriptor = {
18
+ name: GitHubNames.readTool,
19
+ title: Resources.readToolTitle,
20
+ description: Resources.readToolDescription,
21
+ inputSchema: {
22
+ type: Resources.objectType,
23
+ properties: {
24
+ [Resources.ownerArgument]: { type: Resources.stringType, minLength: Resources.minimumArgumentLength, maxLength: Resources.maximumOwnerLength,
25
+ pattern: Resources.ownerSchemaPattern },
26
+ [Resources.repositoryArgument]: { type: Resources.stringType, minLength: Resources.minimumArgumentLength, maxLength: Resources.maximumRepositoryLength,
27
+ pattern: Resources.repositorySchemaPattern },
28
+ [Resources.numberArgument]: { type: Resources.integerType, minimum: Resources.minimumItemNumber, maximum: Resources.maximumItemNumber }
29
+ },
30
+ required: [Resources.ownerArgument, Resources.repositoryArgument, Resources.numberArgument],
31
+ additionalProperties: false
32
+ },
33
+ hints: { isReadOnly: true, isDestructive: false, isOpenWorld: true },
34
+ risk: { default: ToolRisk.Low, maximum: ToolRisk.Low },
35
+ deadline: { default: Resources.readToolDeadline, maximum: Resources.readToolMaximumDeadline }
36
+ };
37
+ constructor(reads, token) {
38
+ this.reads = reads;
39
+ this.token = token;
40
+ }
41
+ async handleAsync(call) {
42
+ const reference = GitHubReadTool.referenceOf(call);
43
+ if (this.token.token === null)
44
+ throw GitHubToolFailures.signedOut();
45
+ let item;
46
+ try {
47
+ item = await this.reads.readReferenceAsync(reference, call.signal);
48
+ }
49
+ catch (error) {
50
+ throw GitHubToolFailures.fromGitHub(error);
51
+ }
52
+ if (item === null)
53
+ throw this.token.token === null
54
+ ? GitHubToolFailures.signedOut()
55
+ : GitHubToolFailures.fail(ToolFailureCode.Unavailable, Resources.itemNotFoundMessage, Resources.notFoundToolCode);
56
+ return new ToolResult([ToolContent.fromText(JSON.stringify(item.toJson()), Resources.gitHubOrigin)]);
57
+ }
58
+ static referenceOf(call) {
59
+ const input = JsonReader.fromValue(call.arguments);
60
+ return new HostReference(GitHubNames.moduleId, input.readString(Resources.ownerArgument), input.readString(Resources.repositoryArgument), input.readInteger(Resources.numberArgument));
61
+ }
62
+ }
@@ -0,0 +1,46 @@
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 { FailureCode } from "@noldova/desktop.protocol";
9
+ import { MethodFailureException, PermissionDecision, ToolFailure, ToolFailureCode, ToolFailureException } from "@noldova/desktop.runtime";
10
+ import { Resources } from "../resources.js";
11
+ export class GitHubToolFailures {
12
+ static DECISIONS = new Map([
13
+ [PermissionDecision.Expired, ToolFailureCode.Expired],
14
+ [PermissionDecision.Withdrawn, ToolFailureCode.Withdrawn]
15
+ ]);
16
+ static fail(code, message, moduleCode) {
17
+ return new ToolFailureException(new ToolFailure(code, message, moduleCode));
18
+ }
19
+ static signedOut() {
20
+ return GitHubToolFailures.fail(ToolFailureCode.Unavailable, Resources.signedOutToolMessage, Resources.signedOutToolCode);
21
+ }
22
+ static notGranted(decision) {
23
+ return GitHubToolFailures.fail(GitHubToolFailures.DECISIONS.get(decision) ?? ToolFailureCode.Denied, Resources.formatNotGranted(decision), null);
24
+ }
25
+ static fromGitHub(error) {
26
+ if (!(error instanceof MethodFailureException))
27
+ return error;
28
+ switch (error.failure.code) {
29
+ case FailureCode.Unauthorized:
30
+ return GitHubToolFailures.signedOut();
31
+ case FailureCode.DeadlineExceeded:
32
+ return GitHubToolFailures.fail(ToolFailureCode.TimedOut, error.failure.message, null);
33
+ case FailureCode.Conflict:
34
+ return GitHubToolFailures.fail(ToolFailureCode.Refused, error.failure.message, Resources.refusedToolCode);
35
+ default:
36
+ return GitHubToolFailures.fail(ToolFailureCode.Unavailable, error.failure.message, Resources.unavailableToolCode);
37
+ }
38
+ }
39
+ static fromGit(error) {
40
+ if (!(error instanceof MethodFailureException))
41
+ return error;
42
+ return error.failure.code === FailureCode.NotFound
43
+ ? GitHubToolFailures.fail(ToolFailureCode.Refused, error.failure.message, Resources.notRepositoryToolCode)
44
+ : GitHubToolFailures.fail(ToolFailureCode.Unavailable, error.failure.message, Resources.gitUnavailableToolCode);
45
+ }
46
+ }
@@ -0,0 +1,25 @@
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 { GitHubPrChecksTool } from "./github-pr-checks-tool.js";
9
+ import { GitHubPrCreateTool } from "./github-pr-create-tool.js";
10
+ import { GitHubReadTool } from "./github-read-tool.js";
11
+ export class GitHubTools {
12
+ registrations = [];
13
+ tools;
14
+ constructor(reads, token, repositories) {
15
+ this.tools = [new GitHubReadTool(reads, token), new GitHubPrChecksTool(reads, token, repositories), new GitHubPrCreateTool(reads, token, repositories)];
16
+ }
17
+ publish(tools) {
18
+ for (const tool of this.tools)
19
+ this.registrations.push(tools.register(tool.descriptor, tool));
20
+ }
21
+ dispose() {
22
+ for (const registration of this.registrations.splice(0))
23
+ registration[Symbol.dispose]();
24
+ }
25
+ }
@@ -0,0 +1,8 @@
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 {};