Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions apptrust/commands/package/bind_package_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
type bindPackageCommand struct {
packageService packages.PackageService
serverDetails *coreConfig.ServerDetails
applicationKey string
requestPayload *model.BindPackageRequest
}

Expand All @@ -25,7 +26,7 @@ func (bp *bindPackageCommand) Run() error {
if err != nil {
return err
}
return bp.packageService.BindPackage(ctx, bp.requestPayload)
return bp.packageService.BindPackage(ctx, bp.applicationKey, bp.requestPayload)
}

func (bp *bindPackageCommand) ServerDetails() (*coreConfig.ServerDetails, error) {
Expand All @@ -46,14 +47,24 @@ func (bp *bindPackageCommand) prepareAndRunCommand(ctx *components.Context) erro
if err != nil {
return err
}
bp.requestPayload, err = BuildPackageRequestPayload(ctx)
if err != nil {
return err
}
bp.extractFromArgs(ctx)

return commonCLiCommands.Exec(bp)
}

func (bp *bindPackageCommand) extractFromArgs(ctx *components.Context) {
bp.applicationKey = ctx.Arguments[0]
packageType := ctx.Arguments[1]
packageName := ctx.Arguments[2]
version := ctx.Arguments[3]

bp.requestPayload = &model.BindPackageRequest{
Type: packageType,
Name: packageName,
Version: version,
}
}

