Search in sources :

Example 1 with OrmException

use of com.google.gwtorm.server.OrmException in project gerrit by GerritCodeReview.

the class AgreementJson method format.

public AgreementInfo format(ContributorAgreement ca) {
    AgreementInfo info = new AgreementInfo();
    info.name = ca.getName();
    info.description = ca.getDescription();
    info.url = ca.getAgreementUrl();
    GroupReference autoVerifyGroup = ca.getAutoVerify();
    if (autoVerifyGroup != null && self.get().isIdentifiedUser()) {
        IdentifiedUser user = identifiedUserFactory.create(self.get().getAccountId());
        try {
            GroupControl gc = genericGroupControlFactory.controlFor(user, autoVerifyGroup.getUUID());
            GroupResource group = new GroupResource(gc);
            info.autoVerifyGroup = groupJson.format(group);
        } catch (NoSuchGroupException | OrmException e) {
            log.warn("autoverify group \"" + autoVerifyGroup.getName() + "\" does not exist, referenced in CLA \"" + ca.getName() + "\"");
        }
    }
    return info;
}
Also used : GroupControl(com.google.gerrit.server.account.GroupControl) OrmException(com.google.gwtorm.server.OrmException) AgreementInfo(com.google.gerrit.extensions.common.AgreementInfo) GroupReference(com.google.gerrit.common.data.GroupReference) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) GroupResource(com.google.gerrit.server.group.GroupResource) NoSuchGroupException(com.google.gerrit.common.errors.NoSuchGroupException)

Example 2 with OrmException

use of com.google.gwtorm.server.OrmException in project gerrit by GerritCodeReview.

the class SubmittedTogether method applyInfo.

public SubmittedTogetherInfo applyInfo(ChangeResource resource) throws AuthException, IOException, OrmException {
    Change c = resource.getChange();
    try {
        List<ChangeData> cds;
        int hidden;
        if (c.getStatus().isOpen()) {
            ChangeSet cs = mergeSuperSet.get().completeChangeSet(dbProvider.get(), c, resource.getControl().getUser());
            cds = cs.changes().asList();
            hidden = cs.nonVisibleChanges().size();
        } else if (c.getStatus().asChangeStatus() == ChangeStatus.MERGED) {
            cds = queryProvider.get().bySubmissionId(c.getSubmissionId());
            hidden = 0;
        } else {
            cds = Collections.emptyList();
            hidden = 0;
        }
        if (hidden != 0 && !options.contains(NON_VISIBLE_CHANGES)) {
            throw new AuthException("change would be submitted with a change that you cannot see");
        }
        if (cds.size() <= 1 && hidden == 0) {
            cds = Collections.emptyList();
        } else {
            // Skip sorting for singleton lists, to avoid WalkSorter opening the
            // repo just to fill out the commit field in PatchSetData.
            cds = sort(cds);
        }
        SubmittedTogetherInfo info = new SubmittedTogetherInfo();
        info.changes = json.create(jsonOpt).formatChangeDatas(cds);
        info.nonVisibleChanges = hidden;
        return info;
    } catch (OrmException | IOException e) {
        log.error("Error on getting a ChangeSet", e);
        throw e;
    }
}
Also used : OrmException(com.google.gwtorm.server.OrmException) AuthException(com.google.gerrit.extensions.restapi.AuthException) Change(com.google.gerrit.reviewdb.client.Change) SubmittedTogetherInfo(com.google.gerrit.extensions.api.changes.SubmittedTogetherInfo) IOException(java.io.IOException) ChangeData(com.google.gerrit.server.query.change.ChangeData) ChangeSet(com.google.gerrit.server.git.ChangeSet)

Example 3 with OrmException

use of com.google.gwtorm.server.OrmException in project gerrit by GerritCodeReview.

the class RequestScopedReviewDbProvider method get.

@SuppressWarnings("resource")
@Override
public ReviewDb get() {
    if (db == null) {
        ReviewDb c;
        try {
            c = schema.open();
        } catch (OrmException e) {
            throw new ProvisionException("Cannot open ReviewDb", e);
        }
        try {
            cleanup.get().add(() -> {
                c.close();
                db = null;
            });
        } catch (Throwable e) {
            c.close();
            throw new ProvisionException("Cannot defer cleanup of ReviewDb", e);
        }
        db = c;
    }
    return db;
}
Also used : ProvisionException(com.google.inject.ProvisionException) OrmException(com.google.gwtorm.server.OrmException) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Example 4 with OrmException

