Search in sources :

Example 1 with Migration

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

the class Copybara method run.

/**
 * Runs the migration specified by {@code migrationName}.
 */
public void run(Options options, ConfigLoader<?> configLoader, String migrationName, Path workdir, @Nullable String sourceRef) throws RepoException, ValidationException, IOException {
    Config config = loadConfig(options, configLoader, migrationName);
    Migration migration = config.getMigration(migrationName);
    if (configLoaderProvider == null) {
        this.migrationRanConsumer.accept(migration);
        migration.run(workdir, sourceRef);
        return;
    }
    // A safeguard, mirror workflows are not supported in the service anyway
    if (!(migration instanceof Workflow)) {
        throw new ValidationException("Flag --read-config-from-change is not supported for non-workflow migrations: %s", migrationName);
    }
    migrationRanConsumer.accept(migration);
    @SuppressWarnings("unchecked") Workflow<? extends Revision, ? extends Revision> workflow = (Workflow<? extends Revision, ? extends Revision>) migration;
    new ReadConfigFromChangeWorkflow<>(workflow, options, configLoaderProvider, configValidator).run(workdir, sourceRef);
}
Also used : ValidationException(com.google.copybara.exception.ValidationException) Config(com.google.copybara.config.Config) Migration(com.google.copybara.config.Migration)

Example 2 with Migration

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

the class InfoCmd method showAllMigrations.

private static void showAllMigrations(CommandEnv commandEnv, Config config) {
    TablePrinter table = new TablePrinter("Name", "Origin", "Destination", "Mode", "Description");
    for (Migration m : config.getMigrations().values().stream().sorted(Comparator.comparing(Migration::getName)).collect(ImmutableList.toImmutableList())) {
        table.addRow(m.getName(), prettyOriginDestination(m.getOriginDescription()), prettyOriginDestination(m.getDestinationDescription()), m.getModeString(), Strings.nullToEmpty(m.getDescription()));
    }
    Console console = commandEnv.getOptions().get(GeneralOptions.class).console();
    for (String line : table.build()) {
        console.info(line);
    }
    console.info("To get information about the state of any migration run:\n\n" + "    copybara info " + config.getLocation() + " [workflow_name]" + "\n");
}
Also used : Migration(com.google.copybara.config.Migration) TablePrinter(com.google.copybara.util.TablePrinter) Console(com.google.copybara.util.console.Console)

Example 3 with Migration

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

the class Main method getCommands.

public ImmutableSet<CopybaraCmd> getCommands(ModuleSet moduleSet, ConfigLoaderProvider configLoaderProvider, JCommander jcommander) throws CommandLineException {
    ConfigValidator validator = getConfigValidator(moduleSet.getOptions());
    Consumer<Migration> consumer = getMigrationRanConsumer();
    return ImmutableSet.of(new MigrateCmd(validator, consumer, configLoaderProvider, moduleSet), new InfoCmd(configLoaderProvider, newInfoContextProvider()), new ValidateCmd(validator, consumer, configLoaderProvider), new HelpCmd(jcommander), new OnboardCmd(), new VersionCmd());
}
Also used : OnboardCmd(com.google.copybara.onboard.OnboardCmd) ConfigValidator(com.google.copybara.config.ConfigValidator) Migration(com.google.copybara.config.Migration)

Example 4 with Migration

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

the class InfoTest method setUp.

@Before
public void setUp() throws IOException {
    dummyOriginDescription = ImmutableMultimap.of("origin", "foo");
    dummyDestinationDescription = ImmutableMultimap.of("dest", "bar");
    console = new TestingConsole();
    temp = Files.createTempDirectory("temp");
    optionsBuilder = new OptionsBuilder();
    optionsBuilder.setConsole(console);
    optionsBuilder.setWorkdirToRealTempDir();
    skylark = new SkylarkTestExecutor(optionsBuilder);
    eventMonitor = new TestingEventMonitor();
    optionsBuilder.general.enableEventMonitor("just testing", eventMonitor);
    optionsBuilder.general.starlarkMode = StarlarkMode.STRICT.name();
    migration = mock(Migration.class);
    config = new Config(ImmutableMap.of("workflow", migration), temp.resolve("copy.bara.sky").toString(), ImmutableMap.of());
    configWithDeps = mock(ConfigWithDependencies.class);
    when(configWithDeps.getConfig()).thenAnswer(i -> config);
    configInfo = "" + "core.workflow(" + "    name = 'workflow'," + "    origin = git.origin(url = 'https://example.com/orig', ref = 'master')," + "    destination = git.destination(url = 'https://example.com/dest')," + "    authoring = authoring.overwrite('Foo <foo@example.com>')" + ")\n\n" + "";
    info = new InfoCmd((configPath, sourceRef) -> new ConfigLoader(skylark.createModuleSet(), skylark.createConfigFile("copy.bara.sky", configInfo), optionsBuilder.general.getStarlarkMode()) {

        @Override
        protected Config doLoadForRevision(Console console, Revision revision) throws ValidationException {
            try {
                return skylark.loadConfig(configPath);
            } catch (IOException e) {
                throw new AssertionError("Should not fail", e);
            }
        }
    }, getFakeContextProvider());
}
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) Migration(com.google.copybara.config.Migration) Config(com.google.copybara.config.Config) IOException(java.io.IOException) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) TestingEventMonitor(com.google.copybara.testing.TestingEventMonitor) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) DummyRevision(com.google.copybara.testing.DummyRevision) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Console(com.google.copybara.util.console.Console) ConfigWithDependencies(com.google.copybara.config.SkylarkParser.ConfigWithDependencies) Before(org.junit.Before)

