readTicketDescription function

String? readTicketDescription(
  1. Directory ticketDir
)

Reads the ticket description from <ticketDir>/ticket.json, or returns null when the file is missing, is not a JSON object, is malformed, or carries an empty description.

The description is the human-written summary of the ticket and therefore the natural default for the messages gg writes on the user's behalf — in particular for the commit that saves pending user changes before a bookkeeping commit.

Implementation

String? readTicketDescription(Directory ticketDir) {
  final file = File(path.join(ticketDir.path, 'ticket.json'));
  if (!file.existsSync()) {
    return null;
  }

  final dynamic decoded;
  try {
    decoded = jsonDecode(file.readAsStringSync());
  } catch (_) {
    // A hand-edited / truncated ticket.json must not crash the caller.
    return null;
  }
  if (decoded is! Map<String, dynamic>) {
    return null;
  }

  final description = decoded['description']?.toString().trim();
  if (description == null || description.isEmpty) {
    return null;
  }

  return description;
}