Search in sources :

Example 96 with Project

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

the class StalenessChecker method check.

/**
 * Returns a {@link StalenessCheckResult} with structured information about staleness of the
 * provided {@link com.google.gerrit.entities.Project.NameKey}.
 */
public StalenessCheckResult check(Project.NameKey project) {
    ProjectData projectData = projectCache.get(project).orElseThrow(illegalState(project)).toProjectData();
    ProjectIndex i = indexes.getSearchIndex();
    if (i == null) {
        return StalenessCheckResult.notStale();
    }
    Optional<FieldBundle> result = i.getRaw(project, QueryOptions.create(indexConfig, 0, 1, FIELDS));
    if (!result.isPresent()) {
        return StalenessCheckResult.stale("Document %s missing from index", project);
    }
    SetMultimap<Project.NameKey, RefState> indexedRefStates = RefState.parseStates(result.get().getValue(ProjectField.REF_STATE));
    SetMultimap<Project.NameKey, RefState> currentRefStates = MultimapBuilder.hashKeys().hashSetValues().build();
    projectData.tree().stream().filter(p -> p.getProject().getConfigRefState() != null).forEach(p -> currentRefStates.put(p.getProject().getNameKey(), RefState.create(RefNames.REFS_CONFIG, p.getProject().getConfigRefState())));
    if (currentRefStates.equals(indexedRefStates)) {
        return StalenessCheckResult.notStale();
    }
    return StalenessCheckResult.stale("Document has unexpected ref states (%s != %s)", currentRefStates, indexedRefStates);
}
Also used : RefState(com.google.gerrit.index.RefState) ProjectCache.illegalState(com.google.gerrit.server.project.ProjectCache.illegalState) ImmutableSet(com.google.common.collect.ImmutableSet) IndexConfig(com.google.gerrit.index.IndexConfig) ProjectCache(com.google.gerrit.server.project.ProjectCache) MultimapBuilder(com.google.common.collect.MultimapBuilder) ProjectField(com.google.gerrit.index.project.ProjectField) Inject(com.google.inject.Inject) QueryOptions(com.google.gerrit.index.QueryOptions) ProjectData(com.google.gerrit.index.project.ProjectData) SetMultimap(com.google.common.collect.SetMultimap) StalenessCheckResult(com.google.gerrit.server.index.StalenessCheckResult) ProjectIndex(com.google.gerrit.index.project.ProjectIndex) ProjectIndexCollection(com.google.gerrit.index.project.ProjectIndexCollection) Project(com.google.gerrit.entities.Project) RefNames(com.google.gerrit.entities.RefNames) FieldBundle(com.google.gerrit.index.query.FieldBundle) Optional(java.util.Optional) RefState(com.google.gerrit.index.RefState) FieldBundle(com.google.gerrit.index.query.FieldBundle) ProjectIndex(com.google.gerrit.index.project.ProjectIndex) ProjectData(com.google.gerrit.index.project.ProjectData)

Example 97 with Project

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

the class DiffOperationsImpl method getModifiedFiles.

