@noldova/git.github 0.0.8 → 0.0.9

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.
@@ -15,7 +15,7 @@ import { BoundRepository } from "./bound-repository.js";
15
15
  import { GitHubToolFailures } from "./github-tool-failures.js";
16
16
  export class GitHubPrCreateTool {
17
17
  reads;
18
- token;
18
+ tokens;
19
19
  repositories;
20
20
  descriptor = {
21
21
  name: GitHubNames.prCreateTool,
@@ -41,31 +41,35 @@ export class GitHubPrCreateTool {
41
41
  resource: Resources.formatBindingResource(GitNames.folderBinding),
42
42
  nextTool: GitHubNames.prChecksTool
43
43
  };
44
- constructor(reads, token, repositories) {
44
+ constructor(reads, tokens, repositories) {
45
45
  this.reads = reads;
46
- this.token = token;
46
+ this.tokens = tokens;
47
47
  this.repositories = repositories;
48
48
  }
49
49
  async handleAsync(call) {
50
50
  const repository = await BoundRepository.readAsync(call, this.repositories);
51
51
  const draft = GitHubPrCreateTool.draftOf(call, repository);
52
- if (this.token.token === null)
52
+ const token = await this.tokens.tokenForAsync(draft.host, draft.path, call.signal, repository.user);
53
+ const account = this.tokens.signedIn.find(t => t.token === token);
54
+ if (account === undefined)
53
55
  throw GitHubToolFailures.signedOut();
54
56
  const request = {
55
57
  action: GitHubNames.createPullRequestMethod,
56
58
  subject: Resources.formatDraftSubject(draft.host, draft.path),
57
59
  title: Resources.formatCreateRequestTitle(draft.path, draft.isDraft),
58
- details: GitHubPrCreateTool.detailsOf(draft),
60
+ details: GitHubPrCreateTool.detailsOf(draft, account.login),
59
61
  risk: ToolRisk.High,
60
62
  options: [PermissionOption.AllowOnce, PermissionOption.Deny]
61
63
  };
62
64
  const outcome = await call.requestPermissionAsync(request);
63
65
  if (!outcome.isAllowed)
64
66
  throw GitHubToolFailures.notGranted(outcome.decision);
67
+ if (!this.tokens.holds(account.token))
68
+ throw GitHubToolFailures.signedOut();
65
69
  call.markSending();
66
70
  let created;
67
71
  try {
68
- created = await this.reads.createPullRequestAsync(draft, call.signal);
72
+ created = await this.reads.createPullRequestAsync(draft, call.signal, account.token);
69
73
  }
70
74
  catch (error) {
71
75
  throw GitHubToolFailures.fromGitHub(error);
@@ -83,8 +87,9 @@ export class GitHubPrCreateTool {
83
87
  throw GitHubToolFailures.fail(ToolFailureCode.Refused, String(error), null);
84
88
  }
85
89
  }
86
- static detailsOf(draft) {
90
+ static detailsOf(draft, login) {
87
91
  const details = [
92
+ { label: Resources.accountLabel, kind: PermissionDetailKind.Text, value: login },
88
93
  { label: Resources.branchLabel, kind: PermissionDetailKind.Code, value: Resources.formatBranches(draft.headBranch, draft.baseBranch) },
89
94
  { label: Resources.titleLabel, kind: PermissionDetailKind.AgentText, value: draft.title },
90
95
  { label: Resources.draftLabel, kind: PermissionDetailKind.Text, value: draft.isDraft ? Resources.yes : Resources.no }
@@ -13,7 +13,7 @@ import { Resources } from "../resources.js";
13
13
  import { GitHubToolFailures } from "./github-tool-failures.js";
14
14
  export class GitHubReadTool {
15
15
  reads;
16
- token;
16
+ tokens;
17
17
  descriptor = {
18
18
  name: GitHubNames.readTool,
19
19
  title: Resources.readToolTitle,
@@ -34,13 +34,13 @@ export class GitHubReadTool {
34
34
  risk: { default: ToolRisk.Low, maximum: ToolRisk.Low },
35
35
  deadline: { default: Resources.readToolDeadline, maximum: Resources.readToolMaximumDeadline }
36
36
  };
37
- constructor(reads, token) {
37
+ constructor(reads, tokens) {
38
38
  this.reads = reads;
39
- this.token = token;
39
+ this.tokens = tokens;
40
40
  }
41
41
  async handleAsync(call) {
42
42
  const reference = GitHubReadTool.referenceOf(call);
43
- if (this.token.token === null)
43
+ if (this.tokens.signedIn.length === 0)
44
44
  throw GitHubToolFailures.signedOut();
45
45
  let item;
46
46
  try {
@@ -50,7 +50,7 @@ export class GitHubReadTool {
50
50
  throw GitHubToolFailures.fromGitHub(error);
51
51
  }
52
52
  if (item === null)
53
- throw this.token.token === null
53
+ throw this.tokens.signedIn.length === 0
54
54
  ? GitHubToolFailures.signedOut()
55
55
  : GitHubToolFailures.fail(ToolFailureCode.Unavailable, Resources.itemNotFoundMessage, Resources.notFoundToolCode);
56
56
  return new ToolResult([ToolContent.fromText(JSON.stringify(item.toJson()), Resources.gitHubOrigin)]);
@@ -11,8 +11,8 @@ import { GitHubReadTool } from "./github-read-tool.js";
11
11
  export class GitHubTools {
12
12
  registrations = [];
13
13
  tools;
14
- constructor(reads, token, repositories) {
15
- this.tools = [new GitHubReadTool(reads, token), new GitHubPrChecksTool(reads, token, repositories), new GitHubPrCreateTool(reads, token, repositories)];
14
+ constructor(reads, tokens, repositories) {
15
+ this.tools = [new GitHubReadTool(reads, tokens), new GitHubPrChecksTool(reads, tokens, repositories), new GitHubPrCreateTool(reads, tokens, repositories)];
16
16
  }
17
17
  publish(tools) {
18
18
  for (const tool of this.tools)