Skip to content

Commit

Permalink
fix: processing invalid GITHUB_TOKEN
Browse files Browse the repository at this point in the history
* check versionsSemantic/judgedVersions before processing
* if we get a 404 code from github when using a token, make a repeat request without a token

Closed: #139
  • Loading branch information
juev committed Oct 1, 2023
1 parent 56a6b97 commit 13a1857
Showing 1 changed file with 43 additions and 23 deletions.
66 changes: 43 additions & 23 deletions gobrew.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,9 @@ func (gb *GoBrew) judgeVersion(version string) string {
versionsSemantic = append(versionsSemantic, v)
}
}
if len(versionsSemantic) == 0 {
return ""
}

// sort semantic versions
sort.Sort(semver.Collection(versionsSemantic))
Expand All @@ -422,6 +425,9 @@ func (gb *GoBrew) judgeVersion(version string) string {
judgedVersions := groupedVersions[versionsSemantic[i].Original()]
// get last element
if version == "dev-latest" {
if len(judgedVersions) == 0 {
return ""
}
return judgedVersions[len(judgedVersions)-1]
}

Expand Down Expand Up @@ -649,26 +655,54 @@ func (gb *GoBrew) getGithubTags(repo string) (result []string) {
}

githubTags = make(map[string][]string)
client := &http.Client{}
url := "https://api.github.com/repos/kevincobain2000/gobrew/git/refs/tags"
if repo == "golang/go" {
url = goBrewTagsApi
}

data := doRequest(url, os.Getenv("GITHUB_TOKEN"))
if len(data) == 0 && os.Getenv("GITHUB_TOKEN") != "" {
color.Warnln("[WARNING] invalid token we are trying a request without a token")
data = doRequest(url, "")
}
if len(data) == 0 {
return
}

type Tag struct {
Ref string
}
var tags []Tag
utils.CheckError(json.Unmarshal(data, &tags), "==> [Error]")

for _, tag := range tags {
t := strings.ReplaceAll(tag.Ref, "refs/tags/", "")
if strings.HasPrefix(t, "v") || strings.HasPrefix(t, "go") {
result = append(result, t)
}
}

githubTags[repo] = result
return
}

func doRequest(url string, token string) (data []byte) {
client := &http.Client{}
request, err := http.NewRequest("GET", url, nil)
if err != nil {
color.Errorf("==> [Error] Cannot create request: %s", err)
color.Errorln(fmt.Sprintf("==> [Error] Cannot create request: %s", err))
return
}

request.Header.Set("User-Agent", "gobrew")

if token, ok := os.LookupEnv("GITHUB_TOKEN"); ok {
if token != "" {
request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
}

response, err := client.Do(request)
if err != nil {
color.Errorf("==> [Error] Cannot get response: %s", err)
color.Errorln(fmt.Sprintf("==> [Error] Cannot get response: %s", err))
return
}

Expand All @@ -677,34 +711,20 @@ func (gb *GoBrew) getGithubTags(repo string) (result []string) {
}(response.Body)

if response.StatusCode == http.StatusTooManyRequests {
color.Errorf("==> [Error] Rate limit exhausted")
color.Errorln("==> [Error] Rate limit exhausted")
return
}

if response.StatusCode != http.StatusOK {
color.Errorf("==> [Error] Cannot read response: %s", response.Status)
color.Errorln(fmt.Sprintf("==> [Error] Cannot read response: %s", response.Status))
return
}

data, err := io.ReadAll(response.Body)
data, err = io.ReadAll(response.Body)
if err != nil {
color.Errorf("==> [Error] Cannot read response: %s", err)
color.Errorln(fmt.Sprintf("==> [Error] Cannot read response Body: %s", err))
return
}

type Tag struct {
Ref string
}
var tags []Tag
utils.CheckError(json.Unmarshal(data, &tags), "==> [Error]")

for _, tag := range tags {
t := strings.ReplaceAll(tag.Ref, "refs/tags/", "")
if strings.HasPrefix(t, "v") || strings.HasPrefix(t, "go") {
result = append(result, t)
}
}

githubTags[repo] = result
return result
return
}

0 comments on commit 13a1857

Please sign in to comment.