Search in sources :

Example 1 with Project

use of com.google.gerrit.entities.Project in project gerrit by GerritCodeReview.

the class GitwebServlet method makeEnv.

private String[] makeEnv(HttpServletRequest req, ProjectState projectState) {
    final EnvList env = new EnvList(_env);
    final int contentLength = Math.max(0, req.getContentLength());
    // These ones are from "The WWW Common Gateway Interface Version 1.1"
    // 
    env.set("AUTH_TYPE", req.getAuthType());
    env.set("CONTENT_LENGTH", Integer.toString(contentLength));
    env.set("CONTENT_TYPE", req.getContentType());
    env.set("GATEWAY_INTERFACE", "CGI/1.1");
    env.set("PATH_INFO", req.getPathInfo());
    env.set("PATH_TRANSLATED", null);
    env.set("QUERY_STRING", req.getQueryString());
    env.set("REMOTE_ADDR", req.getRemoteAddr());
    env.set("REMOTE_HOST", req.getRemoteHost());
    env.set("HTTPS", req.isSecure() ? "ON" : "OFF");
    // The identity information reported about the connection by a
    // RFC 1413 [11] request to the remote agent, if
    // available. Servers MAY choose not to support this feature, or
    // not to request the data for efficiency reasons.
    // "REMOTE_IDENT" => "NYI"
    // 
    env.set("REQUEST_METHOD", req.getMethod());
    env.set("SCRIPT_NAME", req.getContextPath() + req.getServletPath());
    env.set("SCRIPT_FILENAME", gitwebCgi.toAbsolutePath().toString());
    env.set("SERVER_NAME", req.getServerName());
    env.set("SERVER_PORT", Integer.toString(req.getServerPort()));
    env.set("SERVER_PROTOCOL", req.getProtocol());
    env.set("SERVER_SOFTWARE", getServletContext().getServerInfo());
    for (String name : getHeaderNames(req)) {
        final String value = req.getHeader(name);
        env.set("HTTP_" + name.toUpperCase().replace('-', '_'), value);
    }
    Project.NameKey nameKey = projectState.getNameKey();
    env.set("GERRIT_CONTEXT_PATH", req.getContextPath() + "/");
    env.set("GERRIT_PROJECT_NAME", nameKey.get());
    env.set("GITWEB_PROJECTROOT", repoManager.getBasePath(nameKey).toAbsolutePath().toString());
    if (projectState.statePermitsRead() && permissionBackend.user(anonymousUserProvider.get()).project(nameKey).testOrFalse(ProjectPermission.READ)) {
        env.set("GERRIT_ANONYMOUS_READ", "1");
    }
    String remoteUser = null;
    if (userProvider.get().isIdentifiedUser()) {
        IdentifiedUser u = userProvider.get().asIdentifiedUser();
        Optional<String> user = u.getUserName();
        env.set("GERRIT_USER_NAME", user.orElse(null));
        remoteUser = user.orElseGet(() -> "account-" + u.getAccountId());
    }
    env.set("REMOTE_USER", remoteUser);
    // 
    if (gitwebUrl != null) {
        int schemePort = -1;
        if (gitwebUrl.getScheme() != null) {
            if (gitwebUrl.getScheme().equals("http")) {
                env.set("HTTPS", "OFF");
                schemePort = 80;
            } else {
                env.set("HTTPS", "ON");
                schemePort = 443;
            }
        }
        if (gitwebUrl.getHost() != null) {
            env.set("SERVER_NAME", gitwebUrl.getHost());
            env.set("HTTP_HOST", gitwebUrl.getHost());
        }
        if (gitwebUrl.getPort() != -1) {
            env.set("SERVER_PORT", Integer.toString(gitwebUrl.getPort()));
        } else if (schemePort != -1) {
            env.set("SERVER_PORT", Integer.toString(schemePort));
        }
        if (gitwebUrl.getPath() != null) {
            env.set("SCRIPT_NAME", gitwebUrl.getPath().isEmpty() ? "/" : gitwebUrl.getPath());
        }
    }
    return env.getEnvArray();
}
Also used : Project(com.google.gerrit.entities.Project) IdentifiedUser(com.google.gerrit.server.IdentifiedUser)

