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

raystack / meteor / 11607751930

31 Oct 2024 07:23AM UTC coverage: 82.993% (-0.1%) from 83.127%
11607751930

Pull #493

github

ravisuhag
feat: support templating for http sinks
Pull Request #493: feat: support templating for http sinks

45 of 67 new or added lines in 2 files covered. (67.16%)

1 existing line in 1 file now uncovered.

6827 of 8226 relevant lines covered (82.99%)

0.91 hits per line

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

63.27
/plugins/sinks/http/tengo_script.go
1
package http
2

3
import (
4
        "context"
5
        "encoding/json"
6
        "errors"
7
        "fmt"
8

9
        "github.com/d5/tengo/v2"
10
        v1beta2 "github.com/raystack/meteor/models/raystack/assets/v1beta2"
11
        "github.com/raystack/meteor/plugins/internal/tengoutil"
12
        "github.com/raystack/meteor/plugins/internal/tengoutil/structmap"
13
)
14

15
var errUserExit = errors.New("user exit")
16

17
func (s *Sink) executeScript(ctx context.Context, url string, asset *v1beta2.Asset) error {
1✔
18
        scriptCfg := s.config.Script
1✔
19
        script, err := tengoutil.NewSecureScript(
1✔
20
                ([]byte)(scriptCfg.Source), s.scriptGlobals(ctx, url),
1✔
21
        )
1✔
22
        if err != nil {
1✔
NEW
23
                return err
×
NEW
24
        }
×
25
        c, err := script.Compile()
1✔
26
        if err != nil {
1✔
NEW
27
                return fmt.Errorf("compile: %w", err)
×
NEW
28
        }
×
29
        assetMap, err := structmap.AsMap(asset)
1✔
30
        if err != nil {
1✔
NEW
31
                return fmt.Errorf("convert asset to map: %w", err)
×
NEW
32
        }
×
33
        if err := c.Set("asset", assetMap); err != nil {
1✔
NEW
34
                return fmt.Errorf("set asset into vm: %w", err)
×
NEW
35
        }
×
36
        if err := c.RunContext(ctx); err != nil && !errors.Is(err, errUserExit) {
1✔
NEW
37
                return fmt.Errorf("run: %w", err)
×
NEW
38
        }
×
39
        return nil
1✔
40
}
41
func (s *Sink) scriptGlobals(ctx context.Context, url string) map[string]interface{} {
1✔
42
        return map[string]interface{}{
1✔
43
                "asset": map[string]interface{}{},
1✔
44
                "sink": &tengo.UserFunction{
1✔
45
                        Name:  "sink",
1✔
46
                        Value: s.executeRequestWrapper(ctx, url),
1✔
47
                },
1✔
48
                "exit": &tengo.UserFunction{
1✔
49
                        Name: "exit",
1✔
50
                        Value: func(...tengo.Object) (tengo.Object, error) {
1✔
NEW
51
                                return nil, errUserExit
×
NEW
52
                        },
×
53
                },
54
        }
55
}
56
func (s *Sink) executeRequestWrapper(ctx context.Context, url string) tengo.CallableFunc {
1✔
57
        return func(args ...tengo.Object) (tengo.Object, error) {
2✔
58
                if len(args) != 1 {
1✔
NEW
59
                        return nil, fmt.Errorf("execute request: invalid number of arguments of sink function, expected 1, got %d", len(args))
×
NEW
60
                }
×
61
                payloadObj, ok := args[0].(*tengo.Map)
1✔
62
                if !ok {
1✔
NEW
63
                        return nil, fmt.Errorf("execute request: invalid type of argument of sink function, expected map, got %T", args[0])
×
NEW
64
                }
×
65
                payload, err := json.Marshal(tengo.ToInterface(payloadObj))
1✔
66
                if err != nil {
1✔
NEW
67
                        return nil, fmt.Errorf("execute request: marshal payload: %w", err)
×
NEW
68
                }
×
69
                return nil, s.makeRequest(ctx, url, payload)
1✔
70
        }
71
}
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

© 2026 Coveralls, Inc