Search in sources :

Example 11 with RepoException

use of com.google.copybara.exception.RepoException in project copybara by google.

the class GitDestinationIntegrateTest method testGitHubSemiFakeMerge.

@Test
public void testGitHubSemiFakeMerge() throws ValidationException, IOException, RepoException {
    Path workTree = Files.createTempDirectory("test");
    GitRepository repo = fakeHttpsRepo("github.com/example/test_repo").withWorkTree(workTree);
    GitRevision firstChange = singleChange(workTree, repo, "ignore_me", "Feature1 change");
    GitRevision secondChange = singleChange(workTree, repo, "ignore_me2", "Feature2 change");
    repo.simpleCommand("update-ref", "refs/pull/20/head", secondChange.getSha1());
    GitDestination destination = destinationWithDefaultIntegrates();
    GitLogEntry previous = createBaseDestinationChange(destination);
    String label = new GithubPRIntegrateLabel(repo, options.general, "example/test_repo", 20, "some_user:branch", secondChange.getSha1()).toString();
    assertThat(label).isEqualTo("https://github.com/example/test_repo/pull/20" + " from some_user:branch " + secondChange.getSha1());
    migrateOriginChange(destination, "Test change\n" + "\n" + GitModule.DEFAULT_INTEGRATE_LABEL + "=" + label + "\n", "some content");
    // Make sure commit adds new text
    String showResult = git("--git-dir", repoGitDir.toString(), "show", "master");
    assertThat(showResult).contains("some content");
    GitTesting.assertThatCheckout(repo(), "master").containsFile("test.txt", "some content").containsFile("ignore_me", "").containsFile("ignore_me2", "").containsNoMoreFiles();
    GitLogEntry merge = getLastMigratedChange("master");
    assertThat(merge.getBody()).isEqualTo("Merge pull request #20 from some_user:branch\n" + "\n" + "DummyOrigin-RevId: test\n");
    assertThat(Lists.transform(merge.getParents(), GitRevision::getSha1)).isEqualTo(Lists.newArrayList(previous.getCommit().getSha1(), secondChange.getSha1()));
    assertThat(console.getMessages().stream().filter(e -> e.getType() == MessageType.WARNING).collect(Collectors.toList())).isEmpty();
    label = new GithubPRIntegrateLabel(repo, options.general, "example/test_repo", 20, "some_user:branch", firstChange.getSha1()).toString();
    assertThat(label).isEqualTo("https://github.com/example/test_repo/pull/20" + " from some_user:branch " + firstChange.getSha1());
    repo().withWorkTree(workTree).simpleCommand("reset", "--hard", "HEAD~1");
    migrateOriginChange(destination, "Test change\n" + "\n" + GitModule.DEFAULT_INTEGRATE_LABEL + "=" + label + "\n", "some content");
    assertThat(console.getMessages().stream().filter(e -> e.getType() == MessageType.WARNING).findAny().get().getText()).contains("has more changes after " + firstChange.getSha1());
}
Also used : Path(java.nio.file.Path) Writer(com.google.copybara.Destination.Writer) GitTestUtil(com.google.copybara.testing.git.GitTestUtil) Iterables(com.google.common.collect.Iterables) DummyRevision(com.google.copybara.testing.DummyRevision) TransformResult(com.google.copybara.TransformResult) DummyOrigin(com.google.copybara.testing.DummyOrigin) RunWith(org.junit.runner.RunWith) RepoException(com.google.copybara.exception.RepoException) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) MessageType(com.google.copybara.util.console.Message.MessageType) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Strategy(com.google.copybara.git.GitIntegrateChanges.Strategy) GitTestUtil.getGitEnv(com.google.copybara.testing.git.GitTestUtil.getGitEnv) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) Assert.fail(org.junit.Assert.fail) GitTesting(com.google.copybara.git.testing.GitTesting) TransformResults(com.google.copybara.testing.TransformResults) Path(java.nio.file.Path) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Files(java.nio.file.Files) TestGitOptions(com.google.copybara.testing.git.GitTestUtil.TestGitOptions) HttpTransport(com.google.api.client.http.HttpTransport) ValidationException(com.google.copybara.exception.ValidationException) IOException(java.io.IOException) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) Collectors(java.util.stream.Collectors) Glob(com.google.copybara.util.Glob) Rule(org.junit.Rule) Optional(java.util.Optional) INCLUDE_FILES(com.google.copybara.git.GitIntegrateChanges.Strategy.INCLUDE_FILES) FAKE_MERGE(com.google.copybara.git.GitIntegrateChanges.Strategy.FAKE_MERGE) Joiner(com.google.common.base.Joiner) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry) Test(org.junit.Test)

Example 12 with RepoException

use of com.google.copybara.exception.RepoException in project copybara by google.

the class GitDestinationIntegrateTest method testGerritFakeMergeNoChangeId.

