Search in sources :

Example 56 with Project

use of com.google.gerrit.reviewdb.client.Project in project gerrit by GerritCodeReview.

the class ProjectControlHandler method parseArguments.

@Override
public final int parseArguments(final Parameters params) throws CmdLineException {
    String projectName = params.getParameter(0);
    while (projectName.endsWith("/")) {
        projectName = projectName.substring(0, projectName.length() - 1);
    }
    while (projectName.startsWith("/")) {
        // Be nice and drop the leading "/" if supplied by an absolute path.
        // We don't have a file system hierarchy, just a flat namespace in
        // the database's Project entities. We never encode these with a
        // leading '/' but users might accidentally include them in Git URLs.
        //
        projectName = projectName.substring(1);
    }
    String nameWithoutSuffix = ProjectUtil.stripGitSuffix(projectName);
    Project.NameKey nameKey = new Project.NameKey(nameWithoutSuffix);
    ProjectControl control;
    try {
        control = projectControlFactory.controlFor(nameKey, user.get());
        permissionBackend.user(user).project(nameKey).check(ProjectPermission.ACCESS);
    } catch (AuthException e) {
        throw new CmdLineException(owner, new NoSuchProjectException(nameKey).getMessage());
    } catch (NoSuchProjectException e) {
        throw new CmdLineException(owner, e.getMessage());
    } catch (PermissionBackendException | IOException e) {
        log.warn("Cannot load project " + nameWithoutSuffix, e);
        throw new CmdLineException(owner, new NoSuchProjectException(nameKey).getMessage());
    }
    setter.addValue(control);
    return 1;
}
Also used : Project(com.google.gerrit.reviewdb.client.Project) NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException) AuthException(com.google.gerrit.extensions.restapi.AuthException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) IOException(java.io.IOException) ProjectControl(com.google.gerrit.server.project.ProjectControl) CmdLineException(org.kohsuke.args4j.CmdLineException)

Example 57 with Project

use of com.google.gerrit.reviewdb.client.Project in project gerrit by GerritCodeReview.

the class AbandonUtil method abandonInactiveOpenChanges.

public void abandonInactiveOpenChanges(BatchUpdate.Factory updateFactory) {
    if (cfg.getAbandonAfter() <= 0) {
        return;
    }
    try {
        String query = "status:new age:" + TimeUnit.MILLISECONDS.toMinutes(cfg.getAbandonAfter()) + "m";
        if (!cfg.getAbandonIfMergeable()) {
            query += " -is:mergeable";
        }
        List<ChangeData> changesToAbandon = queryProcessor.enforceVisibility(false).query(queryBuilder.parse(query)).entities();
        ImmutableListMultimap.Builder<Project.NameKey, ChangeControl> builder = ImmutableListMultimap.builder();
        for (ChangeData cd : changesToAbandon) {
            ChangeControl control = cd.changeControl(internalUser);
            builder.put(control.getProject().getNameKey(), control);
        }
        int count = 0;
        ListMultimap<Project.NameKey, ChangeControl> abandons = builder.build();
        String message = cfg.getAbandonMessage();
        for (Project.NameKey project : abandons.keySet()) {
            Collection<ChangeControl> changes = getValidChanges(abandons.get(project), query);
            try {
                abandon.batchAbandon(updateFactory, project, internalUser, changes, message);
                count += changes.size();
            } catch (Throwable e) {
                StringBuilder msg = new StringBuilder("Failed to auto-abandon inactive change(s):");
                for (ChangeControl change : changes) {
                    msg.append(" ").append(change.getId().get());
                }
                msg.append(".");
                log.error(msg.toString(), e);
            }
        }
        log.info(String.format("Auto-Abandoned %d of %d changes.", count, changesToAbandon.size()));
    } catch (QueryParseException | OrmException e) {
        log.error("Failed to query inactive open changes for auto-abandoning.", e);
    }
}
Also used : ChangeData(com.google.gerrit.server.query.change.ChangeData) QueryParseException(com.google.gerrit.server.query.QueryParseException) Project(com.google.gerrit.reviewdb.client.Project) OrmException(com.google.gwtorm.server.OrmException) ChangeControl(com.google.gerrit.server.project.ChangeControl) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap)

