Skip to content

Commit

Permalink
internal/civisibility/utils/net: changes from review
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyredondo committed Sep 18, 2024
1 parent 7c60549 commit 95ad5bc
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 34 deletions.
28 changes: 14 additions & 14 deletions internal/civisibility/utils/net/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ type (
client struct {
id string
agentless bool
baseUrl string
baseURL string
environment string
serviceName string
workingDirectory string
repositoryUrl string
repositoryURL string
commitSha string
branchName string
testConfigurations testConfigurations
Expand Down Expand Up @@ -105,7 +105,7 @@ func NewClient() Client {

// create default http headers and get base url
defaultHeaders := map[string]string{}
var baseUrl string
var baseURL string
var requestHandler *RequestHandler

agentlessEnabled := internal.BoolEnv(constants.CIVisibilityAgentlessEnabledEnvironmentVariable, false)
Expand All @@ -129,10 +129,10 @@ func NewClient() Client {
site = v
}

baseUrl = fmt.Sprintf("https://api.%s", site)
baseURL = fmt.Sprintf("https://api.%s", site)
} else {
// Use the custom agentless URL.
baseUrl = agentlessURL
baseURL = agentlessURL
}

requestHandler = NewRequestHandler()
Expand All @@ -152,7 +152,7 @@ func NewClient() Client {
requestHandler = NewRequestHandlerWithClient(&http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
return dialer.DialContext(ctx, "unix", (&net.UnixAddr{
Name: agentURL.Path,
Net: "unix",
Expand All @@ -173,7 +173,7 @@ func NewClient() Client {
requestHandler = NewRequestHandler()
}

baseUrl = agentURL.String()
baseURL = agentURL.String()
}

// create random id (the backend associate all transactions with the client request)
Expand All @@ -184,11 +184,11 @@ func NewClient() Client {
return &client{
id: id,
agentless: agentlessEnabled,
baseUrl: baseUrl,
baseURL: baseURL,
environment: environment,
serviceName: serviceName,
workingDirectory: ciTags[constants.CIWorkspacePath],
repositoryUrl: ciTags[constants.GitRepositoryURL],
repositoryURL: ciTags[constants.GitRepositoryURL],
commitSha: ciTags[constants.GitCommitSHA],
branchName: ciTags[constants.GitBranch],
testConfigurations: testConfigurations{
Expand All @@ -204,18 +204,18 @@ func NewClient() Client {
}
}

func (c *client) getUrlPath(urlPath string) string {
func (c *client) getURLPath(urlPath string) string {
if c.agentless {
return fmt.Sprintf("%s/%s", c.baseUrl, urlPath)
} else {
return fmt.Sprintf("%s/%s/%s", c.baseUrl, "evp_proxy/v2", urlPath)
return fmt.Sprintf("%s/%s", c.baseURL, urlPath)
}

return fmt.Sprintf("%s/%s/%s", c.baseURL, "evp_proxy/v2", urlPath)
}

func (c *client) getPostRequestConfig(url string, body interface{}) *RequestConfig {
return &RequestConfig{
Method: "POST",
URL: c.getUrlPath(url),
URL: c.getURLPath(url),
Headers: c.headers,
Body: body,
Format: FormatJSON,
Expand Down
18 changes: 9 additions & 9 deletions internal/civisibility/utils/net/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func TestNewClient_AgentlessEnabled(t *testing.T) {
}

expectedBaseURL := "https://api.site.com"
if c.baseUrl != expectedBaseURL {
t.Errorf("Expected baseUrl '%s', got '%s'", expectedBaseURL, c.baseUrl)
if c.baseURL != expectedBaseURL {
t.Errorf("Expected baseUrl '%s', got '%s'", expectedBaseURL, c.baseURL)
}

if c.headers["dd-api-key"] != "test_api_key" {
Expand Down Expand Up @@ -125,18 +125,18 @@ func TestNewClient_CustomAgentlessURL(t *testing.T) {
t.Fatal("Expected client to be of type *client")
}

if c.baseUrl != "https://custom.agentless.url" {
t.Errorf("Expected baseUrl 'https://custom.agentless.url', got '%s'", c.baseUrl)
if c.baseURL != "https://custom.agentless.url" {
t.Errorf("Expected baseUrl 'https://custom.agentless.url', got '%s'", c.baseURL)
}
}

func TestClient_getUrlPath_Agentless(t *testing.T) {
c := &client{
agentless: true,
baseUrl: "https://api.customhost.com",
baseURL: "https://api.customhost.com",
}

urlPath := c.getUrlPath("some/path")
urlPath := c.getURLPath("some/path")
expected := "https://api.customhost.com/some/path"
if urlPath != expected {
t.Errorf("Expected urlPath '%s', got '%s'", expected, urlPath)
Expand All @@ -146,10 +146,10 @@ func TestClient_getUrlPath_Agentless(t *testing.T) {
func TestClient_getUrlPath_Agent(t *testing.T) {
c := &client{
agentless: false,
baseUrl: "http://agent.url",
baseURL: "http://agent.url",
}

urlPath := c.getUrlPath("some/path")
urlPath := c.getURLPath("some/path")
expected := "http://agent.url/evp_proxy/v2/some/path"
if urlPath != expected {
t.Errorf("Expected urlPath '%s', got '%s'", expected, urlPath)
Expand All @@ -159,7 +159,7 @@ func TestClient_getUrlPath_Agent(t *testing.T) {
func TestClient_getPostRequestConfig(t *testing.T) {
c := &client{
agentless: false,
baseUrl: "http://agent.url",
baseURL: "http://agent.url",
headers: map[string]string{
"trace_id": "12345",
"parent_id": "12345",
Expand Down
2 changes: 1 addition & 1 deletion internal/civisibility/utils/net/efd_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (c *client) GetEarlyFlakeDetectionData() (*EfdResponseData, error) {
Attributes: EfdRequestData{
Service: c.serviceName,
Env: c.environment,
RepositoryURL: c.repositoryUrl,
RepositoryURL: c.repositoryURL,
Configurations: c.testConfigurations,
},
},
Expand Down
2 changes: 1 addition & 1 deletion internal/civisibility/utils/net/efd_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestEfdApiRequest(t *testing.T) {
assert.Equal(t, efdRequestType, request.Data.Type)
assert.Equal(t, efdURLPath, r.URL.Path[1:])
assert.Equal(t, c.environment, request.Data.Attributes.Env)
assert.Equal(t, c.repositoryUrl, request.Data.Attributes.RepositoryURL)
assert.Equal(t, c.repositoryURL, request.Data.Attributes.RepositoryURL)
assert.Equal(t, c.serviceName, request.Data.Attributes.Service)
assert.Equal(t, c.testConfigurations, request.Data.Attributes.Configurations)

Expand Down
2 changes: 1 addition & 1 deletion internal/civisibility/utils/net/searchcommits_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (c *client) GetCommits(localCommits []string) ([]string, error) {
body := searchCommits{
Data: []searchCommitsData{},
Meta: searchCommitsMeta{
RepositoryURL: c.repositoryUrl,
RepositoryURL: c.repositoryURL,
},
}

Expand Down
2 changes: 1 addition & 1 deletion internal/civisibility/utils/net/searchcommits_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestSearchCommitsApiRequest(t *testing.T) {
if r.Header.Get(HeaderContentType) == ContentTypeJSON {
var request searchCommits
json.Unmarshal(body, &request)
assert.Equal(t, c.repositoryUrl, request.Meta.RepositoryURL)
assert.Equal(t, c.repositoryURL, request.Meta.RepositoryURL)
assert.Equal(t, "commit1", request.Data[0].ID)
assert.Equal(t, searchCommitsType, request.Data[0].Type)
assert.Equal(t, "commit2", request.Data[1].ID)
Expand Down
4 changes: 2 additions & 2 deletions internal/civisibility/utils/net/sendpackfiles_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c *client) SendPackFiles(packFiles []string) (bytes int64, err error) {
Type: searchCommitsType,
},
Meta: pushedShaMeta{
RepositoryURL: c.repositoryUrl,
RepositoryURL: c.repositoryURL,
},
},
ContentType: ContentTypeJSON,
Expand All @@ -57,7 +57,7 @@ func (c *client) SendPackFiles(packFiles []string) (bytes int64, err error) {

request := RequestConfig{
Method: "POST",
URL: c.getUrlPath(sendPackFilesURLPath),
URL: c.getURLPath(sendPackFilesURLPath),
Headers: c.headers,
Files: []FormFile{
pushedShaFormFile,
Expand Down
6 changes: 3 additions & 3 deletions internal/civisibility/utils/net/sendpackfiles_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func TestSendPackFilesApiRequest(t *testing.T) {
containsPushedSha := false
containsPackFile := false
for {
part, err_part := reader.NextPart()
if err_part == io.EOF {
part, errPart := reader.NextPart()
if errPart == io.EOF {
break
}
partName := part.FormName()
Expand All @@ -41,7 +41,7 @@ func TestSendPackFilesApiRequest(t *testing.T) {
assert.Equal(t, ContentTypeJSON, part.Header.Get(HeaderContentType))
var request pushedShaBody
json.Unmarshal(buf.Bytes(), &request)
assert.Equal(t, c.repositoryUrl, request.Meta.RepositoryURL)
assert.Equal(t, c.repositoryURL, request.Meta.RepositoryURL)
assert.Equal(t, c.commitSha, request.Data.ID)
assert.Equal(t, searchCommitsType, request.Data.Type)
containsPushedSha = true
Expand Down
2 changes: 1 addition & 1 deletion internal/civisibility/utils/net/settings_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (c *client) GetSettings() (*SettingsResponseData, error) {
Attributes: SettingsRequestData{
Service: c.serviceName,
Env: c.environment,
RepositoryURL: c.repositoryUrl,
RepositoryURL: c.repositoryURL,
Branch: c.branchName,
Sha: c.commitSha,
Configurations: c.testConfigurations,
Expand Down
2 changes: 1 addition & 1 deletion internal/civisibility/utils/net/settings_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestSettingsApiRequest(t *testing.T) {
assert.Equal(t, c.commitSha, request.Data.Attributes.Sha)
assert.Equal(t, c.branchName, request.Data.Attributes.Branch)
assert.Equal(t, c.environment, request.Data.Attributes.Env)
assert.Equal(t, c.repositoryUrl, request.Data.Attributes.RepositoryURL)
assert.Equal(t, c.repositoryURL, request.Data.Attributes.RepositoryURL)
assert.Equal(t, c.serviceName, request.Data.Attributes.Service)
assert.Equal(t, c.testConfigurations, request.Data.Attributes.Configurations)

Expand Down

0 comments on commit 95ad5bc

Please sign in to comment.