-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
Showing
5 changed files
with
104 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// SPDX-FileCopyrightText: Copyright 2023 The Minder Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package actions | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"sync" | ||
|
||
engif "github.com/mindersec/minder/internal/engine/interfaces" | ||
) | ||
|
||
// SharedActionsContextKey is the key used to store the shared actions context | ||
// in the context.Context. | ||
type SharedActionsContextKey struct{} | ||
|
||
// SharedFlusherKey is the key used to store the shared flusher | ||
type SharedFlusherKey string | ||
|
||
type sharedFlusher struct { | ||
flusher engif.AggregatingAction | ||
items []any | ||
} | ||
|
||
// SharedActionsContext is the shared actions context. | ||
type SharedActionsContext struct { | ||
shared map[SharedFlusherKey]*sharedFlusher | ||
mux sync.Mutex | ||
} | ||
|
||
// WithSharedActionsContext returns a new context.Context with the shared actions | ||
// context set. | ||
func WithSharedActionsContext(ctx context.Context) (context.Context, *SharedActionsContext) { | ||
sac := &SharedActionsContext{} | ||
return context.WithValue(ctx, SharedActionsContextKey{}, sac), sac | ||
} | ||
|
||
// GetSharedActionsContext returns the shared actions context from the context.Context. | ||
func GetSharedActionsContext(ctx context.Context) *SharedActionsContext { | ||
ctxVal := ctx.Value(SharedActionsContextKey{}) | ||
if ctxVal == nil { | ||
return nil | ||
} | ||
|
||
v, ok := ctxVal.(*SharedActionsContext) | ||
if !ok { | ||
return nil | ||
} | ||
|
||
return v | ||
} | ||
|
||
// ShareAndRegister adds a shared value to the shared actions context. It may | ||
// also register a flusher if it does not exist. | ||
func (sac *SharedActionsContext) ShareAndRegister(key SharedFlusherKey, flusher engif.AggregatingAction, item any) { | ||
sac.mux.Lock() | ||
defer sac.mux.Unlock() | ||
|
||
f, ok := sac.shared[key] | ||
if !ok { | ||
f = &sharedFlusher{ | ||
flusher: flusher, | ||
items: []any{item}, | ||
} | ||
sac.shared[key] = f | ||
return | ||
} | ||
|
||
f.items = append(f.items, item) | ||
} | ||
|
||
// Flush returns all the shared values and clears the shared actions context. | ||
func (sac *SharedActionsContext) Flush() error { | ||
sac.mux.Lock() | ||
defer sac.mux.Unlock() | ||
var errs []error | ||
|
||
for key, f := range sac.shared { | ||
err := f.flusher.Flush(f.items...) | ||
if err != nil { | ||
errs = append(errs, err) | ||
} | ||
|
||
delete(sac.shared, key) | ||
} | ||
|
||
return errors.Join(errs...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters