@kaynooo/cli-forgejo 0.15.1 → 0.15.2

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 (2) hide show
  1. package/dist/cli.js +118 -64
  2. package/package.json +2 -2
package/dist/cli.js CHANGED
@@ -2,7 +2,7 @@
2
2
  // @bun
3
3
 
4
4
  // src/cli.ts
5
- import process20 from "process";
5
+ import process21 from "process";
6
6
  // ../kit/src/args.ts
7
7
  import process6 from "process";
8
8
  import { parseArgs } from "util";
@@ -1264,7 +1264,7 @@ function reportThrown(prefix, error, redact = (text) => text, json = false) {
1264
1264
  const help = error instanceof UsageError ? `run ${prefix} --help for its options` : undefined;
1265
1265
  const named = error instanceof Error && !(error instanceof Exit) && error.name !== "Error" ? `${error.name}: ${error.message}` : undefined;
1266
1266
  const cause = error instanceof Error && error.cause instanceof Error ? redact(error.cause.message) : undefined;
1267
- if (error instanceof UsageError)
1267
+ if (error instanceof Exit && error.code === 2)
1268
1268
  noteUsageError(error.message);
1269
1269
  const report = json ? printJsonError : (message, hint, causeText) => printError(prefix, message, hint, causeText);
1270
1270
  report(redact(named ?? (error instanceof Error ? error.message : String(error))), error instanceof Exit ? error.hint ?? help : undefined, cause);
@@ -1758,12 +1758,96 @@ function holderSession(holder, sessions) {
1758
1758
  return lane === undefined ? undefined : sessions.find((session) => session.name.endsWith(`-${lane}`));
1759
1759
  }
1760
1760
 
1761
+ // src/forge.ts
1762
+ import { homedir as homedir4 } from "os";
1763
+ import { join as join5 } from "path";
1764
+ import process16 from "process";
1765
+
1766
+ // src/remote.ts
1767
+ var SCP_LIKE = /^(?:([^@/]+)@)?([^:/]+):(?!\/)(.+?)(?:\.git)?\/?$/;
1768
+ var SSH_URL = /^ssh:\/\/(?:([^@/]+)@)?([^:/]+)(?::(\d+))?\/(.+?)(?:\.git)?\/?$/;
1769
+ var USERINFO = /^([a-z][a-z\d+.-]*:\/\/)[^@/]*@/i;
1770
+ var DEFAULT_PORT = "22";
1771
+ function parseSshRemote(url) {
1772
+ const ssh = SSH_URL.exec(url);
1773
+ const scp = ssh === null && !url.includes("://") ? SCP_LIKE.exec(url) : null;
1774
+ const match = ssh ?? scp;
1775
+ if (match === null || match[2]?.startsWith("-") === true)
1776
+ return;
1777
+ const login = match[1] ?? "git";
1778
+ const user = login.includes(":") ? login.slice(0, login.indexOf(":")) : login;
1779
+ if (ssh === null)
1780
+ return { user, host: match[2] ?? "", path: match[3] ?? "" };
1781
+ return { user, host: match[2] ?? "", path: match[4] ?? "", ...ssh[3] === undefined ? {} : { port: ssh[3] } };
1782
+ }
1783
+ function redactUrl(url) {
1784
+ return url.replace(USERINFO, "$1");
1785
+ }
1786
+ function canonicalRemote(url, host, reaches) {
1787
+ const remote = parseSshRemote(url);
1788
+ if (remote === undefined || remote.host === host || !reaches(remote.host))
1789
+ return;
1790
+ if (remote.port !== undefined && remote.port !== DEFAULT_PORT)
1791
+ return `ssh://${remote.user}@${host}:${remote.port}/${remote.path}.git`;
1792
+ return `${remote.user}@${host}:${remote.path}.git`;
1793
+ }
1794
+ async function reachesHost(hostname, host, hostAddresses, addressesOf) {
1795
+ if (hostname === undefined)
1796
+ return false;
1797
+ return hostname === host || (await addressesOf(hostname)).some((address) => hostAddresses.has(address));
1798
+ }
1799
+
1800
+ // src/forge.ts
1801
+ var LOGIN_HOST = /^\s*(?:-\s*)?(?:url|ssh_host):\s*["']?([^\s"']+)/gm;
1802
+ var PORT = /:\d+$/;
1803
+ var SCP_REMOTE = /^(?:[^@/\s]+@)?([^:/\s]+):(?!\/\/)/;
1804
+ var URL_REMOTE = /^[a-z+]+:\/\/(?:[^@/\s]+@)?(\[[^\]]+\]|[^:/\s]+)/i;
1805
+ function originHost(remote) {
1806
+ const text = remote.trim();
1807
+ return (URL_REMOTE.exec(text) ?? SCP_REMOTE.exec(text))?.[1]?.toLowerCase();
1808
+ }
1809
+ function loginHosts(config) {
1810
+ const hosts = new Set;
1811
+ for (const [, value = ""] of config.matchAll(LOGIN_HOST)) {
1812
+ const host = URL.canParse(value) ? new URL(value).hostname : value.replace(PORT, "");
1813
+ if (host !== "")
1814
+ hosts.add(host.toLowerCase());
1815
+ }
1816
+ return hosts;
1817
+ }
1818
+ function teaConfigPath() {
1819
+ const base = process16.env.XDG_CONFIG_HOME;
1820
+ return join5(base !== undefined && base !== "" ? base : join5(homedir4(), ".config"), "tea", "config.yml");
1821
+ }
1822
+ async function readConfig() {
1823
+ try {
1824
+ return await Bun.file(teaConfigPath()).text();
1825
+ } catch {
1826
+ return "";
1827
+ }
1828
+ }
1829
+ var checked;
1830
+ async function requireForgeOrigin() {
1831
+ checked ??= (async () => {
1832
+ const hosts = loginHosts(await readConfig());
1833
+ if (hosts.size === 0)
1834
+ return;
1835
+ const origin = await runTool("git remote get-url origin", ["git", "remote", "get-url", "origin"], { timeoutMs: 5000 });
1836
+ const host = origin.code === 0 ? originHost(origin.stdout) : undefined;
1837
+ if (host !== undefined && !hosts.has(host))
1838
+ fail(`origin ${redactUrl(origin.stdout.trim())} is not on a Forgejo tea is logged in to (${[...hosts].join(", ")})`, 2, "name the repository with -r owner/name, or run ft in a repository hosted there (an ssh alias origin: ft remote --fix)");
1839
+ })();
1840
+ return checked;
1841
+ }
1842
+
1761
1843
  // src/tea.ts
1762
1844
  var TEA_MS = 60000;
1763
1845
  function repoFlag(repo) {
1764
1846
  return repo === undefined ? [] : ["--repo", repo];
1765
1847
  }
1766
1848
  async function request(path, repo) {
1849
+ if (repo === undefined)
1850
+ await requireForgeOrigin();
1767
1851
  const result = await runTool(`tea api ${path}`, ["tea", "api", ...repoFlag(repo), path], { timeoutMs: TEA_MS });
1768
1852
  if (result.code !== 0)
1769
1853
  throw new Error(`tea api ${path}: ${result.stderr.trim() || `exit ${result.code}`}`);
@@ -1796,6 +1880,8 @@ async function repoApi(path, repo) {
1796
1880
  async function repoWrite(method, path, fields, repo) {
1797
1881
  const full = `repos/{owner}/{repo}/${path}`;
1798
1882
  const flags = Object.entries(fields).flatMap(([name, value]) => ["-f", `${name}=${value}`]);
1883
+ if (repo === undefined)
1884
+ await requireForgeOrigin();
1799
1885
  const result = await runTool(`tea api ${full}`, ["tea", "api", ...repoFlag(repo), "-X", method, ...flags, full], { timeoutMs: TEA_MS });
1800
1886
  if (result.code !== 0)
1801
1887
  throw new Error(`tea api ${full}: ${result.stderr.trim() || `exit ${result.code}`}`);
@@ -1816,6 +1902,8 @@ async function repoApiText(path, repo) {
1816
1902
  return text;
1817
1903
  }
1818
1904
  async function tea(command, args, repo) {
1905
+ if (repo === undefined)
1906
+ await requireForgeOrigin();
1819
1907
  const result = await runTool(`tea ${command.join(" ")}`, ["tea", ...command, ...repoFlag(repo), ...args], { timeoutMs: TEA_MS });
1820
1908
  if (result.code !== 0)
1821
1909
  throw new Error(`tea ${command.join(" ")}: ${result.stderr.trim() || result.stdout.trim() || `exit ${result.code}`}`);
@@ -2219,7 +2307,7 @@ async function drop(args) {
2219
2307
  }
2220
2308
 
2221
2309
  // src/commands/ci.ts
2222
- import process16 from "process";
2310
+ import process17 from "process";
2223
2311
 
2224
2312
  // src/git.ts
2225
2313
  async function tagsAt(commit) {
@@ -2475,7 +2563,7 @@ async function ci2(args) {
2475
2563
  return;
2476
2564
  }
2477
2565
  const json = values.json === true;
2478
- const { dim } = paint(process16.stderr);
2566
+ const { dim } = paint(process17.stderr);
2479
2567
  const runs = await waitForCi({
2480
2568
  tool: "ft ci",
2481
2569
  short,
@@ -2500,7 +2588,7 @@ async function ci2(args) {
2500
2588
  },
2501
2589
  lines: (spinner, found, started) => [
2502
2590
  `${spinner} CI on ${short} ${dim(formatElapsed(Date.now() - started))}`,
2503
- ...(found ?? []).map((run) => ` ${badge(run.status, process16.stderr)} ${run.workflow_id} ${dim(`run ${run.id}`)}`)
2591
+ ...(found ?? []).map((run) => ` ${badge(run.status, process17.stderr)} ${run.workflow_id} ${dim(`run ${run.id}`)}`)
2504
2592
  ]
2505
2593
  });
2506
2594
  const status = overall(runs);
@@ -2530,7 +2618,7 @@ run ${run.id} failed${run.commit_sha === sha ? "" : `, testing every commit from
2530
2618
  }
2531
2619
 
2532
2620
  // src/commands/digest.ts
2533
- import process17 from "process";
2621
+ import process18 from "process";
2534
2622
  var RUNS_SCANNED = 50;
2535
2623
  var TITLE_WIDTH2 = 70;
2536
2624
  var SHOWN = 8;
@@ -2580,7 +2668,7 @@ async function quietly(read) {
2580
2668
  }
2581
2669
  }
2582
2670
  async function staleTodos() {
2583
- const bin = Bun.which("todo-scan", { PATH: process17.env.PATH ?? "" });
2671
+ const bin = Bun.which("todo-scan", { PATH: process18.env.PATH ?? "" });
2584
2672
  if (bin === null)
2585
2673
  return [];
2586
2674
  let found;
@@ -3423,8 +3511,8 @@ function report(plan, added, skipped) {
3423
3511
  }
3424
3512
 
3425
3513
  // src/commands/release.ts
3426
- import { basename as basename3, join as join8 } from "path";
3427
- import process19 from "process";
3514
+ import { basename as basename3, join as join9 } from "path";
3515
+ import process20 from "process";
3428
3516
  var {$: $2 } = globalThis.Bun;
3429
3517
 
3430
3518
  // src/commands/watch.ts
@@ -3606,7 +3694,7 @@ ${push.stderr.trim()}`, 1);
3606
3694
 
3607
3695
  // src/commands/release-many.ts
3608
3696
  import { readdir } from "fs/promises";
3609
- import { join as join5 } from "path";
3697
+ import { join as join6 } from "path";
3610
3698
 
3611
3699
  // src/commands/release-version.ts
3612
3700
  var LEVELS = new Set(["patch", "minor", "major", "prepatch", "preminor", "premajor", "prerelease"]);
@@ -3655,11 +3743,11 @@ function parseTargets(spec, level) {
3655
3743
  });
3656
3744
  }
3657
3745
  async function publishable() {
3658
- const root = join5(await git(["rev-parse", "--show-toplevel"]), "packages");
3746
+ const root = join6(await git(["rev-parse", "--show-toplevel"]), "packages");
3659
3747
  const folders = (await readdir(root, { withFileTypes: true })).filter((entry) => entry.isDirectory()).map((entry) => entry.name).toSorted();
3660
3748
  const found = [];
3661
3749
  for (const folder of folders) {
3662
- const file = Bun.file(join5(root, folder, "package.json"));
3750
+ const file = Bun.file(join6(root, folder, "package.json"));
3663
3751
  if (await file.exists() && (await file.json()).private !== true)
3664
3752
  found.push(folder);
3665
3753
  }
@@ -3667,13 +3755,13 @@ async function publishable() {
3667
3755
  }
3668
3756
 
3669
3757
  // src/commands/release-plan.ts
3670
- import { basename as basename2, join as join6, relative } from "path";
3671
- import process18 from "process";
3758
+ import { basename as basename2, join as join7, relative } from "path";
3759
+ import process19 from "process";
3672
3760
  async function printPlan({ level, dir, prefix, branch, base, timeout, registry }) {
3673
- const file = Bun.file(join6(dir ?? ".", "package.json"));
3761
+ const file = Bun.file(join7(dir ?? ".", "package.json"));
3674
3762
  const pkg = await file.exists() ? await file.json() : undefined;
3675
- const name = pkg?.name ?? basename2(dir ?? process18.cwd());
3676
- const where = dir === undefined ? "" : ` in ${relative(process18.cwd(), dir) || "."}`;
3763
+ const name = pkg?.name ?? basename2(dir ?? process19.cwd());
3764
+ const where = dir === undefined ? "" : ` in ${relative(process19.cwd(), dir) || "."}`;
3677
3765
  const sha = base.slice(0, 7);
3678
3766
  let version;
3679
3767
  let bump;
@@ -3686,7 +3774,7 @@ async function printPlan({ level, dir, prefix, branch, base, timeout, registry }
3686
3774
  console.log(`would release ${name} ${version}, tagged ${tag}`);
3687
3775
  } else {
3688
3776
  if (pkg?.version === undefined)
3689
- fail(`no version in ${join6(dir ?? ".", "package.json")}`);
3777
+ fail(`no version in ${join7(dir ?? ".", "package.json")}`);
3690
3778
  version = nextVersion(pkg.version, level);
3691
3779
  bump = dir === undefined ? `bun pm version ${level}, which commits and tags ${prefix}${version}` : `bun pm version ${level} --no-git-tag-version${where}, then commit it with bun.lock refreshed and tag ${prefix}${version}`;
3692
3780
  console.log(`would release ${name} ${pkg.version} -> ${version}, tagged ${prefix}${version}`);
@@ -3706,25 +3794,25 @@ async function printPlan({ level, dir, prefix, branch, base, timeout, registry }
3706
3794
  }
3707
3795
 
3708
3796
  // src/commands/release-steps.ts
3709
- import { join as join7, relative as relative2, resolve } from "path";
3797
+ import { join as join8, relative as relative2, resolve } from "path";
3710
3798
  var {$ } = globalThis.Bun;
3711
3799
  var registryWait = { tries: 12, poll: 5000 };
3712
3800
  var REGISTRY_INFO_MS = 30000;
3713
3801
  async function packageDir(value) {
3714
- for (const dir of [resolve(value), join7(await git(["rev-parse", "--show-toplevel"]), "packages", value)]) {
3715
- if (await Bun.file(join7(dir, "package.json")).exists())
3802
+ for (const dir of [resolve(value), join8(await git(["rev-parse", "--show-toplevel"]), "packages", value)]) {
3803
+ if (await Bun.file(join8(dir, "package.json")).exists())
3716
3804
  return dir;
3717
3805
  }
3718
3806
  fail(`no package.json in ${value} nor in packages/${value}`);
3719
3807
  }
3720
3808
  async function commitPackageBump(dir, prefix) {
3721
- const { version } = await Bun.file(join7(dir, "package.json")).json();
3809
+ const { version } = await Bun.file(join8(dir, "package.json")).json();
3722
3810
  const root = await git(["rev-parse", "--show-toplevel"]);
3723
3811
  const tag = `${prefix}${version}`;
3724
3812
  const lock = await $`bun install --lockfile-only`.cwd(root).quiet().nothrow();
3725
3813
  if (lock.exitCode !== 0)
3726
3814
  fail(`bun install --lockfile-only after the bump: ${lock.stderr.toString().trim()}`);
3727
- await git(["add", join7(dir, "package.json"), join7(root, "bun.lock")]);
3815
+ await git(["add", join8(dir, "package.json"), join8(root, "bun.lock")]);
3728
3816
  await git(["commit", "-q", "-m", tag]);
3729
3817
  await git(["tag", "-a", tag, "-m", tag]);
3730
3818
  }
@@ -3753,11 +3841,11 @@ async function rollBack(preBump, tagsBefore, dir) {
3753
3841
  await git(["reset", "--keep", preBump]);
3754
3842
  if (dir !== undefined) {
3755
3843
  const root = await git(["rev-parse", "--show-toplevel"]);
3756
- const paths = [join7(dir, "package.json"), join7(root, "bun.lock")].map((path) => relative2(root, path));
3844
+ const paths = [join8(dir, "package.json"), join8(root, "bun.lock")].map((path) => relative2(root, path));
3757
3845
  const tracked = (await git(["ls-tree", "-r", "--full-tree", "--name-only", preBump, "--", ...paths])).split(`
3758
3846
  `).filter((path) => path !== "");
3759
3847
  if (tracked.length > 0)
3760
- await git(["restore", `--source=${preBump}`, "--staged", "--worktree", "--", ...tracked.map((path) => join7(root, path))]);
3848
+ await git(["restore", `--source=${preBump}`, "--staged", "--worktree", "--", ...tracked.map((path) => join8(root, path))]);
3761
3849
  }
3762
3850
  } catch (error) {
3763
3851
  warn(`could not undo the bump, back to ${preBump.slice(0, 7)} by hand`, error instanceof Error ? error.message : String(error));
@@ -3914,7 +4002,7 @@ ${push.stderr.trim()}`, signal === undefined ? 1 : exitCodeOf(null, signal));
3914
4002
  if (!pushed && !isPushUnknown && level !== undefined)
3915
4003
  await rollBack(preBump, tagsBefore, dir);
3916
4004
  }
3917
- const file = Bun.file(join8(dir ?? ".", "package.json"));
4005
+ const file = Bun.file(join9(dir ?? ".", "package.json"));
3918
4006
  const pkg = await file.exists() ? await file.json() : undefined;
3919
4007
  const version = tag.slice(prefix.length);
3920
4008
  let followed = { code: 0, runs: [], failedJobs: [] };
@@ -3932,7 +4020,7 @@ ${push.stderr.trim()}`, signal === undefined ? 1 : exitCodeOf(null, signal));
3932
4020
  if (late !== undefined)
3933
4021
  fail(`interrupted by ${late} after ${tag} was pushed: its runs are not waited for`, exitCodeOf(null, late));
3934
4022
  say(`pushed ${branch} and ${tag}, waiting for their runs`);
3935
- followed = await followRuns((runs) => runs.filter((run) => run.id > before && run.commit_sha === head), timeout, undefined, NO_RUN2, json ? process19.stderr : process19.stdout, { json });
4023
+ followed = await followRuns((runs) => runs.filter((run) => run.id > before && run.commit_sha === head), timeout, undefined, NO_RUN2, json ? process20.stderr : process20.stdout, { json });
3936
4024
  if (followed.code !== 0) {
3937
4025
  outcome(false);
3938
4026
  return followed.code;
@@ -3985,40 +4073,6 @@ async function loadSettings(home) {
3985
4073
  return typeof host === "string" && host !== "" ? { remoteHost: host } : {};
3986
4074
  }
3987
4075
 
3988
- // src/remote.ts
3989
- var SCP_LIKE = /^(?:([^@/]+)@)?([^:/]+):(?!\/)(.+?)(?:\.git)?\/?$/;
3990
- var SSH_URL = /^ssh:\/\/(?:([^@/]+)@)?([^:/]+)(?::(\d+))?\/(.+?)(?:\.git)?\/?$/;
3991
- var USERINFO = /^([a-z][a-z\d+.-]*:\/\/)[^@/]*@/i;
3992
- var DEFAULT_PORT = "22";
3993
- function parseSshRemote(url) {
3994
- const ssh = SSH_URL.exec(url);
3995
- const scp = ssh === null && !url.includes("://") ? SCP_LIKE.exec(url) : null;
3996
- const match = ssh ?? scp;
3997
- if (match === null || match[2]?.startsWith("-") === true)
3998
- return;
3999
- const login = match[1] ?? "git";
4000
- const user = login.includes(":") ? login.slice(0, login.indexOf(":")) : login;
4001
- if (ssh === null)
4002
- return { user, host: match[2] ?? "", path: match[3] ?? "" };
4003
- return { user, host: match[2] ?? "", path: match[4] ?? "", ...ssh[3] === undefined ? {} : { port: ssh[3] } };
4004
- }
4005
- function redactUrl(url) {
4006
- return url.replace(USERINFO, "$1");
4007
- }
4008
- function canonicalRemote(url, host, reaches) {
4009
- const remote = parseSshRemote(url);
4010
- if (remote === undefined || remote.host === host || !reaches(remote.host))
4011
- return;
4012
- if (remote.port !== undefined && remote.port !== DEFAULT_PORT)
4013
- return `ssh://${remote.user}@${host}:${remote.port}/${remote.path}.git`;
4014
- return `${remote.user}@${host}:${remote.path}.git`;
4015
- }
4016
- async function reachesHost(hostname, host, hostAddresses, addressesOf) {
4017
- if (hostname === undefined)
4018
- return false;
4019
- return hostname === host || (await addressesOf(hostname)).some((address) => hostAddresses.has(address));
4020
- }
4021
-
4022
4076
  // src/commands/remote.ts
4023
4077
  var usage7 = `ft remote [--fix]
4024
4078
 
@@ -4056,7 +4110,7 @@ async function restoreOrigin(fetchUrl, pushUrls, changes) {
4056
4110
  await git(["remote", "set-url", "--push", "origin", from, to]);
4057
4111
  }
4058
4112
  }
4059
- function originHost(url) {
4113
+ function originHost2(url) {
4060
4114
  return parseSshRemote(url)?.host ?? (URL.canParse(url) ? new URL(url).hostname : undefined);
4061
4115
  }
4062
4116
  async function remote(args, home) {
@@ -4083,7 +4137,7 @@ async function remote(args, home) {
4083
4137
  }
4084
4138
  const changes = [fetchUrl, ...pushUrls].map((url) => [url, canonicalRemote(url, host, (alias) => reaching.has(alias))]).filter((change) => change[1] !== undefined);
4085
4139
  if (changes.length === 0) {
4086
- const named = originHost(fetchUrl);
4140
+ const named = originHost2(fetchUrl);
4087
4141
  if (named === host) {
4088
4142
  console.log(`origin already names ${host}: ${redactUrl(fetchUrl)}`);
4089
4143
  return 0;
@@ -4161,7 +4215,7 @@ async function runs(args) {
4161
4215
  }
4162
4216
 
4163
4217
  // src/cli.ts
4164
- process20.exitCode = await runCli({
4218
+ process21.exitCode = await runCli({
4165
4219
  name: "ft",
4166
4220
  about: "Forgejo tools over tea, run from a repository whose origin is on Forgejo",
4167
4221
  replaces: "tea api calls piped through jq, and sleep loops waiting on CI",
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kaynooo/cli-forgejo",
3
3
  "type": "module",
4
- "version": "0.15.1",
4
+ "version": "0.15.2",
5
5
  "description": "ft: Forgejo CI runs, logs, releases and issue checklists from the terminal, through tea.",
6
6
  "author": "Kaynooo <contact@kaynooo.fr>",
7
7
  "license": "MIT",
@@ -28,6 +28,6 @@
28
28
  "ft": "$npm_execpath src/cli.ts"
29
29
  },
30
30
  "devDependencies": {
31
- "@kaynooo/cli-kit": "0.21.0"
31
+ "@kaynooo/cli-kit": "0.21.1"
32
32
  }
33
33
  }