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

mindersec / minder / 24052643061

06 Apr 2026 09:41PM UTC coverage: 58.524%. First build
24052643061

Pull #6281

github

web-flow
Merge 6405039c3 into d7c1044e8
Pull Request #6281: refactor: populate provider field in entity context using provider ID fallback

26 of 33 new or added lines in 2 files covered. (78.79%)

19321 of 33014 relevant lines covered (58.52%)

36.58 hits per line

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

75.0
/internal/engine/handler.go
1
// SPDX-FileCopyrightText: Copyright 2024 The Minder Authors
2
// SPDX-License-Identifier: Apache-2.0
3

4
package engine
5

6
import (
7
        "context"
8
        "fmt"
9
        "slices"
10
        "sync"
11
        "time"
12

13
        "github.com/ThreeDotsLabs/watermill/message"
14
        "github.com/rs/zerolog"
15

16
        "github.com/mindersec/minder/internal/engine/engcontext"
17
        "github.com/mindersec/minder/internal/engine/entities"
18
        minderlogger "github.com/mindersec/minder/internal/logger"
19
        pb "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1"
20
        "github.com/mindersec/minder/pkg/eventer/constants"
21
        "github.com/mindersec/minder/pkg/eventer/interfaces"
22
)
23

24
const (
25
        // DefaultExecutionTimeout defines the default timeout for entity execution.
26
        DefaultExecutionTimeout = 5 * time.Minute
27

28
        // ArtifactSignatureWaitPeriod defines delay before processing artifact events.
29
        ArtifactSignatureWaitPeriod = 10 * time.Second
30
)
31

32
// ExecutorEventHandler handles entity evaluation events.
33
type ExecutorEventHandler struct {
34
        evt                    interfaces.Publisher
35
        handlerMiddleware      []message.HandlerMiddleware
36
        wgEntityEventExecution *sync.WaitGroup
37
        executor               Executor
38

39
        executionTimeout time.Duration
40

41
        cancels []*context.CancelFunc
42
        lock    sync.Mutex
43
}
44

45
// NewExecutorEventHandler creates a new ExecutorEventHandler.
46
func NewExecutorEventHandler(
47
        ctx context.Context,
48
        evt interfaces.Publisher,
49
        handlerMiddleware []message.HandlerMiddleware,
50
        executor Executor,
51
        executionTimeout time.Duration,
52
) *ExecutorEventHandler {
1✔
53

1✔
54
        if executionTimeout <= 0 {
1✔
NEW
55
                executionTimeout = DefaultExecutionTimeout
×
NEW
56
        }
×
57

58
        eh := &ExecutorEventHandler{
1✔
59
                evt:                    evt,
1✔
60
                wgEntityEventExecution: &sync.WaitGroup{},
1✔
61
                handlerMiddleware:      handlerMiddleware,
1✔
62
                executor:               executor,
1✔
63
                executionTimeout:       executionTimeout,
1✔
64
        }
1✔
65

1✔
66
        zerolog.Ctx(ctx).Debug().
1✔
67
                Dur("execution_timeout", executionTimeout).
1✔
68
                Msg("executor event handler initialized")
1✔
69

1✔
70
        go func() {
2✔
71
                <-ctx.Done()
1✔
72
                eh.lock.Lock()
1✔
73
                defer eh.lock.Unlock()
1✔
74

1✔
75
                for _, cancel := range eh.cancels {
1✔
76
                        (*cancel)()
×
77
                }
×
78
        }()
79

80
        return eh
1✔
81
}
82

83
// Register registers the handler for entity evaluation events.
84
func (e *ExecutorEventHandler) Register(r interfaces.Registrar) {
×
85
        r.Register(constants.TopicQueueEntityEvaluate, e.HandleEntityEvent, e.handlerMiddleware...)
×
86
}
×
87

88
// Wait blocks until all entity executions are complete.
89
func (e *ExecutorEventHandler) Wait() {
1✔
90
        e.wgEntityEventExecution.Wait()
1✔
91
}
1✔
92

