• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

supabase / cli / 13910325011

17 Mar 2025 09:34PM UTC coverage: 51.053%. First build
13910325011

Pull #3314

github

web-flow
Merge 191509d66 into bc0db16f3
Pull Request #3314: fix: return deploy metadata from bundle

35 of 96 new or added lines in 3 files covered. (36.46%)

6955 of 13623 relevant lines covered (51.05%)

186.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

21.65
/pkg/function/batch.go
1
package function
2

3
import (
4
        "bytes"
5
        "context"
6
        "fmt"
7
        "io"
8
        "os"
9

10
        "github.com/cenkalti/backoff/v4"
11
        "github.com/docker/go-units"
12
        "github.com/go-errors/errors"
13
        "github.com/supabase/cli/pkg/api"
14
        "github.com/supabase/cli/pkg/config"
15
)
16

17
const (
18
        eszipContentType = "application/vnd.denoland.eszip"
19
        maxRetries       = 3
20
)
21

22
func (s *EdgeRuntimeAPI) UpsertFunctions(ctx context.Context, functionConfig config.FunctionConfig, filter ...func(string) bool) error {
4✔
23
        var result []api.FunctionResponse
4✔
24
        if resp, err := s.client.V1ListAllFunctionsWithResponse(ctx, s.project); err != nil {
5✔
25
                return errors.Errorf("failed to list functions: %w", err)
1✔
26
        } else if resp.JSON200 == nil {
5✔
27
                return errors.Errorf("unexpected list functions status %d: %s", resp.StatusCode(), string(resp.Body))
1✔
28
        } else {
3✔
29
                result = *resp.JSON200
2✔
30
        }
2✔
31
        exists := make(map[string]struct{}, len(result))
2✔
32
        for _, f := range result {
3✔
33
                exists[f.Slug] = struct{}{}
1✔
34
        }
1✔
35
        var toUpdate []api.BulkUpdateFunctionBody
2✔
36
OUTER:
2✔
37
        for slug, function := range functionConfig {
4✔
38
                if !function.Enabled {
4✔
39
                        fmt.Fprintln(os.Stderr, "Skipped deploying Function:", slug)
2✔
40
                        continue
2✔
41
                }
42
                for _, keep := range filter {
×
43
                        if !keep(slug) {
×
44
                                continue OUTER
×
45
                        }
46
                }
47
                var body bytes.Buffer
×
NEW
48
                meta, err := s.eszip.Bundle(ctx, slug, function.Entrypoint, function.ImportMap, function.StaticFiles, &body)
×
NEW
49
                if err != nil {
×
50
                        return err
×
51
                }
×
NEW
52
                meta.VerifyJwt = &function.VerifyJWT
×
53
                // Update if function already exists
×
NEW
54
                upsert := func() (api.BulkUpdateFunctionBody, error) {
×
55
                        if _, ok := exists[slug]; ok {
×
NEW
56
                                return s.updateFunction(ctx, slug, meta, bytes.NewReader(body.Bytes()))
×
57
                        }
×
NEW
58
                        return s.createFunction(ctx, slug, meta, bytes.NewReader(body.Bytes()))
×
59
                }
60
                functionSize := units.HumanSize(float64(body.Len()))
×
61
                fmt.Fprintf(os.Stderr, "Deploying Function: %s (script size: %s)\n", slug, functionSize)
×
62
                policy := backoff.WithContext(backoff.WithMaxRetries(backoff.NewExponentialBackOff(), maxRetries), ctx)
×
NEW
63
                result, err := backoff.RetryWithData(upsert, policy)
×
NEW
64
                if err != nil {
×
65
                        return err
×
66
                }
×
NEW
67
                toUpdate = append(toUpdate, result)
×
68
        }
69
        if len(toUpdate) > 1 {
2✔
NEW
70
                if resp, err := s.client.V1BulkUpdateFunctionsWithResponse(ctx, s.project, toUpdate); err != nil {
×
71
                        return errors.Errorf("failed to bulk update: %w", err)
×
72
                } else if resp.JSON200 == nil {
×
73
                        return errors.Errorf("unexpected bulk update status %d: %s", resp.StatusCode(), string(resp.Body))
×
74
                }
×
75
        }
76
        return nil
2✔
77
}
78

