All files / src/commands/issue merge.ts

98.75% Statements 79/80
88.23% Branches 45/51
100% Functions 22/22
98.52% Lines 67/68

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341                                                                            11x   11x   11x                                         8x 11x 8x 8x       1x             1x                           17x 37x                         37x 17x 1x 1x               35x 16x 1x           15x 15x                       33x 33x 15x 2x             13x                                                     13x 2x   11x           11x 11x 13x 13x 13x   22x         13x 14x                         5x 5x         2x   3x           2x       3x 4x 2x 2x       2x   4x         11x                                                                                         20x             20x   20x   3x     3x           17x 13x 21x       9x   20x 21x     20x     21x 9x 20x   20x 1x 1x           9x                
/**
 * sentry issue merge
 *
 * Merge 2+ issues into a single canonical group. Sentry's web UI has this
 * as a bulk action; here we expose it as a direct command.
 *
 * ## Flow
 *
 * 1. Collect 2+ issue args (variadic positional)
 * 2. Resolve each to a numeric group ID + org via `resolveIssue`
 * 3. Verify all issues are in the same org (cross-org merge rejected by API)
 * 4. Optionally pin the canonical parent via `--into`
 * 5. Call `mergeIssues(org, groupIds)` — the API auto-picks the parent
 *    unless we pre-sort with our chosen parent first
 * 6. Emit the merge result
 *
 * Sentry picks the parent by size (largest by event count). When `--into`
 * is provided we sort the selected issue to the front of the list — Sentry
 * honors first-in-list as the parent when multiple issues have equivalent
 * weight. See the Sentry source at `src/sentry/api/helpers/group_index`.
 */
 
import type { SentryContext } from "../../context.js";
import { type MergeIssuesResult, mergeIssues } from "../../lib/api-client.js";
import { buildCommand } from "../../lib/command.js";
import {
  ApiError,
  CliError,
  ResolutionError,
  ValidationError,
} from "../../lib/errors.js";
import { muted } from "../../lib/formatters/index.js";
import { CommandOutput } from "../../lib/formatters/output.js";
import { logger } from "../../lib/logger.js";
import { buildIssueUrl } from "../../lib/sentry-urls.js";
import type { SentryIssue } from "../../types/index.js";
import { resolveIssue } from "./utils.js";
 
const log = logger.withTag("issue.merge");
 
const COMMAND = "merge";
/** Full command path for error messages (this one isn't passed to resolveIssue). */
const COMMAND_PATH = "issue merge";
 
type MergeFlags = {
  readonly json: boolean;
  readonly fields?: string[];
  readonly into?: string;
};
 
/** Result emitted by the command — extends API result with display fields. */
type MergeCommandResult = {
  /** Org slug the merge was scoped to (all inputs must share an org). */
  org: string;
  /** Short ID of the canonical parent the children were merged into. */
  parentShortId: string;
  /** Short IDs of the merged children (excludes parent). */
  childShortIds: string[];
  /** Raw API response (numeric group IDs). */
  raw: MergeIssuesResult;
};
 
function formatMerged(result: MergeCommandResult): string {
  const head = `${muted("Merged")} ${result.childShortIds.length} issue(s) into ${result.parentShortId}`;
  const children = result.childShortIds.map((id) => `  • ${id}`).join("\n");
  const url = buildIssueUrl(result.org, result.raw.parent);
  return `${head}\n${children}\n\nSee: ${url}`;
}
 
function jsonTransform(result: MergeCommandResult): unknown {
  return {
    org: result.org,
    parent: {
      shortId: result.parentShortId,
      id: result.raw.parent,
      url: buildIssueUrl(result.org, result.raw.parent),
    },
    children: result.childShortIds.map((shortId, i) => ({
      shortId,
      id: result.raw.children[i] ?? "",
    })),
  };
}
 
/**
 * Resolve all issue args in parallel and validate they share an org.
 */
