Search in sources :

Example 56 with ProjectState

use of com.google.gerrit.server.project.ProjectState in project gerrit by GerritCodeReview.

the class PluginConfig method withInheritance.

PluginConfig withInheritance(ProjectState.Factory projectStateFactory) {
    checkState(projectConfig().isPresent(), "no project config provided");
    ProjectState state = projectStateFactory.create(projectConfig().get());
    ProjectState parent = Iterables.getFirst(state.parents(), null);
    if (parent == null) {
        return this;
    }
    Map<AccountGroup.UUID, GroupReference> groupReferences = new HashMap<>();
    groupReferences.putAll(groupReferences());
    PluginConfig parentPluginConfig = parent.getPluginConfig(pluginName()).withInheritance(projectStateFactory);
    Set<String> allNames = cfg().getNames(PLUGIN, pluginName());
    Config newCfg = copyConfig(cfg());
    for (String name : parentPluginConfig.cfg().getNames(PLUGIN, pluginName())) {
        if (!allNames.contains(name)) {
            List<String> values = Arrays.asList(parentPluginConfig.cfg().getStringList(PLUGIN, pluginName(), name));
            for (String value : values) {
                Optional<GroupReference> groupRef = parentPluginConfig.projectConfig().get().getGroupByName(GroupReference.extractGroupName(value));
                if (groupRef.isPresent()) {
                    groupReferences.putIfAbsent(groupRef.get().getUUID(), groupRef.get());
                }
            }
            newCfg.setStringList(PLUGIN, pluginName(), name, values);
        }
    }
    return new AutoValue_PluginConfig(pluginName(), newCfg, projectConfig(), ImmutableMap.copyOf(groupReferences));
}
Also used : HashMap(java.util.HashMap) ProjectConfig(com.google.gerrit.server.project.ProjectConfig) Config(org.eclipse.jgit.lib.Config) CachedProjectConfig(com.google.gerrit.entities.CachedProjectConfig) ProjectState(com.google.gerrit.server.project.ProjectState) GroupReference(com.google.gerrit.entities.GroupReference)

Example 57 with ProjectState

use of com.google.gerrit.server.project.ProjectState in project gerrit by GerritCodeReview.

the class CreateChange method execute.

/**
 * Creates the changes in the given project. This is public for reuse in the project API.
 */
public Response<ChangeInfo> execute(BatchUpdate.Factory updateFactory, ChangeInput input, ProjectResource projectResource) throws IOException, RestApiException, UpdateException, PermissionBackendException, ConfigInvalidException {
    if (!user.get().isIdentifiedUser()) {
        throw new AuthException("Authentication required");
    }
    ProjectState projectState = projectResource.getProjectState();
    projectState.checkStatePermitsWrite();
    IdentifiedUser me = user.get().asIdentifiedUser();
    checkAndSanitizeChangeInput(input, me);
    Project.NameKey project = projectResource.getNameKey();
    contributorAgreements.check(project, user.get());
    checkRequiredPermissions(project, input.branch, input.author);
    ChangeInfo newChange = createNewChange(input, me, projectState, updateFactory);
    return Response.created(newChange);
}
Also used : Project(com.google.gerrit.entities.Project) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) AuthException(com.google.gerrit.extensions.restapi.AuthException) ProjectState(com.google.gerrit.server.project.ProjectState) IdentifiedUser(com.google.gerrit.server.IdentifiedUser)

Example 58 with ProjectState

use of com.google.gerrit.server.project.ProjectState in project gerrit by GerritCodeReview.

the class ApplyFix method apply.

@Override
public Response<EditInfo> apply(FixResource fixResource, Input nothing) throws AuthException, BadRequestException, ResourceConflictException, IOException, ResourceNotFoundException, PermissionBackendException {
    RevisionResource revisionResource = fixResource.getRevisionResource();
    Project.NameKey project = revisionResource.getProject();
    ProjectState projectState = projectCache.get(project).orElseThrow(illegalState(project));
    PatchSet patchSet = revisionResource.getPatchSet();
    try (Repository repository = gitRepositoryManager.openRepository(project)) {
        CommitModification commitModification = fixReplacementInterpreter.toCommitModification(repository, projectState, patchSet.commitId(), fixResource.getFixReplacements());
        ChangeEdit changeEdit = changeEditModifier.combineWithModifiedPatchSetTree(repository, revisionResource.getNotes(), patchSet, commitModification);
        return Response.ok(changeEditJson.toEditInfo(changeEdit, false));
    } catch (InvalidChangeOperationException e) {
        throw new ResourceConflictException(e.getMessage());
    }
}
Also used : Project(com.google.gerrit.entities.Project) InvalidChangeOperationException(com.google.gerrit.server.project.InvalidChangeOperationException) Repository(org.eclipse.jgit.lib.Repository) ChangeEdit(com.google.gerrit.server.edit.ChangeEdit) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) CommitModification(com.google.gerrit.server.edit.CommitModification) RevisionResource(com.google.gerrit.server.change.RevisionResource) ProjectState(com.google.gerrit.server.project.ProjectState) PatchSet(com.google.gerrit.entities.PatchSet)

