Search in sources :

Example 16 with Change

use of com.google.copybara.revision.Change in project copybara by google.

the class InfoTest method testInfoAvailableToMigrate.

@Test
public void testInfoAvailableToMigrate() throws Exception {
    info = new InfoCmd((configPath, sourceRef) -> new ConfigLoader(skylark.createModuleSet(), skylark.createConfigFile("copy.bara.sky", configInfo), optionsBuilder.general.getStarlarkMode()) {

        @Override
        public Config load(Console console) {
            return config;
        }

        @Override
        public ConfigWithDependencies loadWithDependencies(Console console) {
            return configWithDeps;
        }
    }, getFakeContextProvider());
    MigrationReference<DummyRevision> workflow = MigrationReference.create("workflow", new DummyRevision("1111"), ImmutableList.of(newChange("2222", "First change", ZonedDateTime.ofInstant(Instant.ofEpochSecond(1541631979), ZoneId.of("-08:00"))), newChange("3333", "Second change", ZonedDateTime.ofInstant(Instant.ofEpochSecond(1541639979), ZoneId.of("-08:00")))));
    Info<?> mockedInfo = Info.create(dummyOriginDescription, dummyDestinationDescription, ImmutableList.of(workflow));
    Mockito.<Info<? extends Revision>>when(migration.getInfo()).thenReturn(mockedInfo);
    // Copybara copybara = new Copybara(new ConfigValidator() {}, migration -> {});
    // copybara.info(optionsBuilder.build(), config, "workflow");
    info.run(new CommandEnv(temp, optionsBuilder.build(), ImmutableList.of("copy.bara.sky", "workflow")));
    assertThat(eventMonitor.infoFinishedEvent).isNotNull();
    assertThat(eventMonitor.infoFinishedEvent.getInfo()).isEqualTo(mockedInfo);
    console.assertThat().onceInLog(MessageType.INFO, ".*last_migrated 1111 - last_available 3333.*").onceInLog(MessageType.INFO, ".*Date.*Revision.*Description.*Author.*").onceInLog(MessageType.INFO, ".*2018-11-07 15:06:19.*2222.*First change.*Foo <Bar>.*").onceInLog(MessageType.INFO, ".*2018-11-07 17:19:39.*3333.*Second change.*Foo <Bar>.*");
}
Also used : MigrationReference(com.google.copybara.Info.MigrationReference) DummyRevision(com.google.copybara.testing.DummyRevision) ZonedDateTime(java.time.ZonedDateTime) RunWith(org.junit.runner.RunWith) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) MessageType(com.google.copybara.util.console.Message.MessageType) ImmutableList(com.google.common.collect.ImmutableList) Author(com.google.copybara.authoring.Author) Revision(com.google.copybara.revision.Revision) ExitCode(com.google.copybara.util.ExitCode) Config(com.google.copybara.config.Config) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) Path(java.nio.file.Path) Before(org.junit.Before) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) ImmutableMap(com.google.common.collect.ImmutableMap) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Files(java.nio.file.Files) Migration(com.google.copybara.config.Migration) ValidationException(com.google.copybara.exception.ValidationException) ConfigWithDependencies(com.google.copybara.config.SkylarkParser.ConfigWithDependencies) Console(com.google.copybara.util.console.Console) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) Instant(java.time.Instant) ZoneId(java.time.ZoneId) Mockito(org.mockito.Mockito) Change(com.google.copybara.revision.Change) TestingEventMonitor(com.google.copybara.testing.TestingEventMonitor) StarlarkMode(com.google.copybara.util.console.StarlarkMode) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) SUCCESS(com.google.copybara.util.ExitCode.SUCCESS) Mockito.mock(org.mockito.Mockito.mock) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Console(com.google.copybara.util.console.Console) DummyRevision(com.google.copybara.testing.DummyRevision) Test(org.junit.Test)

Example 17 with Change

use of com.google.copybara.revision.Change 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.branch("other").run();
    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.revision.Changes) WriterContext(com.google.copybara.WriterContext) TransformResult(com.google.copybara.TransformResult) DestinationEffect(com.google.copybara.effect.DestinationEffect) DummyRevision(com.google.copybara.testing.DummyRevision) Author(com.google.copybara.authoring.Author) Change(com.google.copybara.revision.Change)

Example 18 with Change

use of com.google.copybara.revision.Change 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.revision.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.revision.Change) Test(org.junit.Test)

