uploadFile method

Future<void> uploadFile({
  1. required List<int> data,
  2. required String message,
  3. required String path,
})

Uploads a file to GitHub

Implementation

Future<void> uploadFile({
  required List<int> data,
  required String message,
  required String path,
}) async {
  await _loadData();

  String? sha;
  for (var entry in _tree.entries!) {
    if (path == entry.path) {
      sha = entry.sha;
      break;
    }
  }

  final url = '/repos/${slug.fullName}/contents/${_encodeGitPath(path)}';
  final body = json.encode({
    'branch': branch,
    'committer': _committer,
    'content': base64.encode(data),
    'message': message,
    if (sha != null) 'sha': sha
  });
  final response = await _github.putJSON(
    url,
    body: body,
    headers: {
      'content-type': 'application/json',
    },
  );

  if (response?['commit']?['sha'] == null) {
    throw Exception('[GITHUB UPLOAD]: Error -- [$response]');
  } else {
    _dirty = true;
    _logger.finest(
      '[GITHUB UPLOAD]: [$path] -- [${response?['commit']?['sha']}]',
    );
  }
}