Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a RegisterInterceptor helper function #1285

Merged
merged 1 commit into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}