Example 58 with Project

use of com.google.gerrit.reviewdb.client.Project in project gerrit by GerritCodeReview.

the class ChangeJson method toRevisionInfo.

private RevisionInfo toRevisionInfo(ChangeControl ctl, ChangeData cd, PatchSet in, @Nullable Repository repo, @Nullable RevWalk rw, boolean fillCommit, @Nullable ChangeInfo changeInfo) throws PatchListNotAvailableException, GpgException, OrmException, IOException {
    Change c = ctl.getChange();
    RevisionInfo out = new RevisionInfo();
    out.isCurrent = in.getId().equals(c.currentPatchSetId());
    out._number = in.getId().get();
    out.ref = in.getRefName();
    out.created = in.getCreatedOn();
    out.uploader = accountLoader.get(in.getUploader());
    out.draft = in.isDraft() ? true : null;
    out.fetch = makeFetchMap(ctl, in);
    out.kind = changeKindCache.getChangeKind(rw, repo != null ? repo.getConfig() : null, cd, in);
    out.description = in.getDescription();
    boolean setCommit = has(ALL_COMMITS) || (out.isCurrent && has(CURRENT_COMMIT));
    boolean addFooters = out.isCurrent && has(COMMIT_FOOTERS);
    if (setCommit || addFooters) {
        checkState(rw != null);
        checkState(repo != null);
        Project.NameKey project = c.getProject();
        String rev = in.getRevision().get();
        RevCommit commit = rw.parseCommit(ObjectId.fromString(rev));
        rw.parseBody(commit);
        if (setCommit) {
            out.commit = toCommit(ctl, rw, commit, has(WEB_LINKS), fillCommit);
        }
        if (addFooters) {
            Ref ref = repo.exactRef(ctl.getChange().getDest().get());
            RevCommit mergeTip = null;
            if (ref != null) {
                mergeTip = rw.parseCommit(ref.getObjectId());
                rw.parseBody(mergeTip);
            }
            out.commitWithFooters = mergeUtilFactory.create(projectCache.get(project)).createCommitMessageOnSubmit(commit, mergeTip, ctl, in.getId());
        }
    }
    if (has(ALL_FILES) || (out.isCurrent && has(CURRENT_FILES))) {
        out.files = fileInfoJson.toFileInfoMap(c, in);
        out.files.remove(Patch.COMMIT_MSG);
        out.files.remove(Patch.MERGE_LIST);
    }
    if ((out.isCurrent || (out.draft != null && out.draft)) && has(CURRENT_ACTIONS) && userProvider.get().isIdentifiedUser()) {
        actionJson.addRevisionActions(changeInfo, out, new RevisionResource(changeResourceFactory.create(ctl), in));
    }
    if (gpgApi.isEnabled() && has(PUSH_CERTIFICATES)) {
        if (in.getPushCertificate() != null) {
            out.pushCertificate = gpgApi.checkPushCertificate(in.getPushCertificate(), userFactory.create(in.getUploader()));
        } else {
            out.pushCertificate = new PushCertificateInfo();
        }
    }
    return out;
}
Also used : Project(com.google.gerrit.reviewdb.client.Project) Ref(org.eclipse.jgit.lib.Ref) RevisionInfo(com.google.gerrit.extensions.common.RevisionInfo) Change(com.google.gerrit.reviewdb.client.Change) PushCertificateInfo(com.google.gerrit.extensions.common.PushCertificateInfo) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 59 with Project

use of com.google.gerrit.reviewdb.client.Project in project gerrit by GerritCodeReview.

the class ChangeJson method toCommit.