async function resolveAllIssues(
  args: readonly string[],
  cwd: string
): Promise<{ org: string; issues: SentryIssue[] }> {
  const resolved = await Promise.all(
    args.map((arg) =>
      resolveIssue({
        issueArg: arg,
        cwd,
        command: COMMAND,
      })
    )
  );
 
  // Every resolved issue must have a concrete org slug — otherwise we
  // can't safely decide whether a merge is cross-org. A bare numeric ID
  // that couldn't pick up an org from DSN/env/config would return
  // `org: undefined`; those must error before we proceed.
  const missingOrg = resolved.filter((r) => !r.org);
  if (missingOrg.length > 0) {
    const badIds = missingOrg.map((r) => r.issue.shortId).join(", ");
    throw new ValidationError(
      `Could not determine the organization for: ${badIds}.\n\n` +
        "Provide the org explicitly (e.g. <org>/<issue>) so the merge\n" +
        "can verify all issues belong to the same organization."
    );
  }
 
  // Collect the fully-resolved orgs and require a single one.
  const orgs = new Set(resolved.map((r) => r.org as string));
  if (orgs.size > 1) {
    throw new ValidationError(
      `Cannot merge issues across organizations (${Array.from(orgs).join(", ")}).\n\n` +
        "All issues must belong to the same organization."
    );
  }
 
  const [org] = orgs;
  Iif (!org) {
    // Unreachable — resolved.length >= 1 (callers guard for <2) and we
    // just asserted every entry has a non-empty org.
    throw new CliError("Internal error: resolved issue missing org slug.");
  }
 
  // Dedupe on resolved numeric ID: a user may pass the same issue in
  // multiple forms (`CLI-K9`, `my-org/CLI-K9`, `100`), all of which
  // collapse to the same group after resolution. Without this check we
  // would send `?id=100&id=100` to Sentry, which the API dedupes server
  // side — returning 204 ("no matching issues") — and then we re-throw
  // that as a confusing "no matching issues" error. Catch it here instead.
  const issues = resolved.map((r) => r.issue);
  const uniqueIds = new Set(issues.map((i) => i.id));
  if (uniqueIds.size < 2) {
    throw new ValidationError(
      `Merge needs at least 2 distinct issues (all inputs resolved to ${issues[0]?.shortId ?? "the same issue"}).\n\n` +
        "Check your argument list — you may have passed the same issue in\n" +
        "multiple forms (short ID + org-qualified + numeric all count as one)."
    );
  }
 
  return { org, issues };
}
 
/**
 * Sort issues so that the one matching `into` is first. Sentry's merge
 * endpoint picks the parent by size; pre-sorting nudges the tie-break
 * toward the caller's preference for typical cases.
 *
 * Accepts the same formats as the positional args — bare short ID,
 * numeric group ID, org-qualified short ID, or project-alias suffix
 * (`f-g`, `fr-a3`, etc). Aliases are resolved by running the input
 * through `resolveIssue` and comparing the resulting numeric ID against
 * the already-resolved issues.
 *
 * Fast path: try a direct string match first (avoids an API call when
 * the user passes the canonical form). Fall back to `resolveIssue` only
 * when the direct match misses, then look up by numeric ID.
 *
 * When `into` doesn't match any issue in the list, that's a user error —
 * we throw so the user can correct the mistake instead of silently getting
 * a different parent.
 */
async function orderForMerge(
  issues: SentryIssue[],
  into: string | undefined,
  cwd: string
): Promise<SentryIssue[]> {
  if (!into) {
    return issues;
  }
  const normalized = into.trim();
  // Fast path: direct match on shortId / id (bare or org-qualified form).
  // Short IDs are canonically uppercase (e.g. CLI-K9); users sometimes
  // type them in the case of the org/project part, so match
  // case-insensitively to avoid paying for the API fallback unnecessarily.
  // The Sentry API's `shortId` field is always uppercase.
  const lastSlash = normalized.lastIndexOf("/");
  const bare = lastSlash >= 0 ? normalized.slice(lastSlash + 1) : normalized;
  const bareUpper = bare.toUpperCase();
  const normalizedUpper = normalized.toUpperCase();
  const direct = issues.find(
    (i) =>
      i.shortId === bareUpper ||
      i.id === bare ||
      i.shortId === normalizedUpper ||
      i.id === normalized
  );
  if (direct) {
    return [direct, ...issues.filter((i) => i !== direct)];
  }
 
  // Fallback: resolve `into` through the same pipeline as positional args
  // (handles project-alias suffixes like `f-g`, URLs, @selectors, etc).
  // This adds a second API round trip in the alias-only case, but avoids
  // reimplementing the alias lookup logic here.
  //
  // Only a clean "not found" (ResolutionError, or ApiError with status 404)
  // is swallowed — real errors (auth, 5xx, network, ContextError) propagate
  // so the user sees a proper diagnostic instead of the misleading
  // "did not match any of the provided issues".
  let resolvedId: string | undefined;
  try {
    const { issue: resolvedIssue } = await resolveIssue({
      issueArg: normalized,
      cwd,
      command: COMMAND,
    });
    resolvedId = resolvedIssue.id;
  } catch (error) {
    if (
      error instanceof ResolutionError ||
      (error instanceof ApiError && error.status === 404)
    ) {
      // Clean not-found — fall through to the "not among provided" error.
    } else {
      throw error;
    }
  }
 
  if (resolvedId) {
    const match = issues.find((i) => i.id === resolvedId);
    if (match) {
      return [match, ...issues.filter((i) => i !== match)];
    }
  }
 
  throw new ValidationError(
    `--into '${into}' did not match any of the provided issues.\n\n` +
      `Provided: ${issues.map((i) => i.shortId).join(", ")}`,
    "into"
  );
}
 
