@theholocron/holocron-plugin-github 5.0.0-alpha.584 → 5.0.0-alpha.585

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/dist/index.d.mts CHANGED
@@ -13,6 +13,11 @@ import { GitHubClient, GitHubClientOptions, GraphQLClient, createGitHubClient }
13
13
  export declare const resolveReadToken: import("@theholocron/cli").FeatureResolver;
14
14
  /** Resolves the token for issue operations (`HOLOCRON_ISSUES_TOKEN`, keyring key `github.issues`). */
15
15
  export declare const resolveIssuesToken: import("@theholocron/cli").FeatureResolver;
16
+ /**
17
+ * Resolves the token for pull request operations (`HOLOCRON_PULLS_TOKEN`, keyring key `github.pulls`): open, change and
18
+ * read pull requests, with no administration. Optional: without it the `source` capability uses the admin token.
19
+ */
20
+ export declare const resolvePullsToken: import("@theholocron/cli").FeatureResolver;
16
21
  /** Resolves the token for syncing repo files and settings (`HOLOCRON_SYNC_TOKEN`, keyring key `github.sync`). */
17
22
  export declare const resolveSyncToken: import("@theholocron/cli").FeatureResolver;
18
23
  /** Resolves the token for release operations (`HOLOCRON_RELEASE_TOKEN`, keyring key `github.release`). */
