appendBuildHistory method

Future<void> appendBuildHistory({
  1. required String client,
  2. required String platform,
  3. required String type,
  4. required String? version,
  5. required bool success,
  6. required Duration duration,
  7. String? errorMessage,
  8. String? artifactPath,
  9. String? ref,
  10. String? commit,
})

Appends a build record to the project-local history file, keeping only the most recent _maxHistoryEntries. Newest entries are stored first.

Implementation

Future<void> appendBuildHistory({
  required String client,
  required String platform,
  required String type,
  required String? version,
  required bool success,
  required Duration duration,
  String? errorMessage,
  String? artifactPath,
  String? ref,
  String? commit,
}) async {
  final entry = {
    'timestamp': DateTime.now().toIso8601String(),
    'client': client,
    'platform': platform,
    'type': type,
    'version': version,
    'success': success,
    'durationSeconds': duration.inSeconds,
    if (errorMessage != null) 'error': errorMessage,
    if (artifactPath != null) 'artifact': artifactPath,
    if (ref != null) 'ref': ref,
    if (commit != null) 'commit': commit,
  };

  try {
    final history = await loadBuildHistory();
    history.insert(0, entry);

    if (history.length > _maxHistoryEntries) {
      history.removeRange(_maxHistoryEntries, history.length);
    }

    await File(_historyFilePath).writeAsString(
      const JsonEncoder.withIndent('  ').convert(history),
    );
  } catch (e) {
    // Never let history recording break a build.
    Logger.warning('Failed to record build history: $e');
  }
}