public SUserGroup createUserGroup()

in rest-api/src/jetbrains/buildServer/server/rest/data/DataUpdater.java [140:211]


  public SUserGroup createUserGroup(@Nullable final Group groupDescription, @NotNull final ServiceLocator serviceLocator) {
    myDataProvider.checkGlobalPermission(jetbrains.buildServer.serverSide.auth.Permission.CREATE_USERGROUP);
    if (groupDescription == null) {
      throw new BadRequestException("Empty payload received while group details are expected.");
    }

    if (StringUtil.isEmpty(groupDescription.key)) {
      throw new BadRequestException("Attribute 'key' must not be empty when creating group.");
    }
    if (StringUtil.isEmpty(groupDescription.name)) {
      throw new BadRequestException("Attribute 'name' must not be empty when creating group.");
    }
    SUserGroup resultingGroup;
    try {
      resultingGroup = myGroupManager.createUserGroup(groupDescription.key, groupDescription.name,
                                                      groupDescription.description != null ? groupDescription.description : "");
    } catch (DuplicateKeyException e) {
      throw new BadRequestException(
        "Cannot create group as group with key '" + groupDescription.key + "': group with the same key already exists");
    } catch (DuplicateNameException e) {
      throw new BadRequestException(
        "Cannot create group as group with name '" + groupDescription.name + "': group with the same name already exists");
    }

    if (groupDescription.parentGroups != null) {
      try {
        Group.setGroupParents(resultingGroup, new LinkedHashSet<>(groupDescription.parentGroups.getFromPosted(serviceLocator)), false, serviceLocator);
      } catch (Exception e) {
        myGroupManager.deleteUserGroup(resultingGroup);
        throw new BadRequestException("Cannot create group with specified parent groups", e);
      }
    }

    if (groupDescription.childGroups != null) {
      try {
        groupDescription.childGroups.getFromPosted(serviceLocator).forEach(group -> resultingGroup.addSubgroup(group));
      } catch (Exception e) {
        myGroupManager.deleteUserGroup(resultingGroup);
        throw new BadRequestException("Cannot create group with specified children groups", e);
      }
    }

    if (groupDescription.roleAssignments != null) {
      try {
        Group.setRoles(resultingGroup, groupDescription.roleAssignments, serviceLocator);
      } catch (Exception e) {
        myGroupManager.deleteUserGroup(resultingGroup);
        throw new BadRequestException("Cannot create group with specified roles", e);
      }
    }

    if (groupDescription.properties != null) {
      try {
        resultingGroup
          .setGroupProperties(groupDescription.properties.getMap().entrySet().stream().collect(Collectors.toMap(e -> new SimplePropertyKey(e.getKey()), e -> e.getValue())));
      } catch (Exception e) {
        myGroupManager.deleteUserGroup(resultingGroup);
        throw new BadRequestException("Cannot create group with specified properties", e);
      }
    }

    if (groupDescription.users != null) {
      try {
        groupDescription.users.getFromPosted(serviceLocator.getSingletonService(UserFinder.class)).forEach(user -> resultingGroup.addUser(user));
      } catch (Exception e) {
        myGroupManager.deleteUserGroup(resultingGroup);
        throw new BadRequestException("Cannot create group with specified users", e);
      }
    }

    return resultingGroup;
  }