-
Notifications
You must be signed in to change notification settings - Fork 5
Create application command #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
402ea93
Create application command
asafgabai 81bada5
Create application command
asafgabai bb83487
Create application command
asafgabai 9a463db
Create application command
asafgabai 9f1939d
Create application command
asafgabai 9edf5a4
Create application command
asafgabai 3ef91d9
Static analysis fixes
asafgabai f2106be
Create application command
asafgabai d6552a1
Create application command
asafgabai 362f730
Create application command
asafgabai 730d870
Create application command
asafgabai a38c311
Create application command
asafgabai 9f706be
Create application command
asafgabai e7f7c58
Create application command
asafgabai b9ec8c9
Create application command
asafgabai 0b7e0c3
Create application command
asafgabai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,7 +49,7 @@ prereq:: | |
| GOBIN=${TOOLS_DIR} $(GOCMD) install go.uber.org/mock/[email protected] | ||
| ${TOOLS_DIR}/mockgen --version | ||
|
|
||
| build:: | ||
| build:: clean generate-mock | ||
| $(GOCMD) env GOOS GOARCH | ||
| $(GOCMD) build -ldflags="${LINKERFLAGS}" -gcflags ${COMPILERFLAGS} -o ${BINARY_CLI}/application-cli-plugin main.go | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| package application | ||
|
|
||
| import ( | ||
| pluginsCommon "github.com/jfrog/jfrog-cli-core/v2/plugins/common" | ||
|
|
||
| "github.com/jfrog/jfrog-cli-application/application/commands/utils" | ||
| "github.com/jfrog/jfrog-cli-application/application/model" | ||
| "github.com/jfrog/jfrog-cli-application/application/service" | ||
| commonCLiCommands "github.com/jfrog/jfrog-cli-core/v2/common/commands" | ||
| "github.com/jfrog/jfrog-cli-core/v2/plugins/components" | ||
| coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config" | ||
| "github.com/jfrog/jfrog-client-go/utils/errorutils" | ||
|
|
||
| "github.com/jfrog/jfrog-cli-application/application/app" | ||
| "github.com/jfrog/jfrog-cli-application/application/commands" | ||
| "github.com/jfrog/jfrog-cli-application/application/common" | ||
| "github.com/jfrog/jfrog-cli-application/application/service/applications" | ||
| ) | ||
|
|
||
| type createAppCommand struct { | ||
| serverDetails *coreConfig.ServerDetails | ||
| applicationService applications.ApplicationService | ||
| requestBody *model.CreateAppRequest | ||
| } | ||
|
|
||
| func (cac *createAppCommand) Run() error { | ||
| ctx, err := service.NewContext(*cac.serverDetails) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return cac.applicationService.CreateApplication(ctx, cac.requestBody) | ||
| } | ||
|
|
||
| func (cac *createAppCommand) ServerDetails() (*coreConfig.ServerDetails, error) { | ||
| return cac.serverDetails, nil | ||
| } | ||
|
|
||
| func (cac *createAppCommand) CommandName() string { | ||
| return commands.CreateApp | ||
| } | ||
|
|
||
| func (cac *createAppCommand) buildRequestPayload(ctx *components.Context) (*model.CreateAppRequest, error) { | ||
| applicationKey := ctx.Arguments[0] | ||
| applicationName := ctx.GetStringFlagValue(commands.ApplicationNameFlag) | ||
| if applicationName == "" { | ||
| // Default to the application key if application name is not provided | ||
| applicationName = applicationKey | ||
| } | ||
|
|
||
| project := ctx.GetStringFlagValue(commands.ProjectFlag) | ||
| if project == "" { | ||
| return nil, errorutils.CheckErrorf("--%s is mandatory", commands.ProjectFlag) | ||
| } | ||
|
|
||
| businessCriticalityStr := ctx.GetStringFlagValue(commands.BusinessCriticalityFlag) | ||
| businessCriticality, err := utils.ValidateEnumFlag( | ||
| commands.BusinessCriticalityFlag, | ||
| businessCriticalityStr, | ||
| model.BusinessCriticalityUnspecified, | ||
| model.BusinessCriticalityValues) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| maturityLevelStr := ctx.GetStringFlagValue(commands.MaturityLevelFlag) | ||
| maturityLevel, err := utils.ValidateEnumFlag( | ||
| commands.MaturityLevelFlag, | ||
| maturityLevelStr, | ||
| model.MaturityLevelUnspecified, | ||
| model.MaturityLevelValues) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| description := ctx.GetStringFlagValue(commands.DescriptionFlag) | ||
| userOwners := utils.ParseSliceFlag(ctx.GetStringFlagValue(commands.UserOwnersFlag)) | ||
| groupOwners := utils.ParseSliceFlag(ctx.GetStringFlagValue(commands.GroupOwnersFlag)) | ||
| labelsMap, err := utils.ParseMapFlag(ctx.GetStringFlagValue(commands.LabelsFlag)) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &model.CreateAppRequest{ | ||
| ApplicationName: applicationName, | ||
| ApplicationKey: applicationKey, | ||
| Description: description, | ||
| ProjectKey: project, | ||
| MaturityLevel: maturityLevel, | ||
| BusinessCriticality: businessCriticality, | ||
| Labels: labelsMap, | ||
| UserOwners: userOwners, | ||
| GroupOwners: groupOwners, | ||
| }, nil | ||
| } | ||
|
|
||
| func (cac *createAppCommand) prepareAndRunCommand(ctx *components.Context) error { | ||
| if len(ctx.Arguments) != 1 { | ||
| return pluginsCommon.WrongNumberOfArgumentsHandler(ctx) | ||
| } | ||
|
|
||
| var err error | ||
| cac.requestBody, err = cac.buildRequestPayload(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| cac.serverDetails, err = utils.ServerDetailsByFlags(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return commonCLiCommands.Exec(cac) | ||
| } | ||
|
|
||
| func GetCreateAppCommand(appContext app.Context) components.Command { | ||
| cmd := &createAppCommand{ | ||
| applicationService: appContext.GetApplicationService(), | ||
| } | ||
| return components.Command{ | ||
| Name: "create", | ||
| Description: "Create a new application", | ||
| Category: common.CategoryApplication, | ||
| Arguments: []components.Argument{ | ||
| { | ||
| Name: "application-key", | ||
| Description: "The key of the application to create", | ||
| Optional: false, | ||
| }, | ||
| }, | ||
| Flags: commands.GetCommandFlags(commands.CreateApp), | ||
| Action: cmd.prepareAndRunCommand, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package application | ||
|
|
||
| import ( | ||
| "errors" | ||
| "flag" | ||
| "testing" | ||
|
|
||
| "github.com/urfave/cli" | ||
|
|
||
| "github.com/jfrog/jfrog-cli-application/application/model" | ||
| mockapps "github.com/jfrog/jfrog-cli-application/application/service/applications/mocks" | ||
| "github.com/jfrog/jfrog-cli-core/v2/plugins/components" | ||
| "github.com/jfrog/jfrog-cli-core/v2/utils/config" | ||
| "github.com/stretchr/testify/assert" | ||
| "go.uber.org/mock/gomock" | ||
| ) | ||
|
|
||
| func TestCreateAppCommand_Run(t *testing.T) { | ||
| ctrl := gomock.NewController(t) | ||
| defer ctrl.Finish() | ||
|
|
||
| serverDetails := &config.ServerDetails{Url: "https://example.com"} | ||
| requestPayload := &model.CreateAppRequest{ | ||
| ApplicationKey: "app-key", | ||
| ApplicationName: "app-name", | ||
| ProjectKey: "proj-key", | ||
| } | ||
|
|
||
| mockAppService := mockapps.NewMockApplicationService(ctrl) | ||
| mockAppService.EXPECT().CreateApplication(gomock.Any(), requestPayload).Return(nil).Times(1) | ||
|
|
||
| cmd := &createAppCommand{ | ||
| applicationService: mockAppService, | ||
| serverDetails: serverDetails, | ||
| requestBody: requestPayload, | ||
| } | ||
|
|
||
| err := cmd.Run() | ||
| assert.NoError(t, err) | ||
| } | ||
|
|
||
| func TestCreateAppCommand_Error(t *testing.T) { | ||
| ctrl := gomock.NewController(t) | ||
| defer ctrl.Finish() | ||
|
|
||
| serverDetails := &config.ServerDetails{Url: "https://example.com"} | ||
| requestPayload := &model.CreateAppRequest{ | ||
| ApplicationKey: "app-key", | ||
| ApplicationName: "app-name", | ||
| ProjectKey: "proj-key", | ||
| } | ||
|
|
||
| mockAppService := mockapps.NewMockApplicationService(ctrl) | ||
| mockAppService.EXPECT().CreateApplication(gomock.Any(), requestPayload).Return(errors.New("failed to create an application. Status code: 500")).Times(1) | ||
|
|
||
| cmd := &createAppCommand{ | ||
| applicationService: mockAppService, | ||
| serverDetails: serverDetails, | ||
| requestBody: requestPayload, | ||
| } | ||
|
|
||
| err := cmd.Run() | ||
| assert.Error(t, err) | ||
| assert.Equal(t, "failed to create an application. Status code: 500", err.Error()) | ||
| } | ||
|
|
||
| func TestCreateAppCommand_WrongNumberOfArguments(t *testing.T) { | ||
| ctrl := gomock.NewController(t) | ||
| defer ctrl.Finish() | ||
| app := cli.NewApp() | ||
| set := flag.NewFlagSet("test", 0) | ||
| ctx := cli.NewContext(app, set, nil) | ||
|
|
||
| mockAppService := mockapps.NewMockApplicationService(ctrl) | ||
| cmd := &createAppCommand{ | ||
| applicationService: mockAppService, | ||
| } | ||
|
|
||
| // Test with no arguments | ||
| context, err := components.ConvertContext(ctx) | ||
| assert.NoError(t, err) | ||
|
|
||
| err = cmd.prepareAndRunCommand(context) | ||
| assert.Error(t, err) | ||
| assert.Contains(t, err.Error(), "Wrong number of arguments") | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
G407 is not supported anymore so it can't be excluded. Also, exclude-test-files is not supported (and probably not wanted in the first place).