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 version check timeout #2008

Merged
merged 4 commits into from
Jun 1, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions utils/cliutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"encoding/json"
"fmt"
"github.com/jfrog/gofrog/version"
"github.com/jfrog/jfrog-client-go/http/httpclient"
"github.com/jfrog/jfrog-client-go/utils/io/httputils"
"io"
"net/http"
"os"
Expand Down Expand Up @@ -822,13 +820,16 @@ func shouldCheckLatestCliVersion() (shouldCheck bool, err error) {
}

func getLatestCliVersionFromGithubAPI() (githubVersionInfo githubResponse, err error) {
client, err := httpclient.ClientBuilder().Build()
if err != nil {
client := &http.Client{Timeout: time.Second * 2}
if errorutils.CheckError(err) != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong check

return
}
resp, body, _, err := client.SendGet("https://api.github.com/repos/jfrog/jfrog-cli/releases/latest", true, httputils.HttpClientDetails{HttpTimeout: time.Second * 2}, "")
if err != nil {
err = errors.New("couldn't get latest JFrog CLI latest version info from GitHub API: " + err.Error())
req, err := http.NewRequest("GET", "https://api.github.com/repos/jfrog/jfrog-cli/releases/latest", nil)
asafgabai marked this conversation as resolved.
Show resolved Hide resolved
if errorutils.CheckError(err) != nil {
return
asafgabai marked this conversation as resolved.
Show resolved Hide resolved
}
resp, body, err := doHttpRequest(client, req)
if errorutils.CheckError(err) != nil {
return
}
err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK)
Expand All @@ -838,3 +839,21 @@ func getLatestCliVersionFromGithubAPI() (githubVersionInfo githubResponse, err e
err = json.Unmarshal(body, &githubVersionInfo)
return
}

func doHttpRequest(client *http.Client, req *http.Request) (resp *http.Response, body []byte, err error) {
req.Close = true
resp, err = client.Do(req)
if errorutils.CheckError(err) != nil {
return
}
defer func() {
if resp != nil && resp.Body != nil {
e := resp.Body.Close()
asafgabai marked this conversation as resolved.
Show resolved Hide resolved
if err == nil {
err = errorutils.CheckError(e)
}
}
}()
body, err = io.ReadAll(resp.Body)
return resp, body, errorutils.CheckError(err)
}
Loading