Skip to content

Commit b403b5c

Browse files
committed
Add a shared actions context structure
This shared space will allow actions to gather structures or results that will allow it to aggregate and flush. In more solid terms, we'd be able to aggregate PR comments, iterate them, and issue just one big comment. Signed-off-by: Juan Antonio Osorio <[email protected]>
1 parent f2d6feb commit b403b5c

File tree

5 files changed

+104
-3
lines changed

5 files changed

+104
-3
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ require (
2323
github.com/erikgeiser/promptkit v0.9.0
2424
github.com/evanphx/json-patch/v5 v5.9.0
2525
github.com/fergusstrange/embedded-postgres v1.30.0
26+
github.com/gammazero/deque v0.2.1
2627
github.com/go-git/go-billy/v5 v5.6.0
2728
github.com/go-git/go-git/v5 v5.12.0
2829
github.com/go-playground/validator/v10 v10.23.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,8 @@ github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/
382382
github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
383383
github.com/gabriel-vasile/mimetype v1.4.6 h1:3+PzJTKLkvgjeTbts6msPJt4DixhT4YtFNf1gtGe3zc=
384384
github.com/gabriel-vasile/mimetype v1.4.6/go.mod h1:JX1qVKqZd40hUPpAfiNTe0Sne7hdfKSbOqqmkq8GCXc=
385+
github.com/gammazero/deque v0.2.1 h1:qSdsbG6pgp6nL7A0+K/B7s12mcCY/5l5SIUpMOl+dC0=
386+
github.com/gammazero/deque v0.2.1/go.mod h1:LFroj8x4cMYCukHJDbxFCkT+r9AndaJnFMuZDV34tuU=
385387
github.com/gkampitakis/ciinfo v0.3.0 h1:gWZlOC2+RYYttL0hBqcoQhM7h1qNkVqvRCV1fOvpAv8=
386388
github.com/gkampitakis/ciinfo v0.3.0/go.mod h1:1NIwaOcFChN4fa/B0hEBdAb6npDlFL8Bwx4dfRLRqAo=
387389
github.com/gkampitakis/go-diff v1.3.2 h1:Qyn0J9XJSDTgnsgHRdz9Zp24RaJeKMUHg2+PDZZdC4M=

internal/engine/actions/context.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// SPDX-FileCopyrightText: Copyright 2023 The Minder Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package actions
5+
6+
import (
7+
"context"
8+
"errors"
9+
"sync"
10+
11+
engif "github.com/mindersec/minder/internal/engine/interfaces"
12+
)
13+
14+
// SharedActionsContextKey is the key used to store the shared actions context
15+
// in the context.Context.
16+
type SharedActionsContextKey struct{}
17+
18+
// SharedFlusherKey is the key used to store the shared flusher
19+
type SharedFlusherKey string
20+
21+
type sharedFlusher struct {
22+
flusher engif.AggregatingAction
23+
items []any
24+
}
25+
26+
// SharedActionsContext is the shared actions context.
27+
type SharedActionsContext struct {
28+
shared map[SharedFlusherKey]*sharedFlusher
29+
mux sync.Mutex
30+
}
31+
32+
// WithSharedActionsContext returns a new context.Context with the shared actions
33+
// context set.
34+
func WithSharedActionsContext(ctx context.Context) (context.Context, *SharedActionsContext) {
35+
sac := &SharedActionsContext{}
36+
return context.WithValue(ctx, SharedActionsContextKey{}, sac), sac
37+
}
38+
39+
// GetSharedActionsContext returns the shared actions context from the context.Context.
40+
func GetSharedActionsContext(ctx context.Context) *SharedActionsContext {
41+
ctxVal := ctx.Value(SharedActionsContextKey{})
42+
if ctxVal == nil {
43+
return nil
44+
}
45+
46+
v, ok := ctxVal.(*SharedActionsContext)
47+
if !ok {
48+
return nil
49+
}
50+
51+
return v
52+
}
53+
54+
// ShareAndRegister adds a shared value to the shared actions context. It may
55+
// also register a flusher if it does not exist.
56+
func (sac *SharedActionsContext) ShareAndRegister(key SharedFlusherKey, flusher engif.AggregatingAction, item any) {
57+
sac.mux.Lock()
58+
defer sac.mux.Unlock()
59+
60+
f, ok := sac.shared[key]
61+
if !ok {
62+
f = &sharedFlusher{
63+
flusher: flusher,
64+
items: []any{item},
65+
}
66+
sac.shared[key] = f
67+
return
68+
}
69+
70+
f.items = append(f.items, item)
71+
}
72+
73+
// Flush returns all the shared values and clears the shared actions context.
74+
func (sac *SharedActionsContext) Flush() error {
75+
sac.mux.Lock()
76+
defer sac.mux.Unlock()
77+
var errs []error
78+
79+
for key, f := range sac.shared {
80+
err := f.flusher.Flush(f.items...)
81+
if err != nil {
82+
errs = append(errs, err)
83+
}
84+
85+
delete(sac.shared, key)
86+
}
87+
88+
return errors.Join(errs...)
89+
}

internal/engine/executor.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,21 +138,23 @@ func (e *executor) EvalEntityEvent(ctx context.Context, inf *entities.EntityInfo
138138
return fmt.Errorf("error while retrieving profiles and rule instances: %w", err)
139139
}
140140

141+
sacctx, sac := actions.WithSharedActionsContext(ctx)
142+
141143
// For each profile, get the profileEvalStatus first. Then, if the profileEvalStatus is nil
142144
// evaluate each rule and store the outcome in the database. If profileEvalStatus is non-nil,
143145
// just store it for all rules without evaluation.
144146
for _, profile := range profileAggregates {
145147

146-
profileEvalStatus := e.profileEvalStatus(ctx, inf, profile)
148+
profileEvalStatus := e.profileEvalStatus(sacctx, inf, profile)
147149

148150
for _, rule := range profile.Rules {
149-
if err := e.evaluateRule(ctx, inf, provider, &profile, &rule, ruleEngineCache, profileEvalStatus); err != nil {
151+
if err := e.evaluateRule(sacctx, inf, provider, &profile, &rule, ruleEngineCache, profileEvalStatus); err != nil {
150152
return fmt.Errorf("error evaluating entity event: %w", err)
151153
}
152154
}
153155
}
154156

155-
return nil
157+
return sac.Flush()
156158
}
157159

158160
func (e *executor) evaluateRule(

internal/engine/interfaces/interface.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ type Action interface {
3131
params ActionsParams, metadata *json.RawMessage) (json.RawMessage, error)
3232
}
3333

34+
// AggregatingAction is the interface for an action that aggregates multiple
35+
// pieces to form a final action. Normally this will come from the result of a
36+
// `Do` call on an action.
37+
type AggregatingAction interface {
38+
Flush(item ...any) error
39+
}
40+
3441
// ActionCmd is the type that defines what effect an action should have
3542
type ActionCmd string
3643

0 commit comments

Comments
 (0)