private ImmutableMap<String, FileDiffOutput> getModifiedFiles(DiffParameters diffParams, DiffOptions diffOptions) throws DiffNotAvailableException {
    try {
        Project.NameKey project = diffParams.project();
        ObjectId newCommit = diffParams.newCommit();
        ObjectId oldCommit = diffParams.baseCommit();
        ComparisonType cmp = diffParams.comparisonType();
        ImmutableList<ModifiedFile> modifiedFiles = modifiedFilesCache.get(createModifiedFilesKey(project, oldCommit, newCommit));
        List<FileDiffCacheKey> fileCacheKeys = new ArrayList<>();
        fileCacheKeys.add(createFileDiffCacheKey(project, oldCommit, newCommit, COMMIT_MSG, DEFAULT_DIFF_ALGORITHM, /* useTimeout= */
        true, /* whitespace= */
        null));
        if (cmp.isAgainstAutoMerge() || isMergeAgainstParent(cmp, project, newCommit)) {
            fileCacheKeys.add(createFileDiffCacheKey(project, oldCommit, newCommit, MERGE_LIST, DEFAULT_DIFF_ALGORITHM, /* useTimeout= */
            true, /*whitespace = */
            null));
        }
        if (diffParams.skipFiles() == null) {
            modifiedFiles.stream().map(entity -> createFileDiffCacheKey(project, oldCommit, newCommit, entity.newPath().isPresent() ? entity.newPath().get() : entity.oldPath().get(), DEFAULT_DIFF_ALGORITHM, /* useTimeout= */
            true, /* whitespace= */
            null)).forEach(fileCacheKeys::add);
        }
        return getModifiedFilesForKeys(fileCacheKeys, diffOptions);
    } catch (IOException e) {
        throw new DiffNotAvailableException(e);
    }
}
Also used : Patch(com.google.gerrit.entities.Patch) Module(com.google.inject.Module) ModifiedFilesCache(com.google.gerrit.server.patch.diff.ModifiedFilesCache) Whitespace(com.google.gerrit.extensions.client.DiffPreferencesInfo.Whitespace) DiffAlgorithm(com.google.gerrit.server.patch.gitfilediff.GitFileDiffCacheImpl.DiffAlgorithm) Inject(com.google.inject.Inject) FileDiffCache(com.google.gerrit.server.patch.filediff.FileDiffCache) MERGE_LIST(com.google.gerrit.entities.Patch.MERGE_LIST) ImmutableCollection(com.google.common.collect.ImmutableCollection) Function(java.util.function.Function) ArrayList(java.util.ArrayList) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Config(org.eclipse.jgit.lib.Config) ChangeType(com.google.gerrit.entities.Patch.ChangeType) ImmutableList(com.google.common.collect.ImmutableList) FileDiffOutput(com.google.gerrit.server.patch.filediff.FileDiffOutput) Map(java.util.Map) ModifiedFilesCacheKey(com.google.gerrit.server.patch.diff.ModifiedFilesCacheKey) GitModifiedFilesCacheImpl(com.google.gerrit.server.patch.gitdiff.GitModifiedFilesCacheImpl) DiffPreferencesInfo(com.google.gerrit.extensions.client.DiffPreferencesInfo) ImmutableMap(com.google.common.collect.ImmutableMap) ModifiedFile(com.google.gerrit.server.patch.gitdiff.ModifiedFile) DisabledOutputStream(org.eclipse.jgit.util.io.DisabledOutputStream) FileDiffCacheKey(com.google.gerrit.server.patch.filediff.FileDiffCacheKey) FileDiffCacheImpl(com.google.gerrit.server.patch.filediff.FileDiffCacheImpl) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) COMMIT_MSG(com.google.gerrit.entities.Patch.COMMIT_MSG) CacheModule(com.google.gerrit.server.cache.CacheModule) GitFileDiffCacheImpl(com.google.gerrit.server.patch.gitfilediff.GitFileDiffCacheImpl) ObjectId(org.eclipse.jgit.lib.ObjectId) List(java.util.List) Nullable(com.google.gerrit.common.Nullable) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) AutoValue(com.google.auto.value.AutoValue) Project(com.google.gerrit.entities.Project) Optional(java.util.Optional) DiffEntry(org.eclipse.jgit.diff.DiffEntry) ModifiedFilesCacheImpl(com.google.gerrit.server.patch.diff.ModifiedFilesCacheImpl) ObjectReader(org.eclipse.jgit.lib.ObjectReader) FluentLogger(com.google.common.flogger.FluentLogger) Singleton(com.google.inject.Singleton) ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Project(com.google.gerrit.entities.Project) FileDiffCacheKey(com.google.gerrit.server.patch.filediff.FileDiffCacheKey) ModifiedFile(com.google.gerrit.server.patch.gitdiff.ModifiedFile)

Example 98 with Project

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

the class ProjectConfig method getCacheable.

/**
 * Returns an immutable, thread-safe representation of this object that can be cached.
 */
