getRepositoryUrl method

Future<String?> getRepositoryUrl(
  1. String packageName
)

Get the repository URL for a package from its latest pubspec.

Checks the repository field first, then falls back to homepage. Returns null if neither is present or on failure.

Implementation

Future<String?> getRepositoryUrl(String packageName) async {
  try {
    final info = await getPackageInfo(packageName);
    final latest = info['latest'] as Map<String, dynamic>?;
    if (latest == null) return null;

    final pubspec = latest['pubspec'] as Map<String, dynamic>?;
    if (pubspec == null) return null;

    // Prefer the explicit repository field.
    final repository = pubspec['repository'] as String?;
    if (repository != null && repository.isNotEmpty) return repository;

    // Fall back to homepage if it looks like a repo URL.
    final homepage = pubspec['homepage'] as String?;
    if (homepage != null && homepage.contains('github.com')) return homepage;

    return null;
  } catch (e, stack) {
    Logger.error(
      'Error fetching repository URL for $packageName',
      e,
      stack,
    );
    return null;
  }
}