Example 5 with Migration

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

the class WorkflowTest method reversibleCheckSymlinkError.

@Test
public void reversibleCheckSymlinkError() throws Exception {
    Path someRoot = Files.createTempDirectory("someRoot");
    Path originPath = someRoot.resolve("origin");
    Files.createDirectories(originPath);
    GitRepository origin = GitRepository.newRepo(/*verbose*/
    true, originPath, getGitEnv()).init();
    String primaryBranch = origin.getPrimaryBranch();
    String config = "core.workflow(\n" + "    name = 'default',\n" + String.format("    origin = git.origin( url = 'file://%s', ref = '%s'),\n", origin.getWorkTree(), primaryBranch) + "    destination = testing.destination(),\n" + "    authoring = " + authoring + ",\n" + "    origin_files = glob(['included/**']),\n" + "    reversible_check = True,\n" + "    mode = '" + WorkflowMode.SQUASH + "',\n" + ")\n";
    Migration workflow = loadConfig(config).getMigration("default");
    Path included = originPath.resolve("included");
    Files.createDirectory(included);
    Files.write(originPath.resolve("included/foo.txt"), "a".getBytes(UTF_8));
    Path fileOutsideCheckout = someRoot.resolve("file_outside_checkout");
    Files.write(fileOutsideCheckout, "THE CONTENT".getBytes(UTF_8));
    Files.createSymbolicLink(included.resolve("symlink"), included.relativize(fileOutsideCheckout));
    origin.add().files("included/foo.txt").run();
    origin.add().files("included/symlink").run();
    origin.commit("Foo <foo@bara.com>", ZonedDateTime.now(ZoneId.systemDefault()), "A commit");
    ValidationException expected = assertThrows(ValidationException.class, () -> workflow.run(workdir, ImmutableList.of()));
    assertThat(expected.getMessage()).matches("" + "Failed to perform reversible check of transformations due to symlink '.*' that " + "points outside the checkout dir. Consider removing this symlink from your " + "origin_files or, alternatively, set reversible_check = False in your " + "workflow.");
}
Also used : Path(java.nio.file.Path) FileSubjects.assertThatPath(com.google.copybara.testing.FileSubjects.assertThatPath) GitRepository(com.google.copybara.git.GitRepository) ValidationException(com.google.copybara.exception.ValidationException) Migration(com.google.copybara.config.Migration) Test(org.junit.Test)

Aggregations

Migration (com.google.copybara.config.Migration)30 Test (org.junit.Test)22 ValidationException (com.google.copybara.exception.ValidationException)9 Path (java.nio.file.Path)9 FileSubjects.assertThatPath (com.google.copybara.testing.FileSubjects.assertThatPath)7 GitRepository (com.google.copybara.git.GitRepository)6 Config (com.google.copybara.config.Config)5 GitLogEntry (com.google.copybara.git.GitRepository.GitLogEntry)3 OptionsBuilder (com.google.copybara.testing.OptionsBuilder)3 TestingEventMonitor (com.google.copybara.testing.TestingEventMonitor)3 Console (com.google.copybara.util.console.Console)3 TestingConsole (com.google.copybara.util.console.testing.TestingConsole)3 Before (org.junit.Before)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)2 Truth.assertThat (com.google.common.truth.Truth.assertThat)2 MigrationReference (com.google.copybara.Info.MigrationReference)2 Author (com.google.copybara.authoring.Author)2