Example 2 with Project

use of com.google.gerrit.entities.Project in project gerrit by GerritCodeReview.

the class PerPatchsetOperationsImpl method createRobotComment.

private String createRobotComment(TestRobotCommentCreation robotCommentCreation) throws IOException, RestApiException, UpdateException {
    Project.NameKey project = changeNotes.getProjectName();
    try (Repository repository = repositoryManager.openRepository(project);
        ObjectInserter objectInserter = repository.newObjectInserter();
        RevWalk revWalk = new RevWalk(objectInserter.newReader())) {
        Instant now = TimeUtil.now();
        IdentifiedUser author = getAuthor(robotCommentCreation);
        RobotCommentAdditionOp robotCommentAdditionOp = new RobotCommentAdditionOp(robotCommentCreation);
        try (BatchUpdate batchUpdate = batchUpdateFactory.create(project, author, now)) {
            batchUpdate.setRepository(repository, revWalk, objectInserter);
            batchUpdate.addOp(changeNotes.getChangeId(), robotCommentAdditionOp);
            batchUpdate.execute();
        }
        return robotCommentAdditionOp.createdRobotCommentUuid;
    }
}
Also used : Project(com.google.gerrit.entities.Project) Repository(org.eclipse.jgit.lib.Repository) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) Instant(java.time.Instant) RevWalk(org.eclipse.jgit.revwalk.RevWalk) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) BatchUpdate(com.google.gerrit.server.update.BatchUpdate)

Example 3 with Project

use of com.google.gerrit.entities.Project in project gerrit by GerritCodeReview.

the class PerPatchsetOperationsImpl method createComment.

private String createComment(TestCommentCreation commentCreation) throws IOException, RestApiException, UpdateException {
    Project.NameKey project = changeNotes.getProjectName();
    try (Repository repository = repositoryManager.openRepository(project);
        ObjectInserter objectInserter = repository.newObjectInserter();
        RevWalk revWalk = new RevWalk(objectInserter.newReader())) {
        Instant now = TimeUtil.now();
        IdentifiedUser author = getAuthor(commentCreation);
        CommentAdditionOp commentAdditionOp = new CommentAdditionOp(commentCreation);
        try (BatchUpdate batchUpdate = batchUpdateFactory.create(project, author, now)) {
            batchUpdate.setRepository(repository, revWalk, objectInserter);
            batchUpdate.addOp(changeNotes.getChangeId(), commentAdditionOp);
            batchUpdate.execute();
        }
        return commentAdditionOp.createdCommentUuid;
    }
}
Also used : Project(com.google.gerrit.entities.Project) Repository(org.eclipse.jgit.lib.Repository) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) Instant(java.time.Instant) RevWalk(org.eclipse.jgit.revwalk.RevWalk) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) BatchUpdate(com.google.gerrit.server.update.BatchUpdate)

Example 4 with Project

use of com.google.gerrit.entities.Project in project gerrit by GerritCodeReview.

the class ChangeIdHandler method parseArguments.

