All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.gerrit.server.restapi.change.PreviewFix Maven / Gradle / Ivy

// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,//
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.server.restapi.change;

import static com.google.gerrit.server.project.ProjectCache.illegalState;
import static java.util.stream.Collectors.groupingBy;

import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.gerrit.common.data.PatchScript;
import com.google.gerrit.entities.Change;
import com.google.gerrit.entities.Comment.Range;
import com.google.gerrit.entities.FixReplacement;
import com.google.gerrit.entities.PatchSet;
import com.google.gerrit.extensions.client.DiffPreferencesInfo;
import com.google.gerrit.extensions.common.ApplyProvidedFixInput;
import com.google.gerrit.extensions.common.DiffInfo;
import com.google.gerrit.extensions.common.DiffWebLinkInfo;
import com.google.gerrit.extensions.common.WebLinkInfo;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.Response;
import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.extensions.restapi.RestReadView;
import com.google.gerrit.server.change.FixResource;
import com.google.gerrit.server.change.RevisionResource;
import com.google.gerrit.server.diff.DiffInfoCreator;
import com.google.gerrit.server.diff.DiffSide;
import com.google.gerrit.server.diff.DiffWebLinksProvider;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.git.LargeObjectException;
import com.google.gerrit.server.notedb.ChangeNotes;
import com.google.gerrit.server.patch.PatchScriptFactoryForAutoFix;
import com.google.gerrit.server.permissions.PermissionBackendException;
import com.google.gerrit.server.project.InvalidChangeOperationException;
import com.google.gerrit.server.project.NoSuchChangeException;
import com.google.gerrit.server.project.ProjectCache;
import com.google.gerrit.server.project.ProjectState;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.assistedinject.Assisted;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.jgit.lib.Repository;

public class PreviewFix {
  public interface Factory {
    PreviewFix create(RevisionResource revisionResource);
  }

  private final GitRepositoryManager repoManager;
  private final PatchScriptFactoryForAutoFix.Factory patchScriptFactoryFactory;
  private final PatchSet patchSet;
  private final ChangeNotes notes;
  private final ProjectState state;

  @Inject
  PreviewFix(
      GitRepositoryManager repoManager,
      PatchScriptFactoryForAutoFix.Factory patchScriptFactoryFactory,
      ProjectCache projectCache,
      @Assisted RevisionResource revisionResource) {
    this.repoManager = repoManager;
    this.patchScriptFactoryFactory = patchScriptFactoryFactory;
    patchSet = revisionResource.getPatchSet();
    notes = revisionResource.getNotes();
    Change change = notes.getChange();
    state = projectCache.get(change.getProject()).orElseThrow(illegalState(change.getProject()));
  }

  @Singleton
  public static class Stored implements RestReadView {
    private final PreviewFix.Factory previewFixFactory;

    @Inject
    Stored(PreviewFix.Factory previewFixFactory) {
      this.previewFixFactory = previewFixFactory;
    }

    @Override
    public Response> apply(FixResource fixResource)
        throws PermissionBackendException, ResourceNotFoundException, ResourceConflictException,
            AuthException, IOException, InvalidChangeOperationException {

      PreviewFix previewFix = previewFixFactory.create(fixResource.getRevisionResource());

      Map> fixReplacementsPerFilePath =
          fixResource.getFixReplacements().stream()
              .collect(groupingBy(fixReplacement -> fixReplacement.path));

      return Response.ok(previewFix.previewAllFiles(fixReplacementsPerFilePath));
    }
  }

  @Singleton
  public static class Provided implements RestModifyView {
    private final PreviewFix.Factory previewFixFactory;

    @Inject
    Provided(PreviewFix.Factory previewFixFactory) {
      this.previewFixFactory = previewFixFactory;
    }

    @Override
    public Response> apply(
        RevisionResource revisionResource, ApplyProvidedFixInput applyProvidedFixInput)
        throws BadRequestException, PermissionBackendException, ResourceNotFoundException,
            ResourceConflictException, AuthException, IOException, InvalidChangeOperationException {
      if (applyProvidedFixInput == null) {
        throw new BadRequestException("applyProvidedFixInput is required");
      }
      if (applyProvidedFixInput.fixReplacementInfos == null) {
        throw new BadRequestException("applyProvidedFixInput.fixReplacementInfos is required");
      }

      PreviewFix previewFix = previewFixFactory.create(revisionResource);

      Map> fixReplacementsPerFilePath =
          applyProvidedFixInput.fixReplacementInfos.stream()
              .map(fix -> new FixReplacement(fix.path, new Range(fix.range), fix.replacement))
              .collect(groupingBy(fixReplacement -> fixReplacement.path));

      return Response.ok(previewFix.previewAllFiles(fixReplacementsPerFilePath));
    }
  }

  private Map previewAllFiles(
      Map> fixReplacementsPerFilePath)
      throws PermissionBackendException, ResourceNotFoundException, ResourceConflictException,
          AuthException, IOException, InvalidChangeOperationException {
    Map result = new HashMap<>();
    try (Repository git = repoManager.openRepository(notes.getProjectName())) {
      for (Map.Entry> entry : fixReplacementsPerFilePath.entrySet()) {
        String fileName = entry.getKey();
        DiffInfo diffInfo =
            previewSingleFile(git, 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 result;
  }

  private DiffInfo previewSingleFile(
      Repository git, String fileName, ImmutableList fixReplacements)
      throws PermissionBackendException, AuthException, LargeObjectException,
          InvalidChangeOperationException, IOException, ResourceNotFoundException {
    PatchScriptFactoryForAutoFix psf =
        patchScriptFactoryFactory.create(
            git, notes, fileName, patchSet, fixReplacements, DiffPreferencesInfo.defaults());
    PatchScript ps = psf.call();

    DiffSide sideA =
        DiffSide.create(
            ps.getFileInfoA(),
            MoreObjects.firstNonNull(ps.getOldName(), ps.getNewName()),
            DiffSide.Type.SIDE_A);
    DiffSide sideB = DiffSide.create(ps.getFileInfoB(), ps.getNewName(), DiffSide.Type.SIDE_B);

    DiffInfoCreator diffInfoCreator =
        new DiffInfoCreator(state, new DiffWebLinksProviderImpl(), true);
    return diffInfoCreator.create(ps, sideA, sideB);
  }

  private static class DiffWebLinksProviderImpl implements DiffWebLinksProvider {

    @Override
    public ImmutableList getDiffLinks() {
      return ImmutableList.of();
    }

    @Override
    public ImmutableList getEditWebLinks() {
      return ImmutableList.of();
    }

    @Override
    public ImmutableList getFileWebLinks(DiffSide.Type fileInfoType) {
      return ImmutableList.of();
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy