Skip to content

Commit

Permalink
Merge pull request #838 from Checkmarx/miryamFoifer/updateGroupInCrea…
Browse files Browse the repository at this point in the history
…teRequestAST-64011

update groups when creating a new project(AST-64011)
  • Loading branch information
miryamfoiferCX authored Aug 22, 2024
2 parents efc0b72 + 21b5574 commit 62063da
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 deletions.
14 changes: 14 additions & 0 deletions internal/services/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ import (
"github.com/pkg/errors"
)

func GetGroupMap(groupsWrapper wrappers.GroupsWrapper, projectGroups string, projModelResp *wrappers.ProjectResponseModel,
featureFlagsWrapper wrappers.FeatureFlagsWrapper) ([]*wrappers.Group, []string, error) {
groupsMap, groupErr := CreateGroupsMap(projectGroups, groupsWrapper)
if groupErr != nil {
return nil, nil, errors.Errorf("%s: %v", failedUpdatingProj, groupErr)
}
groups := getGroupsForRequest(groupsMap, featureFlagsWrapper)
if projModelResp != nil {
groups = append(getGroupsForRequest(groupsMap, featureFlagsWrapper), projModelResp.Groups...)
return groupsMap, groups, nil
}
return groupsMap, groups, nil
}

func CreateGroupsMap(groupsStr string, groupsWrapper wrappers.GroupsWrapper) ([]*wrappers.Group, error) {
groups := strings.Split(groupsStr, ",")
var groupsMap []*wrappers.Group
Expand Down
31 changes: 21 additions & 10 deletions internal/services/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ func createProject(
var projModel = wrappers.Project{}
projModel.Name = projectName
projModel.ApplicationIds = applicationID
var groupsMap []*wrappers.Group
if projectGroups != "" {
var groups []string
var groupErr error
groupsMap, groups, groupErr = GetGroupMap(groupsWrapper, projectGroups, nil, featureFlagsWrapper)
if groupErr != nil {
return "", groupErr
}
projModel.Groups = groups
}

if projectPrivatePackage != "" {
projModel.PrivatePackage, _ = strconv.ParseBool(projectPrivatePackage)
Expand All @@ -108,7 +118,7 @@ func createProject(
}

if projectGroups != "" {
err = UpsertProjectGroups(groupsWrapper, &projModel, projectsWrapper, accessManagementWrapper, nil, projectGroups, projectID, projectName, featureFlagsWrapper)
err = UpsertProjectGroups(&projModel, projectsWrapper, accessManagementWrapper, projectID, projectName, featureFlagsWrapper, groupsMap)
if err != nil {
return projectID, err
}
Expand Down Expand Up @@ -215,27 +225,28 @@ func updateProject(
}

if projectGroups != "" {
err = UpsertProjectGroups(groupsWrapper, &projModel, projectsWrapper, accessManagementWrapper, projModelResp, projectGroups, projectID, projectName, featureFlagsWrapper)
err = UpsertProjectGroupsByUpdateFlow(groupsWrapper, &projModel, projectsWrapper, accessManagementWrapper, projModelResp, projectGroups, projectID, projectName, featureFlagsWrapper)
if err != nil {
return projectID, err
}
}
return projectID, nil
}

func UpsertProjectGroups(groupsWrapper wrappers.GroupsWrapper, projModel *wrappers.Project, projectsWrapper wrappers.ProjectsWrapper,
func UpsertProjectGroupsByUpdateFlow(groupsWrapper wrappers.GroupsWrapper, projModel *wrappers.Project, projectsWrapper wrappers.ProjectsWrapper,
accessManagementWrapper wrappers.AccessManagementWrapper, projModelResp *wrappers.ProjectResponseModel,
projectGroups string, projectID string, projectName string, featureFlagsWrapper wrappers.FeatureFlagsWrapper) error {
groupsMap, groupErr := CreateGroupsMap(projectGroups, groupsWrapper)
groupsMap, groups, groupErr := GetGroupMap(groupsWrapper, projectGroups, projModelResp, featureFlagsWrapper)
if groupErr != nil {
return errors.Errorf("%s: %v", failedUpdatingProj, groupErr)
}
projModel.Groups = getGroupsForRequest(groupsMap, featureFlagsWrapper)
if projModelResp != nil {
groups := append(getGroupsForRequest(groupsMap, featureFlagsWrapper), projModelResp.Groups...)
projModel.Groups = groups
return groupErr
}
projModel.Groups = groups
return UpsertProjectGroups(projModel, projectsWrapper, accessManagementWrapper, projectID, projectName, featureFlagsWrapper, groupsMap)
}

func UpsertProjectGroups(projModel *wrappers.Project, projectsWrapper wrappers.ProjectsWrapper,
accessManagementWrapper wrappers.AccessManagementWrapper, projectID string, projectName string,
featureFlagsWrapper wrappers.FeatureFlagsWrapper, groupsMap []*wrappers.Group) error {
err := AssignGroupsToProjectNewAccessManagement(projectID, projectName, groupsMap, accessManagementWrapper, featureFlagsWrapper)
if err != nil {
return err
Expand Down
13 changes: 11 additions & 2 deletions internal/services/projects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ func Test_createProject(t *testing.T) {
projectGroups: "",
featureFlagsWrapper: &mock.FeatureFlagsMockWrapper{},
}, want: "ID-new-project-name", wantErr: false},
{name: "When called with a new project name and existing project groups return the Id of the newly created project", args: args{
projectName: "new-project-name",
cmd: &cobra.Command{},
projectsWrapper: &mock.ProjectsMockWrapper{},
groupsWrapper: &mock.GroupsMockWrapper{},
accessManagementWrapper: &mock.AccessManagementMockWrapper{},
projectGroups: "existsGroup1,existsGroup2",
featureFlagsWrapper: &mock.FeatureFlagsMockWrapper{},
}, want: "ID-new-project-name", wantErr: false},
{name: "When called with a new project name and non existing project groups return error", args: args{
projectName: "new-project-name",
cmd: &cobra.Command{},
Expand All @@ -114,7 +123,7 @@ func Test_createProject(t *testing.T) {
accessManagementWrapper: &mock.AccessManagementMockWrapper{},
projectGroups: "grp1,grp2",
featureFlagsWrapper: &mock.FeatureFlagsMockWrapper{},
}, want: "ID-new-project-name", wantErr: true},
}, want: "", wantErr: true},
{name: "When called with mock fake error model return fake error from project create", args: args{
projectName: "mock-some-error-model",
cmd: &cobra.Command{},
Expand All @@ -132,7 +141,7 @@ func Test_createProject(t *testing.T) {
accessManagementWrapper: &mock.AccessManagementMockWrapper{},
projectGroups: "fake-group-error",
featureFlagsWrapper: &mock.FeatureFlagsMockWrapper{},
}, want: "ID-new-project-name", wantErr: true},
}, want: "", wantErr: true},
{name: "When called with a new project name and projectPrivatePackage set to true return the Id of the newly created project", args: args{
projectName: "new-project-name",
cmd: &cobra.Command{},
Expand Down
3 changes: 3 additions & 0 deletions internal/wrappers/mock/groups-mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ func (g *GroupsMockWrapper) Get(groupName string) ([]wrappers.Group, error) {
if groupName == "fake-group-error" {
return nil, errors.Errorf("fake grroup error")
}
if groupName == "existsGroup1" || groupName == "existsGroup2" {
return []wrappers.Group{{ID: "1", Name: groupName}}, nil
}
return []wrappers.Group{{ID: "1", Name: "group"}}, nil
}

0 comments on commit 62063da

Please sign in to comment.