Search in sources :

Example 26 with WriterContext

use of com.google.copybara.WriterContext in project copybara by google.

the class GitHubPrDestinationTest method testWriteNomain.

@Test
public void testWriteNomain() throws ValidationException, IOException, RepoException {
    GitHubPrDestination d = skylark.eval("r", "r = git.github_pr_destination(" + "    url = 'https://github.com/foo'," + "    destination_ref = 'other'," + ")");
    DummyRevision dummyRevision = new DummyRevision("dummyReference", "feature");
    WriterContext writerContext = new WriterContext("piper_to_github_pr", "TEST", false, dummyRevision, Glob.ALL_FILES.roots());
    String branchName = Identity.computeIdentity("OriginGroupIdentity", dummyRevision.contextReference(), writerContext.getWorkflowName(), "copy.bara.sky", writerContext.getWorkflowIdentityUser());
    mockNoPullRequestsGet(branchName);
    gitUtil.mockApi("POST", "https://api.github.com/repos/foo/pulls", mockResponseAndValidateRequest("{\n" + "  \"id\": 1,\n" + "  \"number\": 12345,\n" + "  \"state\": \"open\",\n" + "  \"title\": \"test summary\",\n" + "  \"body\": \"test summary\"" + "}", MockRequestAssertion.equals("{\"base\":\"other\",\"body\":\"test summary\\n\",\"head\":\"" + branchName + "\",\"title\":\"test summary\"}")));
    Writer<GitRevision> writer = d.newWriter(writerContext);
    GitRepository remote = gitUtil.mockRemoteRepo("github.com/foo");
    addFiles(remote, "main", "first change", ImmutableMap.<String, String>builder().put("foo.txt", "").buildOrThrow());
    addFiles(remote, "other", "second change", ImmutableMap.<String, String>builder().put("foo.txt", "test").buildOrThrow());
    writeFile(this.workdir, "test.txt", "some content");
    writer.write(TransformResults.of(this.workdir, new DummyRevision("one")), Glob.ALL_FILES, console);
    assertThat(remote.refExists(branchName)).isTrue();
    assertThat(Iterables.transform(remote.log(branchName).run(), GitLogEntry::getBody)).containsExactly("first change\n", "second change\n", "test summary\n" + "\n" + "DummyOrigin-RevId: one\n");
}
Also used : WriterContext(com.google.copybara.WriterContext) DummyRevision(com.google.copybara.testing.DummyRevision) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry) Test(org.junit.Test)

Example 27 with WriterContext

use of com.google.copybara.WriterContext in project copybara by google.

the class GitHubPrDestinationTest method testDestinationStatus.

@Test
public void testDestinationStatus() throws ValidationException, IOException, RepoException {
    options.githubDestination.createPullRequest = false;
    gitUtil.mockApi(anyString(), anyString(), new MockLowLevelHttpRequest() {

        @Override
        public LowLevelHttpResponse execute() throws IOException {
            throw new AssertionError("No API calls allowed for this test");
        }
    });
    GitHubPrDestination d = skylark.eval("r", "r = git.github_pr_destination(" + "    url = 'https://github.com/foo'," + "    destination_ref = 'main'" + ")");
    WriterContext writerContext = new WriterContext("piper_to_github", "TEST", false, new DummyRevision("feature", "feature"), Glob.ALL_FILES.roots());
    Writer<GitRevision> writer = d.newWriter(writerContext);
    GitRepository remote = gitUtil.mockRemoteRepo("github.com/foo");
    addFiles(remote, null, "first change\n\nDummyOrigin-RevId: baseline", ImmutableMap.<String, String>builder().put("foo.txt", "").buildOrThrow());
    DestinationStatus status = writer.getDestinationStatus(Glob.ALL_FILES, "DummyOrigin-RevId");
    assertThat(status.getBaseline()).isEqualTo("baseline");
    assertThat(status.getPendingChanges()).isEmpty();
    writeFile(this.workdir, "test.txt", "some content");
    writer.write(TransformResults.of(this.workdir, new DummyRevision("one")), Glob.ALL_FILES, console);
    // New writer since after changes it keeps state internally for ITERATIVE mode
    status = d.newWriter(writerContext).getDestinationStatus(Glob.ALL_FILES, "DummyOrigin-RevId");
    assertThat(status.getBaseline()).isEqualTo("baseline");
    // Not supported for now as we rewrite the whole branch history.
    assertThat(status.getPendingChanges()).isEmpty();
}
Also used : WriterContext(com.google.copybara.WriterContext) DestinationStatus(com.google.copybara.Destination.DestinationStatus) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) DummyRevision(com.google.copybara.testing.DummyRevision) IOException(java.io.IOException) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Test(org.junit.Test)