@Override
public final int parseArguments(Parameters params) throws CmdLineException {
    String token = params.getParameter(0);
    List<String> tokens = Splitter.on(',').splitToList(token);
    if (tokens.size() != 3) {
        throw new CmdLineException(owner, localizable("change should be specified as <project>,<branch>,<change-id>"));
    }
    try {
        Change.Key key = Change.Key.parse(tokens.get(2));
        Project.NameKey project = Project.nameKey(tokens.get(0));
        BranchNameKey branch = BranchNameKey.create(project, tokens.get(1));
        List<ChangeData> changes = queryProvider.get().byBranchKey(branch, key);
        if (!changes.isEmpty()) {
            if (changes.size() > 1) {
                String msg = "\"%s\": resolves to multiple changes";
                logger.atSevere().log(msg, token);
                throw new CmdLineException(owner, localizable(msg), token);
            }
            setter.addValue(changes.get(0).getId());
            return 1;
        }
    } catch (IllegalArgumentException e) {
        throw new CmdLineException(owner, localizable("Change-Id is not valid: %s"), e.getMessage());
    } catch (StorageException e) {
        throw new CmdLineException(owner, localizable("Database error: %s"), e.getMessage());
    }
    throw new CmdLineException(owner, localizable("\"%s\": change not found"), token);
}
Also used : Project(com.google.gerrit.entities.Project) BranchNameKey(com.google.gerrit.entities.BranchNameKey) Change(com.google.gerrit.entities.Change) ChangeData(com.google.gerrit.server.query.change.ChangeData) StorageException(com.google.gerrit.exceptions.StorageException) CmdLineException(org.kohsuke.args4j.CmdLineException)

Example 5 with Project

use of com.google.gerrit.entities.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 = queryProvider.get().enforceVisibility(false).query(queryBuilderProvider.get().parse(query)).entities();
        ImmutableListMultimap.Builder<Project.NameKey, ChangeData> builder = ImmutableListMultimap.builder();
        for (ChangeData cd : changesToAbandon) {
            builder.put(cd.project(), cd);
        }
        int count = 0;
        ListMultimap<Project.NameKey, ChangeData> abandons = builder.build();
        String message = cfg.getAbandonMessage();
        for (Project.NameKey project : abandons.keySet()) {
            Collection<ChangeData> changes = getValidChanges(abandons.get(project), query);
            try {
                batchAbandon.batchAbandon(updateFactory, project, internalUser, changes, message);
                count += changes.size();
            } catch (Exception e) {
                StringBuilder msg = new StringBuilder("Failed to auto-abandon inactive change(s):");
                for (ChangeData change : changes) {
                    msg.append(" ").append(change.getId().get());
                }
                msg.append(".");
                logger.atSevere().withCause(e).log("%s", msg);
            }
        }
        logger.atInfo().log("Auto-Abandoned %d of %d changes.", count, changesToAbandon.size());
    } catch (QueryParseException | StorageException e) {
        logger.atSevere().withCause(e).log("Failed to query inactive open changes for auto-abandoning.");
    }
}
Also used : ChangeData(com.google.gerrit.server.query.change.ChangeData) StorageException(com.google.gerrit.exceptions.StorageException) QueryParseException(com.google.gerrit.index.query.QueryParseException) QueryParseException(com.google.gerrit.index.query.QueryParseException) Project(com.google.gerrit.entities.Project) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) StorageException(com.google.gerrit.exceptions.StorageException)

Aggregations

Project (com.google.gerrit.entities.Project)184 Test (org.junit.Test)109 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)67 Change (com.google.gerrit.entities.Change)43 Repository (org.eclipse.jgit.lib.Repository)34 ChangeInfo (com.google.gerrit.extensions.common.ChangeInfo)33 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)32 InMemoryRepository (org.eclipse.jgit.internal.storage.dfs.InMemoryRepository)31 BranchNameKey (com.google.gerrit.entities.BranchNameKey)30 Config (org.eclipse.jgit.lib.Config)26 ObjectId (org.eclipse.jgit.lib.ObjectId)26 IOException (java.io.IOException)25 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)24 ProjectState (com.google.gerrit.server.project.ProjectState)23 Inject (com.google.inject.Inject)23 List (java.util.List)23 AuthException (com.google.gerrit.extensions.restapi.AuthException)22 ChangeData (com.google.gerrit.server.query.change.ChangeData)22 RevCommit (org.eclipse.jgit.revwalk.RevCommit)22 PatchSet (com.google.gerrit.entities.PatchSet)20