Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: processing invalid GITHUB_TOKEN #140

Merged
merged 3 commits into from
Oct 1, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}