Skip to content

Commit

Permalink
fix(dicehub): public field push exception for the extension (erda-pro…
Browse files Browse the repository at this point in the history
…ject#6265)

* fix public push exception for the extension

Signed-off-by: iutx <[email protected]>

* fix monkey reflect obj error

Signed-off-by: iutx <[email protected]>

---------

Signed-off-by: iutx <[email protected]>
  • Loading branch information
iutx authored Feb 4, 2024
1 parent 7f5a3bc commit daf01c2
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
9 changes: 9 additions & 0 deletions internal/pkg/extension/db/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,12 @@ func (client *Client) ListExtensionVersions(names []string, all bool) (map[strin

return extensions, err
}

func (client *Client) IsExtensionPublicVersionExist(name string) (bool, error) {
var count int64
err := client.Model(&ExtensionVersion{}).
Where("name = ? ", name).
Where("public = ?", true).
Count(&count).Error
return count > 0, err
}
15 changes: 14 additions & 1 deletion internal/pkg/extension/extension.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,15 @@ func (s *provider) CreateExtensionVersionByRequest(req *pb.ExtensionVersionCreat
}
s.triggerPushEvent(specData, "update")
if req.All {
isPublic, err := s.isExtensionPublic(req.Name, req.Public)
if err != nil {
return nil, apierrors.ErrQueryExtension.InternalError(err)
}
extModel.Category = specData.Category
extModel.LogoUrl = specData.LogoUrl
extModel.DisplayName = specData.DisplayName
extModel.Desc = specData.Desc
extModel.Public = req.Public
extModel.Public = isPublic
extModel.Labels = labels
err = s.db.Save(&extModel).Error
if err != nil {
Expand Down Expand Up @@ -360,6 +364,15 @@ func (s *provider) CreateExtensionVersionByRequest(req *pb.ExtensionVersionCreat
}
}

func (s *provider) isExtensionPublic(name string, isPublic bool) (bool, error) {
isExist, err := s.db.IsExtensionPublicVersionExist(name)
if err != nil {
return false, fmt.Errorf("query extension public version error, %v", err)
}

return isExist || isPublic, nil
}

func (s *provider) DeleteExtensionVersion(name, version string) error {
if len(name) == 0 {
return apierrors.ErrDeleteExtensionVersion.MissingParameter("name")
Expand Down
50 changes: 50 additions & 0 deletions internal/pkg/extension/extension.service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ func mockClient() *db.Client {
}
return res, nil
})
monkey.PatchInstanceMethod(reflect.TypeOf(client), "IsExtensionPublicVersionExist", func(_ *db.Client, name string) (bool, error) {
if name == "deprecated-addon" {
return false, nil
}
return true, nil
})
monkey.PatchInstanceMethod(reflect.TypeOf(client.DB), "Save", func(_ *gorm.DB, value interface{}) *gorm.DB {
return &gorm.DB{
Error: nil,
Expand Down Expand Up @@ -643,3 +649,47 @@ func Test_extensionService_QueryExtensionVersions(t *testing.T) {
})
}
}

func Test_isExtensionPublic(t *testing.T) {
p := &provider{
db: cli,
}

type args struct {
name string
isPublic bool
}

tests := []struct {
name string
args args
expect bool
}{
{
name: "update processing extension is private",
args: args{
name: "mysql",
isPublic: false,
},
expect: true,
},
{
name: "all private",
args: args{
name: "deprecated-addon",
isPublic: false,
},
expect: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := p.isExtensionPublic(tt.args.name, tt.args.isPublic)
assert.NoError(t, err)
if got != tt.expect {
t.Fatalf("expect: %v, got: %v", tt.expect, got)
}
})
}
}

0 comments on commit daf01c2

Please sign in to comment.