Search in sources :

Example 6 with Changes

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

the class GitHubPrDestinationTest method emptyChange.

@Test
public void emptyChange() 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();
    addFiles(remote, "test_feature", "second change", ImmutableMap.<String, String>builder().put("foo.txt", "test").buildOrThrow());
    String changeHead = remote.resolveReference("HEAD").getSha1();
    gitUtil.mockApi("GET", getPullRequestsUrl("test_feature"), mockResponse("[{" + "  \"id\": 1,\n" + "  \"number\": 12345,\n" + "  \"state\": \"closed\",\n" + "  \"title\": \"test summary\",\n" + "  \"body\": \"test summary\"," + "  \"head\": {\"sha\": \"" + changeHead + "\"}," + "  \"base\": {\"sha\": \"" + baseline + "\"}" + "}]"));
    writeFile(this.workdir, "foo.txt", "test");
    RedundantChangeException e = assertThrows(RedundantChangeException.class, () -> 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(e).hasMessageThat().contains("Skipping push to the existing pr https://github.com/foo/pull/12345 " + "as the change feature is empty.");
}
Also used : Changes(com.google.copybara.Changes) DummyRevision(com.google.copybara.testing.DummyRevision) RedundantChangeException(com.google.copybara.exception.RedundantChangeException) Author(com.google.copybara.authoring.Author) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 7 with Changes

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

the class GitOriginTest method testChanges.

@Test
public void testChanges() throws Exception {
    // Need to "round" it since git doesn't store the milliseconds
    ZonedDateTime beforeTime = ZonedDateTime.now(ZoneId.systemDefault()).minusSeconds(1);
    String author = "John Name <john@name.com>";
    singleFileCommit(author, "change2", "test.txt", "some content2");
    git("tag", "-m", "This is a tag", "0.1");
    singleFileCommit(author, "change3", "test.txt", "some content3");
    singleFileCommit(author, "change4", "test.txt", "some content4");
    ImmutableList<Change<GitRevision>> changes = newReader().changes(origin.resolve(firstCommitRef), origin.resolve("HEAD")).getChanges();
    assertThat(changes).hasSize(3);
    assertThat(changes.stream().map(c -> c.getRevision().getUrl()).allMatch(c -> c.startsWith("file://"))).isTrue();
    assertThat(changes.get(0).getMessage()).isEqualTo("change2\n");
    assertThat(changes.get(0).getRevision().associatedLabel("GIT_DESCRIBE_CHANGE_VERSION")).contains("0.1");
    assertThat(changes.get(1).getMessage()).isEqualTo("change3\n");
    assertThat(changes.get(1).getRevision().associatedLabel("GIT_DESCRIBE_CHANGE_VERSION")).contains("0.1-1-g" + changes.get(1).getRevision().asString().substring(0, 7));
    assertThat(changes.get(2).getMessage()).isEqualTo("change4\n");
    assertThat(changes.get(2).getRevision().associatedLabel("GIT_DESCRIBE_CHANGE_VERSION")).contains("0.1-2-g" + changes.get(2).getRevision().asString().substring(0, 7));
    TransformWork work = TransformWorks.of(Paths.get(""), "some msg", console).withChanges(new Changes(changes.reverse(), ImmutableList.of()));
    assertThat(work.getLabel("GIT_DESCRIBE_CHANGE_VERSION")).isEqualTo("0.1-2-g" + changes.get(2).getRevision().asString().substring(0, 7));
    assertThat(work.getAllLabels("GIT_DESCRIBE_CHANGE_VERSION").getImmutableList()).isEqualTo(ImmutableList.of("0.1-2-g" + changes.get(2).getRevision().asString().substring(0, 7), "0.1-1-g" + changes.get(1).getRevision().asString().substring(0, 7), "0.1"));
    for (Change<GitRevision> change : changes) {
        assertThat(change.getAuthor().getEmail()).isEqualTo("john@name.com");
        assertThat(change.getDateTime()).isAtLeast(beforeTime);
        assertThat(change.getDateTime()).isAtMost(ZonedDateTime.now(ZoneId.systemDefault()).plusSeconds(1));
    }
}
Also used : RecordsProcessCallDestination(com.google.copybara.testing.RecordsProcessCallDestination) GitTestUtil(com.google.copybara.testing.git.GitTestUtil) AuthoringMappingMode(com.google.copybara.authoring.Authoring.AuthoringMappingMode) ZonedDateTime(java.time.ZonedDateTime) ProcessedChange(com.google.copybara.testing.RecordsProcessCallDestination.ProcessedChange) TransformWork(com.google.copybara.TransformWork) LabelsAwareModule(com.google.copybara.config.LabelsAwareModule) VisitResult(com.google.copybara.ChangeVisitable.VisitResult) Author(com.google.copybara.authoring.Author) Change(com.google.copybara.Change) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) Workflow(com.google.copybara.Workflow) Path(java.nio.file.Path) Reader(com.google.copybara.Origin.Reader) ImmutableSet(com.google.common.collect.ImmutableSet) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) FileSubjects.assertThatPath(com.google.copybara.testing.FileSubjects.assertThatPath) Revision(com.google.copybara.Revision) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) List(java.util.List) Iterables(com.google.common.collect.Iterables) Assert.assertThrows(org.junit.Assert.assertThrows) 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) Changes(com.google.copybara.Changes) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) ChangesResponse(com.google.copybara.Origin.Reader.ChangesResponse) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) ModuleSet(com.google.copybara.ModuleSet) Before(org.junit.Before) Glob.createGlob(com.google.copybara.util.Glob.createGlob) EmptyReason(com.google.copybara.Origin.Reader.ChangesResponse.EmptyReason) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) ValidationException(com.google.copybara.exception.ValidationException) UserPassword(com.google.copybara.git.GitCredential.UserPassword) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) Authoring(com.google.copybara.authoring.Authoring) Glob(com.google.copybara.util.Glob) Paths(java.nio.file.Paths) TransformWorks(com.google.copybara.testing.TransformWorks) GitTestUtil.writeFile(com.google.copybara.testing.git.GitTestUtil.writeFile) Changes(com.google.copybara.Changes) ZonedDateTime(java.time.ZonedDateTime) TransformWork(com.google.copybara.TransformWork) ProcessedChange(com.google.copybara.testing.RecordsProcessCallDestination.ProcessedChange) Change(com.google.copybara.Change) Test(org.junit.Test)

