$sourceType, 'issue_category' => $issueCategory, 'issue_subtype' => $issueSubtype, 'environment_tags' => (function () use ($title) { // Extract each [bracketed] tag, then keep the known environment ones. // (explode('][') leaves brackets stuck to the first/last tag, so e.g. // "[production] ..." never matched and the env tag was dropped from the // dedup hash — letting prod and staging issues collide onto one ticket.) preg_match_all('/\[([^\]]+)\]/', $title, $m); return array_values(array_filter( $m[1], fn($tag) => in_array($tag, ['production', 'development', 'staging', 'single-node', 'cluster-wide'], true) )); })(), ]; // Manual tickets should be unique by title (so different software installs don't collide) if ($sourceType === 'manual') { $stableComponents['title'] = $title; } // Include hostname for node-specific issues if (!$isClusterWide) { $stableComponents['hostname'] = $hostname; } // Include drive identifier for drive-specific tickets. // Use serial when available (stable across reboots/reshuffles); fall back to // device path for tickets created before serial was added to the payload. if ($isDriveTicket) { $stableComponents['drive'] = $serial ?? ($deviceMatches[0] ?? ''); } sort($stableComponents['environment_tags']); return $stableComponents; } /** * Format of a reporter-supplied dedup_key: scope:subject:kind, e.g. * host:compute-storage-01:oom or cluster:ceph:BLUESTORE_SLOW_OP_ALERT. * Upper case is allowed for codes like Ceph's; keys are case-sensitive. */ public const KEY_PATTERN = '/^[A-Za-z0-9_.:-]{1,128}$/'; public static function isValidKey($key): bool { return is_string($key) && preg_match(self::KEY_PATTERN, $key) === 1; } /** The request's dedup_key, or null when it sent none. Validate with isValidKey() first. */ public static function key(array $data): ?string { $key = $data['dedup_key'] ?? null; return ($key === null || $key === '') ? null : (string)$key; } /** Dedup hash for an explicit key. The "key:" prefix keeps it apart from every legacy hash. */ public static function forKey(string $key): string { return hash('sha256', 'key:' . $key); } /** * Dedup hash for a create request. With a dedup_key the reporter decides * what "the same problem" is and the title is not looked at (#123); * without one, the stable components of the title decide, as before. */ public static function generate(array $data): string { $key = self::key($data); if ($key !== null) { return self::forKey($key); } return hash('sha256', json_encode(self::components($data), JSON_UNESCAPED_SLASHES)); } }