@theholocron/holocron-plugin-github 5.0.0-alpha.505 → 5.0.0-alpha.506

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
@@ -30,10 +30,16 @@ export declare const resolveDeployToken: import("@theholocron/cli").FeatureResol
30
30
  export declare const resolveUserSyncToken: import("@theholocron/cli").FeatureResolver;
31
31
  /** Resolves the org-level token (`HOLOCRON_ORG_TOKEN`, keyring key `github.org`). */
32
32
  export declare const resolveOrgToken: import("@theholocron/cli").FeatureResolver;
33
- /** The area declaration the plugin is handed from `repo.labels`. */
34
- interface AreaConfig {
33
+ /** `repo.labels`, which the loader hands the plugin: the area map, the kind tables and the repositories. */
34
+ interface LabelsConfig {
35
35
  areas?: Record<string, string[]>;
36
36
  scopes?: Record<string, string>;
37
+ /** Main kind label to Type (default `bug` Bug, `enhancement` Feature, `chore` Task). */
38
+ kinds?: Record<string, string>;
39
+ /** Kind labels used only when none of `kinds` is on the issue (default the six of #2494, each Task). */
40
+ fallbackKinds?: Record<string, string>;
41
+ /** The repositories to run on when `--repo` is not given. */
42
+ repos?: string[];
37
43
  }
38
44
  interface CiOptions {
39
45
  repo: string;
@@ -437,7 +443,7 @@ export interface GitHubPluginOptions extends ResolveTokenInput {
437
443
  inReview: string;
438
444
  };
439
445
  /** `repo.labels` (the area map), handed in by the loader for the `issue-types` probe. */
440
- areaLabels?: AreaConfig;
446
+ areaLabels?: LabelsConfig;
441
447
  /** Override base URL for tests. Defaults to https://api.github.com. */
442
448
  baseUrl?: string;
443
449
  /** Override `fetch` for tests. Defaults to global `fetch`. */
package/dist/index.mjs CHANGED
@@ -2876,6 +2876,7 @@ async function ensureDir(path) {
2876
2876
  const PAGE = 100;
2877
2877
  const PROJECTS_OF = `query($ids: [ID!]!) { nodes(ids: $ids) { ... on Issue { id projectItems(first: 20) { nodes { project { id } } } } } }`;
2878
2878
  const ADD_ITEM = `mutation($project: ID!, $content: ID!) { addProjectV2ItemById(input: { projectId: $project, contentId: $content }) { item { id } } }`;
2879
+ const ISSUE_TYPES = `query($org: String!) { organization(login: $org) { issueTypes(first: 50) { nodes { name } } } }`;
2879
2880
  function toItem(issue) {
2880
2881
  return {
2881
2882
  number: issue.number,
@@ -2951,6 +2952,13 @@ function createBackfillApi(client, orgClient, repo) {
2951
2952
  async files(number) {
2952
2953
  return (await client.pulls.listFiles(repo, number)).map((f) => f.filename);
2953
2954
  },
2955
+ async typeNames() {
2956
+ const data = await orgClient.graphql.query(ISSUE_TYPES, { org });
2957
+ return new Set((data.organization?.issueTypes?.nodes ?? []).map((t) => t.name));
2958
+ },
2959
+ async readFile(path) {
2960
+ return (await readRepoFile(client, repo, path))?.content ?? null;
2961
+ },
2954
2962
  async setType(number, type) {
2955
2963
  await client.issues.updateIssue(repo, number, { type });
2956
2964
  },
@@ -3033,27 +3041,61 @@ async function gather(api, options, isRateLimited) {
3033
3041
  };
3034
3042
  }
3035
3043
  }
3036
- // src/probes/issue-types/plan-type.ts
3044
+ // src/probes/issue-types/kinds.ts
3037
3045
  /** The kind labels and the Type each one means (spec `tech-github-area-labels`, decision 3). */
3038
- const KIND_TYPES = {
3046
+ const DEFAULT_MAIN_KINDS = {
3039
3047
  bug: "Bug",
3040
3048
  enhancement: "Feature",
3041
3049
  chore: "Task"
3042
3050
  };
3051
+ /** The other kind labels, which mean Task only when none of the main ones is on the issue (issue #2494). */
3052
+ const DEFAULT_FALLBACK_KINDS = Object.fromEntries([
3053
+ "ci",
3054
+ "documentation",
3055
+ "refactor",
3056
+ "test",
3057
+ "performance",
3058
+ "dependencies"
3059
+ ].map((label) => [label, "Task"]));
3060
+ const lowered = (table) => Object.fromEntries(Object.entries(table).map(([label, type]) => [label.toLowerCase(), type]));
3061
+ /** The tables a run uses: what `repo.labels` sets, else the defaults, table by table. */
3062
+ function resolveKinds(config) {
3063
+ return {
3064
+ main: config.kinds ? lowered(config.kinds) : DEFAULT_MAIN_KINDS,
3065
+ fallback: config.fallbackKinds ? lowered(config.fallbackKinds) : DEFAULT_FALLBACK_KINDS
3066
+ };
3067
+ }
3068
+ /** The Types the tables name that `known` (the organization's Types) lacks, each with the labels that use it. */
3069
+ function unknownTypes(kinds, known) {
3070
+ const byType = /* @__PURE__ */ new Map();
3071
+ for (const [label, type] of [...Object.entries(kinds.main), ...Object.entries(kinds.fallback)]) if (!known.has(type)) byType.set(type, [...byType.get(type) ?? [], label]);
3072
+ return [...byType].map(([type, labels]) => ({
3073
+ type,
3074
+ labels
3075
+ }));
3076
+ }
3077
+ // src/probes/issue-types/plan-type.ts
3078
+ /** The labels of an issue that `table` knows, each once (lower case), in the issue's order. */
3079
+ const matching = (labels, table) => [...new Set(labels.map((l) => l.toLowerCase()).filter((l) => Object.hasOwn(table, l)))];
3043
3080
  /**
3044
- * The Type an issue gets from its kind labels. One that has a Type keeps it, whatever its labels say; one with exactly
3045
- * one of `bug`, `enhancement` and `chore` gets that label's Type; one with two or more is `ambiguous` and left for a
3046
- * person (the spec's rule: the probe never picks between two kinds); one with none has no source.
3081
+ * The Type an issue gets from its kind labels. One that has a Type keeps it, whatever its labels say. Otherwise the main
3082
+ * table decides (`bug`, `enhancement`, `chore` by default), and only an issue with none of those looks at the fallback
3083
+ * table (`ci`, `documentation`, `refactor`, `test`, `performance`, `dependencies`, all Task by default; #2494). Labels
3084
+ * that point to one Type set it, however many they are; labels that point to two different Types are `ambiguous` and left
3085
+ * for a person (the spec's rule: the probe never picks between two kinds); one with no label in either table has no source.
3047
3086
  */
3048
- function planType(item) {
3087
+ function planType(item, kinds) {
3049
3088
  if (item.type) return { skip: "typed" };
3050
- const kinds = [...new Set(item.labels.map((l) => l.toLowerCase()).filter((l) => l in KIND_TYPES))];
3051
- if (kinds.length === 0) return { skip: "no-kind-label" };
3052
- if (kinds.length > 1) return {
3053
- skip: "ambiguous",
3054
- labels: kinds
3055
- };
3056
- return { set: KIND_TYPES[kinds[0]] };
3089
+ for (const table of [kinds.main, kinds.fallback]) {
3090
+ const found = matching(item.labels, table);
3091
+ if (found.length === 0) continue;
3092
+ if (new Set(found.map((l) => table[l])).size > 1) return {
3093
+ skip: "ambiguous",
3094
+ labels: found
3095
+ };
3096
+ return { set: table[found[0]] };
3097
+ }
3098
+ return { skip: "no-kind-label" };
3057
3099
  }
3058
3100
  // src/probes/issue-types/report.ts
3059
3101
  const plural = (n, word) => `${n} ${word}${n === 1 ? "" : "s"}`;
@@ -3069,6 +3111,7 @@ function resumeCommand(options, cursor) {
3069
3111
  if (options.issues) flags.push("--issues", options.issues.join(","));
3070
3112
  if (options.since) flags.push("--since", options.since);
3071
3113
  flags.push("--limit", String(options.limit));
3114
+ if (options.repo) flags.push("--repo", options.repo);
3072
3115
  if (options.milestone) flags.push("--milestone", options.milestone);
3073
3116
  if (options.project) flags.push("--project", options.project);
3074
3117
  if (options.pullRequests) flags.push("--pull-requests");
@@ -3092,7 +3135,7 @@ function reportBackfill(ctx, input) {
3092
3135
  ctx.say({
3093
3136
  status: "info",
3094
3137
  label: "Type",
3095
- detail: `${types || "none"} ${verb}; ${tally.typed} already typed, ${tally.noKind} with no kind label, ${tally.ambiguous.length} with two kind labels`
3138
+ detail: `${types || "none"} ${verb}; ${tally.typed} already typed, ${tally.noKind} with no label that maps to a Type, ${tally.ambiguous.length} with two kind labels`
3096
3139
  });
3097
3140
  for (const a of tally.ambiguous) ctx.say({
3098
3141
  status: "info",
@@ -3193,6 +3236,14 @@ const labelOf = (item) => `#${item.number}`;
3193
3236
  /** Reads what the run needs before it starts: labels, the named milestone and project, the organization's issue fields. Says why and returns nothing when a name does not exist. */
3194
3237
  async function prepare(run, tally) {
3195
3238
  const { api, ctx, options } = run;
3239
+ const known = await api.typeNames();
3240
+ const unknown = unknownTypes(run.kinds, known);
3241
+ for (const { type, labels } of unknown) ctx.say({
3242
+ status: "fail",
3243
+ label: "Type",
3244
+ detail: `${type} (for ${labels.join(", ")}) is not an issue Type of this organization; its Types are ${[...known].join(", ") || "none"}`
3245
+ });
3246
+ if (unknown.length > 0) return void 0;
3196
3247
  const setup = {
3197
3248
  labelNames: await api.labelNames(),
3198
3249
  fields: []
@@ -3241,7 +3292,7 @@ async function handle(run, setup, tally, item, projects) {
3241
3292
  tally.areaLabelsAdded++;
3242
3293
  }
3243
3294
  if (item.isPullRequest) return;
3244
- const type = planType(item);
3295
+ const type = planType(item, run.kinds);
3245
3296
  if ("set" in type) {
3246
3297
  await ctx.write(`set Type ${type.set} on ${labelOf(item)}`, () => api.setType(n, type.set));
3247
3298
  tally.typeSet[type.set] = (tally.typeSet[type.set] ?? 0) + 1;
@@ -3282,7 +3333,8 @@ async function handle(run, setup, tally, item, projects) {
3282
3333
  * The backfill of `holocron probe github issue-types` (spec `tech-github-area-labels`, rollout item 5): reads a window of
3283
3334
  * issues, and for each adds what it can derive and nothing else.
3284
3335
  *
3285
- * - **Type** from the kind labels (`bug`, `enhancement`, `chore`); one that has a Type keeps it, one with two kind labels
3336
+ * - **Type** from the kind labels (`bug`, `enhancement`, `chore`, else Task for `ci`, `documentation`, `refactor`, `test`,
3337
+ * `performance`, `dependencies`); one that has a Type keeps it, one with two kind labels
3286
3338
  * is listed and left.
3287
3339
  * - **Area labels** from the title's scope, or, for a pull request with `--pull-requests`, from its changed files; only
3288
3340
  * added, and only when the label exists (`holocron setup` makes them).
@@ -3347,11 +3399,13 @@ const VALUED = [
3347
3399
  "--after",
3348
3400
  "--limit",
3349
3401
  "--milestone",
3350
- "--project"
3402
+ "--project",
3403
+ "--repo"
3351
3404
  ];
3352
3405
  const INTEGER = /^\d+$/;
3353
3406
  const DAY = /^\d{4}-\d{2}-\d{2}$/;
3354
3407
  const MAX_RANGE = 1e3;
3408
+ const REPOSITORY = /^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/;
3355
3409
  /** `12,15,20-23` to `[12, 15, 20, 21, 22, 23]`, or `undefined` for text that is not such a list. */
3356
3410
  function numbersOf(text) {
3357
3411
  const out = [];
@@ -3369,7 +3423,8 @@ function parseArgs(args) {
3369
3423
  const options = {
3370
3424
  after: 0,
3371
3425
  limit: 100,
3372
- pullRequests: false
3426
+ pullRequests: false,
3427
+ repos: []
3373
3428
  };
3374
3429
  for (let i = 0; i < args.length; i++) {
3375
3430
  const flag = args[i];
@@ -3411,6 +3466,12 @@ function parseArgs(args) {
3411
3466
  message: `--limit takes a whole number of at least 1, not ${value}`
3412
3467
  };
3413
3468
  options.limit = Number(value);
3469
+ } else if (flag === "--repo") {
3470
+ if (!REPOSITORY.test(value)) return {
3471
+ ok: false,
3472
+ message: `--repo takes owner/name, not ${value}`
3473
+ };
3474
+ if (!options.repos.includes(value)) options.repos.push(value);
3414
3475
  } else if (flag === "--milestone") options.milestone = value;
3415
3476
  else options.project = value;
3416
3477
  }
@@ -3418,11 +3479,75 @@ function parseArgs(args) {
3418
3479
  ok: false,
3419
3480
  message: "--milestone and --project need a window to apply to: name it with --issues or --since"
3420
3481
  };
3482
+ if (options.repos.length > 1 && (options.issues || options.after > 0)) return {
3483
+ ok: false,
3484
+ message: "--issues and --after are numbers of one repository, so they cannot go with more than one --repo"
3485
+ };
3421
3486
  return {
3422
3487
  ok: true,
3423
3488
  options
3424
3489
  };
3425
3490
  }
3491
+ // src/probes/issue-types/repo-areas.ts
3492
+ /** Where a repository without a `package.json` declares `repo.labels` (the `repo` area file, spec `tech-holocron-directory`). */
3493
+ const AREA_FILE_JSON = ".holocron/repo.json";
3494
+ const AREA_FILE_TS = ".holocron/repo.ts";
3495
+ const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
3496
+ /** `repo.labels.areas` and `scopes` as the mapping reads them, or `undefined` for a shape it cannot use. */
3497
+ function areaMap(labels) {
3498
+ if (!isRecord(labels) || !isRecord(labels.areas) || Object.keys(labels.areas).length === 0) return void 0;
3499
+ const areas = labels.areas;
3500
+ if (!Object.values(areas).every((p) => Array.isArray(p) && p.every((x) => typeof x === "string"))) return void 0;
3501
+ const scopes = isRecord(labels.scopes) ? labels.scopes : {};
3502
+ if (!Object.values(scopes).every((a) => typeof a === "string")) return void 0;
3503
+ return {
3504
+ areas,
3505
+ scopes
3506
+ };
3507
+ }
3508
+ const none = (detail) => ({
3509
+ areas: {},
3510
+ row: {
3511
+ status: "info",
3512
+ label: "area labels",
3513
+ detail
3514
+ }
3515
+ });
3516
+ /**
3517
+ * The area labels another repository declares (spec `tech-backfill-config-mapping`), read from its own
3518
+ * `.holocron/repo.json`. A `.holocron/repo.ts` is TypeScript, which only Sentinel's remote config reader evaluates and a
3519
+ * plugin cannot reach, so that repository gets a skipped row and no area step; so does one with no declaration. Never
3520
+ * throws for a missing or unusable file: the Type step of the repository still runs.
3521
+ */
3522
+ async function readRepoAreas(api) {
3523
+ const json = await api.readFile(AREA_FILE_JSON);
3524
+ if (json !== null) {
3525
+ let parsed;
3526
+ try {
3527
+ parsed = JSON.parse(json);
3528
+ } catch (err) {
3529
+ return {
3530
+ areas: {},
3531
+ row: {
3532
+ status: "fail",
3533
+ label: "area labels",
3534
+ detail: `${AREA_FILE_JSON} is not JSON (${errorMessage(err)})`
3535
+ }
3536
+ };
3537
+ }
3538
+ const found = areaMap(isRecord(parsed) ? parsed.labels : void 0);
3539
+ return found ? {
3540
+ areas: found,
3541
+ row: {
3542
+ status: "info",
3543
+ label: "area labels",
3544
+ detail: `read from ${AREA_FILE_JSON}`
3545
+ }
3546
+ } : none(`skipped: ${AREA_FILE_JSON} declares no usable \`labels.areas\`, so no area labels are added`);
3547
+ }
3548
+ if (await api.readFile(AREA_FILE_TS) !== null) return none(`skipped: its areas are in ${AREA_FILE_TS}, which is TypeScript the probe cannot evaluate (only Sentinel's config reader does); no area labels are added`);
3549
+ return none(`skipped: it has no ${AREA_FILE_JSON} or ${AREA_FILE_TS}, so no area labels are added`);
3550
+ }
3426
3551
  // src/probes/issue-types/issue-types.ts
3427
3552
  /**
3428
3553
  * `holocron probe github issue-types`: the one-time backfill of spec `tech-github-area-labels` (rollout item 5), a writer
@@ -3430,8 +3555,12 @@ function parseArgs(args) {
3430
3555
  * and a project where a source exists, and ends with the list of every issue it read that still lacks a project, a
3431
3556
  * milestone, a Priority or an Effort. See `backfill.ts` for the rules. Run it with `--dry-run` first.
3432
3557
  *
3433
- * Not verified against the live API: writing an issue's Type, adding an issue to a project, and the endpoints that read an
3434
- * issue's field values and parent.
3558
+ * The kind labels and the repositories come from `repo.labels` (`kinds`, `fallbackKinds`, `repos`) and `--repo`, and a
3559
+ * repository other than this one has its area labels read from its own `.holocron/repo.json` (spec
3560
+ * `tech-backfill-config-mapping`).
3561
+ *
3562
+ * Not verified against the live API: writing an issue's Type, adding an issue to a project, reading the organization's
3563
+ * issue Types, and the endpoints that read an issue's field values and parent.
3435
3564
  */
3436
3565
  function issueTypesProbe(ctx) {
3437
3566
  return {
@@ -3447,20 +3576,40 @@ function issueTypesProbe(ctx) {
3447
3576
  });
3448
3577
  return;
3449
3578
  }
3450
- try {
3451
- await runBackfill({
3452
- ctx: probe,
3453
- api: ctx.makeApi(),
3454
- options: parsed.options,
3455
- areas: ctx.areas,
3456
- isRateLimited
3457
- });
3458
- } catch (err) {
3459
- probe.say({
3460
- status: "fail",
3461
- label: "issue-types",
3462
- detail: errorMessage(err)
3579
+ const named = parsed.options.repos;
3580
+ const repos = named.length > 0 ? named : ctx.config.repos?.length ? ctx.config.repos : [ctx.repo];
3581
+ const kinds = resolveKinds(ctx.config);
3582
+ for (const repo of repos) {
3583
+ if (repos.length > 1) probe.say({
3584
+ status: "heading",
3585
+ label: repo
3463
3586
  });
3587
+ try {
3588
+ const api = ctx.makeApi(repo);
3589
+ let areas = ctx.config;
3590
+ if (repo.toLowerCase() !== ctx.repo.toLowerCase()) {
3591
+ const found = await readRepoAreas(api);
3592
+ probe.say(found.row);
3593
+ areas = found.areas;
3594
+ }
3595
+ await runBackfill({
3596
+ ctx: probe,
3597
+ api,
3598
+ options: {
3599
+ ...parsed.options,
3600
+ repo: named.length > 0 || repos.length > 1 ? repo : void 0
3601
+ },
3602
+ areas,
3603
+ kinds,
3604
+ isRateLimited
3605
+ });
3606
+ } catch (err) {
3607
+ probe.say({
3608
+ status: "fail",
3609
+ label: "issue-types",
3610
+ detail: errorMessage(err)
3611
+ });
3612
+ }
3464
3613
  }
3465
3614
  }
3466
3615
  };
@@ -3980,8 +4129,9 @@ function createPlugin(options) {
3980
4129
  tokenNames: Object.keys(TOKEN_RESOLVERS)
3981
4130
  }),
3982
4131
  "issue-types": issueTypesProbe({
3983
- makeApi: () => createBackfillApi(makeCtx(resolveIssuesToken).client, makeCtx(resolveOrgToken).client, options.repo),
3984
- areas: options.areaLabels ?? {}
4132
+ makeApi: (repo) => createBackfillApi(makeCtx(resolveIssuesToken).client, makeCtx(resolveOrgToken).client, repo),
4133
+ repo: options.repo,
4134
+ config: options.areaLabels ?? {}
3985
4135
  }),
3986
4136
  connection: connectionProbe({
3987
4137
  provider: "GitHub",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theholocron/holocron-plugin-github",
3
- "version": "5.0.0-alpha.505",
3
+ "version": "5.0.0-alpha.506",
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,21 +44,21 @@
44
44
  "tsx": "4.23.15",
45
45
  "typescript": "^5.9.3",
46
46
  "vitest": "^5.1.0",
47
- "@theholocron/cli": "5.0.0-alpha.505",
48
- "@theholocron/fs-utils": "^1.17.0-alpha.5",
49
47
  "@theholocron/eslint-config": "^8.9.0-alpha.21",
50
- "@theholocron/http-client": "^1.58.0-alpha.46",
48
+ "@theholocron/cli": "5.0.0-alpha.506",
49
+ "@theholocron/fs-utils": "^1.17.0-alpha.5",
51
50
  "@theholocron/github-client": "^1.58.0-alpha.46",
52
51
  "@theholocron/misc-utils": "^1.17.0-alpha.5",
53
- "@theholocron/tsdown-config": "^8.9.0-alpha.21",
54
52
  "@theholocron/tsconfig": "^8.9.0-alpha.21",
55
- "@theholocron/vitest-config": "^8.9.0-alpha.21"
53
+ "@theholocron/tsdown-config": "^8.9.0-alpha.21",
54
+ "@theholocron/vitest-config": "^8.9.0-alpha.21",
55
+ "@theholocron/http-client": "^1.58.0-alpha.46"
56
56
  },
57
57
  "peerDependencies": {
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.505"
61
+ "@theholocron/cli": "5.0.0-alpha.506"
62
62
  },
63
63
  "engines": {
64
64
  "node": ">=22"