Search in sources :

Example 6 with Project

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

the class SubmitRuleEvaluator method evaluate.

/**
 * Evaluate the submit rules.
 *
 * @return List of {@link SubmitRecord} objects returned from the evaluated rules, including any
 *     errors.
 * @param cd ChangeData to evaluate
 */
public List<SubmitRecord> evaluate(ChangeData cd) {
    logger.atFine().log("Evaluate submit rules for change %d (caller: %s)", cd.change().getId().get(), callerFinder.findCallerLazy());
    try (Timer0.Context ignored = submitRuleEvaluationLatency.start()) {
        Change change;
        ProjectState projectState;
        try {
            change = cd.change();
            if (change == null) {
                throw new StorageException("Change not found");
            }
            Project.NameKey name = cd.project();
            Optional<ProjectState> projectStateOptional = projectCache.get(name);
            if (!projectStateOptional.isPresent()) {
                throw new NoSuchProjectException(name);
            }
            projectState = projectStateOptional.get();
        } catch (NoSuchProjectException e) {
            throw new IllegalStateException("Unable to find project while evaluating submit rule", e);
        }
        if (change.isClosed() && (!opts.recomputeOnClosedChanges() || OnlineReindexMode.isActive())) {
            return cd.notes().getSubmitRecords().stream().map(r -> {
                SubmitRecord record = r.deepCopy();
                if (record.status == SubmitRecord.Status.OK) {
                    // Submit records that were OK when they got merged are CLOSED now.
                    record.status = SubmitRecord.Status.CLOSED;
                }
                return record;
            }).collect(toImmutableList());
        }
        // and then we collect the results in one list.
        return Streams.stream(submitRules).filter(projectState.hasPrologRules() ? rule -> !(rule.get() instanceof DefaultSubmitRule) : rule -> true).map(c -> c.call(s -> {
            Optional<SubmitRecord> record = s.evaluate(cd);
            if (record.isPresent() && record.get().ruleName == null) {
                // Only back-fill the ruleName if it was not populated by the "submit
                // rule".
                record.get().ruleName = c.getPluginName() + "~" + s.getClass().getSimpleName();
            }
            return record;
        })).filter(Optional::isPresent).map(Optional::get).collect(toImmutableList());
    }
}
Also used : Inject(com.google.inject.Inject) Assisted(com.google.inject.assistedinject.Assisted) CallerFinder(com.google.gerrit.server.logging.CallerFinder) Description(com.google.gerrit.metrics.Description) PrologRule(com.google.gerrit.server.rules.PrologRule) Change(com.google.gerrit.entities.Change) OnlineReindexMode(com.google.gerrit.server.index.OnlineReindexMode) ChangeApi(com.google.gerrit.extensions.api.changes.ChangeApi) SubmitRule(com.google.gerrit.server.rules.SubmitRule) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) StorageException(com.google.gerrit.exceptions.StorageException) Timer0(com.google.gerrit.metrics.Timer0) Units(com.google.gerrit.metrics.Description.Units) Streams(com.google.common.collect.Streams) SubmitRecord(com.google.gerrit.entities.SubmitRecord) DefaultSubmitRule(com.google.gerrit.server.rules.DefaultSubmitRule) SubmitTypeRecord(com.google.gerrit.entities.SubmitTypeRecord) ChangeData(com.google.gerrit.server.query.change.ChangeData) List(java.util.List) Project(com.google.gerrit.entities.Project) PluginSetContext(com.google.gerrit.server.plugincontext.PluginSetContext) ChangeJson(com.google.gerrit.server.change.ChangeJson) Optional(java.util.Optional) MetricMaker(com.google.gerrit.metrics.MetricMaker) FluentLogger(com.google.common.flogger.FluentLogger) Optional(java.util.Optional) DefaultSubmitRule(com.google.gerrit.server.rules.DefaultSubmitRule) Change(com.google.gerrit.entities.Change) Project(com.google.gerrit.entities.Project) SubmitRecord(com.google.gerrit.entities.SubmitRecord) Timer0(com.google.gerrit.metrics.Timer0) StorageException(com.google.gerrit.exceptions.StorageException)

Example 7 with Project

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

the class AbstractGitCommand method start.

@Override
public void start(ChannelSession channel, Environment env) {
    enableGracefulStop();
    String gitProtocol = env.getEnv().get(GIT_PROTOCOL);
    if (gitProtocol != null) {
        extraParameters = gitProtocol.split(":");
    }
    Context ctx = context.subContext(newSession(), context.getCommandLine());
    final Context old = sshScope.set(ctx);
    try {
        startThread(new ProjectCommandRunnable() {

            @Override
            public void executeParseCommand(DynamicOptions pluginOptions) throws Exception {
                parseCommandLine(pluginOptions);
            }

            @Override
            public void run() throws Exception {
                AbstractGitCommand.this.service();
            }

            @Override
            public Project.NameKey getProjectName() {
                Project project = projectState.getProject();
                return project.getNameKey();
            }
        }, AccessPath.GIT);
    } finally {
        sshScope.set(old);
    }
}
Also used : Context(com.google.gerrit.sshd.SshScope.Context) DynamicOptions(com.google.gerrit.server.DynamicOptions) Project(com.google.gerrit.entities.Project) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) IOException(java.io.IOException)

