@sayknow-cli/coding-agent 0.7.8 → 0.7.10

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/src/utils/git.ts CHANGED
@@ -1607,3 +1607,131 @@ export const github = {
1607
1607
  return result.stdout;
1608
1608
  },
1609
1609
  };
1610
+
1611
+ // ════════════════════════════════════════════════════════════════════════════
1612
+ // API: gitlab (GitLab CLI)
1613
+ // ════════════════════════════════════════════════════════════════════════════
1614
+
1615
+ export interface GlabCommandOptions {
1616
+ repoProvided?: boolean;
1617
+ }
1618
+
1619
+ /** Collapse glab's boxed, ANSI-colored error banner into one plain line. */
1620
+ export function cleanGlabMessage(text: string): string {
1621
+ return text
1622
+ .replace(/\u001b\[[0-9;?]*[A-Za-z]/g, "")
1623
+ .split("\n")
1624
+ .map(line => line.trim())
1625
+ .filter(
1626
+ line =>
1627
+ line.length > 0 &&
1628
+ line !== "ERROR" &&
1629
+ !line.startsWith("DEPRECATION WARNING") &&
1630
+ !/^A new version of glab|^https:\/\/gitlab\.com\/gitlab-org\/cli\/-\/releases/.test(line),
1631
+ )
1632
+ .map(line => line.replace(/^X\s+/, ""))
1633
+ .join(" ")
1634
+ .trim();
1635
+ }
1636
+
1637
+ export function formatGlabFailure(
1638
+ args: readonly string[],
1639
+ stdout: string,
1640
+ stderr: string,
1641
+ options?: GlabCommandOptions,
1642
+ ): string {
1643
+ const message = cleanGlabMessage(stderr || stdout);
1644
+ if (/401 Unauthorized|glab auth login|No token found|could not authenticate/i.test(message)) {
1645
+ return `GitLab CLI is not authenticated for this host. Run \`glab auth login\`. (${message})`;
1646
+ }
1647
+ if (
1648
+ !options?.repoProvided &&
1649
+ /not a git repository|no git remotes found|none of the git remotes configured/i.test(message)
1650
+ ) {
1651
+ return "GitLab repository context is unavailable. Pass `repo` explicitly or run the tool inside a GitLab checkout.";
1652
+ }
1653
+ if (message.length > 0) return message;
1654
+ return `GitLab CLI command failed: glab ${args.join(" ")}`;
1655
+ }
1656
+
1657
+ /**
1658
+ * Parse `glab --output json` stdout. glab prints deprecation/update notices on stdout ahead of the
1659
+ * payload in some versions, so parsing starts at the first line that opens a JSON value.
1660
+ */
1661
+ export function parseGlabJson<T>(stdout: string): T {
1662
+ const lines = stdout.split("\n");
1663
+ const start = lines.findIndex(line => /^\s*[[{]/.test(line));
1664
+ if (start < 0) {
1665
+ throw new ToolError(
1666
+ stdout.trim() ? "GitLab CLI returned invalid JSON output." : "GitLab CLI returned empty output.",
1667
+ );
1668
+ }
1669
+ try {
1670
+ return JSON.parse(lines.slice(start).join("\n")) as T;
1671
+ } catch {
1672
+ throw new ToolError("GitLab CLI returned invalid JSON output.");
1673
+ }
1674
+ }
1675
+
1676
+ export const gitlab = {
1677
+ /** Check if `glab` CLI is installed. */
1678
+ available(): boolean {
1679
+ return Boolean($which("glab"));
1680
+ },
1681
+
1682
+ /** Run a raw non-interactive `glab` command. Does not throw on non-zero exit. */
1683
+ async run(cwd: string, args: string[], signal?: AbortSignal): Promise<GhCommandResult> {
1684
+ throwIfAborted(signal);
1685
+ if (!$which("glab")) {
1686
+ throw new ToolError("GitLab CLI (glab) is not installed. Install it from https://gitlab.com/gitlab-org/cli.");
1687
+ }
1688
+ try {
1689
+ const child = Bun.spawn(["glab", ...args], {
1690
+ cwd,
1691
+ stdin: "ignore",
1692
+ stdout: "pipe",
1693
+ stderr: "pipe",
1694
+ windowsHide: true,
1695
+ signal,
1696
+ env: {
1697
+ ...process.env,
1698
+ NO_COLOR: "1",
1699
+ GLAB_NO_PROMPT: "1",
1700
+ GLAB_CHECK_UPDATE: "false",
1701
+ GLAB_SEND_TELEMETRY: "false",
1702
+ },
1703
+ });
1704
+ if (!child.stdout || !child.stderr) {
1705
+ throw new ToolError("Failed to capture GitLab CLI output.");
1706
+ }
1707
+ const [stdout, stderr, exitCode] = await Promise.all([
1708
+ new Response(child.stdout).text(),
1709
+ new Response(child.stderr).text(),
1710
+ child.exited,
1711
+ ]);
1712
+ throwIfAborted(signal);
1713
+ return { exitCode: exitCode ?? 0, stdout: stdout.trim(), stderr: stderr.trim() };
1714
+ } catch (error) {
1715
+ if (signal?.aborted) throw new ToolAbortError();
1716
+ throw error;
1717
+ }
1718
+ },
1719
+
1720
+ /** Run `glab` and parse stdout as JSON. Throws on non-zero exit or invalid JSON. */
1721
+ async json<T>(cwd: string, args: string[], signal?: AbortSignal, options?: GlabCommandOptions): Promise<T> {
1722
+ const result = await gitlab.run(cwd, args, signal);
1723
+ if (result.exitCode !== 0) {
1724
+ throw new ToolError(formatGlabFailure(args, result.stdout, result.stderr, options));
1725
+ }
1726
+ return parseGlabJson<T>(result.stdout);
1727
+ },
1728
+
1729
+ /** Run `glab` and return stdout as text. Throws on non-zero exit. */
1730
+ async text(cwd: string, args: string[], signal?: AbortSignal, options?: GlabCommandOptions): Promise<string> {
1731
+ const result = await gitlab.run(cwd, args, signal);
1732
+ if (result.exitCode !== 0) {
1733
+ throw new ToolError(formatGlabFailure(args, result.stdout, result.stderr, options));
1734
+ }
1735
+ return result.stdout;
1736
+ },
1737
+ };