@theholocron/holocron-plugin-github 5.7.0 → 5.8.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
@@ -457,6 +457,14 @@ export declare class GitHubSource implements Source {
457
457
  listPullRequestFiles(number: number, repo?: string): Promise<string[]>;
458
458
  createPullRequest(input: PullRequestInput, repo?: string): Promise<PullRequestResult>;
459
459
  updatePullRequest(number: number, patch: PullRequestPatch, repo?: string): Promise<PullRequestResult>;
460
+ mergePullRequest(number: number, options?: {
461
+ method?: "merge" | "squash" | "rebase";
462
+ title?: string;
463
+ }, repo?: string): Promise<{
464
+ merged: boolean;
465
+ commit?: string;
466
+ reason?: string;
467
+ }>;
460
468
  markReady(number: number, repo?: string): Promise<boolean>;
461
469
  getPullRequestChecks(number: number, repo?: string, required?: SourceRequiredChecks): Promise<PullRequestChecks>;
462
470
  listCheckRuns(ref: string, repo?: string): Promise<SourceCheckRun[]>;
package/dist/index.mjs CHANGED
@@ -2288,6 +2288,7 @@ async function recursiveHead(client, repo) {
2288
2288
  if (tree.truncated) throw new Error(`${repo}: the file list is too large for GitHub to return whole`);
2289
2289
  return {
2290
2290
  commit: head.commit,
2291
+ branch: head.branch,
2291
2292
  items: tree.tree
2292
2293
  };
2293
2294
  }
@@ -2298,9 +2299,10 @@ async function listRepoFiles(client, repo) {
2298
2299
  }
2299
2300
  /** 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
2301
  async function readRepoTree(client, repo) {
2301
- const { commit, items } = await recursiveHead(client, repo);
2302
+ const { commit, branch, items } = await recursiveHead(client, repo);
2302
2303
  return {
2303
2304
  commit,
2305
+ branch,
2304
2306
  entries: items.filter((item) => item.type === "blob").map((item) => ({
2305
2307
  path: item.path,
2306
2308
  mode: item.mode,
@@ -2311,7 +2313,21 @@ async function readRepoTree(client, repo) {
2311
2313
  /** How many blobs are created at once: enough to be quick, few enough to stay clear of GitHub's secondary rate limit. */
2312
2314
  const BLOB_BATCH = 8;
2313
2315
  /**
2314
- * Commits every change in ONE commit on the default branch: a blob per written file (base64, so a binary file is
2316
+ * Makes `branch` point at `commit`: created when it is missing, put back there (forced) when a run before left it
2317
+ * somewhere else, since the branch is only ever a carrier for one pull request.
2318
+ */
2319
+ async function pointBranchAt(client, repo, branch, commit) {
2320
+ let existing;
2321
+ try {
2322
+ existing = await client.git.getRef(repo, branch);
2323
+ } catch (err) {
2324
+ if (!(err instanceof ProviderApiError && err.status === 404)) throw err;
2325
+ }
2326
+ if (!existing) await client.git.createRef(repo, `refs/heads/${branch}`, commit);
2327
+ else if (existing.object.sha !== commit) await client.git.updateRef(repo, `heads/${branch}`, commit, true);
2328
+ }
2329
+ /**
2330
+ * Commits every change in ONE commit on the default branch (or on `branch`, made from its head): a blob per written file (base64, so a binary file is
2315
2331
  * 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
2332
  * and a ref update that is not forced. A branch that moved since `base` (or since this call read it) is refused and
2317
2333
  * nothing is changed: the blobs, tree and commit made are unreachable objects GitHub collects. No changes makes no commit.
@@ -2339,8 +2355,10 @@ async function commitRepoFiles(client, repo, input) {
2339
2355
  })));
2340
2356
  const tree = await client.git.createTree(repo, entries, head.tree);
2341
2357
  const commit = await client.git.createCommit(repo, input.message, tree.sha, [head.commit]);
2358
+ const target = input.branch ?? head.branch;
2359
+ if (input.branch) await pointBranchAt(client, repo, input.branch, head.commit);
2342
2360
  try {
2343
- await client.git.updateRef(repo, `heads/${head.branch}`, commit.sha);
2361
+ await client.git.updateRef(repo, `heads/${target}`, commit.sha);
2344
2362
  } catch (err) {
2345
2363
  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
2364
  throw err;
@@ -2870,6 +2888,32 @@ async function markReady(client, repo, number) {
2870
2888
  await client.pulls.markReadyForReview(raw.node_id);
2871
2889
  return true;
2872
2890
  }
2891
+ /**
2892
+ * Merges a pull request. GitHub answers 405 for one that is not mergeable yet (a required check pending, a review
2893
+ * missing, a conflict) and 409 when the head moved; both come back as `merged: false` with GitHub's reason and the
2894
+ * pull request stays open, so the caller can say so.
2895
+ */
2896
+ async function mergePullRequest(client, repo, number, options = {}) {
2897
+ try {
2898
+ const done = await client.pulls.mergePullRequest(repo, number, {
2899
+ merge_method: options.method ?? "squash",
2900
+ ...options.title === void 0 ? {} : { commit_title: options.title }
2901
+ });
2902
+ return done.merged ? {
2903
+ merged: true,
2904
+ commit: done.sha
2905
+ } : {
2906
+ merged: false,
2907
+ reason: done.message
2908
+ };
2909
+ } catch (err) {
2910
+ if (err instanceof ProviderApiError && (err.status === 405 || err.status === 409)) return {
2911
+ merged: false,
2912
+ reason: String(err.details || err.message)
2913
+ };
2914
+ throw err;
2915
+ }
2916
+ }
2873
2917
  // src/capabilities/read-repo.ts
2874
2918
  /** The settings `setup` writes, which `GET /repos/{repo}` reports under the same names. */
2875
2919
  const SETTING_KEYS = [
@@ -3491,6 +3535,9 @@ var GitHubSource = class {
3491
3535
  async updatePullRequest(number, patch, repo) {
3492
3536
  return updatePullRequest(this.pullClient, repo ?? this.repo, number, patch, this.orgClient ?? this.pullClient);
3493
3537
  }
3538
+ async mergePullRequest(number, options, repo) {
3539
+ return mergePullRequest(this.pullClient, repo ?? this.repo, number, options);
3540
+ }
3494
3541
  async markReady(number, repo) {
3495
3542
  return markReady(this.pullClient, repo ?? this.repo, number);
3496
3543
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theholocron/holocron-plugin-github",
3
- "version": "5.7.0",
3
+ "version": "5.8.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.7.0",
47
+ "@theholocron/cli": "5.8.1",
48
48
  "@theholocron/eslint-config": "^9.1.0",
49
49
  "@theholocron/fs-utils": "^2.0.0",
50
- "@theholocron/github-client": "^2.3.0",
51
- "@theholocron/http-client": "^2.3.0",
50
+ "@theholocron/github-client": "^2.3.1",
51
+ "@theholocron/http-client": "^2.3.1",
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.7.0"
61
+ "@theholocron/cli": "5.8.1"
62
62
  },
63
63
  "engines": {
64
64
  "node": ">=22"