@@ -316,6 +321,11 @@ interface SourceOptions {
316
321
  orgClient?: GitHubClient;
317
322
  /** Client for the files that link GitHub accounts to other services (HOLOCRON_USER_SYNC_TOKEN); falls back to the org client. */
318
323
  userSyncClient?: GitHubClient;
324
+ /**
325
+ * Client for the pull request methods (`HOLOCRON_PULLS_TOKEN`, no administration); falls back to the repository
326
+ * client, so a setup without that token keeps using the admin token.
327
+ */
328
+ pullsClient?: GitHubClient;
319
329
  /** Forwarded to a secondary client created for Pages calls (HOLOCRON_DEPLOY_TOKEN). */
320
330
  baseUrl?: string;
321
331
  fetch?: typeof fetch;
@@ -337,6 +347,7 @@ export declare class GitHubSource implements Source {
337
347
  private readonly workflowDir;
338
348
  private readonly orgClient?;
339
349
  private readonly userSyncClient?;
350
+ private readonly pullsClient?;
340
351
  private readonly baseUrl?;
341
352
  private readonly fetchImpl?;
342
353
  constructor(client: GitHubClient, opts: SourceOptions);
@@ -426,6 +437,8 @@ export declare class GitHubSource implements Source {
426
437
  getSecurity(): Promise<SourceSecurity>;
427
438
  syncDescription(description: string): Promise<string>;
428
439
  syncHomepage(homepage: string): Promise<string>;
440
+ /** The client for pull request calls: the pull requests token's, else the repository client. */
441
+ private get pullClient();
429
442
  getPullRequest(number: number, repo?: string): Promise<PullRequest>;
430
443
  listPullRequests(state: "open" | "closed", repo?: string): Promise<PullRequest[]>;
431
444
  listBranches(repo?: string): Promise<SourceBranch[]>;
@@ -461,7 +474,8 @@ export declare function encryptSecret(publicKeyBase64: string, value: string): P
461
474
  /**
462
475
  * Every token this plugin takes, for `holocron doctor tokens`, with what each layer's token must and must not be able
463
476
  * to do. The release token is `optional`: only CI's release workflow reads it (from the org secret), so a development
464
- * environment has no reason to hold it.
477
+ * environment has no reason to hold it. So is the pull requests token: only the `agent` layer holds it, because a person
478
+ * has the admin token the `source` capability falls back to.
465
479
  */
466
480
  export declare const tokens: PluginToken[];
467
481
  /** Options for `verifyToken`. */
@@ -511,8 +525,8 @@ export interface PluginContext {
511
525
  /** Absolute path to the working repo root. */
512
526
  repoRoot: string;
513
527
  }
514
- /** Builds the `source` capability; the optional clients cover org-level and user-sync access. */
515
- export declare function source(ctx: PluginContext, orgClient?: GitHubClient, userSyncClient?: GitHubClient): Source;
528
+ /** Builds the `source` capability; the optional clients cover org-level, user-sync and pull request access. */
529
+ export declare function source(ctx: PluginContext, orgClient?: GitHubClient, userSyncClient?: GitHubClient, pullsClient?: GitHubClient): Source;
516
530
  /** Builds the `secrets` capability; the optional client handles organization secrets. */
517
531
  export declare function secrets(ctx: PluginContext, orgClient?: GitHubClient): Secrets;
518
532
  /** Builds the `environments` capability. */
package/dist/index.mjs CHANGED
@@ -17,6 +17,14 @@ const resolveIssuesToken = createFeatureResolver({
17
17
  envName: "HOLOCRON_ISSUES_TOKEN",
18
18
  keyringKey: "github.issues"
19
19
  });
20
+ /**
21
+ * Resolves the token for pull request operations (`HOLOCRON_PULLS_TOKEN`, keyring key `github.pulls`): open, change and
22
+ * read pull requests, with no administration. Optional: without it the `source` capability uses the admin token.
23
+ */
24
+ const resolvePullsToken = createFeatureResolver({
25
+ envName: "HOLOCRON_PULLS_TOKEN",
26
+ keyringKey: "github.pulls"
27
+ });
20
28
  /** Resolves the token for syncing repo files and settings (`HOLOCRON_SYNC_TOKEN`, keyring key `github.sync`). */
21
29
  const resolveSyncToken = createFeatureResolver({
22
30
  envName: "HOLOCRON_SYNC_TOKEN",
@@ -2568,9 +2576,10 @@ async function addToBoards(client, repo, nodeId, titles) {
2568
2576
  /**
2569
2577
  * Puts the fields an issue and its pull request share on the pull request (GitHub serves a pull request through
2570
2578
  * the issues API for these): labels and assignees are added to what is there, a milestone is found by title and
2571
- * boards by title. What cannot be applied comes back by name; nothing is created or cleared.
2579
+ * boards by title. What cannot be applied comes back by name; nothing is created or cleared. Boards are the
2580
+ * organization's, so they go through `boardsClient` (the organization token), not the pull request token.
2572
2581
  */
2573
- async function applyFields(client, repo, pr, fields) {
2582
+ async function applyFields(client, repo, pr, fields, boardsClient) {
2574
2583
  const dropped = [];
2575
2584
  const { number } = pr;
2576
2585
  if (fields.labels?.length) await client.issues.addLabels(repo, number, fields.labels);
@@ -2591,13 +2600,16 @@ async function applyFields(client, repo, pr, fields) {
2591
2600
  }
2592
2601
  if (Object.keys(patch).length) await client.issues.updateIssue(repo, number, patch);
2593
2602
  if (fields.projects?.length) {
2594
- if (pr.node_id) dropped.push(...await addToBoards(client, repo, pr.node_id, fields.projects));
2603
+ if (pr.node_id) dropped.push(...await addToBoards(boardsClient, repo, pr.node_id, fields.projects));
2595
2604
  else dropped.push(...fields.projects.map((t) => `project ${t}`));
2596
2605
  }
2597
2606
  return dropped;
2598
2607
  }
2599
- /** Opens a draft pull request, or returns the open one for the same head, and applies the mirrored fields. */
2600
- async function createPullRequest(client, repo, input) {
2608
+ /**
2609
+ * Opens a draft pull request, or returns the open one for the same head, and applies the mirrored fields.
2610
+ * `boardsClient` puts it on boards (the organization token); it defaults to `client`.
2611
+ */
2612
+ async function createPullRequest(client, repo, input, boardsClient = client) {
2601
2613
  const existing = (await client.pulls.listOpenByHead(repo, input.head))[0];
2602
2614
  const created = !existing;
2603
2615
  const raw = existing ?? await client.pulls.createPullRequest(repo, {
@@ -2607,7 +2619,7 @@ async function createPullRequest(client, repo, input) {
2607
2619
  body: withLink(input.body, input.link),
2608
2620
  draft: input.draft ?? true
2609
2621
  });
2610
- const dropped = await applyFields(client, repo, raw, input);
2622
+ const dropped = await applyFields(client, repo, raw, input, boardsClient);
2611
2623
  return {
2612
2624
  pullRequest: toPullRequest(raw),
2613
2625
  created,
@@ -2615,15 +2627,18 @@ async function createPullRequest(client, repo, input) {
2615
2627
  dropped
2616
2628
  };
2617
2629
  }
2618
- /** Changes a pull request's own fields, then the mirrored ones. A patch that names none of the own fields makes no PATCH. */
2619
- async function updatePullRequest(client, repo, number, patch) {
2630
+ /**
2631
+ * Changes a pull request's own fields, then the mirrored ones. A patch that names none of the own fields makes no PATCH.
2632
+ * `boardsClient` puts it on boards (the organization token); it defaults to `client`.
2633
+ */
2634
+ async function updatePullRequest(client, repo, number, patch, boardsClient = client) {
2620
2635
  const own = {};
2621
2636
  if (patch.title !== void 0) own.title = patch.title;
2622
2637
  if (patch.body !== void 0) own.body = patch.body;
2623
2638
  if (patch.base !== void 0) own.base = patch.base;
2624
2639
  if (patch.state !== void 0) own.state = patch.state;
2625
2640
  const raw = Object.keys(own).length ? await client.pulls.updatePullRequest(repo, number, own) : await client.pulls.getPullRequest(repo, number);
2626
- const dropped = await applyFields(client, repo, raw, patch);
2641
+ const dropped = await applyFields(client, repo, raw, patch, boardsClient);
2627
2642
  return {
2628
2643
  pullRequest: toPullRequest(raw),
2629
2644
  created: false,
@@ -2978,6 +2993,7 @@ var GitHubSource = class {
2978
2993
  workflowDir;
2979
2994
  orgClient;
2980
2995
  userSyncClient;
2996
+ pullsClient;
2981
2997
  baseUrl;
2982
2998
  fetchImpl;
2983
2999
  constructor(client, opts) {
@@ -2993,6 +3009,7 @@ var GitHubSource = class {
2993
3009
  this.workflowDir = join(this.repoRoot, ".github", "workflows");
2994
3010
  this.orgClient = opts.orgClient;
2995
3011
  this.userSyncClient = opts.userSyncClient;
3012
+ this.pullsClient = opts.pullsClient;
2996
3013
  this.baseUrl = opts.baseUrl;
2997
3014
  this.fetchImpl = opts.fetch;
2998
3015
  }
@@ -3199,11 +3216,15 @@ var GitHubSource = class {
3199
3216
  await this.client.repos.updateRepo(this.repo, { homepage });
3200
3217
  return "homepage updated";
3201
3218
  }
3219
+ /** The client for pull request calls: the pull requests token's, else the repository client. */
3220
+ get pullClient() {
3221
+ return this.pullsClient ?? this.client;
3222
+ }
3202
3223
  async getPullRequest(number, repo) {
3203
- return toPullRequest(await this.client.pulls.getPullRequest(repo ?? this.repo, number));
3224
+ return toPullRequest(await this.pullClient.pulls.getPullRequest(repo ?? this.repo, number));
3204
3225
  }
3205
3226
  async listPullRequests(state, repo) {
3206
- return (await this.client.pulls.listPullRequests(repo ?? this.repo, state)).map(toPullRequest);
3227
+ return (await this.pullClient.pulls.listPullRequests(repo ?? this.repo, state)).map(toPullRequest);
3207
3228
  }
3208
3229
  async listBranches(repo) {
3209
3230
  return (await this.client.branches.listBranches(repo ?? this.repo)).map((b) => ({
@@ -3220,24 +3241,24 @@ var GitHubSource = class {
3220
3241
  }
3221
3242
  }
3222
3243
  async listPullRequestCommits(number, repo) {
3223
- return (await this.client.pulls.listCommits(repo ?? this.repo, number)).map((commit) => commit.sha);
3244
+ return (await this.pullClient.pulls.listCommits(repo ?? this.repo, number)).map((commit) => commit.sha);
3224
3245
  }
3225
3246
  async listPullRequestFiles(number, repo) {
3226
- const files = await this.client.pulls.listFiles(repo ?? this.repo, number);
3247
+ const files = await this.pullClient.pulls.listFiles(repo ?? this.repo, number);
3227
3248
  return [...new Set(files.flatMap((f) => f.previous_filename ? [f.filename, f.previous_filename] : [f.filename]))];
3228
3249
  }
3229
3250
  async createPullRequest(input, repo) {
3230
- return createPullRequest(this.client, repo ?? this.repo, input);
3251
+ return createPullRequest(this.pullClient, repo ?? this.repo, input, this.orgClient ?? this.pullClient);
3231
3252
  }
3232
3253
  async updatePullRequest(number, patch, repo) {
3233
- return updatePullRequest(this.client, repo ?? this.repo, number, patch);
3254
+ return updatePullRequest(this.pullClient, repo ?? this.repo, number, patch, this.orgClient ?? this.pullClient);
3234
3255
  }
3235
3256
  async markReady(number, repo) {
3236
- return markReady(this.client, repo ?? this.repo, number);
3257
+ return markReady(this.pullClient, repo ?? this.repo, number);
3237
3258
  }
3238
3259
  async getPullRequestChecks(number, repo, required) {
3239
- if (required) return readRequiredPullRequestChecks(this.client, repo ?? this.repo, number, required);
3240
- return readPullRequestChecks(this.client, repo ?? this.repo, number);
3260
+ if (required) return readRequiredPullRequestChecks(this.pullClient, repo ?? this.repo, number, required);
3261
+ return readPullRequestChecks(this.pullClient, repo ?? this.repo, number);
3241
3262
  }
3242
3263
  async listCheckRuns(ref, repo) {
3243
3264
  return readCheckRuns(this.client, repo ?? this.repo, ref);
@@ -4088,6 +4109,14 @@ const EXPECTED = {
4088
4109
  secrets: "absent",
4089
4110
  environments: "absent"
4090
4111
  }, { agent: true }),
4112
+ HOLOCRON_PULLS_TOKEN: { permissions: { agent: {
4113
+ "pull-requests": "required",
4114
+ contents: "required",
4115
+ administration: "absent",
4116
+ secrets: "absent",
4117
+ environments: "absent",
4118
+ actions: "absent"
4119
+ } } },
4091
4120
  HOLOCRON_SYNC_TOKEN: everywhere({
4092
4121
  actions: "required",
4093
4122
  contents: "required",
@@ -4147,11 +4176,16 @@ function declared(token) {
4147
4176
  /**
4148
4177
  * Every token this plugin takes, for `holocron doctor tokens`, with what each layer's token must and must not be able
4149
4178
  * to do. The release token is `optional`: only CI's release workflow reads it (from the org secret), so a development
4150
- * environment has no reason to hold it.
4179
+ * environment has no reason to hold it. So is the pull requests token: only the `agent` layer holds it, because a person
4180
+ * has the admin token the `source` capability falls back to.
4151
4181
  */
4152
4182
  const tokens = [
4153
4183
  declared(resolveReadToken.token),
4154
4184
  declared(resolveIssuesToken.token),
4185
+ declared({
4186
+ ...resolvePullsToken.token,
4187
+ optional: true
4188
+ }),
4155
4189
  declared(resolveSyncToken.token),
4156
4190
  declared({
4157
4191
  ...resolveReleaseToken.token,
@@ -4443,6 +4477,7 @@ function tokenProbe(ctx) {
4443
4477
  const TOKEN_RESOLVERS = {
4444
4478
  HOLOCRON_READ_TOKEN: resolveReadToken,
4445
4479
  HOLOCRON_ISSUES_TOKEN: resolveIssuesToken,
4480
+ HOLOCRON_PULLS_TOKEN: resolvePullsToken,
4446
4481
  HOLOCRON_SYNC_TOKEN: resolveSyncToken,
4447
4482
  HOLOCRON_RELEASE_TOKEN: resolveReleaseToken,
4448
4483
  HOLOCRON_ADMIN_TOKEN: resolveAdminToken,
@@ -4508,13 +4543,14 @@ function whoamiToken(options, deps) {
4508
4543
  };
4509
4544
  }
4510
4545
  // src/index.ts
4511
- /** Builds the `source` capability; the optional clients cover org-level and user-sync access. */
4512
- function source(ctx, orgClient, userSyncClient) {
4546
+ /** Builds the `source` capability; the optional clients cover org-level, user-sync and pull request access. */
4547
+ function source(ctx, orgClient, userSyncClient, pullsClient) {
4513
4548
  return new GitHubSource(ctx.client, {
4514
4549
  repo: ctx.repo,
4515
4550
  repoRoot: ctx.repoRoot,
4516
4551
  orgClient,
4517
4552
  userSyncClient,
4553
+ pullsClient,
4518
4554
  baseUrl: ctx.options.baseUrl,
4519
4555
  fetch: ctx.options.fetch
4520
4556
  });
@@ -4592,6 +4628,30 @@ function createPlugin(options) {
4592
4628
  repoRoot: options.repoRoot ?? process.cwd()
4593
4629
  }, orgClient);
4594
4630
  }
4631
+ /**
4632
+ * Repository settings need the admin token; opening and changing pull requests need only `github.pulls`. Each prefers
4633
+ * its own token and falls back to the other, so a setup with only the admin token keeps working, and an agent that
4634
+ * holds only the pull requests token can still open pull requests (a settings call then answers 403). With neither
4635
+ * configured this fails exactly as the admin-token capabilities do.
4636
+ */
4637
+ function makeSource() {
4638
+ const adminToken = tryResolve(resolveAdminToken);
4639
+ const pullsToken = tryResolve(resolvePullsToken);
4640
+ if (!adminToken && !pullsToken) resolveAdminToken(options);
4641
+ const clientFor = (token) => createGitHubClient$1({
4642
+ token,
4643
+ baseUrl: options.baseUrl,
4644
+ fetch: options.fetch
4645
+ });
4646
+ const repoClient = clientFor(adminToken ?? pullsToken);
4647
+ const pullsClient = clientFor(pullsToken ?? adminToken);
4648
+ return source({
4649
+ options,
4650
+ client: repoClient,
4651
+ repo: options.repo,
4652
+ repoRoot: options.repoRoot ?? process.cwd()
4653
+ }, makeOrgClient(), makeUserSyncClient(), pullsClient);
4654
+ }
4595
4655
  function makeOrgClient() {
4596
4656
  try {
4597
4657
  const token = resolveOrgToken(options);
@@ -4651,7 +4711,7 @@ function createPlugin(options) {
4651
4711
  })
4652
4712
  }),
4653
4713
  capabilities: {
4654
- source: () => source(makeCtx(resolveAdminToken), makeOrgClient(), makeUserSyncClient()),
4714
+ source: () => makeSource(),
4655
4715
  ci: () => ci(makeCtx(resolveReadToken)),
4656
4716
  secrets: () => makeSecrets(),
4657
4717
  environments: () => environments(makeCtx(resolveAdminToken)),
@@ -4665,4 +4725,4 @@ function createPlugin(options) {
4665
4725
  * is supplied or the supplied token is rejected.
4666
4726
  */
4667
4727
  const AUTH_HINT = "generate fine-grained Personal Access Tokens at https://github.com/settings/tokens (one per feature — see docs/tokens.md for required scopes per operation), then run: holocron new token github.<feature> <PAT>";
4668
- export { AUTH_HINT, AuthError, GitHubCi, GitHubEnvironments, GitHubIssues, GitHubProjects, GitHubSecrets, GitHubSource, ci, createFeatureResolver, createGitHubClient, createPlugin, encryptSecret, environments, issues, normalizeTeamEntry, projects, resolveAdminToken, resolveDeployToken, resolveIssuesToken, resolveOrgToken, resolveReadToken, resolveReleaseToken, resolveSecretsToken, resolveSyncToken, resolveUserSyncToken, secrets, source, syncLabels, syncTeams, tokens, verifyToken };
4728
+ export { AUTH_HINT, AuthError, GitHubCi, GitHubEnvironments, GitHubIssues, GitHubProjects, GitHubSecrets, GitHubSource, ci, createFeatureResolver, createGitHubClient, createPlugin, encryptSecret, environments, issues, normalizeTeamEntry, projects, resolveAdminToken, resolveDeployToken, resolveIssuesToken, resolveOrgToken, resolvePullsToken, resolveReadToken, resolveReleaseToken, resolveSecretsToken, resolveSyncToken, resolveUserSyncToken, secrets, source, syncLabels, syncTeams, tokens, verifyToken };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theholocron/holocron-plugin-github",
3
- "version": "5.0.0-alpha.584",
3
+ "version": "5.0.0-alpha.585",
4
4
  "description": "Holocron plugin for GitHub. Implements source, ci, secrets, environments, and issues capabilities against the GitHub REST API.",
5
5
  "keywords": [
6
6
  "ci",
@@ -44,7 +44,7 @@
44
44
  "tsx": "4.23.15",
45
45
  "typescript": "^6.0.3",
46
46
  "vitest": "^5.1.1",
47
- "@theholocron/cli": "5.0.0-alpha.584",
47
+ "@theholocron/cli": "5.0.0-alpha.585",
48
48
  "@theholocron/eslint-config": "^8.9.0-alpha.24",
49
49
  "@theholocron/fs-utils": "^1.17.0-alpha.9",
50
50
  "@theholocron/github-client": "^1.58.0-alpha.70",
@@ -58,7 +58,7 @@
58
58
  "@theholocron/fs-utils": "^1.16.0",
59
59
  "@theholocron/github-client": "^1.58.0",
60
60
  "@theholocron/misc-utils": "^1.16.0",
61
- "@theholocron/cli": "5.0.0-alpha.584"
61
+ "@theholocron/cli": "5.0.0-alpha.585"
62
62
  },
63
63
  "engines": {
64
64
  "node": ">=22"