Skip to content

Commit db33213

Browse files
authored
Merge pull request #420 from netlify/feat-204/add-functions-config-to-buildbot-deploy
feat: add functions_config parameter to deploy
2 parents a025a78 + 3d23b9b commit db33213

File tree

3 files changed

+38
-20
lines changed

3 files changed

+38
-20
lines changed

go/porcelain/deploy.go

Lines changed: 28 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,6 +257,10 @@ 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,
@@ -649,9 +655,9 @@ func addEdgeFunctionsToDeployFiles(dir string, files *deployFiles, observer Depl
649655
})
650656
}
651657

652-
func bundle(ctx context.Context, functionDir string, observer DeployObserver) (*deployFiles, []*models.FunctionSchedule, error) {
658+
func bundle(ctx context.Context, functionDir string, observer DeployObserver) (*deployFiles, []*models.FunctionSchedule, map[string]models.FunctionConfig, error) {
653659
if functionDir == "" {
654-
return nil, nil, nil
660+
return nil, nil, nil, nil
655661
}
656662

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

669675
info, err := ioutil.ReadDir(functionDir)
670676
if err != nil {
671-
return nil, nil, err
677+
return nil, nil, nil, err
672678
}
673679

674680
for _, i := range info {
@@ -678,23 +684,23 @@ func bundle(ctx context.Context, functionDir string, observer DeployObserver) (*
678684
case zipFile(i):
679685
runtime, err := readZipRuntime(filePath)
680686
if err != nil {
681-
return nil, nil, err
687+
return nil, nil, nil, err
682688
}
683689
file, err := newFunctionFile(filePath, i, runtime, observer)
684690
if err != nil {
685-
return nil, nil, err
691+
return nil, nil, nil, err
686692
}
687693
functions.Add(file.Name, file)
688694
case jsFile(i):
689695
file, err := newFunctionFile(filePath, i, jsRuntime, observer)
690696
if err != nil {
691-
return nil, nil, err
697+
return nil, nil, nil, err
692698
}
693699
functions.Add(file.Name, file)
694700
case goFile(filePath, i, observer):
695701
file, err := newFunctionFile(filePath, i, goRuntime, observer)
696702
if err != nil {
697-
return nil, nil, err
703+
return nil, nil, nil, err
698704
}
699705
functions.Add(file.Name, file)
700706
default:
@@ -704,14 +710,14 @@ func bundle(ctx context.Context, functionDir string, observer DeployObserver) (*
704710
}
705711
}
706712

707-
return functions, nil, nil
713+
return functions, nil, nil, nil
708714
}
709715

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

713719
if err != nil {
714-
return nil, nil, err
720+
return nil, nil, nil, err
715721
}
716722

717723
logger := context.GetLogger(ctx)
@@ -722,23 +728,24 @@ func bundleFromManifest(ctx context.Context, manifestFile *os.File, observer Dep
722728
err = json.Unmarshal(manifestBytes, &manifest)
723729

724730
if err != nil {
725-
return nil, nil, fmt.Errorf("malformed functions manifest file: %w", err)
731+
return nil, nil, nil, fmt.Errorf("malformed functions manifest file: %w", err)
726732
}
727733

728734
schedules := make([]*models.FunctionSchedule, 0, len(manifest.Functions))
729735
functions := newDeployFiles()
736+
functionsConfig := make(map[string]models.FunctionConfig)
730737

731738
for _, function := range manifest.Functions {
732739
fileInfo, err := os.Stat(function.Path)
733740

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

738745
file, err := newFunctionFile(function.Path, fileInfo, function.Runtime, observer)
739746

740747
if err != nil {
741-
return nil, nil, err
748+
return nil, nil, nil, err
742749
}
743750

744751
if function.Schedule != "" {
@@ -748,10 +755,16 @@ func bundleFromManifest(ctx context.Context, manifestFile *os.File, observer Dep
748755
})
749756
}
750757

758+
if function.DisplayName != "" {
759+
functionsConfig[file.Name] = models.FunctionConfig{
760+
DisplayName: function.DisplayName,
761+
}
762+
}
763+
751764
functions.Add(file.Name, file)
752765
}
753766

754-
return functions, schedules, nil
767+
return functions, schedules, functionsConfig, nil
755768
}
756769

757770
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)