From 08e6b3b1b7492e88d113cfc054926d34c26ae953 Mon Sep 17 00:00:00 2001
From: Steve Boyd <emteknetnz@gmail.com>
Date: Mon, 11 Jul 2022 11:44:28 +1200
Subject: [PATCH] FIX Hardcode some module versions to installer version

---
 consts.php               | 10 ++++++++++
 job_creator.php          | 19 ++++++++++++++-----
 tests/JobCreatorTest.php |  4 ++++
 3 files changed, 28 insertions(+), 5 deletions(-)

diff --git a/consts.php b/consts.php
index 3d7a86e..38706f0 100644
--- a/consts.php
+++ b/consts.php
@@ -106,3 +106,13 @@
         'silverstripe-installer' => '5',
     ],
 ];
+
+const INSTALLER_TO_REPO_MINOR_VERSIONS = [
+    '4.10' => [
+        'html5' => '2.3',
+        'silverstripe-elemental-bannerblock' => '2.4',
+        'silverstripe-session-manager' => '1.2',
+        'silverstripe-userforms' => '5.12',
+        'silverstripe-totp-authenticator' => '4.3',
+    ]
+];
diff --git a/job_creator.php b/job_creator.php
index 1263b21..91edac6 100644
--- a/job_creator.php
+++ b/job_creator.php
@@ -16,12 +16,13 @@ class JobCreator
     public function getInstallerVersion(): string
     {
         $repo = explode('/', $this->githubRepository)[1];
+        // repo should not use installer
         if (in_array($repo, NO_INSTALLER_LOCKSTEPPED_REPOS) || in_array($repo, NO_INSTALLER_UNLOCKSTEPPED_REPOS)) {
             return '';
         }
         $branch = $this->getCleanedBranch();
         $cmsMajor = $this->getCmsMajorFromBranch();
-        // module is a lockstepped repo
+        // repo is a lockstepped repo
         if (in_array($repo, LOCKSTEPPED_REPOS) && is_numeric($branch)) {
             // e.g. ['4', '11']
             $portions = explode('.', $branch);
@@ -31,11 +32,15 @@ public function getInstallerVersion(): string
                 return $cmsMajor . '.' . $portions[1] . '.x-dev';
             }
         }
-        // use the parent branch
-        if ($this->parentBranch && is_numeric($this->parentBranch)) {
-            return $this->parentBranch . '.x-dev';
+        // hardcoded installer version for repo version
+        foreach (array_keys(INSTALLER_TO_REPO_MINOR_VERSIONS) as $installerVersion) {
+            foreach (INSTALLER_TO_REPO_MINOR_VERSIONS[$installerVersion] as $_repo => $repoVersion) {
+                if ($repo === $_repo && $repoVersion === $branch) {
+                    return $installerVersion . '.x-dev';
+                }
+            }
         }
-        // use the latest minor version of installer
+        // fallback to use the latest minor version of installer
         $installerVersions = array_keys(INSTALLER_TO_PHP_VERSIONS);
         $installerVersions = array_filter($installerVersions, fn($version) => substr($version, 0, 1) === $cmsMajor);
         // remove major versions
@@ -119,6 +124,10 @@ private function getCleanedBranch(): string
         }
         // e.g. 4.10-release
         $branch = preg_replace('#^([0-9\.]+)-release$#', '$1', $branch);
+        // fallback to parent branch if availableable
+        if (!is_numeric($branch) && $this->parentBranch && is_numeric($this->parentBranch)) {
+            $branch = $this->parentBranch;
+        }
         return $branch;
     }
     
diff --git a/tests/JobCreatorTest.php b/tests/JobCreatorTest.php
index 435d507..95e027c 100644
--- a/tests/JobCreatorTest.php
+++ b/tests/JobCreatorTest.php
@@ -126,6 +126,10 @@ public function provideGetInstallerVersion(): array
             ['myaccount/silverstripe-tagfield', 'pulls/burger/myfeature', $latest],
             ['myaccount/silverstripe-tagfield', '2-release', $latest],
             ['myaccount/silverstripe-tagfield', '2.9-release', $latest],
+            // hardcoded repo version
+            ['myaccount/silverstripe-session-manager', '1', $latest],
+            ['myaccount/silverstripe-session-manager', '1.2', '4.10.x-dev'],
+            ['myaccount/silverstripe-session-manager', 'burger', $latest],
         ];
     }