Skip to content

Commit

Permalink
improve github error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bakito committed Oct 12, 2024
1 parent 55d439f commit c232409
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
3 changes: 3 additions & 0 deletions pkg/fetcher/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ func (f *fetcher) Fetch(cfgFile string, selectedTools ...string) error {
}

tb, _, err := ReadToolbox(cfgFile)
if tb.HasGithubTools() && !github.TokenSet() {
log.Printf("⚠️ when using github tools, defining a github token 'GITHUB_TOKEN' is recommended")
}
if err != nil {
return err
}
Expand Down
27 changes: 21 additions & 6 deletions pkg/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import (
"fmt"
"log"
"os"
"strings"

"github.com/bakito/toolbox/pkg/http"
"github.com/bakito/toolbox/pkg/types"
"github.com/go-resty/resty/v2"
)

const EnvGithubToken = "GITHUB_TOKEN" // #nosec G101: variable name for token

var (
releaseURLPattern = "https://api.github.com/repos/%s/releases/tags/%s"
latestReleaseURLPattern = "https://api.github.com/repos/%s/releases/latest"
Expand All @@ -18,15 +21,19 @@ var (

func LatestRelease(client *resty.Client, repo string, quiet bool) (*types.GithubRelease, error) {
ghr := &types.GithubRelease{}

ghErr := &types.GithubError{}
ghc := client.R().
SetResult(ghr).
SetError(ghErr).
SetHeader("Accept", "application/json")
handleGithubToken(ghc, quiet)
_, err := ghc.Get(latestReleaseURL(repo))
resp, err := ghc.Get(latestReleaseURL(repo))
if err != nil {
return nil, http.CheckError(err)
}
if resp.IsError() {
return nil, fmt.Errorf("github request was not successful: %s", ghErr.Message)
}

if ghr.TagName == "" {
ght := &types.GithubTags{}
Expand All @@ -44,30 +51,38 @@ func LatestRelease(client *resty.Client, repo string, quiet bool) (*types.Github
return ghr, nil
}

func TokenSet() bool {
t, ok := os.LookupEnv(EnvGithubToken)
return ok && strings.TrimSpace(t) != ""
}

func handleGithubToken(ghc *resty.Request, quiet bool) {
if t, ok := os.LookupEnv("GITHUB_TOKEN"); ok {
if t, ok := os.LookupEnv(EnvGithubToken); ok && strings.TrimSpace(t) != "" {
if !quiet {
log.Printf("🔑 Using github token\n")
}
ghc.SetAuthToken(t)
} else if !quiet {
log.Printf("⚠️ github token 'GITHUB_TOKEN' is not set.\n")
}
}

func Release(client *resty.Client, repo string, version string, quiet bool) (*types.GithubRelease, error) {
ghr := &types.GithubRelease{}
ghErr := &types.GithubError{}

ghc := client.R().
SetResult(ghr).
SetError(ghErr).
SetHeader("Accept", "application/json")

handleGithubToken(ghc, quiet)

_, err := ghc.Get(releaseURL(repo, version))
resp, err := ghc.Get(releaseURL(repo, version))
if err != nil {
return nil, http.CheckError(err)
}
if resp.IsError() {
return nil, fmt.Errorf("github request was not successful: %s", ghErr.Message)
}

if ghr.TagName == "" {
ght := &types.GithubTags{}
Expand Down
9 changes: 9 additions & 0 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ func (t *Toolbox) Versions() *Versions {
return v
}

func (t *Toolbox) HasGithubTools() bool {
for _, tool := range t.Tools {
if tool.Github != "" {
return true
}
}
return false
}

type Tool struct {
Name string `yaml:"name,omitempty"`
Github string `yaml:"github,omitempty"`
Expand Down
5 changes: 5 additions & 0 deletions pkg/types/types_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import (
"golang.org/x/mod/semver"
)

type GithubError struct {
Message string `json:"message"`
DocumentationUrl string `json:"documentation_url"`
}

type GithubRelease struct {
TagName string `json:"tag_name"`
Name string `json:"name"`
Expand Down

0 comments on commit c232409

Please sign in to comment.