Example 8 with Changes

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

the class MetadataModuleTest method testSquashNotesWithMerge.

@Test
public void testSquashNotesWithMerge() 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.<MetadataSquashNotes>eval("s", "s = metadata.squash_notes()").transform(work);
    assertThat(work.getMessage()).isEqualTo("Copybara import of the project:\n" + "\n" + "  - 3 merge by Foo Bar <foo@bar.com>\n" + "  - 2 change2 by Foo Bar <foo@bar.com>\n" + "  - 1 change1 by Foo Bar <foo@bar.com>\n");
    work = TransformWorks.of(workdir, "the message", testingConsole).withChanges(changes);
    skylark.<MetadataSquashNotes>eval("s", "s = metadata.squash_notes(use_merge = False)").transform(work);
    assertThat(work.getMessage()).isEqualTo("Copybara import of the project:\n" + "\n" + "  - 2 change2 by Foo Bar <foo@bar.com>\n" + "  - 1 change1 by Foo Bar <foo@bar.com>\n");
}
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 9 with Changes

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

the class MetadataModuleTest method testExposeLabelAll.

@Test
public void testExposeLabelAll() throws Exception {
    TransformWork tw = TransformWorks.of(workdir, "some message\n\n" + "LABEL=aaa", testingConsole).withChanges(new Changes(ImmutableList.of(toChange(new DummyRevision("1").withLabels(ImmutableListMultimap.of("LABEL", "bbb")), ORIGINAL_AUTHOR), toChange(new DummyRevision("2").withLabels(ImmutableListMultimap.of("LABEL", "bbb")), ORIGINAL_AUTHOR), toChange(new DummyRevision("2").withLabels(ImmutableListMultimap.of("LABEL", "ccc")), ORIGINAL_AUTHOR)), ImmutableList.of())).withResolvedReference(new DummyRevision("123").withLabels(ImmutableListMultimap.of("LABEL", "ddd")));
    Transformation t = skylark.eval("t", "t = " + "metadata.expose_label('LABEL', 'NEW_VALUE', all = True)");
    t.transform(tw);
    assertThat(tw.getMessage()).isEqualTo("some message\n" + "\n" + "LABEL=aaa\n" + "NEW_VALUE=aaa\n" + "NEW_VALUE=bbb\n" + "NEW_VALUE=ccc\n" + "NEW_VALUE=ddd\n");
}
Also used : Changes(com.google.copybara.Changes) Transformation(com.google.copybara.Transformation) TransformWork(com.google.copybara.TransformWork) DummyRevision(com.google.copybara.testing.DummyRevision) Test(org.junit.Test)

Example 10 with Changes

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

the class GitDestinationTest method testTag.

@Test
public void testTag() throws Exception {
    fetch = primaryBranch;
    push = primaryBranch;
    Files.write(workdir.resolve("test.txt"), "some content".getBytes(UTF_8));
    options.setForce(true);
    WriterContext writerContext = new WriterContext("piper_to_github", "TEST", false, new DummyRevision("test"), Glob.ALL_FILES.roots());
    evalDestination().newWriter(writerContext).write(TransformResults.of(workdir, new DummyRevision("ref1")), destinationFiles, console);
    options.setForce(false);
    Changes changes = new Changes(ImmutableList.of(new Change<>(new DummyRevision("ref2"), new Author("foo", "foo@foo.com"), "message", ZonedDateTime.now(ZoneOffset.UTC), ImmutableListMultimap.of("my_label", "12345"))), ImmutableList.of());
    Files.write(workdir.resolve("test.txt"), "some content 2".getBytes(UTF_8));
    evalDestinationWithTag(null).newWriter(writerContext).write(TransformResults.of(workdir, new DummyRevision("ref2")).withChanges(changes).withSummary("message_tag"), destinationFiles, console);
    CommandOutput commandOutput = repo().simpleCommand("tag", "-n9");
    assertThat(commandOutput.getStdout()).matches(".*test_v1.*message_tag\n" + ".*\n" + ".*DummyOrigin-RevId: ref2\n");
}
Also used : Changes(com.google.copybara.Changes) WriterContext(com.google.copybara.WriterContext) CommandOutput(com.google.copybara.util.CommandOutput) DummyRevision(com.google.copybara.testing.DummyRevision) Author(com.google.copybara.authoring.Author) Change(com.google.copybara.Change) Test(org.junit.Test)

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