-
Notifications
You must be signed in to change notification settings - Fork 48
/
codecommit.go
67 lines (57 loc) · 2.25 KB
/
codecommit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package archetype
import (
"context"
"reflect"
"runtime"
awsLambdaEvents "github.com/aws/aws-lambda-go/events"
sparta "github.com/mweagle/Sparta/v3"
"github.com/pkg/errors"
)
// CodeCommitReactor represents a lambda function that responds to CodeCommit events
type CodeCommitReactor interface {
// OnCodeCommitEvent when an SNS event occurs. Check the codeCommitEvent field
// for the specific event
OnCodeCommitEvent(ctx context.Context, codeCommitEvent awsLambdaEvents.CodeCommitEvent) (interface{}, error)
}
// CodeCommitReactorFunc is a free function that adapts a CodeCommitReactor
// compliant signature into a function that exposes an OnEvent
// function
type CodeCommitReactorFunc func(ctx context.Context,
codeCommitEvent awsLambdaEvents.CodeCommitEvent) (interface{}, error)
// OnCodeCommitEvent satisfies the CodeCommitReactor interface
func (reactorFunc CodeCommitReactorFunc) OnCodeCommitEvent(ctx context.Context,
codeCommitEvent awsLambdaEvents.CodeCommitEvent) (interface{}, error) {
return reactorFunc(ctx, codeCommitEvent)
}
// ReactorName provides the name of the reactor func
func (reactorFunc CodeCommitReactorFunc) ReactorName() string {
return runtime.FuncForPC(reflect.ValueOf(reactorFunc).Pointer()).Name()
}
// NewCodeCommitReactor returns an SNS reactor lambda function
func NewCodeCommitReactor(reactor CodeCommitReactor,
repositoryName string,
branches []string,
events []string,
additionalLambdaPermissions []sparta.IAMRolePrivilege) (*sparta.LambdaAWSInfo, error) {
reactorLambda := func(ctx context.Context, codeCommitEvent awsLambdaEvents.CodeCommitEvent) (interface{}, error) {
return reactor.OnCodeCommitEvent(ctx, codeCommitEvent)
}
lambdaFn, lambdaFnErr := sparta.NewAWSLambda(reactorName(reactor),
reactorLambda,
sparta.IAMRoleDefinition{})
if lambdaFnErr != nil {
return nil, errors.Wrapf(lambdaFnErr, "attempting to create reactor")
}
lambdaFn.Permissions = append(lambdaFn.Permissions, sparta.CodeCommitPermission{
BasePermission: sparta.BasePermission{
SourceArn: repositoryName,
},
RepositoryName: repositoryName,
Branches: branches,
Events: events,
})
if len(additionalLambdaPermissions) != 0 {
lambdaFn.RoleDefinition.Privileges = additionalLambdaPermissions
}
return lambdaFn, nil
}