fix(firehose): skip changelog and breaking changes checks for unpublished packages (#420)
diff --git a/pkgs/firehose/CHANGELOG.md b/pkgs/firehose/CHANGELOG.md
index fde639b..ba2a593 100644
--- a/pkgs/firehose/CHANGELOG.md
+++ b/pkgs/firehose/CHANGELOG.md
@@ -1,5 +1,6 @@
 ## 0.13.2-wip
 
+- Skip changelog and breaking changes checks for unpublished packages.
 - Echo any error output from dependencies task on failure
   (avoids silent failures).
 - Give clear output when packages need a changelog update.
diff --git a/pkgs/firehose/lib/src/health/changelog.dart b/pkgs/firehose/lib/src/health/changelog.dart
index 2515e01..4a389af 100644
--- a/pkgs/firehose/lib/src/health/changelog.dart
+++ b/pkgs/firehose/lib/src/health/changelog.dart
@@ -7,21 +7,43 @@
 import 'package:glob/glob.dart';
 import 'package:path/path.dart' as path;
 
+import 'package:pool/pool.dart';
+
 import '../github.dart';
 import '../repo.dart';
 
 Future<Map<Package, List<GitFile>>> packagesWithoutChangelog(
   GithubApi github,
   List<Glob> ignored,
-  Directory directory,
-) async {
+  Directory directory, {
+  required Future<bool> Function(Package) isPublished,
+}) async {
   final repo = Repository(directory);
   final packages = repo.locatePackages(ignore: ignored);
   final files = await github.listFilesForPR(directory, ignored);
 
+  final pool = Pool(10);
+  final publishedResults = await pool.forEach<Package, (Package, bool)>(
+    packages,
+    (package) async {
+      final published = await isPublished(package);
+      return (package, published);
+    },
+  ).toList();
+
+  final publishedPackages = <Package>[];
+  for (final (package, published) in publishedResults) {
+    if (published) {
+      publishedPackages.add(package);
+    } else {
+      print('Package ${package.name} is not published yet. '
+          'Skipping changelog check.');
+    }
+  }
+
   final packagesWithoutChangedChangelog =
       collectPackagesWithoutChangelogChanges(
-    packages,
+    publishedPackages,
     files,
     directory,
   );
diff --git a/pkgs/firehose/lib/src/health/health.dart b/pkgs/firehose/lib/src/health/health.dart
index ad7eb8f..791784f 100644
--- a/pkgs/firehose/lib/src/health/health.dart
+++ b/pkgs/firehose/lib/src/health/health.dart
@@ -9,9 +9,11 @@
 import 'package:collection/collection.dart';
 import 'package:glob/glob.dart';
 import 'package:path/path.dart' as path;
+import 'package:pool/pool.dart';
 import 'package:pub_semver/pub_semver.dart';
 
 import '../../firehose.dart';
+import '../pub.dart';
 import '../utils.dart';
 import 'changelog.dart';
 import 'coverage.dart';
@@ -251,7 +253,28 @@
     final flutterPackages =
         packagesContaining(filesInPR, only: flutterPackageGlobs);
     log('This list of Flutter packages is $flutterPackages');
-    for (final package in packagesContaining(filesInPR, ignore: ignored)) {
+
+    final pool = Pool(10);
+    final packages = packagesContaining(filesInPR, ignore: ignored);
+    final publishedStatuses = await pool.forEach<Package, (Package, bool)>(
+      packages,
+      (package) async {
+        final published = await isPublished(package);
+        return (package, published);
+      },
+    ).toList();
+
+    final packagesToCheck = <Package>[];
+    for (final (package, published) in publishedStatuses) {
+      if (published) {
+        packagesToCheck.add(package);
+      } else {
+        log('Package ${package.name} is not published yet. '
+            'Skipping breaking changes check.');
+      }
+    }
+
+    for (final package in packagesToCheck) {
       log('Look for changes in $package');
       final absolutePath = package.directory.absolute.path;
       final tempDirectory = Directory.systemTemp.createTempSync();
@@ -332,6 +355,15 @@
 
   String getCurrentVersionOfPackage(Package package) => 'pub://${package.name}';
 
+  Future<bool> isPublished(Package package) async {
+    final pub = Pub();
+    try {
+      return await pub.isPublished(package.name);
+    } finally {
+      pub.close();
+    }
+  }
+
   (ProcessResult, String, String) runDashProcess(
     List<Package> flutterPackages,
     Package package,
@@ -514,6 +546,7 @@
       github,
       ignored,
       directory,
+      isPublished: isPublished,
     );
 
     final markdownResult = '''
diff --git a/pkgs/firehose/lib/src/pub.dart b/pkgs/firehose/lib/src/pub.dart
index 0a83599..aa1d699 100644
--- a/pkgs/firehose/lib/src/pub.dart
+++ b/pkgs/firehose/lib/src/pub.dart
@@ -13,7 +13,7 @@
   http.Client get httpClient => _httpClient ??= http.Client();
 
   Future<bool> hasPublishedVersion(String name, String version) async {
-    final uri = Uri.parse('https://pub.dev/api/packages/$name');
+    final uri = Uri.https('pub.dev', 'api/packages/$name');
     final response = await getCall(uri, retries: 3);
     if (response.statusCode != 200) {
       return false;
@@ -26,6 +26,12 @@
         .contains(version);
   }
 
+  Future<bool> isPublished(String name) async {
+    final uri = Uri.https('pub.dev', 'api/packages/$name');
+    final response = await getCall(uri, retries: 3);
+    return response.statusCode == 200;
+  }
+
   Future<http.Response> getCall(Uri uri, {required int retries}) async {
     for (var i = 0; i < retries + 1; i++) {
       try {
diff --git a/pkgs/firehose/pubspec.yaml b/pkgs/firehose/pubspec.yaml
index 24d2f09..b6622c0 100644
--- a/pkgs/firehose/pubspec.yaml
+++ b/pkgs/firehose/pubspec.yaml
@@ -17,6 +17,7 @@
   glob: ^2.1.2
   http: ^1.0.0
   path: ^1.8.0
+  pool: ^1.5.2
   pub_semver: ^2.1.0
   pubspec_parse: ^1.2.3
   yaml: ^3.1.0
diff --git a/pkgs/firehose/test/health_test.dart b/pkgs/firehose/test/health_test.dart
index 3a21d61..badeb65 100644
--- a/pkgs/firehose/test/health_test.dart
+++ b/pkgs/firehose/test/health_test.dart
@@ -173,4 +173,7 @@
   @override
   String getCurrentVersionOfPackage(Package package) =>
       p.join('../base_test_repo/pkgs', package.name);
+
+  @override
+  Future<bool> isPublished(Package package) async => true;
 }
diff --git a/pkgs/firehose/test/pub_test.dart b/pkgs/firehose/test/pub_test.dart
index a40c480..e315942 100644
--- a/pkgs/firehose/test/pub_test.dart
+++ b/pkgs/firehose/test/pub_test.dart
@@ -36,6 +36,16 @@
           'foo_bar_not_published_package', '1.8.0');
       expect(result, false);
     });
+
+    test('package is published', () async {
+      final result = await pub.isPublished('path');
+      expect(result, true);
+    });
+
+    test('package is not published', () async {
+      final result = await pub.isPublished('foo_bar_not_published_package');
+      expect(result, false);
+    });
   });
 
   group('VersionExtension', () {