func GetBindPackageCommand(appContext app.Context) components.Command {
cmd := &bindPackageCommand{packageService: appContext.GetPackageService()}
return components.Command{
Expand All @@ -75,8 +86,8 @@ func GetBindPackageCommand(appContext app.Context) components.Command {
Description: "Package name.",
},
{
Name: "package-versions",
Description: "Comma-separated versions of the package to bind (e.g., '1.0.0,1.1.0,1.2.0').",
Name: "package-version",
Description: "Package version.",
},
},
Flags: commands.GetCommandFlags(commands.PackageBind),
Expand Down
22 changes: 12 additions & 10 deletions apptrust/commands/package/bind_package_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,21 @@ func TestBindPackageCommand_Run(t *testing.T) {
defer ctrl.Finish()

serverDetails := &config.ServerDetails{Url: "https://example.com"}
applicationKey := "app-key"
requestPayload := &model.BindPackageRequest{
ApplicationKey: "app-key",
Type: "npm",
Name: "test-package",
Versions: []string{"1.0.0"},
Type: "npm",
Name: "test-package",
Version: "1.0.0",
}

mockPackageService := mockpackages.NewMockPackageService(ctrl)
mockPackageService.EXPECT().BindPackage(gomock.Any(), requestPayload).
mockPackageService.EXPECT().BindPackage(gomock.Any(), applicationKey, requestPayload).
Return(nil).Times(1)

cmd := &bindPackageCommand{
packageService: mockPackageService,
serverDetails: serverDetails,
applicationKey: applicationKey,
requestPayload: requestPayload,
}

Expand All @@ -42,20 +43,21 @@ func TestBindPackageCommand_Run_Error(t *testing.T) {
defer ctrl.Finish()

serverDetails := &config.ServerDetails{Url: "https://example.com"}
applicationKey := "app-key"
requestPayload := &model.BindPackageRequest{
ApplicationKey: "app-key",
Type: "npm",
Name: "test-package",
Versions: []string{"1.0.0"},
Type: "npm",
Name: "test-package",
Version: "1.0.0",
}

mockPackageService := mockpackages.NewMockPackageService(ctrl)
mockPackageService.EXPECT().BindPackage(gomock.Any(), requestPayload).
mockPackageService.EXPECT().BindPackage(gomock.Any(), applicationKey, requestPayload).
Return(errors.New("bind error")).Times(1)

cmd := &bindPackageCommand{
packageService: mockPackageService,
serverDetails: serverDetails,
applicationKey: applicationKey,
requestPayload: requestPayload,
}

Expand Down
24 changes: 14 additions & 10 deletions apptrust/commands/package/unbind_package_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/jfrog/jfrog-cli-application/apptrust/commands"
"github.com/jfrog/jfrog-cli-application/apptrust/commands/utils"
"github.com/jfrog/jfrog-cli-application/apptrust/common"
"github.com/jfrog/jfrog-cli-application/apptrust/model"
"github.com/jfrog/jfrog-cli-application/apptrust/service"
"github.com/jfrog/jfrog-cli-application/apptrust/service/packages"
commonCLiCommands "github.com/jfrog/jfrog-cli-core/v2/common/commands"
Expand All @@ -17,15 +16,18 @@ import (
type unbindPackageCommand struct {
packageService packages.PackageService
serverDetails *coreConfig.ServerDetails
requestPayload *model.BindPackageRequest
applicationKey string
packageType string
packageName string
packageVersion string
}

func (up *unbindPackageCommand) Run() error {
ctx, err := service.NewContext(*up.serverDetails)
if err != nil {
return err
}
return up.packageService.UnbindPackage(ctx, up.requestPayload)
return up.packageService.UnbindPackage(ctx, up.applicationKey, up.packageType, up.packageName, up.packageVersion)
}

func (up *unbindPackageCommand) ServerDetails() (*coreConfig.ServerDetails, error) {
Expand All @@ -37,7 +39,7 @@ func (up *unbindPackageCommand) CommandName() string {
}

func (up *unbindPackageCommand) prepareAndRunCommand(ctx *components.Context) error {
if len(ctx.Arguments) < 3 || len(ctx.Arguments) > 4 {
if len(ctx.Arguments) != 4 {
return pluginsCommon.WrongNumberOfArgumentsHandler(ctx)
}

Expand All @@ -46,10 +48,12 @@ func (up *unbindPackageCommand) prepareAndRunCommand(ctx *components.Context) er
if err != nil {
return err
}
up.requestPayload, err = BuildPackageRequestPayload(ctx)
if err != nil {
return err
}

// Extract from arguments
up.applicationKey = ctx.Arguments[0]
up.packageType = ctx.Arguments[1]
up.packageName = ctx.Arguments[2]
up.packageVersion = ctx.Arguments[3]

return commonCLiCommands.Exec(up)
}
Expand All @@ -75,8 +79,8 @@ func GetUnbindPackageCommand(appContext app.Context) components.Command {
Description: "Package name.",
},
{
Name: "package-versions",
Description: "Comma-separated versions of the package to unbind (e.g., '1.0.0,1.1.0,1.2.0'). If omitted, all versions will be unbound.",
Name: "package-version",
Description: "Package version.",
},
},
Flags: commands.GetCommandFlags(commands.PackageUnbind),
Expand Down
35 changes: 18 additions & 17 deletions apptrust/commands/package/unbind_package_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"testing"

"github.com/jfrog/jfrog-cli-application/apptrust/model"
mockpackages "github.com/jfrog/jfrog-cli-application/apptrust/service/packages/mocks"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/stretchr/testify/assert"
Expand All @@ -16,21 +15,22 @@ func TestUnbindPackageCommand_Run(t *testing.T) {
defer ctrl.Finish()

serverDetails := &config.ServerDetails{Url: "https://example.com"}
requestPayload := &model.BindPackageRequest{
ApplicationKey: "app-key",
Type: "npm",
Name: "test-package",
Versions: []string{"1.0.0"},
}
applicationKey := "app-key"
pkgType := "npm"
pkgName := "test-package"
pkgVersion := "1.0.0"

mockPackageService := mockpackages.NewMockPackageService(ctrl)
mockPackageService.EXPECT().UnbindPackage(gomock.Any(), requestPayload).
mockPackageService.EXPECT().UnbindPackage(gomock.Any(), applicationKey, pkgType, pkgName, pkgVersion).
Return(nil).Times(1)

cmd := &unbindPackageCommand{
packageService: mockPackageService,
serverDetails: serverDetails,
requestPayload: requestPayload,
applicationKey: applicationKey,
packageType: pkgType,
packageName: pkgName,
packageVersion: pkgVersion,
}

err := cmd.Run()
Expand All @@ -42,21 +42,22 @@ func TestUnbindPackageCommand_Run_Error(t *testing.T) {
defer ctrl.Finish()

serverDetails := &config.ServerDetails{Url: "https://example.com"}
requestPayload := &model.BindPackageRequest{
ApplicationKey: "app-key",
Type: "npm",
Name: "test-package",
Versions: []string{"1.0.0"},
}
applicationKey := "app-key"
pkgType := "npm"
pkgName := "test-package"
pkgVersion := "1.0.0"

mockPackageService := mockpackages.NewMockPackageService(ctrl)
mockPackageService.EXPECT().UnbindPackage(gomock.Any(), requestPayload).
mockPackageService.EXPECT().UnbindPackage(gomock.Any(), applicationKey, pkgType, pkgName, pkgVersion).
Return(errors.New("unbind error")).Times(1)

cmd := &unbindPackageCommand{
packageService: mockPackageService,
serverDetails: serverDetails,
requestPayload: requestPayload,
applicationKey: applicationKey,
packageType: pkgType,
packageName: pkgName,
packageVersion: pkgVersion,
}

err := cmd.Run()
Expand Down
44 changes: 0 additions & 44 deletions apptrust/commands/package/utils.go

This file was deleted.

61 changes: 0 additions & 61 deletions apptrust/commands/package/utils_test.go

This file was deleted.

14 changes: 3 additions & 11 deletions apptrust/http/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type ApptrustHttpClient interface {
Post(path string, requestBody interface{}, params map[string]string) (resp *http.Response, body []byte, err error)
Get(path string) (resp *http.Response, body []byte, err error)
Patch(path string, requestBody interface{}) (resp *http.Response, body []byte, err error)
Delete(path string, requestBody interface{}) (resp *http.Response, body []byte, err error)
Delete(path string) (resp *http.Response, body []byte, err error)
}

type apptrustHttpClient struct {
Expand Down Expand Up @@ -139,22 +139,14 @@ func (c *apptrustHttpClient) toJsonBytes(payload interface{}) ([]byte, error) {
return jsonBytes, nil
}

func (c *apptrustHttpClient) Delete(path string, requestBody interface{}) (resp *http.Response, body []byte, err error) {
func (c *apptrustHttpClient) Delete(path string) (resp *http.Response, body []byte, err error) {
url, err := utils.BuildUrl(c.serverDetails.Url, apptrustApiPath+path, nil)
if err != nil {
return nil, nil, err
}

var requestContent []byte
if requestBody != nil {
requestContent, err = c.toJsonBytes(requestBody)
if err != nil {
return nil, nil, err
}
}

log.Debug("Sending DELETE request to:", url)
return c.client.SendDelete(url, requestContent, c.getJsonHttpClientDetails())
return c.client.SendDelete(url, nil, c.getJsonHttpClientDetails())
}

func (c *apptrustHttpClient) getJsonHttpClientDetails() *httputils.HttpClientDetails {
Expand Down
7 changes: 3 additions & 4 deletions apptrust/model/bind_package_request.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package model

type BindPackageRequest struct {
ApplicationKey string `json:"application_key"`
Type string `json:"type"`
Name string `json:"name"`
Versions []string `json:"versions,omitempty"`
Type string `json:"package_type"`
Name string `json:"package_name"`
Version string `json:"package_version"`
}
Loading
Loading