fetchRepoHealth method

Future<RepoHealth?> fetchRepoHealth(
  1. Uri repoUrl
)

Fetches RepoHealth for the GitHub repository at repoUrl.

Returns null when repoUrl is not a GitHub URL, when the request fails, or when the response cannot be parsed.

Implementation

Future<RepoHealth?> fetchRepoHealth(Uri repoUrl) async {
  final owner = _owner(repoUrl);
  final repo = _repo(repoUrl);
  if (owner == null || repo == null) return null;

  final cacheKey = 'gh:health:$owner/$repo';
  final hit = await _cache?.get(cacheKey);
  if (hit != null) return _parseHealth(hit);

  final results = await Future.wait([
    _get('/repos/$owner/$repo'),
    _get('/repos/$owner/$repo/commits?per_page=1'),
    _get('/repos/$owner/$repo/issues?state=closed&per_page=100&sort=updated'),
    _get('/repos/$owner/$repo/contributors?per_page=1&anon=false'),
  ]);

  final repoData = results[0];
  if (repoData == null) return null;

  final blob = {
    'repo': repoData,
    'commits': results[1],
    'closed_issues': results[2],
    'contributors': results[3],
  };

  await _cache?.set(cacheKey, blob, ttl: const Duration(hours: 12));
  return _parseHealth(blob);
}