public CachedProjectConfig getCacheable() {
    CachedProjectConfig.Builder builder = CachedProjectConfig.builder().setProject(project).setAccountsSection(accountsSection).setBranchOrderSection(Optional.ofNullable(branchOrderSection)).setMimeTypes(mimeTypes).setRulesId(Optional.ofNullable(rulesId)).setRevision(Optional.ofNullable(getRevision())).setMaxObjectSizeLimit(maxObjectSizeLimit).setCheckReceivedObjects(checkReceivedObjects).setExtensionPanelSections(extensionPanelSections);
    groupList.byUUID().values().forEach(g -> builder.addGroup(g));
    contributorAgreements.values().forEach(c -> builder.addContributorAgreement(c));
    notifySections.values().forEach(n -> builder.addNotifySection(n));
    subscribeSections.values().forEach(s -> builder.addSubscribeSection(s));
    commentLinkSections.values().forEach(c -> builder.addCommentLinkSection(c));
    labelSections.values().forEach(l -> builder.addLabelSection(l));
    submitRequirementSections.values().forEach(sr -> builder.addSubmitRequirementSection(sr));
    pluginConfigs.entrySet().forEach(c -> builder.addPluginConfig(c.getKey(), c.getValue().toText()));
    projectLevelConfigs.entrySet().forEach(c -> builder.addProjectLevelConfig(c.getKey(), c.getValue().toText()));
    if (projectName.equals(allProjectsName)) {
        // Filter out permissions that aren't allowed to be set on All-Projects
        accessSections.values().forEach(a -> {
            List<Permission.Builder> copy = new ArrayList<>();
            for (Permission p : a.getPermissions()) {
                if (Permission.canBeOnAllProjects(a.getName(), p.getName())) {
                    copy.add(p.toBuilder());
                }
            }
            AccessSection section = AccessSection.builder(a.getName()).modifyPermissions(permissions -> permissions.addAll(copy)).build();
            builder.addAccessSection(section);
        });
    } else {
        accessSections.values().forEach(a -> builder.addAccessSection(a));
    }
    return builder.build();
}
Also used : GroupBackend(com.google.gerrit.server.account.GroupBackend) Arrays(java.util.Arrays) GlobalCapability(com.google.gerrit.common.data.GlobalCapability) Inject(com.google.inject.Inject) RevWalk(org.eclipse.jgit.revwalk.RevWalk) InheritableBoolean(com.google.gerrit.extensions.client.InheritableBoolean) Config(org.eclipse.jgit.lib.Config) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) VersionedMetaData(com.google.gerrit.server.git.meta.VersionedMetaData) LabelValue(com.google.gerrit.entities.LabelValue) LabelType(com.google.gerrit.entities.LabelType) ConfigUtil(com.google.gerrit.server.config.ConfigUtil) Locale(java.util.Locale) Map(java.util.Map) ContributorAgreement(com.google.gerrit.entities.ContributorAgreement) RefNames(com.google.gerrit.entities.RefNames) Splitter(com.google.common.base.Splitter) PluginConfig(com.google.gerrit.server.config.PluginConfig) EnumSet(java.util.EnumSet) Permission.isPermission(com.google.gerrit.entities.Permission.isPermission) ImmutableSet(com.google.common.collect.ImmutableSet) PatternSyntaxException(java.util.regex.PatternSyntaxException) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) AccessSection(com.google.gerrit.entities.AccessSection) Collection(java.util.Collection) Set(java.util.Set) Sets(com.google.common.collect.Sets) Preconditions.checkState(com.google.common.base.Preconditions.checkState) GroupReference(com.google.gerrit.entities.GroupReference) Objects(java.util.Objects) List(java.util.List) Nullable(com.google.gerrit.common.Nullable) AllProjectsName(com.google.gerrit.server.config.AllProjectsName) DEFAULT_SUBMIT_TYPE(com.google.gerrit.entities.Project.DEFAULT_SUBMIT_TYPE) ConfiguredMimeTypes(com.google.gerrit.entities.ConfiguredMimeTypes) NotifyConfig(com.google.gerrit.entities.NotifyConfig) BooleanProjectConfig(com.google.gerrit.entities.BooleanProjectConfig) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) AllProjectsConfigProvider(com.google.gerrit.server.config.AllProjectsConfigProvider) FluentLogger(com.google.common.flogger.FluentLogger) Joiner(com.google.common.base.Joiner) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate) Singleton(com.google.inject.Singleton) ValidationError(com.google.gerrit.server.git.ValidationError) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) Shorts(com.google.common.primitives.Shorts) SubmitRequirementExpression(com.google.gerrit.entities.SubmitRequirementExpression) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Strings(com.google.common.base.Strings) PluginPermissionsUtil.isValidPluginPermission(com.google.gerrit.server.permissions.PluginPermissionsUtil.isValidPluginPermission) ImmutableList(com.google.common.collect.ImmutableList) GroupDescription(com.google.gerrit.entities.GroupDescription) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) Objects.requireNonNull(java.util.Objects.requireNonNull) LabelFunction(com.google.gerrit.entities.LabelFunction) PermissionRule(com.google.gerrit.entities.PermissionRule) CachedProjectConfig(com.google.gerrit.entities.CachedProjectConfig) UsedAt(com.google.gerrit.common.UsedAt) Address(com.google.gerrit.entities.Address) SubmitRequirement(com.google.gerrit.entities.SubmitRequirement) AccountGroup(com.google.gerrit.entities.AccountGroup) AccountsSection(com.google.gerrit.entities.AccountsSection) CharMatcher(com.google.common.base.CharMatcher) Permission(com.google.gerrit.entities.Permission) IOException(java.io.IOException) ProjectState(com.google.gerrit.extensions.client.ProjectState) Maps(com.google.common.collect.Maps) ObjectId(org.eclipse.jgit.lib.ObjectId) Consumer(java.util.function.Consumer) Collectors.toList(java.util.stream.Collectors.toList) NotifyType(com.google.gerrit.entities.NotifyConfig.NotifyType) SubscribeSection(com.google.gerrit.entities.SubscribeSection) Project(com.google.gerrit.entities.Project) StoredConfig(org.eclipse.jgit.lib.StoredConfig) VisibleForTesting(com.google.common.annotations.VisibleForTesting) StoredCommentLinkInfo(com.google.gerrit.entities.StoredCommentLinkInfo) BranchOrderSection(com.google.gerrit.entities.BranchOrderSection) Collections(java.util.Collections) Action(com.google.gerrit.entities.PermissionRule.Action) Repository(org.eclipse.jgit.lib.Repository) InvalidNameException(com.google.gerrit.exceptions.InvalidNameException) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) ArrayList(java.util.ArrayList) Permission.isPermission(com.google.gerrit.entities.Permission.isPermission) PluginPermissionsUtil.isValidPluginPermission(com.google.gerrit.server.permissions.PluginPermissionsUtil.isValidPluginPermission) Permission(com.google.gerrit.entities.Permission) CachedProjectConfig(com.google.gerrit.entities.CachedProjectConfig) AccessSection(com.google.gerrit.entities.AccessSection)

