Search in sources :

Example 1 with WatchConfig

use of com.google.gerrit.server.account.WatchConfig 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)

Aggregations

Account (com.google.gerrit.reviewdb.client.Account)1 Project (com.google.gerrit.reviewdb.client.Project)1 WatchConfig (com.google.gerrit.server.account.WatchConfig)1 NotifyType (com.google.gerrit.server.account.WatchConfig.NotifyType)1 ProjectWatchKey (com.google.gerrit.server.account.WatchConfig.ProjectWatchKey)1 MetaDataUpdate (com.google.gerrit.server.git.MetaDataUpdate)1 OrmDuplicateKeyException (com.google.gwtorm.server.OrmDuplicateKeyException)1 OrmException (com.google.gwtorm.server.OrmException)1 IOException (java.io.IOException)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 Collection (java.util.Collection)1 EnumSet (java.util.EnumSet)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)1 BatchRefUpdate (org.eclipse.jgit.lib.BatchRefUpdate)1 Repository (org.eclipse.jgit.lib.Repository)1 RevWalk (org.eclipse.jgit.revwalk.RevWalk)1