Search in sources :

Example 11 with GuruCliException

use of com.amazonaws.gurureviewercli.exceptions.GuruCliException in project aws-codeguru-cli by aws.

the class GitAdapter method tryGetMetaData.

@Nonnull
protected static GitMetaData tryGetMetaData(final Configuration config, final Path gitDir) {
    if (!gitDir.toFile().isDirectory()) {
        throw new GuruCliException(ErrorCodes.GIT_INVALID_DIR);
    }
    val builder = new FileRepositoryBuilder();
    try (val repository = builder.setGitDir(gitDir.toFile()).findGitDir().build()) {
        val userName = repository.getConfig().getString("user", null, "email");
        val urlString = repository.getConfig().getString("remote", "origin", "url");
        val branchName = repository.getBranch();
        if (branchName == null) {
            throw new GuruCliException(ErrorCodes.GIT_BRANCH_MISSING);
        }
        val metadata = GitMetaData.builder().currentBranch(branchName).userName(userName).repoRoot(gitDir.getParent()).remoteUrl(urlString).build();
        metadata.setVersionedFiles(getChangedFiles(repository));
        config.setVersionedFiles(metadata.getVersionedFiles());
        if (config.getBeforeCommit() == null || config.getAfterCommit() == null) {
            // ask if commits should be inferred or if the entire repo should be scanned.
            Log.warn("CodeGuru will perform a full repository analysis if you do not provide a commit range.");
            Log.warn("For pricing details see: https://aws.amazon.com/codeguru/pricing/");
            val doPackageScan = !config.isInteractiveMode() || config.getTextIO().newBooleanInputReader().withTrueInput("y").withFalseInput("n").read("Do you want to perform a full repository analysis?");
            if (doPackageScan) {
                return metadata;
            } else {
                throw new GuruCliException(ErrorCodes.USER_ABORT, "Use --commit-range to set a commit range");
            }
        }
        validateCommits(config, repository);
        metadata.setBeforeCommit(config.getBeforeCommit());
        metadata.setAfterCommit(config.getAfterCommit());
        return metadata;
    } catch (IOException | GitAPIException e) {
        throw new GuruCliException(ErrorCodes.GIT_INVALID_DIR, "Cannot read " + gitDir, e);
    }
}
Also used : lombok.val(lombok.val) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) GuruCliException(com.amazonaws.gurureviewercli.exceptions.GuruCliException) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) Nonnull(javax.annotation.Nonnull)

Example 12 with GuruCliException

use of com.amazonaws.gurureviewercli.exceptions.GuruCliException in project aws-codeguru-cli by aws.

the class GitAdapterTest method test_getGitMetaData_notARepo.

@Test
public void test_getGitMetaData_notARepo() {
    val repo = RESOURCE_ROOT.resolve("fresh-repo-without-remote");
    GuruCliException ret = Assertions.assertThrows(GuruCliException.class, () -> GitAdapter.tryGetMetaData(configWithoutCommits(repo), repo.resolve("notgit")));
    Assertions.assertEquals(ErrorCodes.GIT_INVALID_DIR, ret.getErrorCode());
}
Also used : lombok.val(lombok.val) GuruCliException(com.amazonaws.gurureviewercli.exceptions.GuruCliException) Test(org.junit.jupiter.api.Test)

Example 13 with GuruCliException

use of com.amazonaws.gurureviewercli.exceptions.GuruCliException in project aws-codeguru-cli by aws.

the class GitAdapterTest method test_getGitMetaData_oneCommit_packageScanAbort.

@Test
public void test_getGitMetaData_oneCommit_packageScanAbort() {
    val repo = RESOURCE_ROOT.resolve("one-commit");
    val mockTerminal = new MockTextTerminal();
    mockTerminal.getInputs().add("n");
    val config = Configuration.builder().textIO(new TextIO(mockTerminal)).interactiveMode(true).build();
    GuruCliException ret = Assertions.assertThrows(GuruCliException.class, () -> GitAdapter.tryGetMetaData(config, repo.resolve("git")));
    Assertions.assertEquals(ErrorCodes.USER_ABORT, ret.getErrorCode());
}
Also used : lombok.val(lombok.val) MockTextTerminal(org.beryx.textio.mock.MockTextTerminal) GuruCliException(com.amazonaws.gurureviewercli.exceptions.GuruCliException) TextIO(org.beryx.textio.TextIO) Test(org.junit.jupiter.api.Test)

Aggregations

GuruCliException (com.amazonaws.gurureviewercli.exceptions.GuruCliException)13 lombok.val (lombok.val)13 IOException (java.io.IOException)3 TextIO (org.beryx.textio.TextIO)3 Test (org.junit.jupiter.api.Test)3 ArrayList (java.util.ArrayList)2 MockTextTerminal (org.beryx.textio.mock.MockTextTerminal)2 SdkClientException (software.amazon.awssdk.core.exception.SdkClientException)2 ScanMetaData (com.amazonaws.gurureviewercli.model.ScanMetaData)1 ParameterException (com.beust.jcommander.ParameterException)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 Nonnull (javax.annotation.Nonnull)1 SystemTextTerminal (org.beryx.textio.system.SystemTextTerminal)1 Git (org.eclipse.jgit.api.Git)1 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)1 ObjectReader (org.eclipse.jgit.lib.ObjectReader)1 RevWalk (org.eclipse.jgit.revwalk.RevWalk)1