Search in sources :

Example 11 with Changes

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

the class GitHubDestinationTest method checkPrToUpdateWithRegularString.

private void checkPrToUpdateWithRegularString(String deletePrBranch, boolean expectDeletePrBranch) throws Exception {
    if (expectDeletePrBranch) {
        when(gitUtil.httpTransport().buildRequest(eq("DELETE"), contains("repos/foo/git/refs/heads/other"))).thenReturn(mockResponseWithStatus("", 204));
    }
    gitUtil.mockApi("GET", "https://api.github.com/repos/foo/git/refs/heads/other", mockResponse("{\n" + "\"ref\" : \"refs/heads/test_existing_pr\",\n" + "\"node_id\" : \"MDM6UmVmcmVmcy9oZWFkcy9mZWF0dXJlQQ==\",\n" + "\"url\" :" + " \"https://api.github.com/repos/octocat/Hello-World/git/refs/heads/test_existing_pr\",\n" + "\"object\" : {\n" + "         \"type\" : \"commit\",\n" + "         \"sha\" : \"aa218f56b14c9653891f9e74264a383fa43fefbd\",\n" + "         \"url\" :" + " \"https://api.github.com/repos/octocat/Hello-World/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd\"\n" + "       }\n" + "}"));
    addFiles(remote, primaryBranch, "first change", ImmutableMap.<String, String>builder().put("foo.txt", "foo").buildOrThrow());
    remote.simpleCommand("branch", "other");
    GitTesting.assertThatCheckout(remote, primaryBranch).containsFile("foo.txt", "foo").containsNoMoreFiles();
    GitTesting.assertThatCheckout(remote, "other").containsFile("foo.txt", "foo").containsNoMoreFiles();
    WriterContext writerContext = new WriterContext("piper_to_github", "test", false, new DummyRevision("origin_ref1"), Glob.ALL_FILES.roots());
    writeFile(this.workdir, "test.txt", "some content");
    Writer<GitRevision> writer = destinationWithExistingPrBranch("other", deletePrBranch).newWriter(writerContext);
    DummyRevision ref = new DummyRevision("origin_ref1");
    TransformResult result = TransformResults.of(workdir, ref);
    Changes changes = new Changes(ImmutableList.of(new Change<>(ref, new Author("foo", "foo@foo.com"), "message", ZonedDateTime.now(ZoneOffset.UTC), ImmutableListMultimap.of("my_label", "12345")), new Change<>(ref, new Author("foo", "foo@foo.com"), "message", ZonedDateTime.now(ZoneOffset.UTC), ImmutableListMultimap.of("my_label", "6789"))), ImmutableList.of());
    result = result.withChanges(changes);
    ImmutableList<DestinationEffect> destinationResult = writer.write(result, destinationFiles, console);
    assertThat(destinationResult).hasSize(expectDeletePrBranch ? 3 : 1);
    assertThat(destinationResult.get(0).getErrors()).isEmpty();
    assertThat(destinationResult.get(0).getType()).isEqualTo(Type.CREATED);
    assertThat(destinationResult.get(0).getDestinationRef().getType()).isEqualTo("commit");
    assertThat(destinationResult.get(0).getDestinationRef().getId()).matches("[0-9a-f]{40}");
    // This is a migration of two changes (use the same ref because mocks)
    verify(gitUtil.httpTransport(), times(expectDeletePrBranch ? 2 : 0)).buildRequest(eq("DELETE"), contains("refs/heads/other"));
    GitTesting.assertThatCheckout(remote, primaryBranch).containsFile("test.txt", "some content").containsNoMoreFiles();
    GitTesting.assertThatCheckout(remote, "other").containsFile("test.txt", "some content").containsNoMoreFiles();
}
Also used : Changes(com.google.copybara.Changes) WriterContext(com.google.copybara.WriterContext) TransformResult(com.google.copybara.TransformResult) DestinationEffect(com.google.copybara.DestinationEffect) DummyRevision(com.google.copybara.testing.DummyRevision) Author(com.google.copybara.authoring.Author) Change(com.google.copybara.Change)

Example 12 with Changes

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

the class GitHubPrDestinationTest method changeWithAllowEmptyDiff.

