@theholocron/holocron-plugin-github 5.6.0 → 5.7.0

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
@@ -7,7 +7,7 @@
7
7
  * receives plugin-level options from `holocron.config.json` and
8
8
  * returns the bound implementation.
9
9
  */
10
- import { AddedItem, Auth, AuthError, BoardItem, BoardItemInput, BoardState, BoardSummary, BoardTemplate, Ci, CiRun, CiRunFilter, EnsureRelaySourceInput, EnsureRelaySourceResult, EnsureReport, Environment, Environments, ExportInfo, ImportOptions, ImportResult, InspectRelaySourceInput, Issue, IssueCreateInput, IssuePatch, IssueRecord, IssueSearchFilter, IssueSelector, IssueTypeDeclaration, IssueTypeInfo, IssueUpdateResult, Issues, LabelDef, LifecycleResult, LifecycleSlot, MoveReport, PagesConfig, PluginToken, Projects, PullRequest, PullRequestChecks, PullRequestInput, PullRequestPatch, PullRequestResult, PullRequestState, RelaySourceState, RepoRef, RepoSettings, ResolveTokenInput, Ruleset, SecretAccess, SecretScope, Secrets, Source, SourceBranch, SourceCheckRun, SourceCheckRunWrite, SourceCommitStatus, SourceFile, SourceRepoInfo, SourceRequiredChecks, SourceRulesetDetail, SourceSecurity, SourceTeamMember, SyncResult, TeamEntry, TeamEntry as TeamEntry$1, TeamPermission, TeamPermission as TeamPermission$1, TrackerDoctorReport, TrackerUser, VerifyTokenFailure, VerifyTokenResult, VerifyTokenResult as VerifyTokenResult$1, VerifyTokenSuccess, createFeatureResolver } from "@theholocron/cli";
10
+ import { AddedItem, Auth, AuthError, BoardItem, BoardItemInput, BoardState, BoardSummary, BoardTemplate, Ci, CiRun, CiRunFilter, CommitFilesInput, CommitFilesResult, EnsureRelaySourceInput, EnsureRelaySourceResult, EnsureReport, Environment, Environments, ExportInfo, ImportOptions, ImportResult, InspectRelaySourceInput, Issue, IssueCreateInput, IssuePatch, IssueRecord, IssueSearchFilter, IssueSelector, IssueTypeDeclaration, IssueTypeInfo, IssueUpdateResult, Issues, LabelDef, LifecycleResult, LifecycleSlot, MoveReport, PagesConfig, PluginToken, Projects, PullRequest, PullRequestChecks, PullRequestInput, PullRequestPatch, PullRequestResult, PullRequestState, RelaySourceState, RepoRef, RepoSettings, ResolveTokenInput, Ruleset, SecretAccess, SecretScope, Secrets, Source, SourceBranch, SourceCheckRun, SourceCheckRunWrite, SourceCommitStatus, SourceFile, SourceRepoInfo, SourceRequiredChecks, SourceRulesetDetail, SourceSecurity, SourceTeamMember, SourceTree, SyncResult, TeamEntry, TeamEntry as TeamEntry$1, TeamPermission, TeamPermission as TeamPermission$1, TrackerDoctorReport, TrackerUser, VerifyTokenFailure, VerifyTokenResult, VerifyTokenResult as VerifyTokenResult$1, VerifyTokenSuccess, createFeatureResolver } from "@theholocron/cli";
11
11
  import { GitHubClient, GitHubClientOptions, GraphQLClient, createGitHubClient } from "@theholocron/github-client";
12
12
  /** Resolves the token for read-only GitHub access (`HOLOCRON_READ_TOKEN`, keyring key `github.read`). */
13
13
  export declare const resolveReadToken: import("@theholocron/cli").FeatureResolver;
