use of com.google.gerrit.server.account.ProjectWatches.ProjectWatchKey in project gerrit by GerritCodeReview.
the class AccountState method fromAccountConfig.
/**
* Creates an AccountState from the given account config.
*
* <p>If external ID notes are provided the revision of the external IDs branch from which the
* external IDs for the account should be loaded is taken from the external ID notes. If external
* ID notes are not given the revision of the external IDs branch is taken from the account
* config. Updating external IDs is done via {@link ExternalIdNotes} and if external IDs were
* updated the revision of the external IDs branch in account config is outdated. Hence after
* updating external IDs the external ID notes must be provided.
*
* @param externalIds class to access external IDs
* @param accountConfig the account config, must already be loaded
* @param extIdNotes external ID notes, must already be loaded, may be {@code null}
* @param defaultPreferences the default preferences for this Gerrit installation
* @return the account state, {@link Optional#empty()} if the account doesn't exist
* @throws IOException if accessing the external IDs fails
*/
public static Optional<AccountState> fromAccountConfig(ExternalIds externalIds, AccountConfig accountConfig, @Nullable ExternalIdNotes extIdNotes, CachedPreferences defaultPreferences) throws IOException {
if (!accountConfig.getLoadedAccount().isPresent()) {
return Optional.empty();
}
Account account = accountConfig.getLoadedAccount().get();
Optional<ObjectId> extIdsRev = extIdNotes != null ? Optional.ofNullable(extIdNotes.getRevision()) : accountConfig.getExternalIdsRev();
ImmutableSet<ExternalId> extIds = extIdsRev.isPresent() ? externalIds.byAccount(account.id(), extIdsRev.get()) : ImmutableSet.of();
// Don't leak references to AccountConfig into the AccountState, since it holds a reference to
// an open Repository instance.
ImmutableMap<ProjectWatchKey, ImmutableSet<NotifyType>> projectWatches = accountConfig.getProjectWatches();
return Optional.of(new AutoValue_AccountState(account, extIds, ExternalId.getUserName(extIds), projectWatches, Optional.of(defaultPreferences), Optional.of(accountConfig.asCachedPreferences())));
}
use of com.google.gerrit.server.account.ProjectWatches.ProjectWatchKey in project gerrit by GerritCodeReview.
the class IsWatchedByPredicate method filters.
protected static List<Predicate<ChangeData>> filters(ChangeQueryBuilder.Arguments args) throws QueryParseException {
List<Predicate<ChangeData>> r = new ArrayList<>();
ChangeQueryBuilder builder = new ChangeQueryBuilder(args);
for (ProjectWatchKey w : getWatches(args)) {
Predicate<ChangeData> f = null;
if (w.filter() != null) {
try {
f = builder.parse(w.filter());
if (QueryBuilder.find(f, IsWatchedByPredicate.class) != null) {
// another user is filtering on. :-)
continue;
}
} catch (QueryParseException e) {
continue;
}
}
Predicate<ChangeData> p;
if (w.project().equals(args.allProjectsName)) {
p = null;
} else {
p = builder.project(w.project().get());
}
if (p != null && f != null) {
r.add(and(p, f));
} else if (p != null) {
r.add(p);
} else if (f != null) {
r.add(f);
} else {
r.add(builder.statusOpen());
}
}
if (r.isEmpty()) {
return ImmutableList.of(ChangeIndexPredicate.none());
}
return ImmutableList.of(or(r));
}
use of com.google.gerrit.server.account.ProjectWatches.ProjectWatchKey in project gerrit by GerritCodeReview.
the class ProjectWatch method getWatchers.
/**
* Returns all watchers that are relevant
*/
public final Watchers getWatchers(NotifyConfig.NotifyType type, boolean includeWatchersFromNotifyConfig) {
Watchers matching = new Watchers();
Set<Account.Id> projectWatchers = new HashSet<>();
for (AccountState a : args.accountQueryProvider.get().byWatchedProject(project)) {
Account.Id accountId = a.account().id();
for (Map.Entry<ProjectWatchKey, ImmutableSet<NotifyConfig.NotifyType>> e : a.projectWatches().entrySet()) {
if (project.equals(e.getKey().project()) && add(matching, accountId, e.getKey(), e.getValue(), type)) {
// We only want to prevent matching All-Projects if this filter hits
projectWatchers.add(accountId);
}
}
}
for (AccountState a : args.accountQueryProvider.get().byWatchedProject(args.allProjectsName)) {
for (Map.Entry<ProjectWatchKey, ImmutableSet<NotifyConfig.NotifyType>> e : a.projectWatches().entrySet()) {
if (args.allProjectsName.equals(e.getKey().project())) {
Account.Id accountId = a.account().id();
if (!projectWatchers.contains(accountId)) {
add(matching, accountId, e.getKey(), e.getValue(), type);
}
}
}
}
if (!includeWatchersFromNotifyConfig) {
return matching;
}
for (ProjectState state : projectState.tree()) {
for (NotifyConfig nc : state.getConfig().getNotifySections().values()) {
if (nc.isNotify(type)) {
try {
add(matching, state.getNameKey(), nc);
} catch (QueryParseException e) {
logger.atInfo().log("Project %s has invalid notify %s filter \"%s\": %s", state.getName(), nc.getName(), nc.getFilter(), e.getMessage());
}
}
}
}
return matching;
}
use of com.google.gerrit.server.account.ProjectWatches.ProjectWatchKey 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;
}
use of com.google.gerrit.server.account.ProjectWatches.ProjectWatchKey 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);
}
Aggregations