93
// HandleEntityEvent processes incoming entity events.
94
func (e *ExecutorEventHandler) HandleEntityEvent(msg *message.Message) error {
2✔
95

2✔
96
        // NOTE: we're _deliberately_ "escaping" from the parent context's Cancel/Done
2✔
97
        // completion, because the default watermill behavior for both Go channels and
2✔
98
        // SQL is to process messages sequentially, but we need additional parallelism
2✔
99
        // beyond that. When we switch to a different message processing system, we
2✔
100
        // should aim to remove this goroutine altogether and have the messaging system
2✔
101
        // provide the parallelism.
2✔
102
        // We _do_ still want to cancel on shutdown, however.
2✔
103
        msgCtx := context.WithoutCancel(msg.Context())
2✔
104

2✔
105
        // This allows us to cancel rule evaluation directly when terminationContext
2✔
106
        // is cancelled.
2✔
107
        //nolint:gosec
2✔
108
        msgCtx, shutdownCancel := context.WithCancel(msgCtx)
2✔
109

2✔
110
        e.lock.Lock()
2✔
111
        e.cancels = append(e.cancels, &shutdownCancel)
2✔
112
        e.lock.Unlock()
2✔
113

2✔
114
        msg = msg.Copy()
2✔
115

2✔
116
        inf, err := entities.ParseEntityEvent(msg)
2✔
117
        if err != nil {
2✔
118
                return fmt.Errorf("error unmarshalling payload: %w", err)
×
119
        }
×
120

121
        e.wgEntityEventExecution.Add(1)
2✔
122

2✔
123
        go func() {
4✔
124
                defer e.wgEntityEventExecution.Done()
2✔
125

2✔
126
                if inf.Type == pb.Entity_ENTITY_ARTIFACTS {
2✔
127
                        time.Sleep(ArtifactSignatureWaitPeriod)
×
128
                }
×
129

130
                ctx, cancel := context.WithTimeout(msgCtx, e.executionTimeout)
2✔
131
                defer cancel()
2✔
132

2✔
133
                defer func() {
4✔
134
                        e.lock.Lock()
2✔
135
                        e.cancels = slices.DeleteFunc(e.cancels, func(cf *context.CancelFunc) bool {
5✔
136
                                return cf == &shutdownCancel
3✔
137
                        })
3✔
138
                        e.lock.Unlock()
2✔
139
                }()
140

141
                ctx = engcontext.WithEntityContext(ctx, &engcontext.EntityContext{
2✔
142
                        Project: engcontext.Project{ID: inf.ProjectID},
2✔
143
                        Provider: engcontext.Provider{
2✔
144
                                Name: inf.ProviderID.String(),
2✔
145
                        },
2✔
146
                })
2✔
147

2✔
148
                ts := minderlogger.BusinessRecord(ctx)
2✔
149
                ctx = ts.WithTelemetry(ctx)
2✔
150

2✔
151
                logger := zerolog.Ctx(ctx)
2✔
152

2✔
153
                if err := inf.WithExecutionIDFromMessage(msg); err != nil {
2✔
NEW
154
                        logger.Debug().
×
155
                                Str("message_id", msg.UUID).
×
156
                                Msg("message does not contain execution ID, skipping")
×
157
                        return
×
158
                }
×
159

160
                err := e.executor.EvalEntityEvent(ctx, inf)
2✔
161

2✔
162
                logMsg := logger.Info()
2✔
163
                if err != nil {
2✔
164
                        logMsg = logger.Error()
×
165
                }
×
166
                ts.Record(logMsg).Send()
2✔
167

2✔
168
                // record telemetry regardless of error. We explicitly record telemetry
2✔
169
                // here even though we also record it in the middleware because the evaluation
2✔
170
                // is done in a separate goroutine which usually still runs after the middleware
2✔
171
                // had already recorded the telemetry.
2✔
172

2✔
173
                if err != nil {
2✔
174
                        logger.Info().
×
175
                                Str("project", inf.ProjectID.String()).
×
176
                                Str("provider_id", inf.ProviderID.String()).
×
177
                                Str("entity", inf.Type.String()).
×
178
                                Str("entity_id", inf.EntityID.String()).
×
NEW
179
                                Err(err).
×
NEW
180
                                Msg("got error while evaluating entity event")
×
181
                }
×
182

183
                msg, err := inf.BuildMessage()
2✔
184
                if err != nil {
2✔
185
                        logger.Err(err).Msg("error building message")
×
186
                        return
×
187
                }
×
188

189
                if err := e.evt.Publish(constants.TopicQueueEntityFlush, msg); err != nil {
2✔
190
                        logger.Err(err).Msg("error publishing flush event")
×
191
                }
×
192
        }()
193

194
        return nil
2✔
195
}
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