@Test
public void testGerritFakeMergeNoChangeId() throws ValidationException, IOException, RepoException {
    Path workTree = Files.createTempDirectory("test");
    GitRepository repo = fakeHttpsRepo("example.com/gerrit").withWorkTree(workTree);
    String label = new GerritIntegrateLabel(repo, options.general, "https://example.com/gerrit", 1020, 1, /*changeId=*/
    null).toString();
    assertThat(label).isEqualTo("gerrit https://example.com/gerrit 1020 Patch Set 1");
    GitRevision firstChange = singleChange(workTree, repo, "ignore_me", "Feature1 change");
    repo.simpleCommand("update-ref", "refs/changes/20/1020/1", firstChange.getSha1());
    GitTestUtil.createFakeGerritNodeDbMeta(repo, 1020, CHANGE_ID);
    GitDestination destination = destination(FAKE_MERGE);
    GitLogEntry previous = createBaseDestinationChange(destination);
    migrateOriginChange(destination, "Test change\n" + "\n" + GitModule.DEFAULT_INTEGRATE_LABEL + "=" + label + "\n", "some content");
    // Make sure commit adds new text
    String showResult = git("--git-dir", repoGitDir.toString(), "show", "master");
    assertThat(showResult).contains("some content");
    GitTesting.assertThatCheckout(repo(), "master").containsFile("test.txt", "some content").containsNoMoreFiles();
    GitLogEntry merge = getLastMigratedChange("master");
    assertThat(merge.getBody()).isEqualTo("Merge Gerrit change 1020 Patch Set 1\n" + "\n" + "DummyOrigin-RevId: test\n");
    assertThat(Lists.transform(merge.getParents(), GitRevision::getSha1)).isEqualTo(Lists.newArrayList(previous.getCommit().getSha1(), firstChange.getSha1()));
    assertThat(console.getMessages().stream().filter(e -> e.getType() == MessageType.WARNING).findAny()).isEqualTo(Optional.empty());
}
Also used : Path(java.nio.file.Path) Writer(com.google.copybara.Destination.Writer) GitTestUtil(com.google.copybara.testing.git.GitTestUtil) Iterables(com.google.common.collect.Iterables) DummyRevision(com.google.copybara.testing.DummyRevision) TransformResult(com.google.copybara.TransformResult) DummyOrigin(com.google.copybara.testing.DummyOrigin) RunWith(org.junit.runner.RunWith) RepoException(com.google.copybara.exception.RepoException) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) MessageType(com.google.copybara.util.console.Message.MessageType) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Strategy(com.google.copybara.git.GitIntegrateChanges.Strategy) GitTestUtil.getGitEnv(com.google.copybara.testing.git.GitTestUtil.getGitEnv) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) Assert.fail(org.junit.Assert.fail) GitTesting(com.google.copybara.git.testing.GitTesting) TransformResults(com.google.copybara.testing.TransformResults) Path(java.nio.file.Path) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Files(java.nio.file.Files) TestGitOptions(com.google.copybara.testing.git.GitTestUtil.TestGitOptions) HttpTransport(com.google.api.client.http.HttpTransport) ValidationException(com.google.copybara.exception.ValidationException) IOException(java.io.IOException) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) Collectors(java.util.stream.Collectors) Glob(com.google.copybara.util.Glob) Rule(org.junit.Rule) Optional(java.util.Optional) INCLUDE_FILES(com.google.copybara.git.GitIntegrateChanges.Strategy.INCLUDE_FILES) FAKE_MERGE(com.google.copybara.git.GitIntegrateChanges.Strategy.FAKE_MERGE) Joiner(com.google.common.base.Joiner) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry) Test(org.junit.Test)

Example 13 with RepoException

use of com.google.copybara.exception.RepoException in project copybara by google.

the class ReferenceMigrator method findChange.

