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>
This commit is contained in:
unilock 2023-05-31 20:54:35 -04:00
parent 01945213d7
commit c96a6a30ad
2 changed files with 11 additions and 5 deletions

View File

@ -85,15 +85,20 @@ func getLatestRelease(slug string, branch string) (Release, error) {
if err != nil { if err != nil {
return release, err return release, err
} }
err = json.Unmarshal(body, &releases) err = json.Unmarshal(body, &releases)
if err != nil { if err != nil {
return release, err return release, err
} }
if branch != "" {
for _, r := range releases { for _, r := range releases {
if r.TargetCommitish == branch { if r.TargetCommitish == branch {
return r, nil return r, nil
} }
} }
return release, fmt.Errorf("failed to find release for branch %v", branch)
}
return releases[0], nil 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) fmt.Printf("Installing %s from release %s\n", file.Name, release.TagName)
index, err := pack.LoadIndex() index, err := pack.LoadIndex()
if err != nil { if err != nil {
@ -125,7 +130,7 @@ func installRelease(repo Repo, release Release, pack core.Pack) error {
updateMap["github"], err = ghUpdateData{ updateMap["github"], err = ghUpdateData{
Slug: repo.FullName, Slug: repo.FullName,
Tag: release.TagName, 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() }.ToMap()
if err != nil { if err != nil {
return err return err

View File

@ -7,6 +7,7 @@ import (
"github.com/packwiz/packwiz/core" "github.com/packwiz/packwiz/core"
) )
// TODO: allow setting github api key via env variable
const ghApiServer = "api.github.com" const ghApiServer = "api.github.com"
type ghApiClient struct { type ghApiClient struct {