Example 8 with Project

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

the class SubmoduleSectionParser method parse.

private SubmoduleSubscription parse(String id) {
    final String url = config.getString("submodule", id, "url");
    final String path = config.getString("submodule", id, "path");
    String branch = config.getString("submodule", id, "branch");
    try {
        if (url != null && url.length() > 0 && path != null && path.length() > 0 && branch != null && branch.length() > 0) {
            // All required fields filled.
            String project;
            if (branch.equals(".")) {
                branch = superProjectBranch.branch();
            }
            // relative URL
            if (url.startsWith("../")) {
                // prefix with a slash for easier relative path walks
                project = '/' + superProjectBranch.project().get();
                String hostPart = url;
                while (hostPart.startsWith("../")) {
                    int lastSlash = project.lastIndexOf('/');
                    if (lastSlash < 0) {
                        // too many levels up, ignore for now
                        return null;
                    }
                    project = project.substring(0, lastSlash);
                    hostPart = hostPart.substring(3);
                }
                project = project + "/" + hostPart;
                // remove leading '/'
                project = project.substring(1);
            } else {
                // It is actually an URI. It could be ssh://localhost/project-a.
                URI targetServerURI = new URI(url);
                URI thisServerURI = new URI(canonicalWebUrl);
                String thisHost = thisServerURI.getHost();
                String targetHost = targetServerURI.getHost();
                if (thisHost == null || targetHost == null || !targetHost.equalsIgnoreCase(thisHost)) {
                    return null;
                }
                String p1 = targetServerURI.getPath();
                String p2 = thisServerURI.getPath();
                if (!p1.startsWith(p2)) {
                    // http://server/other-teams-gerrit/
                    return null;
                }
                // skip common part
                project = p1.substring(p2.length());
            }
            while (project.startsWith("/")) {
                project = project.substring(1);
            }
            if (project.endsWith(Constants.DOT_GIT_EXT)) {
                project = project.substring(// 
                0, project.length() - Constants.DOT_GIT_EXT.length());
            }
            Project.NameKey projectKey = Project.nameKey(project);
            return new SubmoduleSubscription(superProjectBranch, BranchNameKey.create(projectKey, branch), path);
        }
    } catch (URISyntaxException e) {
    // Error in url syntax (in fact it is uri syntax)
    }
    return null;
}
Also used : Project(com.google.gerrit.entities.Project) SubmoduleSubscription(com.google.gerrit.entities.SubmoduleSubscription) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 9 with Project

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

the class ProjectIT method maxObjectSizeIsInheritedFromGlobalConfig.

@Test
@GerritConfig(name = "receive.maxObjectSizeLimit", value = "200k")
public void maxObjectSizeIsInheritedFromGlobalConfig() throws Exception {
    Project.NameKey child = projectOperations.newProject().parent(project).create();
    ConfigInfo info = getConfig();
    assertThat(info.maxObjectSizeLimit.value).isEqualTo("204800");
    assertThat(info.maxObjectSizeLimit.configuredValue).isNull();
    assertThat(info.maxObjectSizeLimit.summary).isEqualTo(INHERITED_FROM_GLOBAL);
    info = getConfig(child);
    assertThat(info.maxObjectSizeLimit.value).isEqualTo("204800");
    assertThat(info.maxObjectSizeLimit.configuredValue).isNull();
    assertThat(info.maxObjectSizeLimit.summary).isEqualTo(INHERITED_FROM_GLOBAL);
}
Also used : Project(com.google.gerrit.entities.Project) ConfigInfo(com.google.gerrit.extensions.api.projects.ConfigInfo) GerritConfig(com.google.gerrit.acceptance.config.GerritConfig) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 10 with Project

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

the class ProjectIT method maxObjectSizeDoesNotOverrideParentProjectWhenHigher.

@Test
@GerritConfig(name = "receive.inheritProjectMaxObjectSizeLimit", value = "true")
public void maxObjectSizeDoesNotOverrideParentProjectWhenHigher() throws Exception {
    Project.NameKey child = projectOperations.newProject().parent(project).create();
    ConfigInfo info = setMaxObjectSize("100k");
    assertThat(info.maxObjectSizeLimit.value).isEqualTo("102400");
    assertThat(info.maxObjectSizeLimit.configuredValue).isEqualTo("100k");
    assertThat(info.maxObjectSizeLimit.summary).isNull();
    info = setMaxObjectSize(child, "200k");
    assertThat(info.maxObjectSizeLimit.value).isEqualTo("102400");
    assertThat(info.maxObjectSizeLimit.configuredValue).isEqualTo("200k");
    assertThat(info.maxObjectSizeLimit.summary).isEqualTo(String.format(OVERRIDDEN_BY_PARENT, project));
}
Also used : Project(com.google.gerrit.entities.Project) ConfigInfo(com.google.gerrit.extensions.api.projects.ConfigInfo) GerritConfig(com.google.gerrit.acceptance.config.GerritConfig) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

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