From c96a6a30add211af3617a76d3b50f55f7f40a2ac Mon Sep 17 00:00:00 2001
From: unilock <unilock@fennet.rentals>
Date: Wed, 31 May 2023 20:54:35 -0400
Subject: [PATCH] github: don't return releases on wrong branch

if a branch is specified, but a release cannot be found on that branch, return an error instead of simply the latest release

Signed-off-by: unilock <unilock@fennet.rentals>
---
 github/install.go | 15 ++++++++++-----
 github/request.go |  1 +
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/github/install.go b/github/install.go
index 89f38dc..55a4127 100644
--- a/github/install.go
+++ b/github/install.go
@@ -85,14 +85,19 @@ func getLatestRelease(slug string, branch string) (Release, error) {
 	if err != nil {
 		return release, err
 	}
+
 	err = json.Unmarshal(body, &releases)
 	if err != nil {
 		return release, err
 	}
-	for _, r := range releases {
-		if r.TargetCommitish == branch {
-			return r, nil
+
+	if branch != "" {
+		for _, r := range releases {
+			if r.TargetCommitish == branch {
+				return r, nil
+			}
 		}
+		return release, fmt.Errorf("failed to find release for branch %v", branch)
 	}
 
 	return releases[0], nil
@@ -113,7 +118,7 @@ func installRelease(repo Repo, release Release, pack core.Pack) error {
 		}
 	}
 
-	//Install the file
+	// Install the file
 	fmt.Printf("Installing %s from release %s\n", file.Name, release.TagName)
 	index, err := pack.LoadIndex()
 	if err != nil {
@@ -125,7 +130,7 @@ func installRelease(repo Repo, release Release, pack core.Pack) error {
 	updateMap["github"], err = ghUpdateData{
 		Slug:   repo.FullName,
 		Tag:    release.TagName,
-		Branch: release.TargetCommitish,
+		Branch: release.TargetCommitish, // TODO: if no branch is specified by the user, we shouldn't record it - in order to remain branch-agnostic in getLatestRelease()
 	}.ToMap()
 	if err != nil {
 		return err
diff --git a/github/request.go b/github/request.go
index 6c33887..f3fb5f6 100644
--- a/github/request.go
+++ b/github/request.go
@@ -7,6 +7,7 @@ import (
 	"github.com/packwiz/packwiz/core"
 )
 
+// TODO: allow setting github api key via env variable
 const ghApiServer = "api.github.com"
 
 type ghApiClient struct {