Skip to content

Commit

Permalink
Add a RegisterInterceptor helper function
Browse files Browse the repository at this point in the history
This makes it a lot easier to write custom interceptors while utilizing the
rest of the server helpers that we have created for the packaged core
interceptors.

Signed-off-by: Dibyo Mukherjee <[email protected]>
  • Loading branch information
dibyom authored and tekton-robot committed Dec 16, 2021
1 parent 0417421 commit 13cff21
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pkg/interceptors/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,15 @@ type Server struct {
interceptors map[string]triggersv1.InterceptorInterface
}

func NewWithCoreInterceptors(sl corev1lister.SecretLister, l *zap.SugaredLogger) (*Server, error) {
// RegisterInterceptor sets up the interceptor to be served at the specfied path
func (is *Server) RegisterInterceptor(path string, interceptor triggersv1.InterceptorInterface) {
if is.interceptors == nil {
is.interceptors = map[string]triggersv1.InterceptorInterface{}
}
is.interceptors[path] = interceptor
}

func NewWithCoreInterceptors(sl corev1lister.SecretLister, l *zap.SugaredLogger) (*Server, error) {
i := map[string]triggersv1.InterceptorInterface{
"bitbucket": bitbucket.NewInterceptor(sl, l),
"cel": cel.NewInterceptor(sl, l),
Expand Down
26 changes: 26 additions & 0 deletions pkg/interceptors/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
Expand All @@ -10,6 +11,8 @@ import (
"strings"
"testing"

"github.com/tektoncd/triggers/pkg/apis/triggers/v1beta1"

"google.golang.org/grpc/codes"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -160,3 +163,26 @@ func TestServer_ServeHTTP_Error(t *testing.T) {
})
}
}

type fakeInterceptor struct{}

func (i fakeInterceptor) Process(ctx context.Context, r *v1beta1.InterceptorRequest) *v1beta1.InterceptorResponse {
return nil
}

func TestServer_RegisterInterceptor(t *testing.T) {
s := Server{}
s.RegisterInterceptor("first", fakeInterceptor{})
want := map[string]v1beta1.InterceptorInterface{
"first": fakeInterceptor{},
}
if diff := cmp.Diff(want, s.interceptors); diff != "" {
t.Errorf("RegisterInterceptor first (-want/+got): %s", diff)
}

s.RegisterInterceptor("second", fakeInterceptor{})
want["second"] = fakeInterceptor{}
if diff := cmp.Diff(want, s.interceptors); diff != "" {
t.Errorf("RegisterInterceptor second (-want/+got): %s", diff)
}
}

0 comments on commit 13cff21

Please sign in to comment.