Example 99 with Project

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

the class ProjectCacheWarmer method start.

@Override
public void start() {
    int cpus = Runtime.getRuntime().availableProcessors();
    if (config.getBoolean("cache", "projects", "loadOnStartup", false)) {
        ExecutorService pool = new LoggingContextAwareExecutorService(new ScheduledThreadPoolExecutor(config.getInt("cache", "projects", "loadThreads", cpus), new ThreadFactoryBuilder().setNameFormat("ProjectCacheLoader-%d").build()));
        Thread scheduler = new Thread(() -> {
            for (Project.NameKey name : cache.all()) {
                pool.execute(() -> {
                    Optional<ProjectState> project = cache.get(name);
                    if (!project.isPresent()) {
                        throw new IllegalStateException("race while traversing projects. got " + name + " when loading all projects, but can't load it now");
                    }
                });
            }
            pool.shutdown();
            try {
                pool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
                logger.atInfo().log("Finished loading project cache");
            } catch (InterruptedException e) {
                logger.atWarning().log("Interrupted while waiting for project cache to load");
            }
        });
        scheduler.setName("ProjectCacheWarmer");
        scheduler.setDaemon(true);
        logger.atInfo().log("Loading project cache");
        scheduler.start();
    }
}
Also used : Project(com.google.gerrit.entities.Project) LoggingContextAwareExecutorService(com.google.gerrit.server.logging.LoggingContextAwareExecutorService) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) LoggingContextAwareExecutorService(com.google.gerrit.server.logging.LoggingContextAwareExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder)

Example 100 with Project

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

the class ProjectCreator method createProject.

public ProjectState createProject(CreateProjectArgs args) throws BadRequestException, ResourceConflictException, IOException, ConfigInvalidException {
    final Project.NameKey nameKey = args.getProject();
    try {
        final String head = args.permissionsOnly ? RefNames.REFS_CONFIG : args.branch.get(0);
        Status status = repoManager.getRepositoryStatus(nameKey);
        if (!status.equals(Status.NON_EXISTENT)) {
            throw new RepositoryExistsException(nameKey, "Repository status: " + status);
        }
        try (Repository repo = repoManager.createRepository(nameKey)) {
            RefUpdate u = repo.updateRef(Constants.HEAD);
            u.disableRefLog();
            u.link(head);
            createProjectConfig(args);
            if (!args.permissionsOnly && args.createEmptyCommit) {
                createEmptyCommits(repo, nameKey, args.branch);
            }
            fire(nameKey, head);
            return projectCache.get(nameKey).orElseThrow(illegalState(nameKey));
        }
    } catch (RepositoryExistsException e) {
        throw new ResourceConflictException("Cannot create " + nameKey.get() + " because the name is already occupied by another project.", e);
    } catch (RepositoryNotFoundException badName) {
        throw new BadRequestException("invalid project name: " + nameKey, badName);
    }
}
Also used : Status(com.google.gerrit.server.git.GitRepositoryManager.Status) Project(com.google.gerrit.entities.Project) Repository(org.eclipse.jgit.lib.Repository) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) RepositoryExistsException(com.google.gerrit.server.git.RepositoryExistsException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) RefUpdate(org.eclipse.jgit.lib.RefUpdate)

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