Search in sources :

Example 76 with ProjectState

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

the class Restore method getDescription.

@Override
public UiAction.Description getDescription(ChangeResource rsrc) throws IOException {
    UiAction.Description description = new UiAction.Description().setLabel("Restore").setTitle("Restore the change").setVisible(false);
    Change change = rsrc.getChange();
    if (!change.isAbandoned()) {
        return description;
    }
    if (!projectCache.get(rsrc.getProject()).map(ProjectState::statePermitsRead).orElse(false)) {
        return description;
    }
    if (psUtil.isPatchSetLocked(rsrc.getNotes())) {
        return description;
    }
    boolean visible = rsrc.permissions().testOrFalse(ChangePermission.RESTORE);
    return description.setVisible(visible);
}
Also used : ProjectState(com.google.gerrit.server.project.ProjectState) Change(com.google.gerrit.entities.Change) UiAction(com.google.gerrit.extensions.webui.UiAction)

Example 77 with ProjectState

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

the class GetFixPreview method apply.

@Override
public Response<Map<String, DiffInfo>> apply(FixResource resource) throws PermissionBackendException, ResourceNotFoundException, ResourceConflictException, AuthException, IOException, InvalidChangeOperationException {
    Map<String, DiffInfo> result = new HashMap<>();
    PatchSet patchSet = resource.getRevisionResource().getPatchSet();
    ChangeNotes notes = resource.getRevisionResource().getNotes();
    Change change = notes.getChange();
    ProjectState state = projectCache.get(change.getProject()).orElseThrow(illegalState(change.getProject()));
    Map<String, List<FixReplacement>> fixReplacementsPerFilePath = resource.getFixReplacements().stream().collect(groupingBy(fixReplacement -> fixReplacement.path));
    try {
        try (Repository git = repoManager.openRepository(notes.getProjectName())) {
            for (Map.Entry<String, List<FixReplacement>> entry : fixReplacementsPerFilePath.entrySet()) {
                String fileName = entry.getKey();
                DiffInfo diffInfo = getFixPreviewForSingleFile(git, patchSet, state, notes, fileName, ImmutableList.copyOf(entry.getValue()));
                result.put(fileName, diffInfo);
            }
        }
    } catch (NoSuchChangeException e) {
        throw new ResourceNotFoundException(e.getMessage(), e);
    } catch (LargeObjectException e) {
        throw new ResourceConflictException(e.getMessage(), e);
    }
    return Response.ok(result);
}
Also used : ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) DiffInfo(com.google.gerrit.extensions.common.DiffInfo) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) ProjectCache(com.google.gerrit.server.project.ProjectCache) Inject(com.google.inject.Inject) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) HashMap(java.util.HashMap) Response(com.google.gerrit.extensions.restapi.Response) PatchScriptFactoryForAutoFix(com.google.gerrit.server.patch.PatchScriptFactoryForAutoFix) PatchScript(com.google.gerrit.common.data.PatchScript) ImmutableList(com.google.common.collect.ImmutableList) InvalidChangeOperationException(com.google.gerrit.server.project.InvalidChangeOperationException) Map(java.util.Map) AuthException(com.google.gerrit.extensions.restapi.AuthException) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) Change(com.google.gerrit.entities.Change) PatchSet(com.google.gerrit.entities.PatchSet) FixReplacement(com.google.gerrit.entities.FixReplacement) ProjectCache.illegalState(com.google.gerrit.server.project.ProjectCache.illegalState) DiffPreferencesInfo(com.google.gerrit.extensions.client.DiffPreferencesInfo) RestReadView(com.google.gerrit.extensions.restapi.RestReadView) LargeObjectException(com.google.gerrit.server.git.LargeObjectException) ProjectState(com.google.gerrit.server.project.ProjectState) MoreObjects(com.google.common.base.MoreObjects) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) IOException(java.io.IOException) DiffSide(com.google.gerrit.server.diff.DiffSide) DiffWebLinksProvider(com.google.gerrit.server.diff.DiffWebLinksProvider) WebLinkInfo(com.google.gerrit.extensions.common.WebLinkInfo) List(java.util.List) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) DiffWebLinkInfo(com.google.gerrit.extensions.common.DiffWebLinkInfo) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) DiffInfoCreator(com.google.gerrit.server.diff.DiffInfoCreator) FixResource(com.google.gerrit.server.change.FixResource) Repository(org.eclipse.jgit.lib.Repository) Singleton(com.google.inject.Singleton) HashMap(java.util.HashMap) PatchSet(com.google.gerrit.entities.PatchSet) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) Change(com.google.gerrit.entities.Change) LargeObjectException(com.google.gerrit.server.git.LargeObjectException) Repository(org.eclipse.jgit.lib.Repository) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) ProjectState(com.google.gerrit.server.project.ProjectState) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) HashMap(java.util.HashMap) Map(java.util.Map) DiffInfo(com.google.gerrit.extensions.common.DiffInfo)

Example 78 with ProjectState

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

the class Mergeable method apply.