Example 19 with Change

use of com.google.copybara.revision.Change in project copybara by google.

the class HgOriginTest method testChanges.

@Test
public void testChanges() throws Exception {
    ZonedDateTime beforeTime = ZonedDateTime.now(ZoneId.systemDefault()).minusSeconds(1);
    String author = "Copy Bara <copy@bara.com>";
    singleFileCommit(author, "zero", "foo.txt", "zero");
    singleFileCommit(author, "one", "foo.txt", "one");
    Path filePath = singleFileCommit(author, "two", "foo.txt", "two");
    assertThat(Files.readAllBytes(filePath)).isEqualTo("two".getBytes(UTF_8));
    ImmutableList<Change<HgRevision>> changes = newReader().changes(origin.resolve("0"), origin.resolve("tip")).getChanges();
    assertThat(changes).hasSize(2);
    assertThat(changes.get(0).getMessage()).isEqualTo("one");
    assertThat(changes.get(1).getMessage()).isEqualTo("two");
    for (Change<HgRevision> change : changes) {
        assertThat(change.getAuthor().getEmail()).isEqualTo("copy@bara.com");
        assertThat(change.getChangeFiles()).hasSize(1);
        assertThat(change.getChangeFiles()).containsExactly("foo.txt");
        assertThat(change.getDateTime()).isAtLeast(beforeTime);
        assertThat(change.getDateTime()).isAtMost(ZonedDateTime.now(ZoneId.systemDefault()).plusSeconds(1));
    }
}
Also used : Path(java.nio.file.Path) FileSubjects.assertThatPath(com.google.copybara.testing.FileSubjects.assertThatPath) ZonedDateTime(java.time.ZonedDateTime) Change(com.google.copybara.revision.Change) Test(org.junit.Test)

Example 20 with Change

use of com.google.copybara.revision.Change in project copybara by google.

the class DummyOriginTest method canSetAuthorOfIndividualChanges.

@Test
public void canSetAuthorOfIndividualChanges() throws Exception {
    DummyOrigin origin = new DummyOrigin().setAuthor(new Author("Dummy Origin", "dummy_origin@google.com")).addSimpleChange(/*timestamp*/
    42).setAuthor(new Author("Wise Origin", "wise_origin@google.com")).addSimpleChange(/*timestamp*/
    999);
    Authoring authoring = new Authoring(new Author("foo", "default.example.com"), AuthoringMappingMode.PASS_THRU, ImmutableSet.of());
    ImmutableList<Change<DummyRevision>> changes = origin.newReader(Glob.ALL_FILES, authoring).changes(/*fromRef*/
    null, /*toRef*/
    origin.resolve("1")).getChanges();
    assertThat(changes).hasSize(2);
    assertThat(changes.get(0).getAuthor()).isEqualTo(new Author("Dummy Origin", "dummy_origin@google.com"));
    assertThat(changes.get(1).getAuthor()).isEqualTo(new Author("Wise Origin", "wise_origin@google.com"));
}
Also used : Authoring(com.google.copybara.authoring.Authoring) Author(com.google.copybara.authoring.Author) Change(com.google.copybara.revision.Change) Test(org.junit.Test)

Aggregations

Change (com.google.copybara.revision.Change)24 Test (org.junit.Test)15 Author (com.google.copybara.authoring.Author)13 Changes (com.google.copybara.revision.Changes)11 DummyRevision (com.google.copybara.testing.DummyRevision)11 ValidationException (com.google.copybara.exception.ValidationException)8 Path (java.nio.file.Path)8 ImmutableList (com.google.common.collect.ImmutableList)7 WriterContext (com.google.copybara.WriterContext)7 RepoException (com.google.copybara.exception.RepoException)7 TransformResult (com.google.copybara.TransformResult)6 ZonedDateTime (java.time.ZonedDateTime)6 DestinationEffect (com.google.copybara.effect.DestinationEffect)5 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)4 ImmutableSet (com.google.common.collect.ImmutableSet)4 Truth.assertThat (com.google.common.truth.Truth.assertThat)4 Authoring (com.google.copybara.authoring.Authoring)4 EmptyChangeException (com.google.copybara.exception.EmptyChangeException)4 GitLogEntry (com.google.copybara.git.GitRepository.GitLogEntry)4 OptionsBuilder (com.google.copybara.testing.OptionsBuilder)4