Example 59 with ProjectState

use of com.google.gerrit.server.project.ProjectState in project gerrit by GerritCodeReview.

the class CreateMergePatchSet method apply.

// TODO(issue-15517): Fix the JdkObsolete issue with Date once JGit's PersonIdent class supports
// Instants
@SuppressWarnings("JdkObsolete")
@Override
public Response<ChangeInfo> apply(ChangeResource rsrc, MergePatchSetInput in) throws IOException, RestApiException, UpdateException, PermissionBackendException {
    // Not allowed to create a new patch set if the current patch set is locked.
    psUtil.checkPatchSetNotLocked(rsrc.getNotes());
    rsrc.permissions().check(ChangePermission.ADD_PATCH_SET);
    if (in.author != null) {
        permissionBackend.currentUser().project(rsrc.getProject()).ref(rsrc.getChange().getDest().branch()).check(RefPermission.FORGE_AUTHOR);
    }
    ProjectState projectState = projectCache.get(rsrc.getProject()).orElseThrow(illegalState(rsrc.getProject()));
    projectState.checkStatePermitsWrite();
    MergeInput merge = in.merge;
    if (merge == null || Strings.isNullOrEmpty(merge.source)) {
        throw new BadRequestException("merge.source must be non-empty");
    }
    if (in.author != null && (Strings.isNullOrEmpty(in.author.email) || Strings.isNullOrEmpty(in.author.name))) {
        throw new BadRequestException("Author must specify name and email");
    }
    in.baseChange = Strings.nullToEmpty(in.baseChange).trim();
    PatchSet ps = psUtil.current(rsrc.getNotes());
    Change change = rsrc.getChange();
    Project.NameKey project = change.getProject();
    BranchNameKey dest = change.getDest();
    try (Repository git = gitManager.openRepository(project);
        ObjectInserter oi = git.newObjectInserter();
        ObjectReader reader = oi.newReader();
        CodeReviewRevWalk rw = CodeReviewCommit.newRevWalk(reader)) {
        RevCommit sourceCommit = MergeUtil.resolveCommit(git, rw, merge.source);
        if (!commits.canRead(projectState, git, sourceCommit)) {
            throw new ResourceNotFoundException("cannot find source commit: " + merge.source + " to merge.");
        }
        RevCommit currentPsCommit;
        List<String> groups = null;
        if (!in.inheritParent && !in.baseChange.isEmpty()) {
            PatchSet basePS = findBasePatchSet(in.baseChange);
            currentPsCommit = rw.parseCommit(basePS.commitId());
            groups = basePS.groups();
        } else {
            currentPsCommit = rw.parseCommit(ps.commitId());
        }
        Instant now = TimeUtil.now();
        IdentifiedUser me = user.get().asIdentifiedUser();
        PersonIdent author = in.author == null ? me.newCommitterIdent(now, serverTimeZone) : new PersonIdent(in.author.name, in.author.email, Date.from(now), serverTimeZone);
        CodeReviewCommit newCommit = createMergeCommit(in, projectState, dest, git, oi, rw, currentPsCommit, sourceCommit, author, ObjectId.fromString(change.getKey().get().substring(1)));
        oi.flush();
        PatchSet.Id nextPsId = ChangeUtil.nextPatchSetId(ps.id());
        PatchSetInserter psInserter = patchSetInserterFactory.create(rsrc.getNotes(), nextPsId, newCommit);
        try (BatchUpdate bu = updateFactory.create(project, me, now)) {
            bu.setRepository(git, rw, oi);
            bu.setNotify(NotifyResolver.Result.none());
            psInserter.setMessage(messageForChange(nextPsId, newCommit)).setWorkInProgress(!newCommit.getFilesWithGitConflicts().isEmpty()).setCheckAddPatchSetPermission(false);
            if (groups != null) {
                psInserter.setGroups(groups);
            }
            bu.addOp(rsrc.getId(), psInserter);
            bu.execute();
        }
        ChangeJson json = jsonFactory.create(ListChangesOption.CURRENT_REVISION);
        ChangeInfo changeInfo = json.format(psInserter.getChange());
        changeInfo.containsGitConflicts = !newCommit.getFilesWithGitConflicts().isEmpty() ? true : null;
        return Response.ok(changeInfo);
    } catch (InvalidMergeStrategyException | MergeWithConflictsNotSupportedException e) {
        throw new BadRequestException(e.getMessage());
    }
}
Also used : MergeInput(com.google.gerrit.extensions.common.MergeInput) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) BranchNameKey(com.google.gerrit.entities.BranchNameKey) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ObjectReader(org.eclipse.jgit.lib.ObjectReader) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) ChangeJson(com.google.gerrit.server.change.ChangeJson) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) Instant(java.time.Instant) CodeReviewRevWalk(com.google.gerrit.server.git.CodeReviewCommit.CodeReviewRevWalk) PatchSet(com.google.gerrit.entities.PatchSet) Change(com.google.gerrit.entities.Change) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) CodeReviewCommit(com.google.gerrit.server.git.CodeReviewCommit) Project(com.google.gerrit.entities.Project) Repository(org.eclipse.jgit.lib.Repository) PersonIdent(org.eclipse.jgit.lib.PersonIdent) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) PatchSetInserter(com.google.gerrit.server.change.PatchSetInserter) InvalidMergeStrategyException(com.google.gerrit.exceptions.InvalidMergeStrategyException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) MergeWithConflictsNotSupportedException(com.google.gerrit.exceptions.MergeWithConflictsNotSupportedException) ProjectState(com.google.gerrit.server.project.ProjectState)

