@bensandee/forgejo-cli 0.6.1 → 0.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.
Files changed (3) hide show
  1. package/README.md +1 -1
  2. package/dist/bin.mjs +113 -49
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -48,7 +48,7 @@ command takes `--json`, which prints exactly one JSON document, failures include
48
48
  | `pr:list` | List a repository's pull requests |
49
49
  | `commit:status` | Read the status checks for a commit SHA, branch, tag or git ref |
50
50
  | `issue:*` | `view`, `list`, `comment`, `create`, `update`, `download`, `label`, `unlabel`, `set-labels`, `close`, `reopen` |
51
- | `pr:create` | Push the current branch and open a pull request, deriving the title and body when none is given |
51
+ | `pr:create` | Push the current branch and open a pull request, deriving the title and body when none is given; reports no closed-issue claims, which `bst pr:create` does |
52
52
  | `pr:update` | Push the current branch and update its open pull request |
53
53
  | `branch:list` | List a repository's branches |
54
54
  | `branch:delete` | Delete a branch through the API; reports the forge's reason when it refuses |
package/dist/bin.mjs CHANGED
@@ -160,7 +160,7 @@ function loadEnvFileForBin(rawArgs, bin, io = processIo) {
160
160
  // src/version.ts
161
161
  const parsed = z.object({ version: z.string() }).safeParse({
162
162
  name: "@bensandee/forgejo-cli",
163
- version: "0.6.1",
163
+ version: "0.7.0",
164
164
  description: "bst-fj: Forgejo API commands without the rest of bst",
165
165
  license: "MIT",
166
166
  repository: {
@@ -1283,6 +1283,8 @@ z.array(z.unknown());
1283
1283
  function branchNotFoundMessage(conn, branch) {
1284
1284
  return `Branch ${branch} not found in ${conn.repository}`;
1285
1285
  }
1286
+ z.object({ full_name: z.string() });
1287
+ z.object({ description: z.string().optional() });
1286
1288
  async function getBranchHeadSha(executor, conn, branch) {
1287
1289
  const url = `${repoBase(conn)}/branches/${encodeURIComponent(branch)}`;
1288
1290
  const res = await executor.fetch(url, { headers: authHeaders(conn) });
@@ -1665,6 +1667,45 @@ const commitStatusCommand = defineCommand({
1665
1667
  process.exitCode = report.status === "green" ? 0 : 1;
1666
1668
  }
1667
1669
  });
1670
+ // ../forgejo/src/closing-refs.ts
1671
+ const CLOSING_REF = /(?:^|[\s([])(clos(?:e|es|ed)|fix(?:es|ed)?|resolv(?:e|es|ed))(?::[ \t]*|[ \t]+)((?:[\w.-]+\/[\w.-]+)?#\d+)(?![\w-])/gim;
1672
+ function capitalize(word) {
1673
+ return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
1674
+ }
1675
+ function extractClosingRefs(messages) {
1676
+ const byIssue = /* @__PURE__ */ new Map();
1677
+ for (const message of messages) for (const [, keyword, issue] of message.matchAll(CLOSING_REF)) {
1678
+ if (keyword === void 0 || issue === void 0) continue;
1679
+ const key = issue.toLowerCase();
1680
+ if (!byIssue.has(key)) byIssue.set(key, {
1681
+ text: `${capitalize(keyword)} ${issue}`,
1682
+ issue
1683
+ });
1684
+ }
1685
+ return [...byIssue.values()];
1686
+ }
1687
+ function closedIssues(text) {
1688
+ return new Set(extractClosingRefs([text]).map((ref) => ref.issue.toLowerCase()));
1689
+ }
1690
+ function droppedClosingRefs(messages, body) {
1691
+ const closed = closedIssues(body);
1692
+ return extractClosingRefs(messages).filter((ref) => !closed.has(ref.issue.toLowerCase()));
1693
+ }
1694
+ function carryForwardClosingRefs(source, target) {
1695
+ const missing = droppedClosingRefs([source], target);
1696
+ if (missing.length === 0) return target;
1697
+ const refLines = missing.map((ref) => ref.text).join("\n");
1698
+ return target.trim() === "" ? refLines : `${target}\n\n${refLines}`;
1699
+ }
1700
+ function parseIssueList(value) {
1701
+ const numbers = /* @__PURE__ */ new Set();
1702
+ for (const part of value.split(",")) {
1703
+ const match = /^#?([1-9]\d*)$/.exec(part.trim());
1704
+ if (match?.[1] === void 0) throw new FatalError(`--closes takes issue numbers separated by commas, such as 12 or 12,15; got ${JSON.stringify(value)}.`);
1705
+ numbers.add(Number(match[1]));
1706
+ }
1707
+ return [...numbers];
1708
+ }
1668
1709
  // ../forgejo/src/fetcher.ts
1669
1710
  const LONG_FETCH_TIMEOUT_MS = 6e5;
1670
1711
  function morePagesFollow(res, pageLength, received, pageSize) {
@@ -4024,7 +4065,10 @@ async function findOpenPr(executor, conn, head, signal = null) {
4024
4065
  const PullListItemSchema = z.object({
4025
4066
  number: z.number(),
4026
4067
  title: z.string().nullish(),
4027
- head: z.object({ ref: z.string() }),
4068
+ head: z.object({
4069
+ ref: z.string(),
4070
+ repo: z.object({ full_name: z.string() }).nullish()
4071
+ }),
4028
4072
  base: z.object({ ref: z.string() }).nullish(),
4029
4073
  state: z.enum(["open", "closed"]),
4030
4074
  merged: z.boolean().nullish(),
@@ -4069,6 +4113,7 @@ async function listPulls(executor, conn, options = {}) {
4069
4113
  number: pr.number,
4070
4114
  title: pr.title ?? null,
4071
4115
  headRef: pr.head.ref,
4116
+ headRepo: pr.head.repo?.full_name ?? null,
4072
4117
  baseRef: pr.base?.ref ?? null,
4073
4118
  state: pr.state,
4074
4119
  merged: pr.merged ?? pr.merged_at != null,
@@ -4258,6 +4303,7 @@ const PullReviewCommentSchema = z.object({
4258
4303
  user: z.object({ login: z.string() }).nullish()
4259
4304
  });
4260
4305
  z.array(PullReviewCommentSchema);
4306
+ z.array(z.object({ id: z.number() }));
4261
4307
  // ../forgejo/src/issue-api.ts
4262
4308
  var IssueNumberRequiredError = class extends FatalError {
4263
4309
  constructor() {
@@ -5796,36 +5842,6 @@ const prCloseCommand = defineCommand({
5796
5842
  }
5797
5843
  }
5798
5844
  });
5799
- // ../forgejo/src/closing-refs.ts
5800
- const CLOSING_REF = /(?:^|[\s([])(clos(?:e|es|ed)|fix(?:es|ed)?|resolv(?:e|es|ed))(?::[ \t]*|[ \t]+)((?:[\w.-]+\/[\w.-]+)?#\d+)(?![\w-])/gim;
5801
- function capitalize(word) {
5802
- return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
5803
- }
5804
- function extractClosingRefs(messages) {
5805
- const byIssue = /* @__PURE__ */ new Map();
5806
- for (const message of messages) for (const [, keyword, issue] of message.matchAll(CLOSING_REF)) {
5807
- if (keyword === void 0 || issue === void 0) continue;
5808
- const key = issue.toLowerCase();
5809
- if (!byIssue.has(key)) byIssue.set(key, {
5810
- text: `${capitalize(keyword)} ${issue}`,
5811
- issue
5812
- });
5813
- }
5814
- return [...byIssue.values()];
5815
- }
5816
- function closedIssues(text) {
5817
- return new Set(extractClosingRefs([text]).map((ref) => ref.issue.toLowerCase()));
5818
- }
5819
- function droppedClosingRefs(messages, body) {
5820
- const closed = closedIssues(body);
5821
- return extractClosingRefs(messages).filter((ref) => !closed.has(ref.issue.toLowerCase()));
5822
- }
5823
- function carryForwardClosingRefs(source, target) {
5824
- const missing = droppedClosingRefs([source], target);
5825
- if (missing.length === 0) return target;
5826
- const refLines = missing.map((ref) => ref.text).join("\n");
5827
- return target.trim() === "" ? refLines : `${target}\n\n${refLines}`;
5828
- }
5829
5845
  // ../forgejo/src/git.ts
5830
5846
  const GIT_LOCAL_TIMEOUT_MS = 6e4;
5831
5847
  const GIT_WRITE_TIMEOUT_MS = 3e5;
@@ -6073,7 +6089,51 @@ function checkClosingLinks(messages, plan, allowUnlink) {
6073
6089
  unlinked
6074
6090
  };
6075
6091
  }
6092
+ function addClosingIssues(plan, issues) {
6093
+ const wanted = issues.map((issue) => `Closes #${String(issue)}`).join("\n");
6094
+ const added = droppedClosingRefs([wanted], plan.effective.body).map((ref) => Number(ref.issue.slice(1)));
6095
+ if (added.length === 0) return {
6096
+ plan,
6097
+ added
6098
+ };
6099
+ const body = carryForwardClosingRefs(wanted, plan.effective.body);
6100
+ return {
6101
+ plan: {
6102
+ ...plan,
6103
+ patch: {
6104
+ ...plan.patch,
6105
+ body
6106
+ },
6107
+ effective: {
6108
+ ...plan.effective,
6109
+ body
6110
+ }
6111
+ },
6112
+ added
6113
+ };
6114
+ }
6076
6115
  const ALWAYS_PROTECTED = /* @__PURE__ */ new Set(["main", "master"]);
6116
+ async function planUpsert(host, existing, derived, options) {
6117
+ let plan;
6118
+ if (existing === null) plan = {
6119
+ patch: {
6120
+ title: derived.title,
6121
+ body: derived.body
6122
+ },
6123
+ effective: derived,
6124
+ previousBody: null
6125
+ };
6126
+ else plan = planPrUpdate(options, derived, prUpdateNeedsLive(options) ? await host.readLive(existing, options.signal ?? null) : null, host.wipTitles);
6127
+ if (options.closes === void 0) return {
6128
+ plan,
6129
+ closesAdded: void 0
6130
+ };
6131
+ const linked = addClosingIssues(plan, options.closes);
6132
+ return {
6133
+ plan: linked.plan,
6134
+ closesAdded: linked.added
6135
+ };
6136
+ }
6077
6137
  async function runPrUpsert(git, host, options) {
6078
6138
  const { cwd } = options;
6079
6139
  const mode = options.mode ?? "upsert";
@@ -6106,22 +6166,14 @@ async function runPrUpsert(git, host, options) {
6106
6166
  const existing = open?.number ?? null;
6107
6167
  assertModeAllows(mode, existing, branch);
6108
6168
  if (!options.dryRun && options.skipPush !== true) await pushBranch(git, cwd, branch, options.pushEnv, options.forceWithLease);
6109
- let plan;
6110
- if (existing === null) plan = {
6111
- patch: {
6112
- title: derived.title,
6113
- body: derived.body
6114
- },
6115
- effective: derived,
6116
- previousBody: null
6117
- };
6118
- else plan = planPrUpdate(options, derived, prUpdateNeedsLive(options) ? await host.readLive(existing, signal) : null, host.wipTitles);
6169
+ const { plan, closesAdded } = await planUpsert(host, existing, derived, options);
6119
6170
  const report = checkClosingLinks(messages, plan, options.allowUnlink === true);
6120
6171
  options.onClosingLinks?.(report);
6121
6172
  const shape = {
6122
6173
  branch,
6123
6174
  base: open?.base ?? localBase,
6124
- ...plan.effective
6175
+ ...plan.effective,
6176
+ ...closesAdded === void 0 ? {} : { closesAdded }
6125
6177
  };
6126
6178
  if (options.dryRun) return {
6127
6179
  action: existing === null ? "would-create" : "would-update",
@@ -6157,7 +6209,7 @@ function describeClosingLinks(report, flags) {
6157
6209
  const lines = [];
6158
6210
  if (report.fromCommits.length > 0) {
6159
6211
  const said = report.fromCommits.map((dropped) => dropped.text).join(", ");
6160
- lines.push(`The commits say ${said}, but the PR body does not -- the issue may not close on merge. Add it to ${flags.body}.`);
6212
+ lines.push(`The commits say ${said}, but the PR body does not -- the issue may not close on merge. Link it with ${flags.closes}.`);
6161
6213
  }
6162
6214
  if (report.unlinked.length > 0) {
6163
6215
  const lost = report.unlinked.map((dropped) => dropped.text).join(", ");
@@ -6209,15 +6261,17 @@ function formatPrCreate(result) {
6209
6261
  };
6210
6262
  }
6211
6263
  function formatPrUpdate(result, rewrites, flags) {
6264
+ const added = result.closesAdded ?? [];
6212
6265
  if (isDryRun(result)) return {
6213
6266
  lines: [`[dry-run] Would push ${prTarget(result)} to ${prLabel(result)}`],
6214
6267
  body: {
6215
- heading: rewrites ? "[dry-run] Body" : "[dry-run] Body (unchanged)",
6268
+ heading: rewrites || added.length > 0 ? "[dry-run] Body" : "[dry-run] Body (unchanged)",
6216
6269
  text: result.body
6217
6270
  }
6218
6271
  };
6219
6272
  const lines = [`Updated ${prLabel(result)} (${prTarget(result)})`];
6220
- if (!rewrites) lines.push(`Title and body left as authored -- pass ${flags.title}/${flags.body} to change them.`);
6273
+ if (added.length > 0) lines.push(`Linked ${added.map((issue) => `#${String(issue)}`).join(", ")} with a Closes line.`);
6274
+ else if (!rewrites) lines.push(`Title and body left as authored -- pass ${flags.title}/${flags.body} to change them.`);
6221
6275
  if (result.url !== null) lines.push(result.url);
6222
6276
  return {
6223
6277
  lines,
@@ -6339,7 +6393,7 @@ const COMMANDS = {
6339
6393
  update: "pnpm exec bst-fj pr:update"
6340
6394
  };
6341
6395
  const FLAGS = {
6342
- body: "--body",
6396
+ closes: "--closes",
6343
6397
  allowUnlink: "--allow-unlink"
6344
6398
  };
6345
6399
  function warnClosingLinks(report) {
@@ -6402,13 +6456,17 @@ const prCreateCommand = defineCommand({
6402
6456
  type: "boolean",
6403
6457
  description: "With --upsert: permit a --body that drops a closing keyword the PR carries"
6404
6458
  },
6459
+ closes: {
6460
+ type: "string",
6461
+ description: "Issues the PR closes, comma-separated (12 or 12,15): adds a Closes #<n> line to the body for each one it does not already close"
6462
+ },
6405
6463
  "dry-run": {
6406
6464
  type: "boolean",
6407
6465
  description: "Show what would happen without pushing"
6408
6466
  },
6409
6467
  json: {
6410
6468
  type: "boolean",
6411
- description: "Emit one JSON document: action (created | updated | would-create | would-update), number (or null), url (or null), branch, base, title, body, droppedRefs? ([{text, issue}]), dryRun. A create or would-create carries droppedRefs; on a dry run, number and url are null."
6469
+ description: "Emit one JSON document: action (created | updated | would-create | would-update), number (or null), url (or null), branch, base, title, body, droppedRefs? ([{text, issue}]), closesAdded? (with --closes: the issues it added a Closes line for), dryRun. A create or would-create carries droppedRefs; on a dry run, number and url are null. The document carries no claims member and no claim advice line: the closed-issue claim check reads the repo's .tooling.json and is bst pr:create's alone."
6412
6470
  },
6413
6471
  verbose: {
6414
6472
  type: "boolean",
@@ -6424,6 +6482,7 @@ const prCreateCommand = defineCommand({
6424
6482
  body,
6425
6483
  base: args.base ?? void 0,
6426
6484
  allowUnlink: args["allow-unlink"] === true,
6485
+ closes: args.closes === void 0 ? void 0 : parseIssueList(args.closes),
6427
6486
  draft: args.draft === true ? true : void 0,
6428
6487
  dryRun: args["dry-run"] === true
6429
6488
  });
@@ -7120,13 +7179,17 @@ const prSubCommands = {
7120
7179
  type: "boolean",
7121
7180
  description: "Permit a --body that drops a closing keyword the current body carries"
7122
7181
  },
7182
+ closes: {
7183
+ type: "string",
7184
+ description: "Issues the PR closes, comma-separated (12 or 12,15): adds a Closes #<n> line to the body for each one it does not already close"
7185
+ },
7123
7186
  "dry-run": {
7124
7187
  type: "boolean",
7125
7188
  description: "Show what would happen without pushing"
7126
7189
  },
7127
7190
  json: {
7128
7191
  type: "boolean",
7129
- description: "Emit one JSON document: action (updated | would-update), number (or null), url (or null), branch, base, title, body, dryRun, rewrites. On a dry run, number and url are null."
7192
+ description: "Emit one JSON document: action (updated | would-update), number (or null), url (or null), branch, base, title, body, closesAdded? (with --closes: the issues it added a Closes line for), dryRun, rewrites. On a dry run, number and url are null."
7130
7193
  },
7131
7194
  verbose: {
7132
7195
  type: "boolean",
@@ -7143,6 +7206,7 @@ const prSubCommands = {
7143
7206
  body,
7144
7207
  base: args.base ?? void 0,
7145
7208
  allowUnlink: args["allow-unlink"] === true,
7209
+ closes: args.closes === void 0 ? void 0 : parseIssueList(args.closes),
7146
7210
  draft: void 0,
7147
7211
  dryRun: args["dry-run"] === true
7148
7212
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bensandee/forgejo-cli",
3
- "version": "0.6.1",
3
+ "version": "0.7.0",
4
4
  "description": "bst-fj: Forgejo API commands without the rest of bst",
5
5
  "license": "MIT",
6
6
  "repository": {