Skip to content

Commit 645b9fb

Browse files
4xposedJGAntunes
andauthored
fix: upload a single file per sha (#425)
* uploaded a single file per sha * Test skipping files with the same SHA * test number of server requests, and pass corret `Required` array to the function call * fix typo * chore: simplify file sum fixture logic * chore: remove log assertions in test * chore: add flag and test for function bundle uploads * chore: missing comment * chore: drop the option used for release candidate release --------- Co-authored-by: JGAntunes <[email protected]>
1 parent a938bce commit 645b9fb

File tree

2 files changed

+80
-8
lines changed

2 files changed

+80
-8
lines changed

go/porcelain/deploy.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -412,14 +412,21 @@ func (n *Netlify) uploadFiles(ctx context.Context, d *models.Deploy, files *depl
412412

413413
for _, sha := range required {
414414
if files, exist := files.Hashed[sha]; exist {
415-
for _, file := range files {
416-
select {
417-
case sem <- 1:
418-
wg.Add(1)
419-
go n.uploadFile(ctx, d, file, observer, t, timeout, wg, sem, sharedErr)
420-
case <-ctx.Done():
421-
log.Info("Context terminated, aborting file upload")
422-
return errors.Wrap(ctx.Err(), "aborted file upload early")
415+
file := files[0]
416+
417+
select {
418+
case sem <- 1:
419+
wg.Add(1)
420+
go n.uploadFile(ctx, d, file, observer, t, timeout, wg, sem, sharedErr)
421+
case <-ctx.Done():
422+
log.Info("Context terminated, aborting file upload")
423+
return errors.Wrap(ctx.Err(), "aborted file upload early")
424+
}
425+
426+
if len(files) > 1 {
427+
skippedFiles := files[1:]
428+
for _, file := range skippedFiles {
429+
log.Infof("Skipping file with content already uploaded: %s", file.Name)
423430
}
424431
}
425432
}

go/porcelain/deploy_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,71 @@ func TestUploadFiles_Cancelation(t *testing.T) {
262262
require.ErrorIs(t, err, gocontext.Canceled)
263263
}
264264

265+
func TestUploadFiles_SkipEqualFiles(t *testing.T) {
266+
ctx := gocontext.Background()
267+
268+
serverRequests := 0
269+
270+
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
271+
serverRequests++
272+
273+
rw.Header().Set("Content-Type", "application/json; charset=utf-8")
274+
rw.Write([]byte(`{}`))
275+
}))
276+
defer server.Close()
277+
278+
hu, _ := url.Parse(server.URL)
279+
tr := apiClient.NewWithClient(hu.Host, "/api/v1", []string{"http"}, http.DefaultClient)
280+
client := NewRetryable(tr, strfmt.Default, 1)
281+
client.uploadLimit = 1
282+
ctx = context.WithAuthInfo(ctx, apiClient.BearerToken("token"))
283+
284+
// Create some files to deploy
285+
dir, err := ioutil.TempDir("", "deploy")
286+
require.NoError(t, err)
287+
defer os.RemoveAll(dir)
288+
289+
fileBody := []byte("Hello")
290+
291+
require.NoError(t, ioutil.WriteFile(filepath.Join(dir, "a.html"), fileBody, 0644))
292+
require.NoError(t, ioutil.WriteFile(filepath.Join(dir, "b.html"), fileBody, 0644))
293+
294+
files, err := walk(dir, nil, false, false)
295+
require.NoError(t, err)
296+
297+
// Create some fake function bundles to deploy
298+
functionsDir, err := ioutil.TempDir("", "deploy-functions")
299+
require.NoError(t, err)
300+
defer os.RemoveAll(functionsDir)
301+
302+
// Get the JS function bundle
303+
cwd, _ := os.Getwd()
304+
basePath := path.Join(filepath.Dir(cwd), "internal", "data")
305+
jsFunctionPath := strings.Replace(filepath.Join(basePath, "hello-js-function-test.zip"), "\\", "/", -1)
306+
bundleBody, err := ioutil.ReadFile(jsFunctionPath)
307+
require.NoError(t, err)
308+
309+
require.NoError(t, ioutil.WriteFile(filepath.Join(functionsDir, "a.zip"), bundleBody, 0644))
310+
require.NoError(t, ioutil.WriteFile(filepath.Join(functionsDir, "b.zip"), bundleBody, 0644))
311+
312+
functions, _, _, err := bundle(ctx, functionsDir, mockObserver{})
313+
require.NoError(t, err)
314+
315+
d := &models.Deploy{}
316+
// uploadFiles relies on the fact that the list of sums is an array of unique values, as both
317+
// the files and bundles have the same SHA we only need one of them for the Required array
318+
d.Required = []string{files.Sums["a.html"]}
319+
d.RequiredFunctions = []string{functions.Sums["a"]}
320+
321+
err = client.uploadFiles(ctx, d, files, nil, fileUpload, time.Minute)
322+
require.NoError(t, err)
323+
assert.Equal(t, 1, serverRequests)
324+
325+
err = client.uploadFiles(ctx, d, functions, nil, functionUpload, time.Minute)
326+
require.NoError(t, err)
327+
assert.Equal(t, 2, serverRequests)
328+
}
329+
265330
func TestUploadFunctions_RetryCountHeader(t *testing.T) {
266331
attempts := 0
267332
ctx, cancel := gocontext.WithCancel(gocontext.Background())

0 commit comments

Comments
 (0)