enrich method
Returns current enriched with GitHub metadata.
Implementation
@override
/// Returns [current] enriched with GitHub metadata.
Future<PackageHealth> enrich(
PackageHealth current, DepSherpaConfig config) async {
final String? repositoryUrl = current.repositoryUrl;
if (!config.network.enabled ||
config.network.offline ||
repositoryUrl == null ||
!repositoryUrl.contains('github.com/')) {
return current;
}
final _GithubRepoRef? ref = _GithubRepoRef.parse(repositoryUrl);
if (ref == null) {
return current;
}
final Duration ttl = Duration(hours: config.network.cacheTtlHours);
final String repoKey = '${ref.owner}_${ref.repo}';
String? repoPayload;
String? contributorsPayload;
if (config.network.useCache) {
repoPayload =
_cacheService.read(_cacheDir, 'github', '${repoKey}_repo', ttl: ttl);
contributorsPayload = _cacheService.read(
_cacheDir,
'github',
'${repoKey}_contributors',
ttl: ttl,
);
}
final Map<String, String> headers = <String, String>{
'accept': 'application/vnd.github+json',
if (Platform.environment[config.network.githubTokenEnv]?.isNotEmpty ==
true)
'authorization':
'Bearer ${Platform.environment[config.network.githubTokenEnv]}',
};
try {
repoPayload ??= await _fetchBody(
Uri.https('api.github.com', '/repos/${ref.owner}/${ref.repo}'),
config,
headers,
);
contributorsPayload ??= await _fetchBody(
Uri.https(
'api.github.com',
'/repos/${ref.owner}/${ref.repo}/contributors',
<String, String>{'per_page': '100'},
),
config,
headers,
);
} catch (_) {
return current;
}
if (config.network.useCache) {
_cacheService.write(_cacheDir, 'github', '${repoKey}_repo', repoPayload);
_cacheService.write(
_cacheDir,
'github',
'${repoKey}_contributors',
contributorsPayload,
);
}
final Map<String, dynamic> repoMap =
jsonDecode(repoPayload) as Map<String, dynamic>;
final List<dynamic> contributors =
jsonDecode(contributorsPayload) as List<dynamic>;
return current.merge(
PackageHealth(
githubOrg: ref.owner,
stars: (repoMap['stargazers_count'] as num?)?.toInt(),
contributors: contributors.length,
lastCommitAt: repoMap['pushed_at'] == null
? null
: DateTime.parse(repoMap['pushed_at'] as String).toUtc(),
openIssues: (repoMap['open_issues_count'] as num?)?.toInt(),
),
);
}