Search in sources :

Example 36 with Console

use of com.google.copybara.util.console.Console in project copybara by google.

the class ValidateCmd method run.

@Override
public ExitCode run(CommandEnv commandEnv) throws ValidationException, IOException, RepoException {
    ConfigFileArgs configFileArgs = commandEnv.parseConfigFileArgs(this, /*useSourceRef*/
    false);
    ConfigLoader configLoader = configLoaderProvider.newLoader(configFileArgs.getConfigPath(), configFileArgs.getSourceRef());
    ValidationResult result = validate(commandEnv.getOptions(), configLoader, configFileArgs.getWorkflowName());
    Console console = commandEnv.getOptions().get(GeneralOptions.class).console();
    for (ValidationMessage message : result.getAllMessages()) {
        switch(message.getLevel()) {
            case WARNING:
                console.warn(message.getMessage());
                break;
            case ERROR:
                console.error(message.getMessage());
                break;
        }
    }
    if (result.hasErrors()) {
        console.errorFmt("Configuration '%s' is invalid.", configLoader.location());
        return ExitCode.CONFIGURATION_ERROR;
    }
    console.infoFmt("Configuration '%s' is valid.", configLoader.location());
    return ExitCode.SUCCESS;
}
Also used : ValidationMessage(com.google.copybara.config.ValidationResult.ValidationMessage) Console(com.google.copybara.util.console.Console) ValidationResult(com.google.copybara.config.ValidationResult)

Example 37 with Console

use of com.google.copybara.util.console.Console in project copybara by google.

the class InfoCmd method info.

/**
 * Retrieves the {@link Info} of the {@code migrationName} and prints it to the console.
 */
private static void info(Options options, Config config, String migrationName, ImmutableMap<String, String> context) throws ValidationException, RepoException {
    Info<? extends Revision> info = getInfo(migrationName, config);
    Console console = options.get(GeneralOptions.class).console();
    int outputSize = 0;
    for (MigrationReference<? extends Revision> migrationRef : info.migrationReferences()) {
        console.info(String.format("'%s': last_migrated %s - last_available %s.", migrationRef.getLabel(), migrationRef.getLastMigrated() != null ? migrationRef.getLastMigrated().asString() : "None", migrationRef.getLastAvailableToMigrate() != null ? migrationRef.getLastAvailableToMigrate().asString() : "None"));
        ImmutableList<? extends Change<? extends Revision>> availableToMigrate = migrationRef.getAvailableToMigrate();
        int outputLimit = options.get(GeneralOptions.class).getOutputLimit();
        if (!availableToMigrate.isEmpty()) {
            console.infoFmt("Available changes %s:", availableToMigrate.size() <= outputLimit ? String.format("(%d)", availableToMigrate.size()) : String.format("(showing only first %d out of %d)", outputLimit, availableToMigrate.size()));
            TablePrinter table = new TablePrinter("Date", "Revision", "Description", "Author");
            for (Change<? extends Revision> change : Iterables.limit(availableToMigrate, outputLimit)) {
                outputSize++;
                table.addRow(change.getDateTime().format(DATE_FORMATTER), Ascii.truncate(change.getRevision().asString(), REVISION_MAX_LENGTH, ""), Ascii.truncate(change.firstLineMessage(), DESCRIPTION_MAX_LENGTH, "..."), Ascii.truncate(change.getAuthor().toString(), AUTHOR_MAX_LENGTH, "..."));
            }
            for (String line : table.build()) {
                console.info(line);
            }
        }
        if (outputSize > 100) {
            console.infoFmt("Use %s to limit the output of the command.", GeneralOptions.OUTPUT_LIMIT_FLAG);
        }
    }
    options.get(GeneralOptions.class).eventMonitors().dispatchEvent(e -> e.onInfoFinished(new InfoFinishedEvent(info, context)));
}
Also used : InfoFinishedEvent(com.google.copybara.monitor.EventMonitor.InfoFinishedEvent) TablePrinter(com.google.copybara.util.TablePrinter) Console(com.google.copybara.util.console.Console)

Example 38 with Console

use of com.google.copybara.util.console.Console in project copybara by google.

the class GerritOrigin method newReader.

@Override
public Reader<GitRevision> newReader(Glob originFiles, Authoring authoring) {
    return new GitOrigin.ReaderImpl(repoUrl, originFiles, authoring, gitOptions, gitOriginOptions, generalOptions, includeBranchCommitLogs, submoduleStrategy, firstParent, partialFetch, patchTransformation, describeVersion, /*configPath=*/
    null, /*workflowName=*/
    null) {

        @Override
        public ImmutableList<GitRevision> findBaselinesWithoutLabel(GitRevision startRevision, int limit) throws RepoException, ValidationException {
            // Skip the first change as it is the Gerrit review change
            BaselinesWithoutLabelVisitor<GitRevision> visitor = new BaselinesWithoutLabelVisitor<>(originFiles, limit, /*skipFirst=*/
            true);
            visitChanges(startRevision, visitor);
            return visitor.getResult();
        }

        @Override
        public Endpoint getFeedbackEndPoint(Console console) throws ValidationException {
            gerritOptions.validateEndpointChecker(endpointChecker, repoUrl);
            return new GerritEndpoint(gerritOptions.newGerritApiSupplier(repoUrl, endpointChecker), repoUrl, console);
        }

        @Override
        public ChangesResponse<GitRevision> changes(@Nullable GitRevision fromRef, GitRevision toRef) throws RepoException, ValidationException {
            ChangesResponse<GitRevision> result = super.changes(fromRef, toRef);
            Change<GitRevision> change = change(toRef);
            if (!ignoreGerritNoop || change.getChangeFiles() == null || !toRef.associatedLabels().containsKey(GerritChange.GERRIT_COMPLETE_CHANGE_ID_LABEL)) {
                return result;
            }
            PathMatcher pathMatcher = originFiles.relativeTo(Paths.get("/"));
            if (change.getChangeFiles().stream().noneMatch(x -> pathMatcher.matches(Paths.get("/", x)))) {
                logger.atInfo().log("Skipping a Gerrit noop change with ref: %s", toRef.getSha1());
                return ChangesResponse.noChanges(EmptyReason.NO_CHANGES);
            }
            return result;
        }
    };
}
Also used : PathMatcher(java.nio.file.PathMatcher) Console(com.google.copybara.util.console.Console) BaselinesWithoutLabelVisitor(com.google.copybara.BaselinesWithoutLabelVisitor) Nullable(javax.annotation.Nullable)

Aggregations

Console (com.google.copybara.util.console.Console)38 Test (org.junit.Test)19 ValidationException (com.google.copybara.exception.ValidationException)14 TestingConsole (com.google.copybara.util.console.testing.TestingConsole)11 IOException (java.io.IOException)11 ImmutableList (com.google.common.collect.ImmutableList)10 Config (com.google.copybara.config.Config)8 Path (java.nio.file.Path)7 OptionsBuilder (com.google.copybara.testing.OptionsBuilder)6 SkylarkTestExecutor (com.google.copybara.testing.SkylarkTestExecutor)6 Before (org.junit.Before)6 Truth.assertThat (com.google.common.truth.Truth.assertThat)5 Migration (com.google.copybara.config.Migration)5 Glob (com.google.copybara.util.Glob)5 RunWith (org.junit.runner.RunWith)5 JUnit4 (org.junit.runners.JUnit4)5 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)4 ConfigWithDependencies (com.google.copybara.config.SkylarkParser.ConfigWithDependencies)4 RepoException (com.google.copybara.exception.RepoException)4 Change (com.google.copybara.revision.Change)4