Search in sources :

Example 1 with NotifyType

use of com.google.gerrit.entities.NotifyConfig.NotifyType in project gerrit by GerritCodeReview.

the class ProjectConfig method loadNotifySections.

/**
 * Parses the [notify] sections out of the configuration file.
 *
 * <pre>
 *   [notify "reviewers"]
 *     email = group Reviewers
 *     type = new_changes
 *
 *   [notify "dev-team"]
 *     email = dev-team@example.com
 *     filter = branch:master
 *
 *   [notify "qa"]
 *     email = qa@example.com
 *     filter = branch:\"^(maint|stable)-.*\"
 *     type = submitted_changes
 * </pre>
 */
private void loadNotifySections(Config rc) {
    notifySections = new HashMap<>();
    for (String sectionName : rc.getSubsections(NOTIFY)) {
        NotifyConfig.Builder n = NotifyConfig.builder();
        n.setName(sectionName);
        n.setFilter(rc.getString(NOTIFY, sectionName, KEY_FILTER));
        EnumSet<NotifyType> types = EnumSet.noneOf(NotifyType.class);
        types.addAll(ConfigUtil.getEnumList(rc, NOTIFY, sectionName, KEY_TYPE, NotifyType.ALL));
        n.setNotify(types);
        n.setHeader(rc.getEnum(NOTIFY, sectionName, KEY_HEADER, NotifyConfig.Header.BCC));
        for (String dst : rc.getStringList(NOTIFY, sectionName, KEY_EMAIL)) {
            String groupName = GroupReference.extractGroupName(dst);
            if (groupName != null) {
                GroupReference ref = groupList.byName(groupName);
                if (ref == null) {
                    ref = groupList.resolve(GroupReference.create(groupName));
                }
                if (ref.getUUID() != null) {
                    n.addGroup(ref);
                } else {
                    error(String.format("group \"%s\" not in %s", ref.getName(), GroupList.FILE_NAME));
                }
            } else if (dst.startsWith("user ")) {
                error(String.format("%s not supported", dst));
            } else {
                try {
                    n.addAddress(Address.parse(dst));
                } catch (IllegalArgumentException err) {
                    error(String.format("notify section \"%s\" has invalid email \"%s\"", sectionName, dst));
                }
            }
        }
        notifySections.put(sectionName, n.build());
    }
}
Also used : NotifyType(com.google.gerrit.entities.NotifyConfig.NotifyType) NotifyConfig(com.google.gerrit.entities.NotifyConfig) GroupReference(com.google.gerrit.entities.GroupReference)

Example 2 with NotifyType

use of com.google.gerrit.entities.NotifyConfig.NotifyType in project gerrit by GerritCodeReview.

the class PostWatchedProjects method asMap.

private Map<ProjectWatchKey, Set<NotifyType>> asMap(List<ProjectWatchInfo> input) throws RestApiException, IOException, PermissionBackendException {
    Map<ProjectWatchKey, Set<NotifyType>> m = new HashMap<>();
    for (ProjectWatchInfo info : input) {
        if (info.project == null || info.project.trim().isEmpty()) {
            throw new BadRequestException("project name must be specified");
        }
        ProjectWatchKey key = ProjectWatchKey.create(projectsCollection.parse(info.project).getNameKey(), info.filter);
        if (m.containsKey(key)) {
            throw new BadRequestException("duplicate entry for project " + format(info.project, info.filter));
        }
        Set<NotifyType> notifyValues = EnumSet.noneOf(NotifyType.class);
        if (toBoolean(info.notifyAbandonedChanges)) {
            notifyValues.add(NotifyType.ABANDONED_CHANGES);
        }
        if (toBoolean(info.notifyAllComments)) {
            notifyValues.add(NotifyType.ALL_COMMENTS);
        }
        if (toBoolean(info.notifyNewChanges)) {
            notifyValues.add(NotifyType.NEW_CHANGES);
        }
        if (toBoolean(info.notifyNewPatchSets)) {
            notifyValues.add(NotifyType.NEW_PATCHSETS);
        }
        if (toBoolean(info.notifySubmittedChanges)) {
            notifyValues.add(NotifyType.SUBMITTED_CHANGES);
        }
        m.put(key, notifyValues);
    }
    return m;
}
Also used : NotifyType(com.google.gerrit.entities.NotifyConfig.NotifyType) EnumSet(java.util.EnumSet) Set(java.util.Set) ProjectWatchInfo(com.google.gerrit.extensions.client.ProjectWatchInfo) HashMap(java.util.HashMap) ProjectWatchKey(com.google.gerrit.server.account.ProjectWatches.ProjectWatchKey) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException)

Example 3 with NotifyType

use of com.google.gerrit.entities.NotifyConfig.NotifyType in project gerrit by GerritCodeReview.

the class WatchConfigTest method parseWatchConfig.