@Test
public void changeWithAllowEmptyDiff() throws Exception {
    Writer<GitRevision> writer = getWriterForTestEmptyDiff();
    GitRepository remote = gitUtil.mockRemoteRepo("github.com/foo");
    addFiles(remote, null, "first change", ImmutableMap.<String, String>builder().put("foo.txt", "").buildOrThrow());
    String baseline = remote.resolveReference("HEAD").getSha1();
    String changeHead = remote.resolveReference("HEAD").getSha1();
    gitUtil.mockApi("GET", getPullRequestsUrl("test_feature"), mockResponse("[{" + "  \"id\": 1,\n" + "  \"number\": 12345,\n" + "  \"state\": \"open\",\n" + "  \"title\": \"test summary\",\n" + "  \"body\": \"test summary\"," + "  \"head\": {\"sha\": \"" + changeHead + "\", \"ref\": \"test_feature\"}," + "  \"base\": {\"sha\": \"" + baseline + "\", \"ref\": \"master\"}" + "}]"));
    writeFile(this.workdir, "foo.txt", "test");
    ImmutableList<DestinationEffect> results = writer.write(TransformResults.of(this.workdir, new DummyRevision("one")).withBaseline(baseline).withChanges(new Changes(ImmutableList.of(toChange(new DummyRevision("feature"), new Author("Foo Bar", "foo@bar.com"))), ImmutableList.of())).withLabelFinder(Functions.forMap(ImmutableMap.of("aaa", ImmutableList.of("first a", "second a")))), Glob.ALL_FILES, console);
    assertThat(results.size()).isEqualTo(2);
}
Also used : Changes(com.google.copybara.Changes) DestinationEffect(com.google.copybara.DestinationEffect) DummyRevision(com.google.copybara.testing.DummyRevision) Author(com.google.copybara.authoring.Author) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 13 with Changes

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

the class MetadataModuleTest method testLastMessageWithMerge.

@Test
public void testLastMessageWithMerge() throws Exception {
    Changes changes = new Changes(ImmutableList.of(new Change<>(new DummyRevision("3"), ORIGINAL_AUTHOR, "merge", fakeDate(), ImmutableListMultimap.of(), /*changeFiles=*/
    null, /*merge=*/
    true, ImmutableList.of(new DummyRevision("10"), new DummyRevision("20"))), new Change<>(new DummyRevision("2"), ORIGINAL_AUTHOR, "change2", fakeDate(), ImmutableListMultimap.of()), new Change<>(new DummyRevision("1"), ORIGINAL_AUTHOR, "change1", fakeDate(), ImmutableListMultimap.of())), ImmutableList.of());
    TransformWork work = TransformWorks.of(workdir, "the message", testingConsole).withChanges(changes);
    // The default is to use merges, since git.origin does --first-parent by default
    skylark.<UseLastChange>eval("s", "s = metadata.use_last_change()").transform(work);
    assertThat(work.getMessage()).isEqualTo("merge");
    work = TransformWorks.of(workdir, "the message", testingConsole).withChanges(changes);
    skylark.<UseLastChange>eval("s", "s = metadata.use_last_change(use_merge = False)").transform(work);
    assertThat(work.getMessage()).isEqualTo("change2");
}
Also used : Changes(com.google.copybara.Changes) TransformWork(com.google.copybara.TransformWork) DummyRevision(com.google.copybara.testing.DummyRevision) TransformWorks.toChange(com.google.copybara.testing.TransformWorks.toChange) ProcessedChange(com.google.copybara.testing.RecordsProcessCallDestination.ProcessedChange) Change(com.google.copybara.Change) Test(org.junit.Test)

Example 14 with Changes

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

the class MetadataModuleTest method checkLabelChange.

private void checkLabelChange(String msg, String transform, String expectedOutput) throws Exception {
    TransformWork tw = TransformWorks.of(workdir, msg, testingConsole).withChanges(new Changes(ImmutableList.of(toChange(new DummyRevision("1").withLabels(ImmutableListMultimap.of("FROM_CHANGE", "from_change_value")), ORIGINAL_AUTHOR)), ImmutableList.of())).withResolvedReference(new DummyRevision("123").withLabels(ImmutableListMultimap.of("SOME", "value")));
    Transformation t = skylark.eval("t", "t = " + transform);
    t.transform(tw);
    assertThat(tw.getMessage()).isEqualTo(expectedOutput);
}
Also used : Changes(com.google.copybara.Changes) Transformation(com.google.copybara.Transformation) TransformWork(com.google.copybara.TransformWork) DummyRevision(com.google.copybara.testing.DummyRevision)

Aggregations

Changes (com.google.copybara.Changes)14 DummyRevision (com.google.copybara.testing.DummyRevision)12 Test (org.junit.Test)11 Author (com.google.copybara.authoring.Author)10 Change (com.google.copybara.Change)9 TransformWork (com.google.copybara.TransformWork)6 WriterContext (com.google.copybara.WriterContext)6 DestinationEffect (com.google.copybara.DestinationEffect)5 TransformResult (com.google.copybara.TransformResult)4 ProcessedChange (com.google.copybara.testing.RecordsProcessCallDestination.ProcessedChange)3 Strings (com.google.common.base.Strings)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Iterables (com.google.common.collect.Iterables)2 Truth.assertThat (com.google.common.truth.Truth.assertThat)2 Truth.assertWithMessage (com.google.common.truth.Truth.assertWithMessage)2 CheckerException (com.google.copybara.checks.CheckerException)2 RedundantChangeException (com.google.copybara.exception.RedundantChangeException)2 RepoException (com.google.copybara.exception.RepoException)2 ValidationException (com.google.copybara.exception.ValidationException)2