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

Popesku/qd 7958 #249

Merged
merged 2 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
35 changes: 19 additions & 16 deletions cloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ const (
)

func getCloudBaseUrl() string {
return fmt.Sprintf("https://%s", GetEnvWithDefault(QodanaEndpoint, DefaultEndpoint))
return GetEnvWithDefault(QodanaEndpoint, fmt.Sprintf("https://%s", DefaultEndpoint))
}

func getCloudApiBaseUrl() string {
return fmt.Sprintf("https://api.%s", GetEnvWithDefault(QodanaEndpoint, DefaultEndpoint))
return GetEnvWithDefault(QodanaEndpoint, fmt.Sprintf("https://api.%s", DefaultEndpoint))
}

// GetCloudTeamsPageUrl returns the team page URL on Qodana Cloud
Expand Down Expand Up @@ -126,20 +126,23 @@ func (client *QdClient) doRequest(path, method string, headers map[string]string
if responseErr != nil {
return RequestError{Err: responseErr}
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Fatal(err)
}
}(resp.Body)

responseBody, _ := io.ReadAll(resp.Body)
if resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices {
var data map[string]interface{}
if err := json.Unmarshal(responseBody, &data); err != nil {
return RequestError{Err: err}
if resp != nil {
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Fatal(err)
}
}(resp.Body)

responseBody, _ := io.ReadAll(resp.Body)
if resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices {
var data map[string]interface{}
if err := json.Unmarshal(responseBody, &data); err != nil {
return RequestError{Err: err}
}
return Success{Data: data}
}
return Success{Data: data}
return APIError{StatusCode: resp.StatusCode, Message: string(responseBody)}
}
return APIError{StatusCode: resp.StatusCode, Message: string(responseBody)}
return APIError{StatusCode: 400, Message: "Wrong endpoint"}
}
21 changes: 21 additions & 0 deletions cloud/cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@ func TestGetProjectByBadToken(t *testing.T) {
}
}

func TestGetProjectByStaging(t *testing.T) {
err := os.Setenv(QodanaEndpoint, "https://cloud.sssa-stgn.aws.intellij.net")
if err != nil {
t.Fatal(err)
}
token := os.Getenv("QODANA_TOKEN")
if token == "" {
t.Skip()
}
client := NewQdClient(token)
result := client.getProject()
switch v := result.(type) {
case APIError:
if v.StatusCode > http.StatusBadRequest {
t.Errorf("Expected status code %d, got %d", http.StatusBadRequest, v.StatusCode)
}
case RequestError:
t.Errorf("Did not expect request error: %v", v)
}
}

func TestValidateToken(t *testing.T) {
client := NewQdClient("kek")
if projectName := client.ValidateToken(); projectName != "" {
Expand Down
Loading