fix: More robust version check (#104)

* fix: More combust version check

* Switch to regex extraction

Reverted logic in firehose.dart
Updated latestVersion
Added a new latestHeading method
Added lots of test cases

* comment

* Version bump + remove prints
diff --git a/pkgs/firehose/CHANGELOG.md b/pkgs/firehose/CHANGELOG.md
index 6550057..40a47c0 100644
--- a/pkgs/firehose/CHANGELOG.md
+++ b/pkgs/firehose/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 0.3.16
+
+- More robust version checking, now more diverse changelog formats are accepted.
+
 ## 0.3.15
 
 - Make publish tags link to the new release page for that tag, with
diff --git a/pkgs/firehose/lib/src/changelog.dart b/pkgs/firehose/lib/src/changelog.dart
index 0e61df4..76b65ef 100644
--- a/pkgs/firehose/lib/src/changelog.dart
+++ b/pkgs/firehose/lib/src/changelog.dart
@@ -14,8 +14,27 @@
   bool get exists => file.existsSync();
 
   String? get latestVersion {
+    var input = latestHeading;
+
+    if (input == null) {
+      return null;
+    }
+
+    final versionRegex = RegExp(r'[0-9]+\.[0-9]+\.[0-9]+(\+[0-9]+)?');
+
+    var match = versionRegex.firstMatch(input);
+
+    if (match != null) {
+      var version = match.group(0);
+      return version;
+    }
+    return null;
+  }
+
+  String? get latestHeading {
     var sections = _parseSections();
-    return sections.firstOrNull?.title.substring(3).trim();
+    // Remove all leading "#"
+    return sections.firstOrNull?.title.replaceAll(RegExp(r'^#*'), '').trim();
   }
 
   List<String> get latestChangeEntries {
diff --git a/pkgs/firehose/pubspec.yaml b/pkgs/firehose/pubspec.yaml
index 9151609..42d6581 100644
--- a/pkgs/firehose/pubspec.yaml
+++ b/pkgs/firehose/pubspec.yaml
@@ -1,6 +1,6 @@
 name: firehose
 description: A tool to automate publishing of Pub packages from GitHub actions.
-version: 0.3.15
+version: 0.3.16
 repository: https://github.com/dart-lang/ecosystem/tree/main/pkgs/firehose
 
 environment:
diff --git a/pkgs/firehose/test/changelog_test.dart b/pkgs/firehose/test/changelog_test.dart
index 6ec0e8f..3b1cc00 100644
--- a/pkgs/firehose/test/changelog_test.dart
+++ b/pkgs/firehose/test/changelog_test.dart
@@ -17,11 +17,79 @@
       });
     });
 
+    test('latestHeading', () {
+      withChangelog(_defaultContents, (file) {
+        var changelog = Changelog(file);
+        var heading = changelog.latestHeading;
+        expect(heading, '0.3.7+1');
+      });
+    });
+
+    test('Custom heading extraction', () {
+      withChangelog('''
+## 1.2.3+4 is the new version ## :) 
+''', (file) {
+        var changelog = Changelog(file);
+        var heading = changelog.latestHeading;
+        expect(heading, '1.2.3+4 is the new version ## :)');
+      });
+    });
+
     test('latestVersion', () {
       withChangelog(_defaultContents, (file) {
         var changelog = Changelog(file);
         var version = changelog.latestVersion;
-        expect(version, isNotNull);
+        expect(version, '0.3.7+1');
+      });
+    });
+
+    test('on digit version + x', () {
+      withChangelog('''
+## 1.2.3+4
+''', (file) {
+        var changelog = Changelog(file);
+        var version = changelog.latestVersion;
+        expect(version, '1.2.3+4');
+      });
+    });
+
+    test('multi digit version + x', () {
+      withChangelog('''
+## 123.456.789+123456789
+''', (file) {
+        var changelog = Changelog(file);
+        var version = changelog.latestVersion;
+        expect(version, '123.456.789+123456789');
+      });
+    });
+
+    test('no "+ x" at the end', () {
+      withChangelog('''
+## 123.456.789
+''', (file) {
+        var changelog = Changelog(file);
+        var version = changelog.latestVersion;
+        expect(version, '123.456.789');
+      });
+    });
+
+    test('custom heading version', () {
+      withChangelog('''
+## [4.7.0](https://github.com/...) (2023-05-06)
+''', (file) {
+        var changelog = Changelog(file);
+        var version = changelog.latestVersion;
+        expect(version, '4.7.0');
+      });
+    });
+
+    test('multiple versions mentioned', () {
+      withChangelog('''
+## [4.7.0](https://github.com/.../.../compare/v4.6.0...v4.7.0) (25.05.23)
+''', (file) {
+        var changelog = Changelog(file);
+        var version = changelog.latestVersion;
+        expect(version, '4.7.0');
       });
     });