Search in sources :

Example 11 with Config

use of com.google.copybara.config.Config in project copybara by google.

the class MigrateCmd method run.

/**
 * Runs the migration specified by {@code migrationName}.
 */
private void run(Options options, ConfigLoader configLoader, String migrationName, Path workdir, ImmutableList<String> sourceRefs) throws RepoException, ValidationException, IOException {
    Config config = loadConfig(options, configLoader, migrationName);
    Migration migration = config.getMigration(migrationName);
    if (!options.get(WorkflowOptions.class).isReadConfigFromChange()) {
        this.migrationRanConsumer.accept(migration);
        migration.run(workdir, sourceRefs);
        return;
    }
    checkCondition(configLoader.supportsLoadForRevision(), "%s flag is not supported for the origin/config file path", WorkflowOptions.READ_CONFIG_FROM_CHANGE);
    // A safeguard, mirror workflows are not supported in the service anyway
    checkCondition(migration instanceof Workflow, "Flag --read-config-from-change is not supported for non-workflow migrations: %s", migrationName);
    migrationRanConsumer.accept(migration);
    Workflow<? extends Revision, ? extends Revision> workflow = (Workflow<? extends Revision, ? extends Revision>) migration;
    new ReadConfigFromChangeWorkflow<>(workflow, options, configLoader, configValidator).run(workdir, sourceRefs);
}
Also used : Config(com.google.copybara.config.Config) Migration(com.google.copybara.config.Migration)

Example 12 with Config

use of com.google.copybara.config.Config 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) 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) 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 13 with Config

use of com.google.copybara.config.Config in project copybara by google.

the class InfoTest method testInfoUpToDate.

@Test
public void testInfoUpToDate() 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());
    Info<?> mockedInfo = Info.create(dummyOriginDescription, dummyDestinationDescription, ImmutableList.of(workflow));
    Mockito.<Info<? extends Revision>>when(migration.getInfo()).thenReturn(mockedInfo);
    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 None.*");
}
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) 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) 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 14 with Config

use of com.google.copybara.config.Config in project copybara by google.

the class WorkflowTest method customIdentityTest.

@Test
public void customIdentityTest() throws Exception {
    options.workflowOptions.initHistory = true;
    Config config = loadConfig("" + "core.workflow(\n" + "    name = 'one',\n" + "    authoring = " + authoring + "\n," + "    origin = testing.origin(),\n" + "    destination = testing.destination(),\n" + "    transformations = [metadata.expose_label('some_label', 'new_label')],\n" + "    change_identity = '${copybara_config_path}foo${label:new_label}',\n" + "    mode = 'ITERATIVE',\n" + ")\n\n" + "core.workflow(\n" + "    name = 'two',\n" + "    authoring = " + authoring + "\n," + "    origin = testing.origin(),\n" + "    destination = testing.destination(),\n" + "    change_identity = '${copybara_config_path}foo${label:some_label}',\n" + "    mode = 'ITERATIVE',\n" + ")\n\n" + "core.workflow(\n" + "    name = 'three',\n" + "    authoring = " + authoring + "\n," + "    origin = testing.origin(),\n" + "    destination = testing.destination(),\n" + "    change_identity = '${copybara_config_path}foo${label:not_found}',\n" + "    mode = 'ITERATIVE',\n" + ")\n\n" + "");
    origin.addSimpleChange(1, "change\n\nsome_label=a");
    origin.addSimpleChange(2, "change\n\nsome_label=b");
    origin.addSimpleChange(3, "change\n\nsome_label=c");
    config.getMigration("one").run(workdir, ImmutableList.of());
    assertThat(destination.processed).hasSize(3);
    ImmutableList<String> oneResult = destination.processed.stream().map(ProcessedChange::getChangeIdentity).collect(ImmutableList.toImmutableList());
    // Different identities
    assertThat(ImmutableSet.copyOf(oneResult)).hasSize(3);
    destination.processed.clear();
    config.getMigration("two").run(workdir, ImmutableList.of());
    ImmutableList<String> twoResult = destination.processed.stream().map(ProcessedChange::getChangeIdentity).collect(ImmutableList.toImmutableList());
    assertThat(oneResult).isEqualTo(twoResult);
    destination.processed.clear();
    config.getMigration("three").run(workdir, ImmutableList.of());
    ImmutableList<String> threeResult = destination.processed.stream().map(ProcessedChange::getChangeIdentity).collect(ImmutableList.toImmutableList());
    assertThat(oneResult).isNotEqualTo(threeResult);
}
Also used : Config(com.google.copybara.config.Config) Test(org.junit.Test)

Example 15 with Config

use of com.google.copybara.config.Config in project copybara by google.

the class WorkflowTest method runWorkflowForMessageTransform.

private void runWorkflowForMessageTransform(WorkflowMode mode, @Nullable String thirdTransform) throws IOException, RepoException, ValidationException {
    origin.addSimpleChange(0, "first commit").setAuthor(new Author("Foo Bar", "foo@bar.com")).addSimpleChange(1, "second commit").setAuthor(new Author("Foo Baz", "foo@baz.com")).addSimpleChange(2000000000, "third commit");
    options.workflowOptions.lastRevision = "0";
    passThruAuthoring();
    Config config = loadConfig("" + "def first(ctx):\n" + "  msg =''\n" + "  for c in ctx.changes.current:\n" + "    msg+='CHANGE: %s (%s) by %s\\n' %  (c.message, c.ref, c.author.name)\n" + "  ctx.set_message(msg)\n" + "def second(ctx):\n" + "  ctx.set_message(ctx.message +'\\nBAR = foo\\n')\n" + "  ctx.set_author(new_author('Someone <someone@somewhere.com>'))\n" + "\n" + (thirdTransform == null ? "" : thirdTransform) + "core.workflow(\n" + "    name = 'default',\n" + "    origin =  testing.origin(),\n" + "    authoring = " + authoring + "\n," + "    destination = testing.destination(),\n" + "    mode = '" + mode + "',\n" + "    transformations = [\n" + "      first, second" + (thirdTransform == null ? "" : ", third") + "]\n" + ")\n");
    config.getMigration("default").run(workdir, ImmutableList.of("2"));
}
Also used : Config(com.google.copybara.config.Config) Author(com.google.copybara.authoring.Author)

Aggregations

Config (com.google.copybara.config.Config)17 ValidationException (com.google.copybara.exception.ValidationException)8 Console (com.google.copybara.util.console.Console)8 Test (org.junit.Test)8 Migration (com.google.copybara.config.Migration)7 IOException (java.io.IOException)5 ImmutableList (com.google.common.collect.ImmutableList)4 MigrationReference (com.google.copybara.Info.MigrationReference)4 Author (com.google.copybara.authoring.Author)4 DummyRevision (com.google.copybara.testing.DummyRevision)4 OptionsBuilder (com.google.copybara.testing.OptionsBuilder)4 TestingEventMonitor (com.google.copybara.testing.TestingEventMonitor)4 MessageType (com.google.copybara.util.console.Message.MessageType)4 TestingConsole (com.google.copybara.util.console.testing.TestingConsole)4 Path (java.nio.file.Path)4 Before (org.junit.Before)4 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)3 Truth.assertThat (com.google.common.truth.Truth.assertThat)3