@@ -443,6 +443,8 @@ export declare class GitHubSource implements Source {
443
443
  listPullRequests(state: "open" | "closed", repo?: string): Promise<PullRequest[]>;
444
444
  readPullRequestState(number: number, repo?: string, required?: SourceRequiredChecks): Promise<PullRequestState | undefined>;
445
445
  listFiles(repo?: string): Promise<string[]>;
446
+ readTree(repo?: string): Promise<SourceTree>;
447
+ commitFiles(input: CommitFilesInput): Promise<CommitFilesResult>;
446
448
  deleteFile(path: string, opts: {
447
449
  message: string;
448
450
  repo?: string;
package/dist/index.mjs CHANGED
@@ -2264,18 +2264,19 @@ async function writeRepoFile(client, repo, path, content, opts) {
2264
2264
  throw err;
2265
2265
  }
2266
2266
  }
2267
- /** The default branch's commit and tree, read from the branch the repository names. */
2267
+ /** The default branch's name, commit and tree, read from the branch the repository names. */
2268
2268
  async function defaultHead(client, repo) {
2269
2269
  const { default_branch } = await client.repos.getRepo(repo);
2270
2270
  const ref = await client.git.getRef(repo, default_branch);
2271
2271
  const commit = await client.git.getCommit(repo, ref.object.sha);
2272
2272
  return {
2273
+ branch: default_branch,
2273
2274
  commit: commit.sha,
2274
2275
  tree: commit.tree.sha
2275
2276
  };
2276
2277
  }
2277
- /** Every file (not folder) on the default branch, as a path from the repository root, sorted. Throws when GitHub cut the tree short. */
2278
- async function listRepoFiles(client, repo) {
2278
+ /** The default branch's recursive tree and the commit it was read at. Throws when the repo is not found or GitHub cut the tree short. */
2279
+ async function recursiveHead(client, repo) {
2279
2280
  let head;
2280
2281
  try {
2281
2282
  head = await defaultHead(client, repo);
@@ -2285,7 +2286,73 @@ async function listRepoFiles(client, repo) {
2285
2286
  }
2286
2287
  const tree = await client.git.getTree(repo, head.tree, true);
2287
2288
  if (tree.truncated) throw new Error(`${repo}: the file list is too large for GitHub to return whole`);
2288
- return tree.tree.filter((item) => item.type === "blob").map((item) => item.path).sort();
2289
+ return {
2290
+ commit: head.commit,
2291
+ items: tree.tree
2292
+ };
2293
+ }
2294
+ /** Every file (not folder) on the default branch, as a path from the repository root, sorted. Throws when GitHub cut the tree short. */
2295
+ async function listRepoFiles(client, repo) {
2296
+ const { items } = await recursiveHead(client, repo);
2297
+ return items.filter((item) => item.type === "blob").map((item) => item.path).sort();
2298
+ }
2299
+ /** Every file on the default branch with its mode and blob sha, sorted by path, and the commit they were read at. Throws when GitHub cut the tree short. */
2300
+ async function readRepoTree(client, repo) {
2301
+ const { commit, items } = await recursiveHead(client, repo);
2302
+ return {
2303
+ commit,
2304
+ entries: items.filter((item) => item.type === "blob").map((item) => ({
2305
+ path: item.path,
2306
+ mode: item.mode,
2307
+ sha: item.sha
2308
+ })).sort((a, b) => Number(a.path > b.path) - Number(a.path < b.path))
2309
+ };
2310
+ }
2311
+ /** How many blobs are created at once: enough to be quick, few enough to stay clear of GitHub's secondary rate limit. */
2312
+ const BLOB_BATCH = 8;
2313
+ /**
2314
+ * Commits every change in ONE commit on the default branch: a blob per written file (base64, so a binary file is
2315
+ * exact), one tree on top of the head's tree (a removal is an entry with no sha), one commit whose parent is the head,
2316
+ * and a ref update that is not forced. A branch that moved since `base` (or since this call read it) is refused and
2317
+ * nothing is changed: the blobs, tree and commit made are unreachable objects GitHub collects. No changes makes no commit.
2318
+ */
2319
+ async function commitRepoFiles(client, repo, input) {
2320
+ if (input.changes.length === 0) return { committed: false };
2321
+ try {
2322
+ const head = await defaultHead(client, repo);
2323
+ if (input.base && input.base !== head.commit) throw new Error(`${repo}: ${head.branch} moved since it was read (${input.base.slice(0, 7)} to ${head.commit.slice(0, 7)}); nothing was changed, run it again`);
2324
+ const entries = [];
2325
+ for (let i = 0; i < input.changes.length; i += BLOB_BATCH) entries.push(...await Promise.all(input.changes.slice(i, i + BLOB_BATCH).map(async (change) => {
2326
+ if ("remove" in change) return {
2327
+ path: change.path,
2328
+ mode: "100644",
2329
+ type: "blob",
2330
+ sha: null
2331
+ };
2332
+ const blob = await client.git.createBlob(repo, Buffer.from(change.content).toString("base64"), "base64");
2333
+ return {
2334
+ path: change.path,
2335
+ mode: change.mode,
2336
+ type: "blob",
2337
+ sha: blob.sha
2338
+ };
2339
+ })));
2340
+ const tree = await client.git.createTree(repo, entries, head.tree);
2341
+ const commit = await client.git.createCommit(repo, input.message, tree.sha, [head.commit]);
2342
+ try {
2343
+ await client.git.updateRef(repo, `heads/${head.branch}`, commit.sha);
2344
+ } catch (err) {
2345
+ if (err instanceof ProviderApiError && (err.status === 422 || err.status === 409)) throw new Error(`${repo}: ${head.branch} moved while the commit was made, so the update was refused (not a fast-forward); nothing was changed, run it again`, { cause: err });
2346
+ throw err;
2347
+ }
2348
+ return {
2349
+ committed: true,
2350
+ commit: commit.sha
2351
+ };
2352
+ } catch (err) {
2353
+ if (err instanceof ProviderApiError && err.status === 403) throw noAccess(repo, "write", err);
2354
+ throw err;
2355
+ }
2289
2356
  }
2290
2357
  /** Removes one file in a commit on the default branch; a file that is already gone is what was asked for. */
2291
2358
  async function deleteRepoFile(client, repo, path, opts) {
@@ -3382,6 +3449,12 @@ var GitHubSource = class {
3382
3449
  async listFiles(repo = this.repo) {
3383
3450
  return listRepoFiles(this.userSyncClient ?? this.orgClient ?? this.client, repo);
3384
3451
  }
3452
+ async readTree(repo = this.repo) {
3453
+ return readRepoTree(this.userSyncClient ?? this.orgClient ?? this.client, repo);
3454
+ }
3455
+ async commitFiles(input) {
3456
+ return commitRepoFiles(this.userSyncClient ?? this.orgClient ?? this.client, input.repo ?? this.repo, input);
3457
+ }
3385
3458
  async deleteFile(path, opts) {
3386
3459
  return deleteRepoFile(this.userSyncClient ?? this.orgClient ?? this.client, opts.repo ?? this.repo, path, opts);
3387
3460
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theholocron/holocron-plugin-github",
3
- "version": "5.6.0",
3
+ "version": "5.7.0",
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,11 +44,11 @@
44
44
  "tsx": "4.23.15",
45
45
  "typescript": "^6.0.3",
46
46
  "vitest": "^5.1.2",
47
- "@theholocron/cli": "5.6.0",
47
+ "@theholocron/cli": "5.7.0",
48
48
  "@theholocron/eslint-config": "^9.1.0",
49
49
  "@theholocron/fs-utils": "^2.0.0",
50
- "@theholocron/github-client": "^2.2.0",
51
- "@theholocron/http-client": "^2.2.0",
50
+ "@theholocron/github-client": "^2.3.0",
51
+ "@theholocron/http-client": "^2.3.0",
52
52
  "@theholocron/misc-utils": "^2.0.0",
53
53
  "@theholocron/tsconfig": "^9.1.0",
54
54
  "@theholocron/tsdown-config": "^9.1.0",
@@ -58,7 +58,7 @@
58
58
  "@theholocron/fs-utils": "^2.0.0",
59
59
  "@theholocron/github-client": "^2.0.0",
60
60
  "@theholocron/misc-utils": "^2.0.0",
61
- "@theholocron/cli": "5.6.0"
61
+ "@theholocron/cli": "5.7.0"
62
62
  },
63
63
  "engines": {
64
64
  "node": ">=22"