From 5c07741447c8674529697060a1ffb6a4ccdcf5c6 Mon Sep 17 00:00:00 2001
From: Tricked <72335827+SkyBlockDev@users.noreply.github.com>
Date: Wed, 15 Jun 2022 16:38:07 +0200
Subject: [PATCH] feat: add branch support for github mods

---
 github/github.go  |  2 +-
 github/install.go | 16 +++++++++-------
 github/updater.go |  7 ++++---
 3 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/github/github.go b/github/github.go
index b63ccdb..a61c2ad 100644
--- a/github/github.go
+++ b/github/github.go
@@ -45,7 +45,7 @@ type ModReleases struct {
 	} `json:"author"`
 	NodeID          string  `json:"node_id"`
 	TagName         string  `json:"tag_name"`
-	TargetCommitish string  `json:"target_commitish"`
+	TargetCommitish string  `json:"target_commitish"` // The branch of the release
 	Name            string  `json:"name"`
 	Draft           bool    `json:"draft"`
 	Prerelease      bool    `json:"prerelease"`
diff --git a/github/install.go b/github/install.go
index 1ac9113..9a7aa2d 100644
--- a/github/install.go
+++ b/github/install.go
@@ -8,7 +8,6 @@ import (
 	"net/http"
 	"os"
 	"path/filepath"
-	"regexp"
 	"strings"
 	"time"
 
@@ -18,9 +17,6 @@ import (
 	"github.com/spf13/viper"
 )
 
-var modSiteRegex = regexp.MustCompile("modrinth\\.com/mod/([^/]+)/?$")
-var versionSiteRegex = regexp.MustCompile("modrinth\\.com/mod/([^/]+)/version/([^/]+)/?$")
-
 // installCmd represents the install command
 var installCmd = &cobra.Command{
 	Use:     "install [mod]",
@@ -134,7 +130,7 @@ func fetchMod(slug string) (Mod, error) {
 func installMod(mod Mod, pack core.Pack) error {
 	fmt.Printf("Found mod %s: '%s'.\n", mod.Title, mod.Description)
 
-	latestVersion, err := getLatestVersion(mod.Slug, pack)
+	latestVersion, err := getLatestVersion(mod.Slug, pack, "")
 	if err != nil {
 		return fmt.Errorf("failed to get latest version: %v", err)
 	}
@@ -145,7 +141,7 @@ func installMod(mod Mod, pack core.Pack) error {
 	return installVersion(mod, latestVersion, pack)
 }
 
-func getLatestVersion(slug string, pack core.Pack) (ModReleases, error) {
+func getLatestVersion(slug string, pack core.Pack, branch string) (ModReleases, error) {
 	var modReleases []ModReleases
 	var release ModReleases
 
@@ -171,6 +167,11 @@ func getLatestVersion(slug string, pack core.Pack) (ModReleases, error) {
 	if err != nil {
 		return release, err
 	}
+	for _, r := range modReleases {
+		if r.TargetCommitish == branch {
+			return r, nil
+		}
+	}
 
 	return modReleases[0], nil
 }
@@ -200,8 +201,9 @@ func installVersion(mod Mod, version ModReleases, pack core.Pack) error {
 	updateMap := make(map[string]map[string]interface{})
 
 	updateMap["github"], err = ghUpdateData{
-		ModID:            mod.ID,
+		ModID:            mod.Slug,
 		InstalledVersion: version.TagName,
+		Branch:           version.TargetCommitish,
 	}.ToMap()
 	if err != nil {
 		return err
diff --git a/github/updater.go b/github/updater.go
index 138b963..587d864 100644
--- a/github/updater.go
+++ b/github/updater.go
@@ -12,6 +12,7 @@ import (
 type ghUpdateData struct {
 	ModID            string `mapstructure:"mod-id"` // The slug of the repo but named modId for consistency reasons
 	InstalledVersion string `mapstructure:"version"`
+	Branch           string `mapstructure:"branch"`
 }
 
 type ghUpdater struct{}
@@ -31,7 +32,7 @@ func (u ghUpdater) CheckUpdate(mods []core.Mod, mcVersion string, pack core.Pack
 	results := make([]core.UpdateCheck, len(mods))
 
 	for i, mod := range mods {
-		rawData, ok := mod.GetParsedUpdateData("modrinth")
+		rawData, ok := mod.GetParsedUpdateData("github")
 		if !ok {
 			results[i] = core.UpdateCheck{Error: errors.New("couldn't parse mod data")}
 			continue
@@ -39,7 +40,7 @@ func (u ghUpdater) CheckUpdate(mods []core.Mod, mcVersion string, pack core.Pack
 
 		data := rawData.(ghUpdateData)
 
-		newVersion, err := getLatestVersion(data.ModID, pack)
+		newVersion, err := getLatestVersion(data.ModID, pack, data.Branch)
 		if err != nil {
 			results[i] = core.UpdateCheck{Error: fmt.Errorf("failed to get latest version: %v", err)}
 			continue
@@ -90,7 +91,7 @@ func (u ghUpdater) DoUpdate(mods []*core.Mod, cachedState []interface{}) error {
 			HashFormat: "sha256",
 			Hash:       hash,
 		}
-		mod.Update["modrinth"]["version"] = version.ID
+		mod.Update["github"]["version"] = version.ID
 	}
 
 	return nil