Skip to content

Commit

Permalink
Merge pull request #802 from traPtitech/repo-scoped-fetch
Browse files Browse the repository at this point in the history
Repo scoped apps fetch
  • Loading branch information
motoki317 authored Dec 4, 2023
2 parents ae4c3c0 + 17db285 commit cd16614
Show file tree
Hide file tree
Showing 7 changed files with 420 additions and 353 deletions.
2 changes: 2 additions & 0 deletions api/proto/neoshowcase/protobuf/gateway.proto
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,10 @@ message GetApplicationsRequest {
enum Scope {
MINE = 0;
ALL = 1;
REPOSITORY = 2;
}
Scope scope = 1;
optional string repository_id = 2;
}

message UpdateApplicationRequest {
Expand Down
12 changes: 12 additions & 0 deletions dashboard/src/api/neoshowcase/protobuf/gateway_pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2630,6 +2630,11 @@ export class GetApplicationsRequest extends Message<GetApplicationsRequest> {
*/
scope = GetApplicationsRequest_Scope.MINE;

/**
* @generated from field: optional string repository_id = 2;
*/
repositoryId?: string;

constructor(data?: PartialMessage<GetApplicationsRequest>) {
super();
proto3.util.initPartial(data, this);
Expand All @@ -2639,6 +2644,7 @@ export class GetApplicationsRequest extends Message<GetApplicationsRequest> {
static readonly typeName = "neoshowcase.protobuf.GetApplicationsRequest";
static readonly fields: FieldList = proto3.util.newFieldList(() => [
{ no: 1, name: "scope", kind: "enum", T: proto3.getEnumType(GetApplicationsRequest_Scope) },
{ no: 2, name: "repository_id", kind: "scalar", T: 9 /* ScalarType.STRING */, opt: true },
]);

static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): GetApplicationsRequest {
Expand Down Expand Up @@ -2671,11 +2677,17 @@ export enum GetApplicationsRequest_Scope {
* @generated from enum value: ALL = 1;
*/
ALL = 1,

/**
* @generated from enum value: REPOSITORY = 2;
*/
REPOSITORY = 2,
}
// Retrieve enum metadata with: proto3.getEnumType(GetApplicationsRequest_Scope)
proto3.util.setEnumType(GetApplicationsRequest_Scope, "neoshowcase.protobuf.GetApplicationsRequest.Scope", [
{ no: 0, name: "MINE" },
{ no: 1, name: "ALL" },
{ no: 2, name: "REPOSITORY" },
]);

/**
Expand Down
8 changes: 4 additions & 4 deletions dashboard/src/pages/repos/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ export default () => {
)
const [apps] = createResource(
() => params.id,
async () => {
const allAppsRes = await client.getApplications({ scope: GetApplicationsRequest_Scope.ALL })
return repo() ? allAppsRes.applications.filter((app) => app.repositoryId === repo()?.id) : []
},
(id) =>
client
.getApplications({ scope: GetApplicationsRequest_Scope.REPOSITORY, repositoryId: id })
.then((r) => r.applications),
)

const { Modal: DeleteRepoModal, open: openDeleteRepoModal, close: closeDeleteRepoModal } = useModal()
Expand Down
7 changes: 5 additions & 2 deletions pkg/infrastructure/grpc/api_app_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ func (s *APIService) CreateApplication(ctx context.Context, req *connect.Request
}

func (s *APIService) GetApplications(ctx context.Context, req *connect.Request[pb.GetApplicationsRequest]) (*connect.Response[pb.GetApplicationsResponse], error) {
all := req.Msg.Scope == pb.GetApplicationsRequest_ALL
apps, err := s.svc.GetApplications(ctx, all)
scope := apiserver.GetAppScope{
Scope: pbconvert.AppScopeMapper.IntoMust(req.Msg.Scope),
RepositoryID: optional.FromPtr(req.Msg.RepositoryId),
}
apps, err := s.svc.GetApplications(ctx, scope)
if err != nil {
return nil, handleUseCaseError(err)
}
Expand Down
703 changes: 360 additions & 343 deletions pkg/infrastructure/grpc/pb/gateway.pb.go

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions pkg/infrastructure/grpc/pbconvert/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ import (

"github.com/traPtitech/neoshowcase/pkg/domain"
"github.com/traPtitech/neoshowcase/pkg/infrastructure/grpc/pb"
"github.com/traPtitech/neoshowcase/pkg/usecase/apiserver"
"github.com/traPtitech/neoshowcase/pkg/util/ds"
"github.com/traPtitech/neoshowcase/pkg/util/mapper"
)

var AppScopeMapper = mapper.MustNewValueMapper(map[pb.GetApplicationsRequest_Scope]apiserver.GetAppScopeType{
pb.GetApplicationsRequest_MINE: apiserver.GetAppScopeMine,
pb.GetApplicationsRequest_ALL: apiserver.GetAppScopeAll,
pb.GetApplicationsRequest_REPOSITORY: apiserver.GetAppScopeRepository,
})

var DeployTypeMapper = mapper.MustNewValueMapper(map[domain.DeployType]pb.DeployType{
domain.DeployTypeRuntime: pb.DeployType_RUNTIME,
domain.DeployTypeStatic: pb.DeployType_STATIC,
Expand Down
34 changes: 30 additions & 4 deletions pkg/usecase/apiserver/app_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"context"
"fmt"
"github.com/regclient/regclient/types/ref"
"github.com/traPtitech/neoshowcase/pkg/util/regutil"
"strconv"

"github.com/traPtitech/neoshowcase/pkg/util/regutil"

"github.com/friendsofgo/errors"
"github.com/samber/lo"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -150,17 +151,42 @@ func (s *Service) createApplicationDatabase(ctx context.Context, app *domain.App
return nil
}

type GetAppScopeType int

const (
GetAppScopeMine GetAppScopeType = iota
GetAppScopeAll
GetAppScopeRepository
)

type GetAppScope struct {
Scope GetAppScopeType
RepositoryID optional.Of[string]
}

type TopAppInfo struct {
App *domain.Application
LatestBuild *domain.Build
}

func (s *Service) GetApplications(ctx context.Context, all bool) ([]*TopAppInfo, error) {
// Fetch apps
func (s *Service) GetApplications(ctx context.Context, scope GetAppScope) ([]*TopAppInfo, error) {
// Build fetch app condition
var cond domain.GetApplicationCondition
if !all {
switch scope.Scope {
case GetAppScopeMine:
cond.UserID = optional.From(web.GetUser(ctx).ID)
case GetAppScopeAll:
// No scope
case GetAppScopeRepository:
if !scope.RepositoryID.Valid {
return nil, errors.New("repository id not set")
}
cond.RepositoryID = scope.RepositoryID
default:
return nil, errors.New("unexpected scope type")
}

// Fetch apps
apps, err := s.appRepo.GetApplications(ctx, cond)
if err != nil {
return nil, err
Expand Down

0 comments on commit cd16614

Please sign in to comment.