@Test
public void parseWatchConfig() throws Exception {
    Config cfg = new Config();
    cfg.fromText("[project \"myProject\"]\n" + "  notify = * [ALL_COMMENTS, NEW_PATCHSETS]\n" + "  notify = branch:master [NEW_CHANGES]\n" + "  notify = branch:master [NEW_PATCHSETS]\n" + "  notify = branch:foo []\n" + "[project \"otherProject\"]\n" + "  notify = [NEW_PATCHSETS]\n" + "  notify = * [NEW_PATCHSETS, ALL_COMMENTS]\n");
    Map<ProjectWatchKey, ImmutableSet<NotifyType>> projectWatches = ProjectWatches.parse(Account.id(1000000), cfg, this);
    assertThat(validationErrors).isEmpty();
    Project.NameKey myProject = Project.nameKey("myProject");
    Project.NameKey otherProject = Project.nameKey("otherProject");
    Map<ProjectWatchKey, Set<NotifyType>> expectedProjectWatches = new HashMap<>();
    expectedProjectWatches.put(ProjectWatchKey.create(myProject, null), EnumSet.of(NotifyType.ALL_COMMENTS, NotifyType.NEW_PATCHSETS));
    expectedProjectWatches.put(ProjectWatchKey.create(myProject, "branch:master"), EnumSet.of(NotifyType.NEW_CHANGES, NotifyType.NEW_PATCHSETS));
    expectedProjectWatches.put(ProjectWatchKey.create(myProject, "branch:foo"), EnumSet.noneOf(NotifyType.class));
    expectedProjectWatches.put(ProjectWatchKey.create(otherProject, null), EnumSet.of(NotifyType.NEW_PATCHSETS));
    expectedProjectWatches.put(ProjectWatchKey.create(otherProject, null), EnumSet.of(NotifyType.ALL_COMMENTS, NotifyType.NEW_PATCHSETS));
    assertThat(projectWatches).containsExactlyEntriesIn(expectedProjectWatches);
}
Also used : Project(com.google.gerrit.entities.Project) NotifyType(com.google.gerrit.entities.NotifyConfig.NotifyType) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) EnumSet(java.util.EnumSet) ImmutableSet(com.google.common.collect.ImmutableSet) HashMap(java.util.HashMap) Config(org.eclipse.jgit.lib.Config) ProjectWatchKey(com.google.gerrit.server.account.ProjectWatches.ProjectWatchKey) Test(org.junit.Test)

Example 4 with NotifyType

use of com.google.gerrit.entities.NotifyConfig.NotifyType in project gerrit by GerritCodeReview.

the class ProjectConfig method saveNotifySections.

private void saveNotifySections(Config rc, Set<AccountGroup.UUID> keepGroups) {
    unsetSection(rc, NOTIFY);
    for (NotifyConfig nc : sort(notifySections.values())) {
        nc.getGroups().stream().map(GroupReference::getUUID).filter(Objects::nonNull).forEach(keepGroups::add);
        List<String> email = nc.getGroups().stream().map(gr -> PermissionRule.create(gr).asString(false)).sorted().collect(toList());
        // Separate stream operation so that emails list contains 2 sorted sub-lists.
        nc.getAddresses().stream().map(Address::toString).sorted().forEach(email::add);
        set(rc, NOTIFY, nc.getName(), KEY_HEADER, nc.getHeader(), NotifyConfig.Header.BCC);
        if (email.isEmpty()) {
            rc.unset(NOTIFY, nc.getName(), KEY_EMAIL);
        } else {
            rc.setStringList(NOTIFY, nc.getName(), KEY_EMAIL, email);
        }
        if (nc.getNotify().equals(Sets.immutableEnumSet(NotifyType.ALL))) {
            rc.unset(NOTIFY, nc.getName(), KEY_TYPE);
        } else {
            List<String> types = new ArrayList<>(4);
            for (NotifyType t : NotifyType.values()) {
                if (nc.isNotify(t)) {
                    types.add(t.name().toLowerCase(Locale.US));
                }
            }
            rc.setStringList(NOTIFY, nc.getName(), KEY_TYPE, types);
        }
        set(rc, NOTIFY, nc.getName(), KEY_FILTER, nc.getFilter());
    }
}
Also used : NotifyType(com.google.gerrit.entities.NotifyConfig.NotifyType) Address(com.google.gerrit.entities.Address) ArrayList(java.util.ArrayList) NotifyConfig(com.google.gerrit.entities.NotifyConfig) GroupReference(com.google.gerrit.entities.GroupReference)

Aggregations

NotifyType (com.google.gerrit.entities.NotifyConfig.NotifyType)4 GroupReference (com.google.gerrit.entities.GroupReference)2 NotifyConfig (com.google.gerrit.entities.NotifyConfig)2 ProjectWatchKey (com.google.gerrit.server.account.ProjectWatches.ProjectWatchKey)2 EnumSet (java.util.EnumSet)2 HashMap (java.util.HashMap)2 Set (java.util.Set)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 Address (com.google.gerrit.entities.Address)1 Project (com.google.gerrit.entities.Project)1 ProjectWatchInfo (com.google.gerrit.extensions.client.ProjectWatchInfo)1 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)1 ArrayList (java.util.ArrayList)1 Config (org.eclipse.jgit.lib.Config)1 Test (org.junit.Test)1