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

Break up client package and delete unused code #4303

Merged
merged 1 commit into from
Mar 22, 2023
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
61 changes: 61 additions & 0 deletions cli/internal/client/analytics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package client

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"

"github.com/hashicorp/go-retryablehttp"
"github.com/vercel/turbo/cli/internal/ci"
)

// RecordAnalyticsEvents is a specific method for POSTing events to Vercel
func (c *APIClient) RecordAnalyticsEvents(events []map[string]interface{}) error {
if err := c.okToRequest(); err != nil {
return err
}
params := url.Values{}
c.addTeamParam(&params)
encoded := params.Encode()
if encoded != "" {
encoded = "?" + encoded
}
body, err := json.Marshal(events)
if err != nil {
return err
}

requestURL := c.makeURL("/v8/artifacts/events" + encoded)
allowAuth := true
if c.usePreflight {
resp, latestRequestURL, err := c.doPreflight(requestURL, http.MethodPost, "Content-Type, Authorization, User-Agent")
if err != nil {
return fmt.Errorf("pre-flight request failed before trying to store in HTTP cache: %w", err)
}
requestURL = latestRequestURL
headers := resp.Header.Get("Access-Control-Allow-Headers")
allowAuth = strings.Contains(strings.ToLower(headers), strings.ToLower("Authorization"))
}

req, err := retryablehttp.NewRequest(http.MethodPost, requestURL, body)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
if allowAuth {
req.Header.Set("Authorization", "Bearer "+c.token)
}
req.Header.Set("User-Agent", c.userAgent())
if ci.IsCi() {
req.Header.Set("x-artifact-client-ci", ci.Constant())
}
resp, err := c.HTTPClient.Do(req)
if resp != nil && resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
b, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("%s", string(b))
}
return err
}
167 changes: 167 additions & 0 deletions cli/internal/client/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package client

import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"

"github.com/hashicorp/go-retryablehttp"
"github.com/vercel/turbo/cli/internal/ci"
"github.com/vercel/turbo/cli/internal/util"
)

// PutArtifact uploads an artifact associated with a given hash string to the remote cache
func (c *APIClient) PutArtifact(hash string, artifactBody []byte, duration int, tag string) error {
if err := c.okToRequest(); err != nil {
return err
}
params := url.Values{}
c.addTeamParam(&params)
// only add a ? if it's actually needed (makes logging cleaner)
encoded := params.Encode()
if encoded != "" {
encoded = "?" + encoded
}

requestURL := c.makeURL("/v8/artifacts/" + hash + encoded)
allowAuth := true
if c.usePreflight {
resp, latestRequestURL, err := c.doPreflight(requestURL, http.MethodPut, "Content-Type, x-artifact-duration, Authorization, User-Agent, x-artifact-tag")
if err != nil {
return fmt.Errorf("pre-flight request failed before trying to store in HTTP cache: %w", err)
}
requestURL = latestRequestURL
headers := resp.Header.Get("Access-Control-Allow-Headers")
allowAuth = strings.Contains(strings.ToLower(headers), strings.ToLower("Authorization"))
}

req, err := retryablehttp.NewRequest(http.MethodPut, requestURL, artifactBody)
req.Header.Set("Content-Type", "application/octet-stream")
req.Header.Set("x-artifact-duration", fmt.Sprintf("%v", duration))
if allowAuth {
req.Header.Set("Authorization", "Bearer "+c.token)
}
req.Header.Set("User-Agent", c.userAgent())
if ci.IsCi() {
req.Header.Set("x-artifact-client-ci", ci.Constant())
}
if tag != "" {
req.Header.Set("x-artifact-tag", tag)
}
if err != nil {
return fmt.Errorf("[WARNING] Invalid cache URL: %w", err)
}

resp, err := c.HTTPClient.Do(req)
if err != nil {
return fmt.Errorf("[ERROR] Failed to store files in HTTP cache: %w", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode == http.StatusForbidden {
return c.handle403(resp.Body)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("[ERROR] Failed to store files in HTTP cache: %s against URL %s", resp.Status, requestURL)
}
return nil
}

// FetchArtifact attempts to retrieve the build artifact with the given hash from the remote cache
func (c *APIClient) FetchArtifact(hash string) (*http.Response, error) {
return c.getArtifact(hash, http.MethodGet)
}

// ArtifactExists attempts to determine if the build artifact with the given hash exists in the Remote Caching server
func (c *APIClient) ArtifactExists(hash string) (*http.Response, error) {
return c.getArtifact(hash, http.MethodHead)
}

// getArtifact attempts to retrieve the build artifact with the given hash from the remote cache
func (c *APIClient) getArtifact(hash string, httpMethod string) (*http.Response, error) {
if httpMethod != http.MethodHead && httpMethod != http.MethodGet {
return nil, fmt.Errorf("invalid httpMethod %v, expected GET or HEAD", httpMethod)
}

if err := c.okToRequest(); err != nil {
return nil, err
}
params := url.Values{}
c.addTeamParam(&params)
// only add a ? if it's actually needed (makes logging cleaner)
encoded := params.Encode()
if encoded != "" {
encoded = "?" + encoded
}

requestURL := c.makeURL("/v8/artifacts/" + hash + encoded)
allowAuth := true
if c.usePreflight {
resp, latestRequestURL, err := c.doPreflight(requestURL, http.MethodGet, "Authorization, User-Agent")
if err != nil {
return nil, fmt.Errorf("pre-flight request failed before trying to fetch files in HTTP cache: %w", err)
}
requestURL = latestRequestURL
headers := resp.Header.Get("Access-Control-Allow-Headers")
allowAuth = strings.Contains(strings.ToLower(headers), strings.ToLower("Authorization"))
}

req, err := retryablehttp.NewRequest(httpMethod, requestURL, nil)
if allowAuth {
req.Header.Set("Authorization", "Bearer "+c.token)
}
req.Header.Set("User-Agent", c.userAgent())
if err != nil {
return nil, fmt.Errorf("invalid cache URL: %w", err)
}

resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to fetch artifact: %v", err)
} else if resp.StatusCode == http.StatusForbidden {
err = c.handle403(resp.Body)
_ = resp.Body.Close()
return nil, err
}
return resp, nil
}

func (c *APIClient) handle403(body io.Reader) error {
raw, err := ioutil.ReadAll(body)
if err != nil {
return fmt.Errorf("failed to read response %v", err)
}
apiError := &apiError{}
err = json.Unmarshal(raw, apiError)
if err != nil {
return fmt.Errorf("failed to read response (%v): %v", string(raw), err)
}
disabledErr, err := apiError.cacheDisabled()
if err != nil {
return err
}
return disabledErr
}

type apiError struct {
Code string `json:"code"`
Message string `json:"message"`
}

func (ae *apiError) cacheDisabled() (*util.CacheDisabledError, error) {
if strings.HasPrefix(ae.Code, "remote_caching_") {
statusString := ae.Code[len("remote_caching_"):]
status, err := util.CachingStatusFromString(statusString)
if err != nil {
return nil, err
}
return &util.CacheDisabledError{
Status: status,
Message: ae.Message,
}, nil
}
return nil, fmt.Errorf("unknown status %v: %v", ae.Code, ae.Message)
}
Loading