Search in sources :

Example 1 with ProjectWatchKey

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())));
}
Also used : Account(com.google.gerrit.entities.Account) ImmutableSet(com.google.common.collect.ImmutableSet) ObjectId(org.eclipse.jgit.lib.ObjectId) ProjectWatchKey(com.google.gerrit.server.account.ProjectWatches.ProjectWatchKey) ExternalId(com.google.gerrit.server.account.externalids.ExternalId)

Example 2 with ProjectWatchKey

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));
}
Also used : ProjectWatchKey(com.google.gerrit.server.account.ProjectWatches.ProjectWatchKey) ArrayList(java.util.ArrayList) AndPredicate(com.google.gerrit.index.query.AndPredicate) Predicate(com.google.gerrit.index.query.Predicate) QueryParseException(com.google.gerrit.index.query.QueryParseException)

Example 3 with ProjectWatchKey

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;
}
Also used : Account(com.google.gerrit.entities.Account) ProjectWatchKey(com.google.gerrit.server.account.ProjectWatches.ProjectWatchKey) AccountState(com.google.gerrit.server.account.AccountState) QueryParseException(com.google.gerrit.index.query.QueryParseException) ImmutableSet(com.google.common.collect.ImmutableSet) NotifyConfig(com.google.gerrit.entities.NotifyConfig) ProjectState(com.google.gerrit.server.project.ProjectState) Map(java.util.Map) HashSet(java.util.HashSet)

Example 4 with ProjectWatchKey

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;
}
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 5 with ProjectWatchKey

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);
}
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)

Aggregations

ProjectWatchKey (com.google.gerrit.server.account.ProjectWatches.ProjectWatchKey)5 ImmutableSet (com.google.common.collect.ImmutableSet)3 Account (com.google.gerrit.entities.Account)2 NotifyType (com.google.gerrit.entities.NotifyConfig.NotifyType)2 QueryParseException (com.google.gerrit.index.query.QueryParseException)2 EnumSet (java.util.EnumSet)2 HashMap (java.util.HashMap)2 Set (java.util.Set)2 NotifyConfig (com.google.gerrit.entities.NotifyConfig)1 Project (com.google.gerrit.entities.Project)1 ProjectWatchInfo (com.google.gerrit.extensions.client.ProjectWatchInfo)1 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)1 AndPredicate (com.google.gerrit.index.query.AndPredicate)1 Predicate (com.google.gerrit.index.query.Predicate)1 AccountState (com.google.gerrit.server.account.AccountState)1 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)1 ProjectState (com.google.gerrit.server.project.ProjectState)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1