NEW
79
func (s *EdgeRuntimeAPI) updateFunction(ctx context.Context, slug string, meta api.FunctionDeployMetadata, body io.Reader) (api.BulkUpdateFunctionBody, error) {
×
NEW
80
        resp, err := s.client.V1UpdateAFunctionWithBodyWithResponse(ctx, s.project, slug, &api.V1UpdateAFunctionParams{
×
NEW
81
                VerifyJwt:      meta.VerifyJwt,
×
NEW
82
                ImportMapPath:  meta.ImportMapPath,
×
NEW
83
                EntrypointPath: &meta.EntrypointPath,
×
NEW
84
        }, eszipContentType, body)
×
85
        if err != nil {
×
NEW
86
                return api.BulkUpdateFunctionBody{}, errors.Errorf("failed to update function: %w", err)
×
NEW
87
        } else if resp.JSON200 == nil {
×
NEW
88
                return api.BulkUpdateFunctionBody{}, errors.Errorf("unexpected update function status %d: %s", resp.StatusCode(), string(resp.Body))
×
89
        }
×
NEW
90
        return api.BulkUpdateFunctionBody{
×
NEW
91
                Id:             resp.JSON200.Id,
×
NEW
92
                Name:           resp.JSON200.Name,
×
NEW
93
                Slug:           resp.JSON200.Slug,
×
NEW
94
                Version:        resp.JSON200.Version,
×
NEW
95
                EntrypointPath: resp.JSON200.EntrypointPath,
×
NEW
96
                ImportMap:      resp.JSON200.ImportMap,
×
NEW
97
                ImportMapPath:  resp.JSON200.ImportMapPath,
×
NEW
98
                VerifyJwt:      resp.JSON200.VerifyJwt,
×
NEW
99
                Status:         api.BulkUpdateFunctionBodyStatus(resp.JSON200.Status),
×
NEW
100
                CreatedAt:      &resp.JSON200.CreatedAt,
×
NEW
101
        }, nil
×
102
}
103

NEW
104
func (s *EdgeRuntimeAPI) createFunction(ctx context.Context, slug string, meta api.FunctionDeployMetadata, body io.Reader) (api.BulkUpdateFunctionBody, error) {
×
NEW
105
        resp, err := s.client.V1CreateAFunctionWithBodyWithResponse(ctx, s.project, &api.V1CreateAFunctionParams{
×
NEW
106
                Slug:           &slug,
×
NEW
107
                Name:           &slug,
×
NEW
108
                VerifyJwt:      meta.VerifyJwt,
×
NEW
109
                ImportMapPath:  meta.ImportMapPath,
×
NEW
110
                EntrypointPath: &meta.EntrypointPath,
×
NEW
111
        }, eszipContentType, body)
×
NEW
112
        if err != nil {
×
NEW
113
                return api.BulkUpdateFunctionBody{}, errors.Errorf("failed to create function: %w", err)
×
NEW
114
        } else if resp.JSON201 == nil {
×
NEW
115
                return api.BulkUpdateFunctionBody{}, errors.Errorf("unexpected create function status %d: %s", resp.StatusCode(), string(resp.Body))
×
NEW
116
        }
×
NEW
117
        return api.BulkUpdateFunctionBody{
×
NEW
118
                Id:             resp.JSON201.Id,
×
NEW
119
                Name:           resp.JSON201.Name,
×
NEW
120
                Slug:           resp.JSON201.Slug,
×
NEW
121
                Version:        resp.JSON201.Version,
×
NEW
122
                EntrypointPath: resp.JSON201.EntrypointPath,
×
NEW
123
                ImportMap:      resp.JSON201.ImportMap,
×
NEW
124
                ImportMapPath:  resp.JSON201.ImportMapPath,
×
NEW
125
                VerifyJwt:      resp.JSON201.VerifyJwt,
×
NEW
126
                Status:         api.BulkUpdateFunctionBodyStatus(resp.JSON201.Status),
×
NEW
127
                CreatedAt:      &resp.JSON201.CreatedAt,
×
NEW
128
        }, nil
×
129
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc