Files
tinker_tickets/scripts/backfill_dedup_keys.php
T
jaredandClaude Opus 5.5 7061c40d4d
Lint / JS (eslint) (push) Successful in 14s
Lint / PHP (phpcs PSR-12) (push) Successful in 44s
Lint / PHP requirements (version + extensions) (push) Successful in 42s
Lint / Deploy (push) Skipped
Lint / Notify on failure (push) Skipped
Lint / PHP (phpcs PSR-12) (pull_request) Successful in 1m15s
Lint / JS (eslint) (pull_request) Successful in 34s
Lint / PHP requirements (version + extensions) (pull_request) Successful in 42s
Lint / Deploy (pull_request) Skipped
Lint / Notify on failure (pull_request) Skipped
Security / PHP Security (semgrep) (push) Successful in 4m13s
Security / PHP Security (semgrep) (pull_request) Successful in 3m25s
Ticket contract for automated reporters: dedup_key, source, links, reopen window (#123)
From the thor#1 plan: each reporter (gandalf, hwmonDaemon, thor) decides
what "the same problem" is, and Tinker enforces it.

- dedup_key on create_ticket_api.php (scope:subject:kind, A-Za-z0-9_.:-,
  case-sensitive). The hash becomes sha256("key:" + key) and the title is
  not looked at; an invalid key is a 400. Keyless callers hash exactly as
  before (checked over every prod title).
- Recurrence window for every reporter, keyed or not: a match on a ticket
  closed under AUTO_REOPEN_WINDOW_DAYS (7) ago reopens it as before
  (subject to the Workflow Designer); an older one gets a new ticket that
  takes over the dedup slot (tickets.hash stays UNIQUE, the old ticket
  gives it up), carries the key, records recurrence_of, is linked
  relates_to, and gets a history comment. action: new_recurrence.
- GET api/tickets_api.php?dedup_key= returns the newest ticket with that
  key plus its recurrence chain; ?source= filters the list.
- New api/ticket_link_api.php (link/unlink, read_write) through
  DependencyService::link()/unlink(), now shared with MCP
  link_tickets/unlink_tickets; audit rows record the key name.
- tickets.source records the API key name. Shown as the creator instead of
  "System" on the dashboard and ticket page, a Source filter in the
  sidebar, source in MCP results and a search_tickets filter. Comments the
  create API writes name the reporter instead of a hard-coded
  "hwmonDaemon", and system-linked comments display that name.
- Migration 008 (dedup_key, source, recurrence_of). Scripts:
  backfill_ticket_source.php (pre-#123 tickets, where certain) and
  backfill_dedup_keys.php (mapping file supplied by the app).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 20:22:06 -05:00

115 lines
4.1 KiB
PHP

#!/usr/bin/env php
<?php
/**
* Give existing tickets the dedup_key a reporter now sends (#123).
*
* When an app starts sending dedup_key, its next report hashes on the key
* instead of the title, so without this its open tickets would get
* duplicates. The app owns its key scheme, so it supplies the mapping as a
* JSON object of ticket_id => dedup_key:
*
* {"673679581": "cluster:ceph:OSDMAP_FLAGS", "629475198": "cluster:ceph:RECENT_CRASH"}
*
* php scripts/backfill_dedup_keys.php --map=thor-ceph.json --dry-run
* php scripts/backfill_dedup_keys.php --map=thor-ceph.json
*
* Each listed ticket gets dedup_key set and its hash moved to the key's hash.
* A ticket whose key hash already belongs to another ticket is skipped and
* reported, as is an invalid key or a missing ticket. Closed tickets are
* re-keyed too, with a warning (a report inside the reopen window will
* reopen them). Each change writes an audit_log row. Safe to re-run.
*/
if (php_sapi_name() !== 'cli') {
http_response_code(403);
exit('CLI access only');
}
require_once dirname(__DIR__) . '/config/config.php';
require_once dirname(__DIR__) . '/helpers/Database.php';
require_once dirname(__DIR__) . '/helpers/TicketHash.php';
require_once dirname(__DIR__) . '/models/UserModel.php';
require_once dirname(__DIR__) . '/models/AuditLogModel.php';
$dryRun = in_array('--dry-run', $argv, true);
$mapFile = null;
foreach ($argv as $arg) {
if (str_starts_with($arg, '--map=')) {
$mapFile = substr($arg, 6);
}
}
if ($mapFile === null || !is_readable($mapFile)) {
fwrite(STDERR, "usage: php scripts/backfill_dedup_keys.php --map=file.json [--dry-run]\n");
exit(2);
}
$map = json_decode((string)file_get_contents($mapFile), true);
if (!is_array($map) || array_is_list($map)) {
fwrite(STDERR, "$mapFile must be a JSON object of ticket_id => dedup_key\n");
exit(2);
}
$conn = Database::getConnection();
$system = (new UserModel($conn))->getSystemUser();
$actorId = $system ? (int)$system['user_id'] : null;
$audit = new AuditLogModel($conn);
$get = $conn->prepare("SELECT ticket_id, title, status, hash, dedup_key FROM tickets WHERE ticket_id = ?");
$holder = $conn->prepare("SELECT ticket_id FROM tickets WHERE hash = ? AND ticket_id <> ? LIMIT 1");
$update = $conn->prepare("UPDATE tickets SET dedup_key = ?, hash = ? WHERE ticket_id = ?");
$counts = ['updated' => 0, 'unchanged' => 0, 'skipped' => 0];
foreach ($map as $ticketId => $key) {
$ticketId = (string)$ticketId;
if (!TicketHash::isValidKey($key)) {
$counts['skipped']++;
echo "INVALID KEY $ticketId " . json_encode($key) . "\n";
continue;
}
$get->bind_param('s', $ticketId);
$get->execute();
$ticket = $get->get_result()->fetch_assoc();
if (!$ticket) {
$counts['skipped']++;
echo "NOT FOUND $ticketId\n";
continue;
}
$newHash = TicketHash::forKey($key);
if ($ticket['dedup_key'] === $key && $ticket['hash'] === $newHash) {
$counts['unchanged']++;
echo "unchanged $ticketId $key\n";
continue;
}
$holder->bind_param('ss', $newHash, $ticketId);
$holder->execute();
$other = $holder->get_result()->fetch_assoc();
if ($other) {
$counts['skipped']++;
echo "CONFLICT $ticketId $key (already the live ticket for that key: {$other['ticket_id']})\n";
continue;
}
$warn = $ticket['status'] === 'Closed' ? ' [Closed]' : '';
if (!$dryRun) {
$update->bind_param('sss', $key, $newHash, $ticketId);
$update->execute();
$audit->log($actorId, 'update', 'ticket', $ticketId, [
'dedup_key' => ['from' => $ticket['dedup_key'], 'to' => $key],
'reason' => 'dedup_key backfilled from ' . basename($mapFile) . ' (tinker_tickets#123)',
]);
}
$counts['updated']++;
echo ($dryRun ? 'would update' : 'updated ') . " $ticketId $key$warn " . mb_substr((string)$ticket['title'], 0, 80) . "\n";
}
printf(
"%s%d updated, %d already current, %d skipped\n",
$dryRun ? '[dry run] ' : '',
$counts['updated'],
$counts['unchanged'],
$counts['skipped']
);
exit($counts['skipped'] > 0 ? 1 : 0);