From 38f841434405dc1e0d3cb37206096f9e7dfd5288 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Rodr=C3=ADguez?= Date: Sun, 5 Feb 2017 15:14:42 -0300 Subject: [PATCH 1/2] Add post-receive grpc notification call --- .../service/notifications/post_receive.go | 14 ++ .../notifications/post_receive_test.go | 77 +++++++++++ internal/service/notifications/server.go | 7 + internal/service/register.go | 2 + protos/gitaly.proto | 10 ++ protos/go/gitaly.pb.go | 123 ++++++++++++++++-- protos/ruby/lib/gitaly/gitaly_pb.rb | 7 + protos/ruby/lib/gitaly/gitaly_services_pb.rb | 14 ++ 8 files changed, 240 insertions(+), 14 deletions(-) create mode 100644 internal/service/notifications/post_receive.go create mode 100644 internal/service/notifications/post_receive_test.go create mode 100644 internal/service/notifications/server.go diff --git a/internal/service/notifications/post_receive.go b/internal/service/notifications/post_receive.go new file mode 100644 index 0000000000..f0be78a270 --- /dev/null +++ b/internal/service/notifications/post_receive.go @@ -0,0 +1,14 @@ +package notifications + +import ( + "log" + + pb "gitlab.com/gitlab-org/gitaly/protos/go" + "golang.org/x/net/context" +) + +func (s *server) PostReceive(ctx context.Context, in *pb.PostReceiveRequest) (*pb.PostReceiveResponse, error) { + // TODO: Invalidate InfoRefs cache + log.Printf("PostReceive: RepoPath=%q", in.Repository.Path) + return &pb.PostReceiveResponse{}, nil +} diff --git a/internal/service/notifications/post_receive_test.go b/internal/service/notifications/post_receive_test.go new file mode 100644 index 0000000000..5c0dfebd52 --- /dev/null +++ b/internal/service/notifications/post_receive_test.go @@ -0,0 +1,77 @@ +package notifications + +import ( + "log" + "net" + "os" + "path" + "testing" + "time" + + pb "gitlab.com/gitlab-org/gitaly/protos/go" + "golang.org/x/net/context" + "google.golang.org/grpc" + "google.golang.org/grpc/reflection" +) + +const ( + scratchDir = "testdata/scratch" + testRepoRoot = "testdata/data" + testRepo = "group/test.git" +) + +var serverSocketPath = path.Join(scratchDir, "gitaly.sock") + +func TestMain(m *testing.M) { + if err := os.MkdirAll(scratchDir, 0755); err != nil { + log.Fatal(err) + } + + os.Exit(func() int { + return m.Run() + }()) +} + +func TestSuccessfulPostReceive(t *testing.T) { + server := runNotificationsServer(t) + defer server.Stop() + + client := newNotificationsClient(t) + repo := &pb.Repository{Path: path.Join(testRepoRoot, testRepo)} + rpcRequest := &pb.PostReceiveRequest{Repository: repo} + + _, err := client.PostReceive(context.Background(), rpcRequest) + if err != nil { + t.Fatal(err) + } +} + +func runNotificationsServer(t *testing.T) *grpc.Server { + server := grpc.NewServer() + listener, err := net.Listen("unix", serverSocketPath) + if err != nil { + t.Fatal(err) + } + + pb.RegisterNotificationsServer(server, NewServer()) + reflection.Register(server) + + go server.Serve(listener) + + return server +} + +func newNotificationsClient(t *testing.T) pb.NotificationsClient { + connOpts := []grpc.DialOption{ + grpc.WithInsecure(), + grpc.WithDialer(func(addr string, _ time.Duration) (net.Conn, error) { + return net.Dial("unix", addr) + }), + } + conn, err := grpc.Dial(serverSocketPath, connOpts...) + if err != nil { + t.Fatal(err) + } + + return pb.NewNotificationsClient(conn) +} diff --git a/internal/service/notifications/server.go b/internal/service/notifications/server.go new file mode 100644 index 0000000000..5995f6d357 --- /dev/null +++ b/internal/service/notifications/server.go @@ -0,0 +1,7 @@ +package notifications + +type server struct{} + +func NewServer() *server { + return &server{} +} diff --git a/internal/service/register.go b/internal/service/register.go index 37e08ec525..5525271e7b 100644 --- a/internal/service/register.go +++ b/internal/service/register.go @@ -1,6 +1,7 @@ package service import ( + "gitlab.com/gitlab-org/gitaly/internal/service/notifications" "gitlab.com/gitlab-org/gitaly/internal/service/smarthttp" pb "gitlab.com/gitlab-org/gitaly/protos/go" @@ -8,5 +9,6 @@ import ( ) func RegisterAll(grpcServer *grpc.Server) { + pb.RegisterNotificationsServer(grpcServer, notifications.NewServer()) pb.RegisterSmartHTTPServer(grpcServer, smarthttp.NewServer()) } diff --git a/protos/gitaly.proto b/protos/gitaly.proto index a5edf7b84d..3415970481 100644 --- a/protos/gitaly.proto +++ b/protos/gitaly.proto @@ -11,6 +11,10 @@ service SmartHTTP { rpc InfoRefsReceivePack(InfoRefsRequest) returns (stream InfoRefsResponse) {} } +service Notifications { + rpc PostReceive(PostReceiveRequest) returns (PostReceiveResponse) {} +} + message InfoRefsRequest { Repository repository = 1; } @@ -22,3 +26,9 @@ message InfoRefsResponse { message Repository { string path = 1; } + +message PostReceiveRequest { + Repository repository = 1; +} + +message PostReceiveResponse {} diff --git a/protos/go/gitaly.pb.go b/protos/go/gitaly.pb.go index e1d5864aae..1ccf60c6c3 100644 --- a/protos/go/gitaly.pb.go +++ b/protos/go/gitaly.pb.go @@ -12,6 +12,8 @@ It has these top-level messages: InfoRefsRequest InfoRefsResponse Repository + PostReceiveRequest + PostReceiveResponse */ package gitaly @@ -83,10 +85,36 @@ func (m *Repository) GetPath() string { return "" } +type PostReceiveRequest struct { + Repository *Repository `protobuf:"bytes,1,opt,name=repository" json:"repository,omitempty"` +} + +func (m *PostReceiveRequest) Reset() { *m = PostReceiveRequest{} } +func (m *PostReceiveRequest) String() string { return proto.CompactTextString(m) } +func (*PostReceiveRequest) ProtoMessage() {} +func (*PostReceiveRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{3} } + +func (m *PostReceiveRequest) GetRepository() *Repository { + if m != nil { + return m.Repository + } + return nil +} + +type PostReceiveResponse struct { +} + +func (m *PostReceiveResponse) Reset() { *m = PostReceiveResponse{} } +func (m *PostReceiveResponse) String() string { return proto.CompactTextString(m) } +func (*PostReceiveResponse) ProtoMessage() {} +func (*PostReceiveResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{4} } + func init() { proto.RegisterType((*InfoRefsRequest)(nil), "gitaly.InfoRefsRequest") proto.RegisterType((*InfoRefsResponse)(nil), "gitaly.InfoRefsResponse") proto.RegisterType((*Repository)(nil), "gitaly.Repository") + proto.RegisterType((*PostReceiveRequest)(nil), "gitaly.PostReceiveRequest") + proto.RegisterType((*PostReceiveResponse)(nil), "gitaly.PostReceiveResponse") } // Reference imports to suppress errors if they are not otherwise used. @@ -252,21 +280,88 @@ var _SmartHTTP_serviceDesc = grpc.ServiceDesc{ Metadata: "gitaly.proto", } +// Client API for Notifications service + +type NotificationsClient interface { + PostReceive(ctx context.Context, in *PostReceiveRequest, opts ...grpc.CallOption) (*PostReceiveResponse, error) +} + +type notificationsClient struct { + cc *grpc.ClientConn +} + +func NewNotificationsClient(cc *grpc.ClientConn) NotificationsClient { + return ¬ificationsClient{cc} +} + +func (c *notificationsClient) PostReceive(ctx context.Context, in *PostReceiveRequest, opts ...grpc.CallOption) (*PostReceiveResponse, error) { + out := new(PostReceiveResponse) + err := grpc.Invoke(ctx, "/gitaly.Notifications/PostReceive", in, out, c.cc, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// Server API for Notifications service + +type NotificationsServer interface { + PostReceive(context.Context, *PostReceiveRequest) (*PostReceiveResponse, error) +} + +func RegisterNotificationsServer(s *grpc.Server, srv NotificationsServer) { + s.RegisterService(&_Notifications_serviceDesc, srv) +} + +func _Notifications_PostReceive_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(PostReceiveRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(NotificationsServer).PostReceive(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/gitaly.Notifications/PostReceive", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(NotificationsServer).PostReceive(ctx, req.(*PostReceiveRequest)) + } + return interceptor(ctx, in, info, handler) +} + +var _Notifications_serviceDesc = grpc.ServiceDesc{ + ServiceName: "gitaly.Notifications", + HandlerType: (*NotificationsServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "PostReceive", + Handler: _Notifications_PostReceive_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "gitaly.proto", +} + func init() { proto.RegisterFile("gitaly.proto", fileDescriptor0) } var fileDescriptor0 = []byte{ - // 202 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xe2, 0x49, 0xcf, 0x2c, 0x49, - 0xcc, 0xa9, 0xd4, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x83, 0xf0, 0x94, 0x5c, 0xb9, 0xf8, - 0x3d, 0xf3, 0xd2, 0xf2, 0x83, 0x52, 0xd3, 0x8a, 0x83, 0x52, 0x0b, 0x4b, 0x53, 0x8b, 0x4b, 0x84, - 0x8c, 0xb8, 0xb8, 0x8a, 0x52, 0x0b, 0xf2, 0x8b, 0x33, 0x4b, 0xf2, 0x8b, 0x2a, 0x25, 0x18, 0x15, - 0x18, 0x35, 0xb8, 0x8d, 0x84, 0xf4, 0xa0, 0xba, 0x83, 0xe0, 0x32, 0x41, 0x48, 0xaa, 0x94, 0xd4, - 0xb8, 0x04, 0x10, 0xc6, 0x14, 0x17, 0xe4, 0xe7, 0x15, 0xa7, 0x0a, 0x09, 0x71, 0xb1, 0xa4, 0x24, - 0x96, 0x24, 0x82, 0x4d, 0xe0, 0x09, 0x02, 0xb3, 0x95, 0x14, 0xb8, 0xb8, 0x10, 0x26, 0x80, 0x54, - 0x14, 0x24, 0x96, 0x64, 0x80, 0x55, 0x70, 0x06, 0x81, 0xd9, 0x46, 0xcb, 0x18, 0xb9, 0x38, 0x83, - 0x73, 0x13, 0x8b, 0x4a, 0x3c, 0x42, 0x42, 0x02, 0x84, 0xbc, 0xb9, 0x84, 0x60, 0xe6, 0x86, 0x16, - 0xe4, 0xe4, 0x27, 0xa6, 0x04, 0x24, 0x26, 0x67, 0x0b, 0x89, 0xc3, 0x5c, 0x83, 0xe6, 0x74, 0x29, - 0x09, 0x4c, 0x09, 0x88, 0x63, 0x94, 0x18, 0x0c, 0x18, 0x85, 0x7c, 0xb8, 0x84, 0x11, 0xe2, 0xc9, - 0xa9, 0x99, 0x65, 0xa9, 0x14, 0x98, 0x96, 0xc4, 0x06, 0x0e, 0x48, 0x63, 0x40, 0x00, 0x00, 0x00, - 0xff, 0xff, 0x35, 0xf6, 0x6a, 0x74, 0x58, 0x01, 0x00, 0x00, + // 256 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xa4, 0x92, 0xc1, 0x4a, 0xc3, 0x40, + 0x10, 0x86, 0x0d, 0x48, 0xa1, 0xd3, 0x8a, 0x32, 0x45, 0x2c, 0xf1, 0x52, 0xf6, 0x20, 0x9e, 0x8a, + 0xc4, 0x67, 0x10, 0x22, 0x8a, 0x84, 0xb5, 0x1e, 0x3c, 0xae, 0xe9, 0x44, 0x17, 0x6b, 0x66, 0xcd, + 0x8e, 0x42, 0x5f, 0xc8, 0xe7, 0x14, 0x37, 0xae, 0xa9, 0xd6, 0x5b, 0x6e, 0xb3, 0xfb, 0xff, 0xf3, + 0xed, 0xbf, 0xc3, 0xc0, 0xf8, 0xd1, 0x8a, 0x59, 0xad, 0xe7, 0xae, 0x61, 0x61, 0x1c, 0xb4, 0x27, + 0x75, 0x01, 0xfb, 0x97, 0x75, 0xc5, 0x9a, 0x2a, 0xaf, 0xe9, 0xf5, 0x8d, 0xbc, 0x60, 0x06, 0xd0, + 0x90, 0x63, 0x6f, 0x85, 0x9b, 0xf5, 0x34, 0x99, 0x25, 0xa7, 0xa3, 0x0c, 0xe7, 0xdf, 0xdd, 0xfa, + 0x47, 0xd1, 0x1b, 0x2e, 0x75, 0x02, 0x07, 0x1d, 0xc6, 0x3b, 0xae, 0x3d, 0x21, 0xc2, 0xee, 0xd2, + 0x88, 0x09, 0x84, 0xb1, 0x0e, 0xb5, 0x9a, 0x01, 0x74, 0x84, 0x2f, 0x87, 0x33, 0xf2, 0x14, 0x1c, + 0x43, 0x1d, 0x6a, 0x95, 0x03, 0x16, 0xec, 0x45, 0x53, 0x49, 0xf6, 0x9d, 0xfa, 0x64, 0x3a, 0x84, + 0xc9, 0x2f, 0x52, 0x1b, 0x2b, 0xfb, 0x48, 0x60, 0x78, 0xfb, 0x62, 0x1a, 0xc9, 0x17, 0x8b, 0x02, + 0xaf, 0x00, 0x63, 0xf0, 0x3b, 0xb7, 0x62, 0xb3, 0x2c, 0x4c, 0xf9, 0x8c, 0x47, 0x11, 0xfd, 0x67, + 0x36, 0xe9, 0x74, 0x5b, 0x68, 0xb1, 0x6a, 0xe7, 0x2c, 0xc1, 0x6b, 0x98, 0x74, 0xf7, 0xe1, 0xd5, + 0x1e, 0xb4, 0xec, 0x1e, 0xf6, 0x6e, 0x58, 0x6c, 0x65, 0x4b, 0x23, 0x96, 0x6b, 0x8f, 0x39, 0x8c, + 0x36, 0x3e, 0x84, 0x69, 0xec, 0xde, 0x9e, 0x57, 0x7a, 0xfc, 0xaf, 0x16, 0xe1, 0x0f, 0x83, 0xb0, + 0x04, 0xe7, 0x9f, 0x01, 0x00, 0x00, 0xff, 0xff, 0x86, 0x0b, 0x65, 0xc1, 0x14, 0x02, 0x00, 0x00, } diff --git a/protos/ruby/lib/gitaly/gitaly_pb.rb b/protos/ruby/lib/gitaly/gitaly_pb.rb index 8067e68edb..9a040343e7 100644 --- a/protos/ruby/lib/gitaly/gitaly_pb.rb +++ b/protos/ruby/lib/gitaly/gitaly_pb.rb @@ -13,10 +13,17 @@ Google::Protobuf::DescriptorPool.generated_pool.build do add_message "gitaly.Repository" do optional :path, :string, 1 end + add_message "gitaly.PostReceiveRequest" do + optional :repository, :message, 1, "gitaly.Repository" + end + add_message "gitaly.PostReceiveResponse" do + end end module Gitaly InfoRefsRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("gitaly.InfoRefsRequest").msgclass InfoRefsResponse = Google::Protobuf::DescriptorPool.generated_pool.lookup("gitaly.InfoRefsResponse").msgclass Repository = Google::Protobuf::DescriptorPool.generated_pool.lookup("gitaly.Repository").msgclass + PostReceiveRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("gitaly.PostReceiveRequest").msgclass + PostReceiveResponse = Google::Protobuf::DescriptorPool.generated_pool.lookup("gitaly.PostReceiveResponse").msgclass end diff --git a/protos/ruby/lib/gitaly/gitaly_services_pb.rb b/protos/ruby/lib/gitaly/gitaly_services_pb.rb index ec707bc581..52872cd371 100644 --- a/protos/ruby/lib/gitaly/gitaly_services_pb.rb +++ b/protos/ruby/lib/gitaly/gitaly_services_pb.rb @@ -21,6 +21,20 @@ module Gitaly rpc :InfoRefsReceivePack, InfoRefsRequest, stream(InfoRefsResponse) end + Stub = Service.rpc_stub_class + end + module Notifications + class Service + + include GRPC::GenericService + + self.marshal_class_method = :encode + self.unmarshal_class_method = :decode + self.service_name = 'gitaly.Notifications' + + rpc :PostReceive, PostReceiveRequest, PostReceiveResponse + end + Stub = Service.rpc_stub_class end end -- GitLab From 83ef4c3440edec0233698a00aa79e346822c9274 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Rodr=C3=ADguez?= Date: Fri, 10 Feb 2017 08:55:36 -0300 Subject: [PATCH 2/2] Handle empty post-receive requests --- internal/service/notifications/post_receive.go | 7 +++++++ internal/service/notifications/post_receive_test.go | 13 +++++++++++++ 2 files changed, 20 insertions(+) diff --git a/internal/service/notifications/post_receive.go b/internal/service/notifications/post_receive.go index f0be78a270..c3546eab56 100644 --- a/internal/service/notifications/post_receive.go +++ b/internal/service/notifications/post_receive.go @@ -1,6 +1,7 @@ package notifications import ( + "errors" "log" pb "gitlab.com/gitlab-org/gitaly/protos/go" @@ -9,6 +10,12 @@ import ( func (s *server) PostReceive(ctx context.Context, in *pb.PostReceiveRequest) (*pb.PostReceiveResponse, error) { // TODO: Invalidate InfoRefs cache + if in.Repository == nil { + message := "Bad Request (empty repository)" + log.Printf("PostReceive: %q", message) + return &pb.PostReceiveResponse{}, errors.New(message) + } + log.Printf("PostReceive: RepoPath=%q", in.Repository.Path) return &pb.PostReceiveResponse{}, nil } diff --git a/internal/service/notifications/post_receive_test.go b/internal/service/notifications/post_receive_test.go index 5c0dfebd52..bdc501c78a 100644 --- a/internal/service/notifications/post_receive_test.go +++ b/internal/service/notifications/post_receive_test.go @@ -46,6 +46,19 @@ func TestSuccessfulPostReceive(t *testing.T) { } } +func TestEmptyPostReceiveRequest(t *testing.T) { + server := runNotificationsServer(t) + defer server.Stop() + + client := newNotificationsClient(t) + rpcRequest := &pb.PostReceiveRequest{} + + _, err := client.PostReceive(context.Background(), rpcRequest) + if err.Error() != "rpc error: code = 2 desc = Bad Request (empty repository)" { + t.Fatal(err) + } +} + func runNotificationsServer(t *testing.T) *grpc.Server { server := grpc.NewServer() listener, err := net.Listen("unix", serverSocketPath) -- GitLab