107 lines
3.2 KiB
PHP
107 lines
3.2 KiB
PHP
#!/usr/bin/env php
|
|||
|
|
<?php
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Fill in tickets.source for tickets the API created before #123.
|
||
|
|
*
|
||
|
|
* Since #123, create_ticket_api.php records the API key name (gandalf,
|
||
|
|
* hwmonDaemon, thor) as the ticket's source. Older API tickets only show the
|
||
|
|
* "system" user. This script sets source where the reporter is certain and
|
||
|
|
* leaves the rest NULL:
|
||
|
|
*
|
||
|
|
* - hwmonDaemon: description uses its box-drawing layout
|
||
|
|
* - gandalf: [auto] title and Gandalf's own footer
|
||
|
|
* ("automatically by Gandalf" / "from the Gandalf monitor")
|
||
|
|
*
|
||
|
|
* Only tickets created by the system user with no source yet are touched.
|
||
|
|
* Each change writes an audit_log row. Safe to re-run.
|
||
|
|
*
|
||
|
|
* php scripts/backfill_ticket_source.php --dry-run # report only
|
||
|
|
* php scripts/backfill_ticket_source.php
|
||
|
|
*/
|
||
|
|
|
||
|
|
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__) . '/models/UserModel.php';
|
||
|
|
require_once dirname(__DIR__) . '/models/AuditLogModel.php';
|
||
|
|
|
||
|
|
$dryRun = in_array('--dry-run', $argv, true);
|
||
|
|
$conn = Database::getConnection();
|
||
|
|
|
||
|
|
$system = (new UserModel($conn))->getSystemUser();
|
||
|
|
if (!$system) {
|
||
|
|
fwrite(STDERR, "No system user found\n");
|
||
|
|
exit(1);
|
||
|
|
}
|
||
|
|
$systemId = (int)$system['user_id'];
|
||
|
|
|
||
|
|
/** The reporter a ticket certainly came from, or null. */
|
||
|
|
function detectSource(array $t): ?string
|
||
|
|
{
|
||
|
|
$title = (string)$t['title'];
|
||
|
|
$description = (string)$t['description'];
|
||
|
|
if (preg_match('/[┏┃┗━┣]/u', $description)) {
|
||
|
|
return 'hwmonDaemon';
|
||
|
|
}
|
||
|
|
if (
|
||
|
|
stripos($title, '[auto]') !== false
|
||
|
|
&& preg_match('/automatically by Gandalf|from the Gandalf monitor/i', $description)
|
||
|
|
) {
|
||
|
|
return 'gandalf';
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
$stmt = $conn->prepare(
|
||
|
|
"SELECT ticket_id, title, description FROM tickets WHERE created_by = ? AND source IS NULL ORDER BY ticket_id"
|
||
|
|
);
|
||
|
|
$stmt->bind_param('i', $systemId);
|
||
|
|
$stmt->execute();
|
||
|
|
$rows = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
|
||
|
|
$stmt->close();
|
||
|
|
|
||
|
|
$update = $conn->prepare("UPDATE tickets SET source = ? WHERE ticket_id = ? AND source IS NULL");
|
||
|
|
$audit = new AuditLogModel($conn);
|
||
|
|
$counts = [];
|
||
|
|
$unknown = [];
|
||
|
|
|
||
|
|
foreach ($rows as $row) {
|
||
|
|
$source = detectSource($row);
|
||
|
|
if ($source === null) {
|
||
|
|
$unknown[] = $row;
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
$counts[$source] = ($counts[$source] ?? 0) + 1;
|
||
|
|
if ($dryRun) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
$update->bind_param('ss', $source, $row['ticket_id']);
|
||
|
|
$update->execute();
|
||
|
|
if ($update->affected_rows > 0) {
|
||
|
|
$audit->log($systemId, 'update', 'ticket', $row['ticket_id'], [
|
||
|
|
'source' => ['from' => null, 'to' => $source],
|
||
|
|
'reason' => 'source backfilled from the description (tinker_tickets#123)',
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
foreach ($unknown as $row) {
|
||
|
|
echo 'left unset ' . $row['ticket_id'] . ' ' . mb_substr((string)$row['title'], 0, 100) . "\n";
|
||
|
|
}
|
||
|
|
ksort($counts);
|
||
|
|
$summary = [];
|
||
|
|
foreach ($counts as $source => $n) {
|
||
|
|
$summary[] = "$n $source";
|
||
|
|
}
|
||
|
|
printf(
|
||
|
|
"%s%s %s, %d left unset\n",
|
||
|
|
$dryRun ? '[dry run] ' : '',
|
||
|
|
$dryRun ? 'would set' : 'set',
|
||
|
|
$summary ? implode(', ', $summary) : 'none',
|
||
|
|
count($unknown)
|
||
|
|
);
|