use of com.google.gerrit.server.account.WatchConfig.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, Set<NotifyType>> projectWatches = WatchConfig.parse(new Account.Id(1000000), cfg, this);
assertThat(validationErrors).isEmpty();
Project.NameKey myProject = new Project.NameKey("myProject");
Project.NameKey otherProject = new 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);
}
use of com.google.gerrit.server.account.WatchConfig.NotifyType in project gerrit by GerritCodeReview.
the class Schema_139 method migrateData.
@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException, SQLException {
ListMultimap<Account.Id, ProjectWatch> imports = MultimapBuilder.hashKeys().arrayListValues().build();
try (Statement stmt = ((JdbcSchema) db).getConnection().createStatement();
ResultSet rs = stmt.executeQuery("SELECT " + "account_id, " + "project_name, " + "filter, " + "notify_abandoned_changes, " + "notify_all_comments, " + "notify_new_changes, " + "notify_new_patch_sets, " + "notify_submitted_changes " + "FROM account_project_watches")) {
while (rs.next()) {
Account.Id accountId = new Account.Id(rs.getInt(1));
ProjectWatch.Builder b = ProjectWatch.builder().project(new Project.NameKey(rs.getString(2))).filter(rs.getString(3)).notifyAbandonedChanges(rs.getBoolean(4)).notifyAllComments(rs.getBoolean(5)).notifyNewChanges(rs.getBoolean(6)).notifyNewPatchSets(rs.getBoolean(7)).notifySubmittedChanges(rs.getBoolean(8));
imports.put(accountId, b.build());
}
}
if (imports.isEmpty()) {
return;
}
try (Repository git = repoManager.openRepository(allUsersName);
RevWalk rw = new RevWalk(git)) {
BatchRefUpdate bru = git.getRefDatabase().newBatchUpdate();
bru.setRefLogIdent(serverUser);
bru.setRefLogMessage(MSG, false);
for (Map.Entry<Account.Id, Collection<ProjectWatch>> e : imports.asMap().entrySet()) {
Map<ProjectWatchKey, Set<NotifyType>> projectWatches = new HashMap<>();
for (ProjectWatch projectWatch : e.getValue()) {
ProjectWatchKey key = ProjectWatchKey.create(projectWatch.project(), projectWatch.filter());
if (projectWatches.containsKey(key)) {
throw new OrmDuplicateKeyException("Duplicate key for watched project: " + key.toString());
}
Set<NotifyType> notifyValues = EnumSet.noneOf(NotifyType.class);
if (projectWatch.notifyAbandonedChanges()) {
notifyValues.add(NotifyType.ABANDONED_CHANGES);
}
if (projectWatch.notifyAllComments()) {
notifyValues.add(NotifyType.ALL_COMMENTS);
}
if (projectWatch.notifyNewChanges()) {
notifyValues.add(NotifyType.NEW_CHANGES);
}
if (projectWatch.notifyNewPatchSets()) {
notifyValues.add(NotifyType.NEW_PATCHSETS);
}
if (projectWatch.notifySubmittedChanges()) {
notifyValues.add(NotifyType.SUBMITTED_CHANGES);
}
projectWatches.put(key, notifyValues);
}
try (MetaDataUpdate md = new MetaDataUpdate(GitReferenceUpdated.DISABLED, allUsersName, git, bru)) {
md.getCommitBuilder().setAuthor(serverUser);
md.getCommitBuilder().setCommitter(serverUser);
md.setMessage(MSG);
WatchConfig watchConfig = new WatchConfig(e.getKey());
watchConfig.load(md);
watchConfig.setProjectWatches(projectWatches);
watchConfig.commit(md);
}
}
bru.execute(rw, NullProgressMonitor.INSTANCE);
} catch (IOException | ConfigInvalidException ex) {
throw new OrmException(ex);
}
}
use of com.google.gerrit.server.account.WatchConfig.NotifyType in project gerrit by GerritCodeReview.
the class PostWatchedProjects method asMap.
private Map<ProjectWatchKey, Set<NotifyType>> asMap(List<ProjectWatchInfo> input) throws BadRequestException, UnprocessableEntityException, IOException, PermissionBackendException {
Map<ProjectWatchKey, Set<NotifyType>> m = new HashMap<>();
for (ProjectWatchInfo info : input) {
if (info.project == null) {
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;
}
use of com.google.gerrit.server.account.WatchConfig.NotifyType in project gerrit by GerritCodeReview.
the class ProjectConfig method saveNotifySections.
private void saveNotifySections(Config rc, Set<AccountGroup.UUID> keepGroups) {
for (NotifyConfig nc : sort(notifySections.values())) {
List<String> email = new ArrayList<>();
for (GroupReference gr : nc.getGroups()) {
if (gr.getUUID() != null) {
keepGroups.add(gr.getUUID());
}
email.add(new PermissionRule(gr).asString(false));
}
Collections.sort(email);
List<String> addrs = new ArrayList<>();
for (Address addr : nc.getAddresses()) {
addrs.add(addr.toString());
}
Collections.sort(addrs);
email.addAll(addrs);
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(EnumSet.of(NotifyType.ALL))) {
rc.unset(NOTIFY, nc.getName(), KEY_TYPE);
} else {
List<String> types = Lists.newArrayListWithCapacity(4);
for (NotifyType t : NotifyType.values()) {
if (nc.isNotify(t)) {
types.add(StringUtils.toLowerCase(t.name()));
}
}
rc.setStringList(NOTIFY, nc.getName(), KEY_TYPE, types);
}
set(rc, NOTIFY, nc.getName(), KEY_FILTER, nc.getFilter());
}
}
use of com.google.gerrit.server.account.WatchConfig.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, Map<String, GroupReference> groupsByName) {
notifySections = new HashMap<>();
for (String sectionName : rc.getSubsections(NOTIFY)) {
NotifyConfig n = new NotifyConfig();
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.setTypes(types);
n.setHeader(rc.getEnum(NOTIFY, sectionName, KEY_HEADER, NotifyConfig.Header.BCC));
for (String dst : rc.getStringList(NOTIFY, sectionName, KEY_EMAIL)) {
if (dst.startsWith("group ")) {
String groupName = dst.substring(6).trim();
GroupReference ref = groupsByName.get(groupName);
if (ref == null) {
ref = new GroupReference(null, groupName);
groupsByName.put(ref.getName(), ref);
}
if (ref.getUUID() != null) {
n.addEmail(ref);
} else {
error(new ValidationError(PROJECT_CONFIG, "group \"" + ref.getName() + "\" not in " + GroupList.FILE_NAME));
}
} else if (dst.startsWith("user ")) {
error(new ValidationError(PROJECT_CONFIG, dst + " not supported"));
} else {
try {
n.addEmail(Address.parse(dst));
} catch (IllegalArgumentException err) {
error(new ValidationError(PROJECT_CONFIG, "notify section \"" + sectionName + "\" has invalid email \"" + dst + "\""));
}
}
}
notifySections.put(sectionName, n);
}
}
Aggregations