@Override
public Response<MergeableInfo> apply(RevisionResource resource) throws AuthException, ResourceConflictException, BadRequestException, IOException {
    Change change = resource.getChange();
    PatchSet ps = resource.getPatchSet();
    MergeableInfo result = new MergeableInfo();
    if (!change.isNew()) {
        throw new ResourceConflictException("change is " + ChangeUtil.status(change));
    } else if (!ps.id().equals(change.currentPatchSetId())) {
        // Only the current revision is mergeable. Others always fail.
        return Response.ok(result);
    }
    ChangeData cd = changeDataFactory.create(resource.getNotes());
    result.submitType = getSubmitType(cd);
    try (Repository git = gitManager.openRepository(change.getProject())) {
        ObjectId commit = ps.commitId();
        Ref ref = git.getRefDatabase().exactRef(change.getDest().branch());
        ProjectState projectState = projectCache.get(change.getProject()).orElseThrow(illegalState(change.getProject()));
        String strategy = mergeUtilFactory.create(projectState).mergeStrategyName();
        result.strategy = strategy;
        result.mergeable = isMergable(git, change, commit, ref, result.submitType, strategy);
        if (otherBranches) {
            result.mergeableInto = new ArrayList<>();
            Optional<BranchOrderSection> branchOrder = projectState.getBranchOrderSection();
            if (branchOrder.isPresent()) {
                int prefixLen = Constants.R_HEADS.length();
                List<String> names = branchOrder.get().getMoreStable(ref.getName());
                Map<String, Ref> refs = git.getRefDatabase().exactRef(names.toArray(new String[names.size()]));
                for (String n : names) {
                    Ref other = refs.get(n);
                    if (other == null) {
                        continue;
                    }
                    if (cache.get(commit, other, SubmitType.CHERRY_PICK, strategy, change.getDest(), git)) {
                        result.mergeableInto.add(other.getName().substring(prefixLen));
                    }
                }
            }
        }
    }
    return Response.ok(result);
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) PatchSet(com.google.gerrit.entities.PatchSet) Change(com.google.gerrit.entities.Change) BranchOrderSection(com.google.gerrit.entities.BranchOrderSection) ChangeData(com.google.gerrit.server.query.change.ChangeData) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) MergeableInfo(com.google.gerrit.extensions.common.MergeableInfo) ProjectState(com.google.gerrit.server.project.ProjectState)

Example 79 with ProjectState

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

the class SubmitStrategyOp method postUpdate.

@Override
public final void postUpdate(PostUpdateContext ctx) throws Exception {
    if (changeAlreadyMerged) {
        // TODO(dborowitz): This is suboptimal behavior in the presence of retries: postUpdate steps
        // will never get run for changes that submitted successfully on any but the final attempt.
        // This is primarily a temporary workaround for the fact that the submitter field is not
        // populated in the changeAlreadyMerged case.
        // 
        // If we naively execute postUpdate even if the change is already merged when updateChange
        // being, then we are subject to a race where postUpdate steps are run twice if two submit
        // processes run at the same time.
        logger.atFine().log("Skipping post-update steps for change %s; submitter is %s", getId(), submitter);
        return;
    }
    logger.atFine().log("Begin post-update steps for change %s; submitter is %s", getId(), submitter);
    postUpdateImpl(ctx);
    if (command != null) {
        args.tagCache.updateFastForward(getProject(), command.getRefName(), command.getOldId(), command.getNewId());
        // per project even if multiple changes to refs/meta/config are submitted.
        if (RefNames.REFS_CONFIG.equals(getDest().branch())) {
            args.projectCache.evictAndReindex(getProject());
            ProjectState p = args.projectCache.get(getProject()).orElseThrow(illegalState(getProject()));
            try (Repository git = args.repoManager.openRepository(getProject())) {
                git.setGitwebDescription(p.getProject().getDescription());
            } catch (IOException e) {
                logger.atSevere().withCause(e).log("cannot update description of %s", p.getName());
            }
        }
    }
    logger.atFine().log("Begin sending emails for submitting change %s; submitter is %s", getId(), submitter);
    // have failed fast in one of the other steps.
    try {
        args.mergedSenderFactory.create(ctx.getProject(), toMerge.change(), args.caller, ctx.getNotify(getId()), ctx.getRepoView(), stickyApprovalDiff).sendAsync();
    } catch (Exception e) {
        logger.atSevere().withCause(e).log("Cannot email merged notification for %s", getId());
    }
    if (mergeResultRev != null && !args.dryrun) {
        args.changeMerged.fire(ctx.getChangeData(updatedChange), mergedPatchSet, args.accountCache.get(submitter.accountId()).orElse(null), args.mergeTip.getCurrentTip().name(), ctx.getWhen());
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ProjectState(com.google.gerrit.server.project.ProjectState) IOException(java.io.IOException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) InvalidChangeOperationException(com.google.gerrit.server.project.InvalidChangeOperationException) AuthException(com.google.gerrit.extensions.restapi.AuthException) StorageException(com.google.gerrit.exceptions.StorageException) IOException(java.io.IOException)

Example 80 with ProjectState

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

the class MergeOpRepoManager method getRepo.

public OpenRepo getRepo(Project.NameKey project) throws NoSuchProjectException, IOException {
    if (openRepos.containsKey(project)) {
        return openRepos.get(project);
    }
    ProjectState projectState = projectCache.get(project).orElseThrow(noSuchProject(project));
    try {
        OpenRepo or = new OpenRepo(repoManager.openRepository(project), projectState);
        openRepos.put(project, or);
        return or;
    } catch (RepositoryNotFoundException e) {
        throw new NoSuchProjectException(project, e);
    }
}
Also used : NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException) ProjectState(com.google.gerrit.server.project.ProjectState) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException)

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