use of com.google.gwtorm.server.OrmException in project gerrit by GerritCodeReview.

the class Schema_135 method migrateData.

@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException {
    try (Repository git = repoManager.openRepository(allProjectsName);
        MetaDataUpdate md = new MetaDataUpdate(GitReferenceUpdated.DISABLED, allProjectsName, git)) {
        ProjectConfig config = ProjectConfig.read(md);
        AccessSection meta = config.getAccessSection(RefNames.REFS_CONFIG, true);
        Permission createRefsMetaConfigPermission = meta.getPermission(Permission.CREATE, true);
        Set<GroupReference> groups = Stream.concat(config.getAccessSection(AccessSection.GLOBAL_CAPABILITIES, true).getPermission(GlobalCapability.ADMINISTRATE_SERVER, true).getRules().stream().map(PermissionRule::getGroup), Stream.of(systemGroupBackend.getGroup(PROJECT_OWNERS))).filter(g -> createRefsMetaConfigPermission.getRule(g) == null).collect(toSet());
        for (GroupReference group : groups) {
            createRefsMetaConfigPermission.add(new PermissionRule(config.resolve(group)));
        }
        md.getCommitBuilder().setAuthor(serverUser);
        md.getCommitBuilder().setCommitter(serverUser);
        md.setMessage(COMMIT_MSG);
        config.commit(md);
    } catch (ConfigInvalidException | IOException ex) {
        throw new OrmException(ex);
    }
}
Also used : PermissionRule(com.google.gerrit.common.data.PermissionRule) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) OrmException(com.google.gwtorm.server.OrmException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) GlobalCapability(com.google.gerrit.common.data.GlobalCapability) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate) Inject(com.google.inject.Inject) AccessSection(com.google.gerrit.common.data.AccessSection) SystemGroupBackend(com.google.gerrit.server.group.SystemGroupBackend) PROJECT_OWNERS(com.google.gerrit.server.group.SystemGroupBackend.PROJECT_OWNERS) Collectors.toSet(java.util.stream.Collectors.toSet) Permission(com.google.gerrit.common.data.Permission) Set(java.util.Set) IOException(java.io.IOException) PersonIdent(org.eclipse.jgit.lib.PersonIdent) Provider(com.google.inject.Provider) AllProjectsName(com.google.gerrit.server.config.AllProjectsName) Stream(java.util.stream.Stream) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) RefNames(com.google.gerrit.reviewdb.client.RefNames) ProjectConfig(com.google.gerrit.server.git.ProjectConfig) GroupReference(com.google.gerrit.common.data.GroupReference) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) GitReferenceUpdated(com.google.gerrit.server.extensions.events.GitReferenceUpdated) Repository(org.eclipse.jgit.lib.Repository) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) PermissionRule(com.google.gerrit.common.data.PermissionRule) IOException(java.io.IOException) AccessSection(com.google.gerrit.common.data.AccessSection) ProjectConfig(com.google.gerrit.server.git.ProjectConfig) Repository(org.eclipse.jgit.lib.Repository) OrmException(com.google.gwtorm.server.OrmException) Permission(com.google.gerrit.common.data.Permission) GroupReference(com.google.gerrit.common.data.GroupReference) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate)

Example 5 with OrmException

use of com.google.gwtorm.server.OrmException 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

OrmException (com.google.gwtorm.server.OrmException)108 IOException (java.io.IOException)63 Repository (org.eclipse.jgit.lib.Repository)31 Change (com.google.gerrit.reviewdb.client.Change)30 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)27 RevWalk (org.eclipse.jgit.revwalk.RevWalk)24 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)23 ObjectId (org.eclipse.jgit.lib.ObjectId)21 Account (com.google.gerrit.reviewdb.client.Account)20 ChangeData (com.google.gerrit.server.query.change.ChangeData)20 ArrayList (java.util.ArrayList)18 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)16 Inject (com.google.inject.Inject)16 Map (java.util.Map)16 Provider (com.google.inject.Provider)15 RestApiException (com.google.gerrit.extensions.restapi.RestApiException)13 Set (java.util.Set)13 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)12 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)12 CurrentUser (com.google.gerrit.server.CurrentUser)11