From 13a18577db1502acad1c496ac2274bab1b4af293 Mon Sep 17 00:00:00 2001 From: Evsyukov Denis Date: Sun, 1 Oct 2023 11:08:19 +0300 Subject: [PATCH 1/3] fix: processing invalid GITHUB_TOKEN * 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 --- gobrew.go | 66 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 23 deletions(-) diff --git a/gobrew.go b/gobrew.go index 3fdc0de..d546ce2 100644 --- a/gobrew.go +++ b/gobrew.go @@ -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)) @@ -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] } @@ -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 } @@ -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 } From 6bfcb8f812fb3f15f348ea0bff383f12f0672dc0 Mon Sep 17 00:00:00 2001 From: Evsyukov Denis Date: Sun, 1 Oct 2023 12:22:31 +0300 Subject: [PATCH 2/3] feat: remove Sprintf from color.Errorln --- gobrew.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gobrew.go b/gobrew.go index d546ce2..de2ec3c 100644 --- a/gobrew.go +++ b/gobrew.go @@ -690,7 +690,7 @@ func doRequest(url string, token string) (data []byte) { client := &http.Client{} request, err := http.NewRequest("GET", url, nil) if err != nil { - color.Errorln(fmt.Sprintf("==> [Error] Cannot create request: %s", err)) + color.Errorln("==> [Error] Cannot create request:", err.Error()) return } @@ -702,7 +702,7 @@ func doRequest(url string, token string) (data []byte) { response, err := client.Do(request) if err != nil { - color.Errorln(fmt.Sprintf("==> [Error] Cannot get response: %s", err)) + color.Errorln("==> [Error] Cannot get response:", err.Error()) return } @@ -716,13 +716,13 @@ func doRequest(url string, token string) (data []byte) { } if response.StatusCode != http.StatusOK { - color.Errorln(fmt.Sprintf("==> [Error] Cannot read response: %s", response.Status)) + color.Errorln("==> [Error] Cannot read response:", response.Status) return } data, err = io.ReadAll(response.Body) if err != nil { - color.Errorln(fmt.Sprintf("==> [Error] Cannot read response Body: %s", err)) + color.Errorln("==> [Error] Cannot read response Body:", err.Error()) return } From 2353cdbb8af26d07addf4c858fe4ff5887c42d52 Mon Sep 17 00:00:00 2001 From: Evsyukov Denis Date: Sun, 1 Oct 2023 14:54:02 +0300 Subject: [PATCH 3/3] feat: simplifying the use of the github api * use prepared json to take a list of golang versions * use a single query to take the latest version of gobrew * now we don't need to use a token in requests --- gobrew.go | 44 +++++++++++++++----------------------------- 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/gobrew.go b/gobrew.go index de2ec3c..8179e47 100644 --- a/gobrew.go +++ b/gobrew.go @@ -56,7 +56,6 @@ type GoBrew struct { } var gb GoBrew -var githubTags map[string][]string // NewGoBrew instance func NewGoBrew() GoBrew { @@ -185,7 +184,7 @@ func (gb *GoBrew) ListVersions() { // ListRemoteVersions that are installed by dir ls func (gb *GoBrew) ListRemoteVersions(print bool) map[string][]string { color.Infoln("==> [Info] Fetching remote versions\n") - tags := gb.getGithubTags("golang/go") + tags := gb.getGolangVersions() var versions []string for _, tag := range tags { @@ -640,53 +639,44 @@ func (gb *GoBrew) changeSymblinkGo(version string) { } func (gb *GoBrew) getLatestVersion() string { - tags := gb.getGithubTags("kevincobain2000/gobrew") - - if len(tags) == 0 { + url := "https://api.github.com/repos/kevincobain2000/gobrew/releases/latest" + data := doRequest(url) + if len(data) == 0 { return "" } - return tags[len(tags)-1] -} - -func (gb *GoBrew) getGithubTags(repo string) (result []string) { - if len(githubTags[repo]) > 0 { - return githubTags[repo] + type Tag struct { + TagName string `json:"tag_name"` } + var tag Tag + utils.CheckError(json.Unmarshal(data, &tag), "==> [Error]") - githubTags = make(map[string][]string) - url := "https://api.github.com/repos/kevincobain2000/gobrew/git/refs/tags" - if repo == "golang/go" { - url = goBrewTagsApi - } + return tag.TagName +} - 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, "") - } +func (gb *GoBrew) getGolangVersions() (result []string) { + data := doRequest(goBrewTagsApi) if len(data) == 0 { return } type Tag struct { - Ref string + Ref string `json:"ref"` } 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") { + if strings.HasPrefix(t, "go") { result = append(result, t) } } - githubTags[repo] = result return } -func doRequest(url string, token string) (data []byte) { +func doRequest(url string) (data []byte) { client := &http.Client{} request, err := http.NewRequest("GET", url, nil) if err != nil { @@ -696,10 +686,6 @@ func doRequest(url string, token string) (data []byte) { request.Header.Set("User-Agent", "gobrew") - if token != "" { - request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token)) - } - response, err := client.Do(request) if err != nil { color.Errorln("==> [Error] Cannot get response:", err.Error())