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
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>
125 lines
4.6 KiB
PHP
125 lines
4.6 KiB
PHP
<?php
|
|
|
|
/**
|
|
* ticket_link_api.php — Bearer-key endpoint to link or unlink two tickets (#123).
|
|
*
|
|
* POST only. Requires 'read_write' scope. Runs through
|
|
* DependencyService::link()/unlink(), the same code as the ticket page's
|
|
* Dependencies tab and the MCP link_tickets / unlink_tickets tools, so the
|
|
* checks (duplicates from either side, circular blocking chains) and audit
|
|
* entries match. Audit entries also record the API key's name.
|
|
*
|
|
* Body (JSON): {
|
|
* "ticket_id": "NNN" (required),
|
|
* "other_ticket_id": "NNN" (required),
|
|
* "relation": "blocks" | "blocked_by" | "relates_to" | "duplicates",
|
|
* "action": "link" (default) | "unlink"
|
|
* }
|
|
* Read it as "ticket_id <relation> other_ticket_id". Unlink also finds a link
|
|
* recorded from the other ticket's side.
|
|
*
|
|
* Responses: 200 {success, ticket_id, relation, other_ticket_id[, removed]};
|
|
* 400 bad input; 404 a ticket missing or not visible to this key, or no such
|
|
* link to remove; 409 already linked or a circular blocking chain.
|
|
*/
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
require_once dirname(__DIR__) . '/helpers/ErrorHandler.php';
|
|
ErrorHandler::init();
|
|
|
|
require_once dirname(__DIR__) . '/middleware/RateLimitMiddleware.php';
|
|
RateLimitMiddleware::apply('api');
|
|
|
|
require_once dirname(__DIR__) . '/config/config.php';
|
|
require_once dirname(__DIR__) . '/helpers/Database.php';
|
|
require_once dirname(__DIR__) . '/middleware/ApiKeyAuth.php';
|
|
require_once dirname(__DIR__) . '/models/TicketModel.php';
|
|
require_once dirname(__DIR__) . '/services/DependencyService.php';
|
|
|
|
try {
|
|
$conn = Database::getConnection();
|
|
} catch (Throwable $e) {
|
|
error_log('ticket_link_api: DB connection failed: ' . $e->getMessage());
|
|
http_response_code(500);
|
|
echo json_encode(['success' => false, 'error' => 'Internal server error']);
|
|
exit;
|
|
}
|
|
|
|
$apiKeyAuth = new ApiKeyAuth($conn);
|
|
|
|
try {
|
|
$systemUser = $apiKeyAuth->authenticate();
|
|
} catch (Exception $e) {
|
|
// ApiKeyAuth already sent the 401 response.
|
|
exit;
|
|
}
|
|
|
|
// Linking is a write: reject 'read' keys with 403 before any mutation.
|
|
$apiKeyAuth->requireScope('read_write');
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
http_response_code(405);
|
|
echo json_encode(['success' => false, 'error' => 'Method not allowed. Use POST.']);
|
|
exit;
|
|
}
|
|
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
if (!is_array($data)) {
|
|
http_response_code(400);
|
|
echo json_encode(['success' => false, 'error' => 'Invalid JSON body']);
|
|
exit;
|
|
}
|
|
|
|
$ticketId = trim((string)($data['ticket_id'] ?? ''));
|
|
$otherId = trim((string)($data['other_ticket_id'] ?? ''));
|
|
$relation = (string)($data['relation'] ?? '');
|
|
$action = strtolower(trim((string)($data['action'] ?? 'link')));
|
|
if ($ticketId === '' || $otherId === '' || $relation === '') {
|
|
http_response_code(400);
|
|
echo json_encode(['success' => false, 'error' => 'ticket_id, other_ticket_id and relation are required']);
|
|
exit;
|
|
}
|
|
if (!in_array($action, ['link', 'unlink'], true)) {
|
|
http_response_code(400);
|
|
echo json_encode(['success' => false, 'error' => 'action must be "link" or "unlink"']);
|
|
exit;
|
|
}
|
|
|
|
$context = $apiKeyAuth->getKeyContext();
|
|
$keyName = (string)($context['key_name'] ?? 'API');
|
|
|
|
// Keys see public tickets only unless marked see_all_visibility (#70), the
|
|
// same rule as tickets_api.php. Check that first with the same synthetic
|
|
// no-access user (user_id -1, so it can never match a creator or assignee),
|
|
// answering a hidden ticket exactly like a missing one.
|
|
if (empty($context['see_all_visibility'])) {
|
|
$ticketModel = new TicketModel($conn);
|
|
$visibilityUser = ['user_id' => -1, 'is_admin' => false, 'groups' => ''];
|
|
foreach ([$ticketId, $otherId] as $id) {
|
|
$ticket = ctype_digit($id) ? $ticketModel->getTicketById($id) : null;
|
|
if (!$ticket || !$ticketModel->canUserAccessTicket($ticket, $visibilityUser)) {
|
|
http_response_code(404);
|
|
echo json_encode(['success' => false, 'error' => "Ticket {$id} not found"]);
|
|
exit;
|
|
}
|
|
}
|
|
}
|
|
|
|
// The link is created and audited as the system user, like other API writes;
|
|
// past the check above the key may act on both tickets.
|
|
$actor = array_merge($systemUser, ['is_admin' => true]);
|
|
$auditExtra = ['api_key' => $keyName];
|
|
$result = $action === 'unlink'
|
|
? DependencyService::unlink($conn, $actor, $ticketId, $relation, $otherId, $auditExtra)
|
|
: DependencyService::link($conn, $actor, $ticketId, $relation, $otherId, $auditExtra);
|
|
|
|
if (empty($result['success'])) {
|
|
http_response_code((int)($result['http_status'] ?? 400));
|
|
echo json_encode(['success' => false, 'error' => $result['error'] ?? 'Request failed']);
|
|
exit;
|
|
}
|
|
|
|
echo json_encode($result);
|
|
exit;
|