export const mergeCommand = buildCommand({
  docs: {
    brief: "Merge 2+ issues into a single canonical group",
    fullDescription:
      "Consolidate multiple issues into one. Useful when the same logical\n" +
      "error was split into separate groups (e.g. by Sentry's default\n" +
      "stack-trace grouping before fingerprint rules were applied).\n\n" +
      "Sentry picks the canonical parent based on event count — typically\n" +
      "the largest group. --into is a preference, not a guarantee: if your\n" +
      "choice has fewer events, Sentry may still pick a different parent,\n" +
      "in which case a warning is printed to stderr.\n\n" +
      "All issues must belong to the same organization. Only error-type\n" +
      "issues can be merged (the API rejects performance/info issues).\n\n" +
      "Examples:\n" +
      "  sentry issue merge CLI-K9 CLI-15H CLI-15N\n" +
      "  sentry issue merge CLI-K9 CLI-15H --into CLI-K9\n" +
      "  sentry issue merge my-org/CLI-AB my-org/CLI-CD",
  },
  output: {
    human: formatMerged,
    jsonTransform,
  },
  parameters: {
    positional: {
      kind: "array",
      parameter: {
        placeholder: "issue",
        brief: "Issue IDs to merge (2 or more required)",
        parse: String,
      },
    },
    flags: {
      into: {
        kind: "parsed",
        parse: String,
        brief:
          "Prefer this issue as the canonical parent (must match one of the provided IDs)",
        optional: true,
      },
    },
    aliases: {
      i: "into",
    },
  },
  async *func(this: SentryContext, flags: MergeFlags, ...args: string[]) {
    const { cwd } = this;
 
    // Accept "sentry issue merge A --into B" as a valid 2-issue merge.
    // --into already designates which issue is the merge target, so passing
    // it as a positional too would be redundant. Append it to args so the
    // rest of the pipeline sees 2+ issues and orderForMerge puts it first.
    const effectiveArgs =
      args.length === 1 && flags.into ? [...args, flags.into] : args;
 
    if (effectiveArgs.length < 2) {
      const hint =
        args.length === 1
          ? "Tip: you can also write: sentry issue merge CLI-K9 --into CLI-15H"
          : "Example: sentry issue merge CLI-K9 CLI-15H CLI-15N";
      throw new ValidationError(
        `'sentry ${COMMAND_PATH}' needs at least 2 issue IDs (got ${args.length}).\n\n` +
          hint
      );
    }
 
    const { org, issues } = await resolveAllIssues(effectiveArgs, cwd);
    const ordered = await orderForMerge(issues, flags.into, cwd);
    const groupIds = ordered.map((i) => i.id);
    // `--into` is a preference, not a guarantee — track it so we can warn
    // if Sentry picks a different parent (typically the largest by event
    // count takes precedence over the requested ordering).
    const requestedParentId = flags.into ? groupIds[0] : undefined;
 
    log.debug(
      `Merging ${groupIds.length} issues in ${org}: ${ordered.map((i) => i.shortId).join(", ")}`
    );
 
    const raw = await mergeIssues(org, groupIds);
 
    // Map numeric IDs back to short IDs for display.
    const idToShort = new Map(ordered.map((i) => [i.id, i.shortId]));
    const parentShortId = idToShort.get(raw.parent) ?? raw.parent;
    const childShortIds = raw.children.map((id) => idToShort.get(id) ?? id);
 
    if (requestedParentId && requestedParentId !== raw.parent) {
      const requestedShortId = idToShort.get(requestedParentId) ?? flags.into;
      log.warn(
        `--into '${requestedShortId}' was a preference, not a guarantee. ` +
          `Sentry selected ${parentShortId} as the canonical parent based on event count.`
      );
    }
 
    yield new CommandOutput<MergeCommandResult>({
      org,
      parentShortId,
      childShortIds,
      raw,
    });
  },
});