@Nullable
private String findChange(final String refBeingMigrated, final String originLabel, final ChangeVisitable<?> destinationReader) throws ValidationException {
    final AtomicInteger changesVisited = new AtomicInteger(0);
    ImmutableList<String> originLabels = ImmutableList.<String>builder().add(originLabel).addAll(additionalLabels).build();
    if (destinationReader == null) {
        throw new ValidationException("Destination does not support reading change history.");
    }
    if (knownChanges.containsKey(refBeingMigrated)) {
        return knownChanges.get(refBeingMigrated);
    } else {
        try {
            destinationReader.visitChangesWithAnyLabel(null, originLabels, (input, labels) -> {
                for (String labelValue : labels.values()) {
                    knownChanges.putIfAbsent(labelValue, input.refAsString());
                    if (labelValue.equals(refBeingMigrated)) {
                        return VisitResult.TERMINATE;
                    }
                }
                if (changesVisited.incrementAndGet() > MAX_CHANGES_TO_VISIT) {
                    return VisitResult.TERMINATE;
                } else {
                    return VisitResult.CONTINUE;
                }
            });
            String retVal = knownChanges.get(refBeingMigrated);
            if (reversePattern != null && retVal != null && !reversePattern.matches(retVal)) {
                throw new ValidationException("Reference %s does not match regex '%s'", retVal, reversePattern);
            }
            return retVal;
        } catch (RepoException exception) {
            throw new ValidationException(exception, "Exception finding reference.");
        }
    }
}
Also used : ValidationException(com.google.copybara.exception.ValidationException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RepoException(com.google.copybara.exception.RepoException) Nullable(javax.annotation.Nullable)

Example 14 with RepoException

use of com.google.copybara.exception.RepoException in project copybara by google.

the class GitHubApiTransportImpl method post.

@SuppressWarnings("unchecked")
@Override
public <T> T post(String path, Object request, Type responseType) throws RepoException, ValidationException {
    HttpRequestFactory requestFactory = getHttpRequestFactory(getCredentials());
    GenericUrl url = new GenericUrl(URI.create(API_URL + "/" + path));
    try {
        HttpRequest httpRequest = requestFactory.buildPostRequest(url, new JsonHttpContent(JSON_FACTORY, request));
        HttpResponse response = httpRequest.execute();
        return (T) response.parseAs(responseType);
    } catch (HttpResponseException e) {
        try {
            throw new GitHubApiException(e.getStatusCode(), parseErrorOrIgnore(e), "POST", path, JSON_FACTORY.toPrettyString(request), e.getContent());
        } catch (IOException e1) {
            logger.log(Level.SEVERE, "Error serializing request for error", e1);
            throw new GitHubApiException(e.getStatusCode(), parseErrorOrIgnore(e), "POST", path, "unknown request", e.getContent());
        }
    } catch (IOException e) {
        throw new RepoException("Error running GitHub API operation " + path, e);
    }
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) HttpResponse(com.google.api.client.http.HttpResponse) HttpResponseException(com.google.api.client.http.HttpResponseException) GenericUrl(com.google.api.client.http.GenericUrl) JsonHttpContent(com.google.api.client.http.json.JsonHttpContent) IOException(java.io.IOException) RepoException(com.google.copybara.exception.RepoException)

Example 15 with RepoException

use of com.google.copybara.exception.RepoException in project copybara by google.

the class GitHubApiTransportImpl method get.

@SuppressWarnings("unchecked")
@Override
public <T> T get(String path, Type responseType) throws RepoException, ValidationException {
    HttpRequestFactory requestFactory = getHttpRequestFactory(getCredentialsIfPresent());
    GenericUrl url = new GenericUrl(URI.create(API_URL + "/" + path));
    try {
        HttpRequest httpRequest = requestFactory.buildGetRequest(url);
        HttpResponse response = httpRequest.execute();
        return (T) response.parseAs(responseType);
    } catch (HttpResponseException e) {
        throw new GitHubApiException(e.getStatusCode(), parseErrorOrIgnore(e), "GET", path, null, e.getContent());
    } catch (IOException e) {
        throw new RepoException("Error running GitHub API operation " + path, e);
    }
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) HttpResponse(com.google.api.client.http.HttpResponse) HttpResponseException(com.google.api.client.http.HttpResponseException) GenericUrl(com.google.api.client.http.GenericUrl) IOException(java.io.IOException) RepoException(com.google.copybara.exception.RepoException)

Aggregations

RepoException (com.google.copybara.exception.RepoException)28 IOException (java.io.IOException)14 ValidationException (com.google.copybara.exception.ValidationException)11 ImmutableList (com.google.common.collect.ImmutableList)8 TestingConsole (com.google.copybara.util.console.testing.TestingConsole)8 Path (java.nio.file.Path)8 OptionsBuilder (com.google.copybara.testing.OptionsBuilder)7 Before (org.junit.Before)7 SkylarkTestExecutor (com.google.copybara.testing.SkylarkTestExecutor)6 TestGitOptions (com.google.copybara.testing.git.GitTestUtil.TestGitOptions)6 HttpTransport (com.google.api.client.http.HttpTransport)5 CannotResolveRevisionException (com.google.copybara.exception.CannotResolveRevisionException)5 EmptyChangeException (com.google.copybara.exception.EmptyChangeException)5 Glob (com.google.copybara.util.Glob)5 GenericUrl (com.google.api.client.http.GenericUrl)4 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)4 Iterables (com.google.common.collect.Iterables)4 GitLogEntry (com.google.copybara.git.GitRepository.GitLogEntry)4 DummyRevision (com.google.copybara.testing.DummyRevision)4 GitApiMockHttpTransport (com.google.copybara.testing.OptionsBuilder.GitApiMockHttpTransport)4