Skip to content

Commit 92c921d

Browse files
committed
feat: add functions_config parameter to deploy
1 parent a025a78 commit 92c921d

File tree

3 files changed

+39
-20
lines changed

3 files changed

+39
-20
lines changed

go/porcelain/deploy.go

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ type DeployOptions struct {
9595
files *deployFiles
9696
functions *deployFiles
9797
functionSchedules []*models.FunctionSchedule
98+
functionsConfig map[string]models.FunctionConfig
9899
}
99100

100101
type uploadError struct {
@@ -225,7 +226,7 @@ func (n *Netlify) DoDeploy(ctx context.Context, options *DeployOptions, deploy *
225226

226227
options.files = files
227228

228-
functions, schedules, err := bundle(ctx, options.FunctionsDir, options.Observer)
229+
functions, schedules, functionsConfig, err := bundle(ctx, options.FunctionsDir, options.Observer)
229230
if err != nil {
230231
if options.Observer != nil {
231232
options.Observer.OnFailedWalk()
@@ -234,6 +235,7 @@ func (n *Netlify) DoDeploy(ctx context.Context, options *DeployOptions, deploy *
234235
}
235236
options.functions = functions
236237
options.functionSchedules = schedules
238+
options.functionsConfig = functionsConfig
237239

238240
deployFiles := &models.DeployFiles{
239241
Files: options.files.Sums,
@@ -255,11 +257,16 @@ func (n *Netlify) DoDeploy(ctx context.Context, options *DeployOptions, deploy *
255257
deployFiles.FunctionSchedules = schedules
256258
}
257259

260+
if options.functionsConfig != nil {
261+
deployFiles.FunctionsConfig = options.functionsConfig
262+
}
263+
258264
l := context.GetLogger(ctx)
259265
l.WithFields(logrus.Fields{
260266
"site_id": options.SiteID,
261267
"deploy_files": len(options.files.Sums),
262268
"scheduled_functions": len(schedules),
269+
"functions_config": functionsConfig,
263270
}).Debug("Starting to deploy files")
264271
authInfo := context.GetAuthInfo(ctx)
265272

@@ -649,9 +656,9 @@ func addEdgeFunctionsToDeployFiles(dir string, files *deployFiles, observer Depl
649656
})
650657
}
651658

652-
func bundle(ctx context.Context, functionDir string, observer DeployObserver) (*deployFiles, []*models.FunctionSchedule, error) {
659+
func bundle(ctx context.Context, functionDir string, observer DeployObserver) (*deployFiles, []*models.FunctionSchedule, map[string]models.FunctionConfig, error) {
653660
if functionDir == "" {
654-
return nil, nil, nil
661+
return nil, nil, nil, nil
655662
}
656663

657664
manifestFile, err := os.Open(filepath.Join(functionDir, "manifest.json"))
@@ -668,7 +675,7 @@ func bundle(ctx context.Context, functionDir string, observer DeployObserver) (*
668675

669676
info, err := ioutil.ReadDir(functionDir)
670677
if err != nil {
671-
return nil, nil, err
678+
return nil, nil, nil, err
672679
}
673680

674681
for _, i := range info {
@@ -678,23 +685,23 @@ func bundle(ctx context.Context, functionDir string, observer DeployObserver) (*
678685
case zipFile(i):
679686
runtime, err := readZipRuntime(filePath)
680687
if err != nil {
681-
return nil, nil, err
688+
return nil, nil, nil, err
682689
}
683690
file, err := newFunctionFile(filePath, i, runtime, observer)
684691
if err != nil {
685-
return nil, nil, err
692+
return nil, nil, nil, err
686693
}
687694
functions.Add(file.Name, file)
688695
case jsFile(i):
689696
file, err := newFunctionFile(filePath, i, jsRuntime, observer)
690697
if err != nil {
691-
return nil, nil, err
698+
return nil, nil, nil, err
692699
}
693700
functions.Add(file.Name, file)
694701
case goFile(filePath, i, observer):
695702
file, err := newFunctionFile(filePath, i, goRuntime, observer)
696703
if err != nil {
697-
return nil, nil, err
704+
return nil, nil, nil, err
698705
}
699706
functions.Add(file.Name, file)
700707
default:
@@ -704,14 +711,14 @@ func bundle(ctx context.Context, functionDir string, observer DeployObserver) (*
704711
}
705712
}
706713

707-
return functions, nil, nil
714+
return functions, nil, nil, nil
708715
}
709716

710-
func bundleFromManifest(ctx context.Context, manifestFile *os.File, observer DeployObserver) (*deployFiles, []*models.FunctionSchedule, error) {
717+
func bundleFromManifest(ctx context.Context, manifestFile *os.File, observer DeployObserver) (*deployFiles, []*models.FunctionSchedule, map[string]models.FunctionConfig, error) {
711718
manifestBytes, err := ioutil.ReadAll(manifestFile)
712719

713720
if err != nil {
714-
return nil, nil, err
721+
return nil, nil, nil, err
715722
}
716723

717724
logger := context.GetLogger(ctx)
@@ -722,23 +729,24 @@ func bundleFromManifest(ctx context.Context, manifestFile *os.File, observer Dep
722729
err = json.Unmarshal(manifestBytes, &manifest)
723730

724731
if err != nil {
725-
return nil, nil, fmt.Errorf("malformed functions manifest file: %w", err)
732+
return nil, nil, nil, fmt.Errorf("malformed functions manifest file: %w", err)
726733
}
727734

728735
schedules := make([]*models.FunctionSchedule, 0, len(manifest.Functions))
729736
functions := newDeployFiles()
737+
functionsConfig := make(map[string]models.FunctionConfig)
730738

731739
for _, function := range manifest.Functions {
732740
fileInfo, err := os.Stat(function.Path)
733741

734742
if err != nil {
735-
return nil, nil, fmt.Errorf("manifest file specifies a function path that cannot be found: %s", function.Path)
743+
return nil, nil, nil, fmt.Errorf("manifest file specifies a function path that cannot be found: %s", function.Path)
736744
}
737745

738746
file, err := newFunctionFile(function.Path, fileInfo, function.Runtime, observer)
739747

740748
if err != nil {
741-
return nil, nil, err
749+
return nil, nil, nil, err
742750
}
743751

744752
if function.Schedule != "" {
@@ -748,10 +756,16 @@ func bundleFromManifest(ctx context.Context, manifestFile *os.File, observer Dep
748756
})
749757
}
750758

759+
if function.DisplayName != "" {
760+
functionsConfig[file.Name] = models.FunctionConfig{
761+
DisplayName: function.DisplayName,
762+
}
763+
}
764+
751765
functions.Add(file.Name, file)
752766
}
753767

754-
return functions, schedules, nil
768+
return functions, schedules, functionsConfig, nil
755769
}
756770

757771
func readZipRuntime(filePath string) (string, error) {

go/porcelain/deploy_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ func TestUploadFunctions_RetryCountHeader(t *testing.T) {
306306
defer os.RemoveAll(dir)
307307
require.NoError(t, ioutil.WriteFile(filepath.Join(functionsPath, "foo.js"), []byte("module.exports = () => {}"), 0644))
308308

309-
files, _, err := bundle(ctx, functionsPath, mockObserver{})
309+
files, _, _, err := bundle(ctx, functionsPath, mockObserver{})
310310
require.NoError(t, err)
311311
d := &models.Deploy{}
312312
for _, bundle := range files.Files {
@@ -317,11 +317,12 @@ func TestUploadFunctions_RetryCountHeader(t *testing.T) {
317317
}
318318

319319
func TestBundle(t *testing.T) {
320-
functions, schedules, err := bundle(gocontext.Background(), "../internal/data", mockObserver{})
320+
functions, schedules, functionsConfig, err := bundle(gocontext.Background(), "../internal/data", mockObserver{})
321321

322322
assert.Nil(t, err)
323323
assert.Equal(t, 3, len(functions.Files))
324324
assert.Empty(t, schedules)
325+
assert.Nil(t, functionsConfig)
325326

326327
jsFunction := functions.Files["hello-js-function-test"]
327328
pyFunction := functions.Files["hello-py-function-test"]
@@ -344,6 +345,7 @@ func TestBundleWithManifest(t *testing.T) {
344345
"path": "%s",
345346
"runtime": "a-runtime",
346347
"mainFile": "/some/path/hello-js-function-test.js",
348+
"displayName": "Hello Javascript Function",
347349
"name": "hello-js-function-test",
348350
"schedule": "* * * * *"
349351
},
@@ -352,7 +354,7 @@ func TestBundleWithManifest(t *testing.T) {
352354
"runtime": "some-other-runtime",
353355
"mainFile": "/some/path/hello-py-function-test",
354356
"name": "hello-py-function-test"
355-
}
357+
}
356358
],
357359
"version": 1
358360
}`, jsFunctionPath, pyFunctionPath)
@@ -361,8 +363,7 @@ func TestBundleWithManifest(t *testing.T) {
361363
defer os.Remove(manifestPath)
362364
assert.Nil(t, err)
363365

364-
functions, schedules, err := bundle(gocontext.Background(), "../internal/data", mockObserver{})
365-
366+
functions, schedules, functionsConfig, err := bundle(gocontext.Background(), "../internal/data", mockObserver{})
366367
assert.Nil(t, err)
367368

368369
assert.Equal(t, 1, len(schedules))
@@ -372,6 +373,9 @@ func TestBundleWithManifest(t *testing.T) {
372373
assert.Equal(t, 2, len(functions.Files))
373374
assert.Equal(t, "a-runtime", functions.Files["hello-js-function-test"].Runtime)
374375
assert.Equal(t, "some-other-runtime", functions.Files["hello-py-function-test"].Runtime)
376+
377+
assert.Equal(t, 1, len(functionsConfig))
378+
assert.Equal(t, "Hello Javascript Function", functionsConfig["hello-js-function-test"].DisplayName)
375379
}
376380

377381
func TestReadZipRuntime(t *testing.T) {

go/porcelain/functions_manifest.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ type functionsManifestEntry struct {
1212
Path string `json:"path"`
1313
Runtime string `json:"runtime"`
1414
Schedule string `json:"schedule"`
15+
DisplayName string `json:"displayName"`
1516
}

0 commit comments

Comments
 (0)