From 45c5871bc893b7c364b8da5537555b8713b795cb Mon Sep 17 00:00:00 2001 From: Manfred Touron <94029+moul@users.noreply.github.com> Date: Thu, 30 Apr 2020 20:48:47 +0200 Subject: [PATCH] feat: 'yolo dump-objects' cli --- go/cmd/yolo/main.go | 12 +++-- go/pkg/yolosvc/api_dev.go | 100 ++++++++++++++++++++++++++++---------- 2 files changed, 82 insertions(+), 30 deletions(-) diff --git a/go/cmd/yolo/main.go b/go/cmd/yolo/main.go index c6be24c1..d3e8e4a4 100644 --- a/go/cmd/yolo/main.go +++ b/go/cmd/yolo/main.go @@ -12,6 +12,7 @@ import ( "time" "berty.tech/yolo/v2/go/pkg/bintray" + "berty.tech/yolo/v2/go/pkg/yolopb" "berty.tech/yolo/v2/go/pkg/yolosvc" bearer "github.com/Bearer/bearer-go" "github.com/buildkite/go-buildkite/buildkite" @@ -52,6 +53,7 @@ func yolo(args []string) error { bintrayToken string circleciToken string dbStorePath string + withPreloading bool grpcBind string httpBind string corsAllowedOrigins string @@ -77,8 +79,9 @@ func yolo(args []string) error { serverFlagSet.StringVar(&bintrayToken, "bintray-token", "", "Bintray API Token") serverFlagSet.StringVar(&circleciToken, "circleci-token", "", "CircleCI API Token") serverFlagSet.StringVar(&githubToken, "github-token", "", "GitHub API Token") - serverFlagSet.StringVar(&dbStorePath, "db-path", ":temp:", "DB Store path") - storeFlagSet.StringVar(&dbStorePath, "db-path", ":temp:", "DB Store path") + serverFlagSet.StringVar(&dbStorePath, "db-path", ":memory:", "DB Store path") + storeFlagSet.StringVar(&dbStorePath, "db-path", ":memory:", "DB Store path") + storeFlagSet.BoolVar(&withPreloading, "with-preloading", false, "with auto DB preloading") serverFlagSet.IntVar(&maxBuilds, "max-builds", 100, "maximum builds to fetch from external services (pagination)") serverFlagSet.StringVar(&httpBind, "http-bind", ":8000", "HTTP bind address") serverFlagSet.StringVar(&grpcBind, "grpc-bind", ":9000", "gRPC bind address") @@ -227,7 +230,10 @@ func yolo(args []string) error { } ctx := context.Background() - ret, err := svc.DevDumpObjects(ctx, nil) + input := &yolopb.DevDumpObjects_Request{ + WithPreloading: withPreloading, + } + ret, err := svc.DevDumpObjects(ctx, input) if err != nil { return err } diff --git a/go/pkg/yolosvc/api_dev.go b/go/pkg/yolosvc/api_dev.go index b9b6c4bb..7f06d492 100644 --- a/go/pkg/yolosvc/api_dev.go +++ b/go/pkg/yolosvc/api_dev.go @@ -2,7 +2,6 @@ package yolosvc import ( "context" - "fmt" "berty.tech/yolo/v2/go/pkg/yolopb" "google.golang.org/grpc/codes" @@ -10,40 +9,87 @@ import ( ) func (svc service) DevDumpObjects(ctx context.Context, req *yolopb.DevDumpObjects_Request) (*yolopb.DevDumpObjects_Response, error) { + if req == nil { + req = &yolopb.DevDumpObjects_Request{} + } if !svc.devMode { return nil, status.Error(codes.PermissionDenied, "Permission Denied") } batch := yolopb.NewBatch() - db := svc.db if req.WithPreloading { - fmt.Println("####################", true) - db = db.Set("gorm:auto_preload", true) + if err := svc.db. + Preload("HasOwner"). + Find(&batch.Projects).Error; err != nil { + return nil, err + } + if err := svc.db. + // FIXME: TODO + Find(&batch.Entities).Error; err != nil { + return nil, err + } + if err := svc.db. + Preload("HasCommit"). + Preload("HasProject"). + Preload("HasMergerequest"). + Preload("HasMergerequest.HasProject"). + Preload("HasMergerequest.HasProject.HasOwner"). + Preload("HasMergerequest.HasAuthor"). + Preload("HasMergerequest.HasCommit"). + Find(&batch.Builds).Error; err != nil { + return nil, err + } + if err := svc.db. + Preload("HasBuild"). + Preload("HasBuild.HasProject"). + Preload("HasBuild.HasMergerequest"). + Preload("HasBuild.HasMergerequest.HasProject"). + Preload("HasBuild.HasMergerequest.HasAuthor"). + Preload("HasBuild.HasMergerequest.HasCommit"). + Preload("HasBuild.HasCommit"). + Find(&batch.Artifacts).Error; err != nil { + return nil, err + } + if err := svc.db. + Preload("HasCommit"). + Preload("HasProject"). + Preload("HasAuthor"). + Find(&batch.MergeRequests).Error; err != nil { + return nil, err + } + if err := svc.db. + // FIXME: TODO + Find(&batch.Releases).Error; err != nil { + return nil, err + } + if err := svc.db. + // FIXME: TODO + Find(&batch.Commits).Error; err != nil { + return nil, err + } } else { - fmt.Println("####################", false) - } - - if err := db.Find(&batch.Projects).Error; err != nil { - return nil, err - } - if err := db.Find(&batch.Entities).Error; err != nil { - return nil, err - } - if err := db.Find(&batch.Builds).Error; err != nil { - return nil, err - } - if err := db.Find(&batch.Artifacts).Error; err != nil { - return nil, err - } - if err := db.Find(&batch.MergeRequests).Error; err != nil { - return nil, err - } - if err := db.Find(&batch.Releases).Error; err != nil { - return nil, err - } - if err := db.Find(&batch.Commits).Error; err != nil { - return nil, err + if err := svc.db.Find(&batch.Projects).Error; err != nil { + return nil, err + } + if err := svc.db.Find(&batch.Entities).Error; err != nil { + return nil, err + } + if err := svc.db.Find(&batch.Builds).Error; err != nil { + return nil, err + } + if err := svc.db.Find(&batch.Artifacts).Error; err != nil { + return nil, err + } + if err := svc.db.Find(&batch.MergeRequests).Error; err != nil { + return nil, err + } + if err := svc.db.Find(&batch.Releases).Error; err != nil { + return nil, err + } + if err := svc.db.Find(&batch.Commits).Error; err != nil { + return nil, err + } } resp := yolopb.DevDumpObjects_Response{