Example 60 with ProjectState

use of com.google.gerrit.server.project.ProjectState in project gerrit by GerritCodeReview.

the class ConfigInfoCreator method constructInfo.

@SuppressWarnings("deprecation")
public static ConfigInfo constructInfo(boolean serverEnableSignedPush, ProjectState projectState, CurrentUser user, DynamicMap<ProjectConfigEntry> pluginConfigEntries, PluginConfigFactory cfgFactory, AllProjectsName allProjects, UiActions uiActions, DynamicMap<RestView<ProjectResource>> views) {
    ConfigInfo configInfo = new ConfigInfo();
    Project p = projectState.getProject();
    configInfo.description = Strings.emptyToNull(p.getDescription());
    ProjectState parentState = Iterables.getFirst(projectState.parents(), null);
    for (BooleanProjectConfig cfg : BooleanProjectConfig.values()) {
        InheritedBooleanInfo info = new InheritedBooleanInfo();
        info.configuredValue = p.getBooleanConfig(cfg);
        if (parentState != null) {
            info.inheritedValue = parentState.is(cfg);
        }
        BooleanProjectConfigTransformations.set(cfg, configInfo, info);
    }
    if (!serverEnableSignedPush) {
        configInfo.enableSignedPush = null;
        configInfo.requireSignedPush = null;
    }
    configInfo.maxObjectSizeLimit = getMaxObjectSizeLimit(projectState, p);
    configInfo.defaultSubmitType = new SubmitTypeInfo();
    configInfo.defaultSubmitType.value = projectState.getSubmitType();
    configInfo.defaultSubmitType.configuredValue = MoreObjects.firstNonNull(projectState.getConfig().getProject().getSubmitType(), Project.DEFAULT_SUBMIT_TYPE);
    ProjectState parent = projectState.isAllProjects() ? projectState : projectState.parents().get(0);
    configInfo.defaultSubmitType.inheritedValue = parent.getSubmitType();
    configInfo.submitType = configInfo.defaultSubmitType.value;
    configInfo.state = p.getState() != com.google.gerrit.extensions.client.ProjectState.ACTIVE ? p.getState() : null;
    configInfo.commentlinks = new LinkedHashMap<>();
    for (CommentLinkInfo cl : projectState.getCommentLinks()) {
        configInfo.commentlinks.put(cl.name, cl);
    }
    configInfo.pluginConfig = getPluginConfig(projectState, pluginConfigEntries, cfgFactory, allProjects);
    configInfo.actions = new TreeMap<>();
    for (UiAction.Description d : uiActions.from(views, new ProjectResource(projectState, user))) {
        configInfo.actions.put(d.getId(), new ActionInfo(d));
    }
    configInfo.extensionPanelNames = projectState.getConfig().getExtensionPanelSections();
    return configInfo;
}
Also used : ActionInfo(com.google.gerrit.extensions.common.ActionInfo) ConfigInfo(com.google.gerrit.extensions.api.projects.ConfigInfo) UiAction(com.google.gerrit.extensions.webui.UiAction) InheritedBooleanInfo(com.google.gerrit.extensions.api.projects.ConfigInfo.InheritedBooleanInfo) Project(com.google.gerrit.entities.Project) SubmitTypeInfo(com.google.gerrit.extensions.api.projects.ConfigInfo.SubmitTypeInfo) CommentLinkInfo(com.google.gerrit.extensions.api.projects.CommentLinkInfo) ProjectState(com.google.gerrit.server.project.ProjectState) ProjectResource(com.google.gerrit.server.project.ProjectResource) BooleanProjectConfig(com.google.gerrit.entities.BooleanProjectConfig)

Aggregations

ProjectState (com.google.gerrit.server.project.ProjectState)86 Project (com.google.gerrit.entities.Project)20 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)19 IOException (java.io.IOException)18 Repository (org.eclipse.jgit.lib.Repository)18 AuthException (com.google.gerrit.extensions.restapi.AuthException)16 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)14 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)14 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)13 Change (com.google.gerrit.entities.Change)13 ObjectId (org.eclipse.jgit.lib.ObjectId)13 Test (org.junit.Test)13 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)12 ChangeData (com.google.gerrit.server.query.change.ChangeData)11 Inject (com.google.inject.Inject)10 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)10 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)10 StorageException (com.google.gerrit.exceptions.StorageException)9 ProjectInfo (com.google.gerrit.extensions.common.ProjectInfo)9 ProjectResource (com.google.gerrit.server.project.ProjectResource)9