Search in sources :

Example 1 with ProjectWatchKey

use of com.google.gerrit.server.account.WatchConfig.ProjectWatchKey in project gerrit by GerritCodeReview.

the class IsWatchedByPredicate method filters.

protected static List<Predicate<ChangeData>> filters(ChangeQueryBuilder.Arguments args, boolean checkIsVisible) 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.status_open());
        }
    }
    if (r.isEmpty()) {
        return none();
    } else if (checkIsVisible) {
        return ImmutableList.of(or(r), builder.is_visible());
    } else {
        return ImmutableList.of(or(r));
    }
}
Also used : ProjectWatchKey(com.google.gerrit.server.account.WatchConfig.ProjectWatchKey) ArrayList(java.util.ArrayList) AndPredicate(com.google.gerrit.server.query.AndPredicate) Predicate(com.google.gerrit.server.query.Predicate) QueryParseException(com.google.gerrit.server.query.QueryParseException)

Example 2 with ProjectWatchKey

use of com.google.gerrit.server.account.WatchConfig.ProjectWatchKey in project gerrit by GerritCodeReview.

the class GetWatchedProjects method apply.

@Override
public List<ProjectWatchInfo> apply(AccountResource rsrc) throws OrmException, AuthException, IOException, ConfigInvalidException, PermissionBackendException {
    if (self.get() != rsrc.getUser()) {
        permissionBackend.user(self).check(GlobalPermission.ADMINISTRATE_SERVER);
    }
    Account.Id accountId = rsrc.getUser().getAccountId();
    List<ProjectWatchInfo> projectWatchInfos = new ArrayList<>();
    for (Map.Entry<ProjectWatchKey, Set<NotifyType>> e : watchConfig.getProjectWatches(accountId).entrySet()) {
        ProjectWatchInfo pwi = new ProjectWatchInfo();
        pwi.filter = e.getKey().filter();
        pwi.project = e.getKey().project().get();
        pwi.notifyAbandonedChanges = toBoolean(e.getValue().contains(NotifyType.ABANDONED_CHANGES));
        pwi.notifyNewChanges = toBoolean(e.getValue().contains(NotifyType.NEW_CHANGES));
        pwi.notifyNewPatchSets = toBoolean(e.getValue().contains(NotifyType.NEW_PATCHSETS));
        pwi.notifySubmittedChanges = toBoolean(e.getValue().contains(NotifyType.SUBMITTED_CHANGES));
        pwi.notifyAllComments = toBoolean(e.getValue().contains(NotifyType.ALL_COMMENTS));
        projectWatchInfos.add(pwi);
    }
    Collections.sort(projectWatchInfos, new Comparator<ProjectWatchInfo>() {

        @Override
        public int compare(ProjectWatchInfo pwi1, ProjectWatchInfo pwi2) {
            return ComparisonChain.start().compare(pwi1.project, pwi2.project).compare(Strings.nullToEmpty(pwi1.filter), Strings.nullToEmpty(pwi2.filter)).result();
        }
    });
    return projectWatchInfos;
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) ProjectWatchInfo(com.google.gerrit.extensions.client.ProjectWatchInfo) Set(java.util.Set) ProjectWatchKey(com.google.gerrit.server.account.WatchConfig.ProjectWatchKey) ArrayList(java.util.ArrayList) Map(java.util.Map)

Example 3 with ProjectWatchKey

use of com.google.gerrit.server.account.WatchConfig.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, 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);
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) Project(com.google.gerrit.reviewdb.client.Project) NotifyType(com.google.gerrit.server.account.WatchConfig.NotifyType) Set(java.util.Set) EnumSet(java.util.EnumSet) HashMap(java.util.HashMap) Config(org.eclipse.jgit.lib.Config) ProjectWatchKey(com.google.gerrit.server.account.WatchConfig.ProjectWatchKey) Test(org.junit.Test)

Example 4 with ProjectWatchKey

use of com.google.gerrit.server.account.WatchConfig.ProjectWatchKey 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);
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) ResultSet(java.sql.ResultSet) EnumSet(java.util.EnumSet) Set(java.util.Set) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) HashMap(java.util.HashMap) ProjectWatchKey(com.google.gerrit.server.account.WatchConfig.ProjectWatchKey) OrmException(com.google.gwtorm.server.OrmException) ResultSet(java.sql.ResultSet) WatchConfig(com.google.gerrit.server.account.WatchConfig) NotifyType(com.google.gerrit.server.account.WatchConfig.NotifyType) Statement(java.sql.Statement) OrmDuplicateKeyException(com.google.gwtorm.server.OrmDuplicateKeyException) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Project(com.google.gerrit.reviewdb.client.Project) Repository(org.eclipse.jgit.lib.Repository) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map) BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate)

Example 5 with ProjectWatchKey

use of com.google.gerrit.server.account.WatchConfig.ProjectWatchKey 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;
}
Also used : NotifyType(com.google.gerrit.server.account.WatchConfig.NotifyType) Set(java.util.Set) EnumSet(java.util.EnumSet) ProjectWatchInfo(com.google.gerrit.extensions.client.ProjectWatchInfo) HashMap(java.util.HashMap) ProjectWatchKey(com.google.gerrit.server.account.WatchConfig.ProjectWatchKey) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException)

Aggregations

ProjectWatchKey (com.google.gerrit.server.account.WatchConfig.ProjectWatchKey)7 Set (java.util.Set)6 Account (com.google.gerrit.reviewdb.client.Account)5 NotifyType (com.google.gerrit.server.account.WatchConfig.NotifyType)3 EnumSet (java.util.EnumSet)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ProjectWatchInfo (com.google.gerrit.extensions.client.ProjectWatchInfo)2 Project (com.google.gerrit.reviewdb.client.Project)2 QueryParseException (com.google.gerrit.server.query.QueryParseException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)1 AccountState (com.google.gerrit.server.account.AccountState)1 WatchConfig (com.google.gerrit.server.account.WatchConfig)1 MetaDataUpdate (com.google.gerrit.server.git.MetaDataUpdate)1 NotifyConfig (com.google.gerrit.server.git.NotifyConfig)1 ProjectState (com.google.gerrit.server.project.ProjectState)1 AndPredicate (com.google.gerrit.server.query.AndPredicate)1