Skip to content

Commit

Permalink
feat: code ref service
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubisoft-potato committed Dec 31, 2024
1 parent bb4423e commit 762205c
Show file tree
Hide file tree
Showing 29 changed files with 7,132 additions and 1,876 deletions.
4 changes: 2 additions & 2 deletions manifests/bucketeer/charts/web/values.yaml

Large diffs are not rendered by default.

146 changes: 146 additions & 0 deletions pkg/coderef/api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// Copyright 2024 The Bucketeer Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package api

import (
"context"

"go.uber.org/zap"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

v2 "github.com/bucketeer-io/bucketeer/pkg/coderef/storage/v2"
"github.com/bucketeer-io/bucketeer/pkg/locale"
"github.com/bucketeer-io/bucketeer/pkg/log"
"github.com/bucketeer-io/bucketeer/pkg/pubsub/publisher"
"github.com/bucketeer-io/bucketeer/pkg/role"
"github.com/bucketeer-io/bucketeer/pkg/storage/v2/mysql"
accountproto "github.com/bucketeer-io/bucketeer/proto/account"
proto "github.com/bucketeer-io/bucketeer/proto/coderef"
eventproto "github.com/bucketeer-io/bucketeer/proto/event/domain"
)

type options struct {
logger *zap.Logger
}

var defaultOptions = options{
logger: zap.NewNop(),
}

type Option func(*options)

func WithLogger(logger *zap.Logger) Option {
return func(opts *options) {
opts.logger = logger
}
}

type CodeReferenceService struct {
codeRefStorage v2.CodeReferenceStorage
publisher publisher.Publisher
opts *options
logger *zap.Logger
}

func NewCodeReferenceService(
mysqlClient mysql.Client,
publisher publisher.Publisher,
opts ...Option,
) *CodeReferenceService {
options := defaultOptions
for _, opt := range opts {
opt(&options)
}
return &CodeReferenceService{
codeRefStorage: v2.NewCodeReferenceStorage(mysqlClient),
publisher: publisher,
opts: &options,
logger: options.logger.Named("api"),
}
}

func (s *CodeReferenceService) Register(server *grpc.Server) {
proto.RegisterCodeReferenceServiceServer(server, s)
}

func (s *CodeReferenceService) checkEnvironmentRole(
ctx context.Context,
requiredRole accountproto.AccountV2_Role_Environment,
environmentID string,
localizer locale.Localizer,
) (*eventproto.Editor, error) {
editor, err := role.CheckEnvironmentRole(
ctx,
requiredRole,
environmentID,
nil, // TODO: implement account lookup function
)
if err != nil {
switch status.Code(err) {
case codes.Unauthenticated:
s.logger.Info(
"Unauthenticated",
log.FieldsFromImcomingContext(ctx).AddFields(
zap.Error(err),
zap.String("environmentId", environmentID),
)...,
)
dt, err := statusUnauthenticated.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.UnauthenticatedError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
case codes.PermissionDenied:
s.logger.Info(
"Permission denied",
log.FieldsFromImcomingContext(ctx).AddFields(
zap.Error(err),
zap.String("environmentId", environmentID),
)...,
)
dt, err := statusPermissionDenied.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.PermissionDenied),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
default:
s.logger.Error(
"Failed to check role",
log.FieldsFromImcomingContext(ctx).AddFields(
zap.Error(err),
zap.String("environmentId", environmentID),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
}
}
return editor, nil
}
Loading

0 comments on commit 762205c

Please sign in to comment.