CommitInfo toCommit(ChangeControl ctl, RevWalk rw, RevCommit commit, boolean addLinks, boolean fillCommit) throws IOException {
    Project.NameKey project = ctl.getProject().getNameKey();
    CommitInfo info = new CommitInfo();
    if (fillCommit) {
        info.commit = commit.name();
    }
    info.parents = new ArrayList<>(commit.getParentCount());
    info.author = toGitPerson(commit.getAuthorIdent());
    info.committer = toGitPerson(commit.getCommitterIdent());
    info.subject = commit.getShortMessage();
    info.message = commit.getFullMessage();
    if (addLinks) {
        List<WebLinkInfo> links = webLinks.getPatchSetLinks(project, commit.name());
        info.webLinks = links.isEmpty() ? null : links;
    }
    for (RevCommit parent : commit.getParents()) {
        rw.parseBody(parent);
        CommitInfo i = new CommitInfo();
        i.commit = parent.name();
        i.subject = parent.getShortMessage();
        if (addLinks) {
            List<WebLinkInfo> parentLinks = webLinks.getParentLinks(project, parent.name());
            i.webLinks = parentLinks.isEmpty() ? null : parentLinks;
        }
        info.parents.add(i);
    }
    return info;
}
Also used : Project(com.google.gerrit.reviewdb.client.Project) WebLinkInfo(com.google.gerrit.extensions.common.WebLinkInfo) CommitInfo(com.google.gerrit.extensions.common.CommitInfo) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 60 with Project

use of com.google.gerrit.reviewdb.client.Project in project gerrit by GerritCodeReview.

the class ChangeIdHandler method parseArguments.

@Override
public final int parseArguments(final Parameters params) throws CmdLineException {
    final String token = params.getParameter(0);
    final String[] tokens = token.split(",");
    if (tokens.length != 3) {
        throw new CmdLineException(owner, "change should be specified as <project>,<branch>,<change-id>");
    }
    try {
        final Change.Key key = Change.Key.parse(tokens[2]);
        final Project.NameKey project = new Project.NameKey(tokens[0]);
        final Branch.NameKey branch = new Branch.NameKey(project, tokens[1]);
        for (final ChangeData cd : queryProvider.get().byBranchKey(branch, key)) {
            setter.addValue(cd.getId());
            return 1;
        }
    } catch (IllegalArgumentException e) {
        throw new CmdLineException(owner, "Change-Id is not valid");
    } catch (OrmException e) {
        throw new CmdLineException(owner, "Database error: " + e.getMessage());
    }
    throw new CmdLineException(owner, "\"" + token + "\": change not found");
}
Also used : Change(com.google.gerrit.reviewdb.client.Change) ChangeData(com.google.gerrit.server.query.change.ChangeData) Project(com.google.gerrit.reviewdb.client.Project) OrmException(com.google.gwtorm.server.OrmException) Branch(com.google.gerrit.reviewdb.client.Branch) CmdLineException(org.kohsuke.args4j.CmdLineException)

Aggregations

Project (com.google.gerrit.reviewdb.client.Project)145 Test (org.junit.Test)73 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)47 Change (com.google.gerrit.reviewdb.client.Change)32 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)29 Branch (com.google.gerrit.reviewdb.client.Branch)25 Account (com.google.gerrit.reviewdb.client.Account)23 InMemoryRepository (org.eclipse.jgit.internal.storage.dfs.InMemoryRepository)22 Config (org.eclipse.jgit.lib.Config)20 Repository (org.eclipse.jgit.lib.Repository)20 RevCommit (org.eclipse.jgit.revwalk.RevCommit)20 ProjectConfig (com.google.gerrit.server.git.ProjectConfig)19 SubmoduleSubscription (com.google.gerrit.reviewdb.client.SubmoduleSubscription)17 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)16 IOException (java.io.IOException)16 ObjectId (org.eclipse.jgit.lib.ObjectId)16 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)15 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)15 MetaDataUpdate (com.google.gerrit.server.git.MetaDataUpdate)14 SubmoduleSectionParser (com.google.gerrit.server.util.SubmoduleSectionParser)14