@theholocron/holocron-plugin-github 5.4.0 → 5.5.1

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
@@ -442,6 +442,12 @@ export declare class GitHubSource implements Source {
442
442
  getPullRequest(number: number, repo?: string): Promise<PullRequest>;
443
443
  listPullRequests(state: "open" | "closed", repo?: string): Promise<PullRequest[]>;
444
444
  readPullRequestState(number: number, repo?: string, required?: SourceRequiredChecks): Promise<PullRequestState | undefined>;
445
+ listFiles(repo?: string): Promise<string[]>;
446
+ deleteFile(path: string, opts: {
447
+ message: string;
448
+ repo?: string;
449
+ }): Promise<void>;
450
+ ensureTag(tag: string, repo?: string): Promise<boolean>;
445
451
  listBranches(repo?: string): Promise<SourceBranch[]>;
446
452
  deleteBranch(name: string, repo?: string): Promise<void>;
447
453
  listPullRequestCommits(number: number, repo?: string): Promise<string[]>;
package/dist/index.mjs CHANGED
@@ -2264,6 +2264,64 @@ 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. */
2268
+ async function defaultHead(client, repo) {
2269
+ const { default_branch } = await client.repos.getRepo(repo);
2270
+ const ref = await client.git.getRef(repo, default_branch);
2271
+ const commit = await client.git.getCommit(repo, ref.object.sha);
2272
+ return {
2273
+ commit: commit.sha,
2274
+ tree: commit.tree.sha
2275
+ };
2276
+ }
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) {
2279
+ let head;
2280
+ try {
2281
+ head = await defaultHead(client, repo);
2282
+ } catch (err) {
2283
+ if (err instanceof ProviderApiError && err.status === 404) throw new Error(`${repo} was not found: it does not exist, or the token cannot see it`, { cause: err });
2284
+ throw err;
2285
+ }
2286
+ const tree = await client.git.getTree(repo, head.tree, true);
2287
+ 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
+ }
2290
+ /** Removes one file in a commit on the default branch; a file that is already gone is what was asked for. */
2291
+ async function deleteRepoFile(client, repo, path, opts) {
2292
+ const file = await readRepoFile(client, repo, path);
2293
+ if (!file) return;
2294
+ try {
2295
+ await client.git.deleteContents(repo, path, {
2296
+ message: opts.message,
2297
+ sha: file.version
2298
+ });
2299
+ } catch (err) {
2300
+ if (err instanceof ProviderApiError && err.status === 403) throw noAccess(repo, "write", err);
2301
+ throw err;
2302
+ }
2303
+ }
2304
+ /**
2305
+ * Tags the default branch's head `tag`. A tag already there at the same content (the same commit, or another commit
2306
+ * with the same tree) is left alone and answers `false`; one at other content throws, because a version tag is never
2307
+ * moved. `true` when it made the tag.
2308
+ */
2309
+ async function ensureRepoTag(client, repo, tag) {
2310
+ const head = await defaultHead(client, repo);
2311
+ let existing;
2312
+ try {
2313
+ existing = await client.git.getTagRef(repo, tag);
2314
+ } catch (err) {
2315
+ if (!(err instanceof ProviderApiError && err.status === 404)) throw err;
2316
+ }
2317
+ if (!existing) {
2318
+ await client.git.createRef(repo, `refs/tags/${tag}`, head.commit);
2319
+ return true;
2320
+ }
2321
+ if (existing.object.sha === head.commit) return false;
2322
+ if ((await client.git.getCommit(repo, existing.object.sha)).tree.sha === head.tree) return false;
2323
+ throw new Error(`${repo} already has the tag ${tag} at other content: bump the version instead of moving the tag`);
2324
+ }
2267
2325
  // src/capabilities/labels.ts
2268
2326
  /**
2269
2327
  * Idempotently synchronise a repo's labels to the canonical set.
@@ -3291,6 +3349,15 @@ var GitHubSource = class {
3291
3349
  async readPullRequestState(number, repo, required) {
3292
3350
  return readPullRequestState(this.pullClient, repo ?? this.repo, number, required);
3293
3351
  }
3352
+ async listFiles(repo = this.repo) {
3353
+ return listRepoFiles(this.userSyncClient ?? this.orgClient ?? this.client, repo);
3354
+ }
3355
+ async deleteFile(path, opts) {
3356
+ return deleteRepoFile(this.userSyncClient ?? this.orgClient ?? this.client, opts.repo ?? this.repo, path, opts);
3357
+ }
3358
+ async ensureTag(tag, repo = this.repo) {
3359
+ return ensureRepoTag(this.userSyncClient ?? this.orgClient ?? this.client, repo, tag);
3360
+ }
3294
3361
  async listBranches(repo) {
3295
3362
  return (await this.client.branches.listBranches(repo ?? this.repo)).map((b) => ({
3296
3363
  name: b.name,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theholocron/holocron-plugin-github",
3
- "version": "5.4.0",
3
+ "version": "5.5.1",
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.4.0",
47
+ "@theholocron/cli": "5.5.1",
48
48
  "@theholocron/eslint-config": "^9.0.0",
49
49
  "@theholocron/fs-utils": "^2.0.0",
50
- "@theholocron/github-client": "^2.1.0",
51
- "@theholocron/http-client": "^2.1.0",
50
+ "@theholocron/github-client": "^2.2.0",
51
+ "@theholocron/http-client": "^2.2.0",
52
52
  "@theholocron/misc-utils": "^2.0.0",
53
53
  "@theholocron/tsconfig": "^9.0.0",
54
54
  "@theholocron/tsdown-config": "^9.0.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.4.0"
61
+ "@theholocron/cli": "5.5.1"
62
62
  },
63
63
  "engines": {
64
64
  "node": ">=22"