Example 28 with WriterContext

use of com.google.copybara.WriterContext in project copybara by google.

the class GitHubPrDestinationTest method testPrExistsButIsClosed.

@Test
public void testPrExistsButIsClosed() throws ValidationException, IOException, RepoException {
    options.githubDestination.destinationPrBranch = "feature";
    gitUtil.mockApi("GET", getPullRequestsUrl("feature"), mockResponse("[{" + "  \"id\": 1,\n" + "  \"number\": 12345,\n" + "  \"state\": \"closed\",\n" + "  \"title\": \"test summary\",\n" + "  \"body\": \"test summary\"," + "  \"head\": {\"ref\": \"feature\"}," + "  \"base\": {\"ref\": \"base\"}" + "}]"));
    gitUtil.mockApi("POST", "https://api.github.com/repos/foo/pulls/12345", mockResponseAndValidateRequest("{\n" + "  \"id\": 1,\n" + "  \"number\": 12345,\n" + "  \"state\": \"open\",\n" + "  \"title\": \"test summary\",\n" + "  \"body\": \"test summary\"" + "}", MockRequestAssertion.equals("{\"state\":\"open\"}")));
    GitHubPrDestination d = skylark.eval("r", "r = git.github_pr_destination(" + "    url = 'https://github.com/foo'," + "    destination_ref = 'main'" + ")");
    WriterContext writerContext = new WriterContext("piper_to_github", "test", false, new DummyRevision("feature", "feature"), Glob.ALL_FILES.roots());
    Writer<GitRevision> writer = d.newWriter(writerContext);
    GitRepository remote = gitUtil.mockRemoteRepo("github.com/foo");
    addFiles(remote, null, "first change", ImmutableMap.<String, String>builder().put("foo.txt", "").buildOrThrow());
    writeFile(this.workdir, "test.txt", "some content");
    writer.write(TransformResults.of(this.workdir, new DummyRevision("one")).withSummary("\n\n\n\n\nInternal change."), Glob.ALL_FILES, console);
    verify(gitUtil.httpTransport(), times(1)).buildRequest("GET", getPullRequestsUrl("feature"));
    verify(gitUtil.httpTransport(), times(1)).buildRequest("POST", "https://api.github.com/repos/foo/pulls/12345");
}
Also used : WriterContext(com.google.copybara.WriterContext) DummyRevision(com.google.copybara.testing.DummyRevision) Test(org.junit.Test)

Example 29 with WriterContext

use of com.google.copybara.WriterContext in project copybara by google.

the class SubmodulesInDestinationTest method writeWithSubmoduleInDestination.

private void writeWithSubmoduleInDestination() throws Exception {
    fetch = primaryBranch;
    push = primaryBranch;
    Path scratchTree = Files.createTempDirectory("SubmodulesInDestinationTest-scratchTree");
    GitRepository scratchRepo = repo().withWorkTree(scratchTree);
    scratchRepo.simpleCommand("submodule", "add", "file://" + submodule.getGitDir(), "submodule");
    scratchRepo.simpleCommand("commit", "-m", "commit submodule");
    Files.write(workdir.resolve("test42"), new byte[] { 42 });
    WriterContext writerContext = new WriterContext("SubmodulesInDestinationTest", "Test", false, new DummyRevision("test"), Glob.ALL_FILES.roots());
    Destination.Writer<GitRevision> writer = destination().newWriter(writerContext);
    ImmutableList<DestinationEffect> result = writer.write(TransformResults.of(workdir, new DummyRevision("ref1")), destinationFiles, console);
    assertThat(result).hasSize(1);
    assertThat(result.get(0).getErrors()).isEmpty();
    assertThat(result.get(0).getType()).isEqualTo(Type.CREATED);
    assertThat(result.get(0).getDestinationRef().getType()).isEqualTo("commit");
    assertThat(result.get(0).getDestinationRef().getId()).matches("[0-9a-f]{40}");
}
Also used : Path(java.nio.file.Path) WriterContext(com.google.copybara.WriterContext) Destination(com.google.copybara.Destination) DestinationEffect(com.google.copybara.DestinationEffect) DummyRevision(com.google.copybara.testing.DummyRevision)

Example 30 with WriterContext

use of com.google.copybara.WriterContext in project copybara by google.

the class SubmodulesInDestinationTest method submoduleInSubdirectoryWithSiblingFiles.

@Test
public void submoduleInSubdirectoryWithSiblingFiles() throws Exception {
    destinationFiles = Glob.createGlob(ImmutableList.of("foo/a", "foo/c"));
    fetch = primaryBranch;
    push = primaryBranch;
    Path scratchTree = Files.createTempDirectory("SubmodulesInDestinationTest-scratchTree");
    GitRepository scratchRepo = repo().withWorkTree(scratchTree);
    Files.createDirectories(scratchTree.resolve("foo"));
    Files.write(scratchTree.resolve("foo/a"), new byte[] { 1 });
    scratchRepo.add().files("foo/a").run();
    scratchRepo.simpleCommand("submodule", "add", "file://" + submodule.getGitDir(), "foo/b");
    scratchRepo.simpleCommand("commit", "-m", "commit submodule and foo/a");
    // Create a commit that removes foo/a and adds foo/c
    Files.createDirectories(workdir.resolve("foo"));
    Files.write(workdir.resolve("foo/c"), new byte[] { 1 });
    WriterContext writerContext = new WriterContext("SubmodulesInDestinationTest", "Test", false, new DummyRevision("test"), Glob.ALL_FILES.roots());
    Destination.Writer<GitRevision> writer = destination().newWriter(writerContext);
    ImmutableList<DestinationEffect> result = writer.write(TransformResults.of(workdir, new DummyRevision("ref1")), destinationFiles, console);
    assertThat(result).hasSize(1);
    assertThat(result.get(0).getErrors()).isEmpty();
    assertThat(result.get(0).getType()).isEqualTo(Type.CREATED);
    assertThat(result.get(0).getDestinationRef().getType()).isEqualTo("commit");
    assertThat(result.get(0).getDestinationRef().getId()).matches("[0-9a-f]{40}");
    GitTesting.assertThatCheckout(repo(), primaryBranch).containsFiles("foo/c", "foo/b").containsNoFiles("foo/a");
}
Also used : Path(java.nio.file.Path) WriterContext(com.google.copybara.WriterContext) Destination(com.google.copybara.Destination) DestinationEffect(com.google.copybara.DestinationEffect) DummyRevision(com.google.copybara.testing.DummyRevision) Test(org.junit.Test)

Aggregations

WriterContext (com.google.copybara.WriterContext)58 DummyRevision (com.google.copybara.testing.DummyRevision)58 Test (org.junit.Test)46 DestinationEffect (com.google.copybara.DestinationEffect)21 Glob (com.google.copybara.util.Glob)17 Path (java.nio.file.Path)15 Author (com.google.copybara.authoring.Author)14 Changes (com.google.copybara.Changes)13 ValidationException (com.google.copybara.exception.ValidationException)12 GitLogEntry (com.google.copybara.git.GitRepository.GitLogEntry)11 CheckerException (com.google.copybara.checks.CheckerException)10 DummyChecker (com.google.copybara.testing.DummyChecker)10 MockRequestAssertion (com.google.copybara.testing.git.GitTestUtil.MockRequestAssertion)10 CommandOutput (com.google.copybara.util.CommandOutput)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 ImmutableList (com.google.common.collect.ImmutableList)8 ImmutableSet (com.google.common.collect.ImmutableSet)8 Truth.assertThat (com.google.common.truth.Truth.assertThat)8 ChangeMessage (com.google.copybara.ChangeMessage)8 Writer (com.google.copybara.Destination.Writer)8