use of com.google.copybara.util.console.Message in project copybara by google.
the class Copybara method validate.
/**
* Validates that the configuration is correct and that there is a valid migration specified by
* {@code migrationName}.
*
* <p>Note that, besides validating the specific migration, all the configuration will be
* validated syntactically.
*/
public boolean validate(Options options, ConfigLoader<?> configLoader, String migrationName) throws IOException {
Console console = options.get(GeneralOptions.class).console();
ArrayList<Message> messages = new ArrayList<>();
try {
Config config = configLoader.loadConfig(options, console);
messages.addAll(validateConfig(config, migrationName));
} catch (ValidationException e) {
// The validate subcommand should not throw Validation exceptions but log a result
StringBuilder error = new StringBuilder(e.getMessage()).append("\n");
Throwable cause = e.getCause();
while (cause != null) {
error.append(" CAUSED BY: ").append(cause.getMessage()).append("\n");
cause = cause.getCause();
}
messages.add(Message.error(error.toString()));
}
messages.forEach(message -> message.printTo(console));
boolean hasNoErrors = messages.stream().noneMatch(message -> message.getType() == MessageType.ERROR);
if (hasNoErrors) {
console.info(String.format("Configuration '%s' is valid.", configLoader.location()));
} else {
console.error(String.format("Configuration '%s' is invalid.", configLoader.location()));
}
return hasNoErrors;
}
use of com.google.copybara.util.console.Message in project copybara by google.
the class Copybara method loadConfig.
protected Config loadConfig(Options options, ConfigLoader<?> configLoader, String migrationName) throws IOException, ValidationException {
GeneralOptions generalOptions = options.get(GeneralOptions.class);
Console console = generalOptions.console();
Config config = configLoader.loadConfig(options, console);
console.progress("Validating configuration");
List<Message> validationMessages = validateConfig(config, migrationName);
List<Message> errors = validationMessages.stream().filter(message -> message.getType() == MessageType.ERROR).collect(Collectors.toList());
if (errors.isEmpty()) {
return config;
}
errors.forEach(error -> error.printTo(console));
console.error("Configuration is invalid.");
throw new ValidationException("Error validating configuration: Configuration is invalid.");
}
use of com.google.copybara.util.console.Message in project copybara by google.
the class GerritDestinationTest method testReviewerFieldWithNoTopic.
@Test
public void testReviewerFieldWithNoTopic() throws Exception {
pushToRefsFor = "master";
writeFile(workdir, "file", "some content");
fetch = "master";
options.setForce(true);
url = "https://localhost:33333/foo/bar";
mockNoChangesFound();
DummyRevision originRef = new DummyRevision("origin_ref");
GerritDestination destination = destination("submit = False", "reviewers = [\"${SOME_REVIEWER}\"]");
Glob glob = Glob.createGlob(ImmutableList.of("**"), excludedDestinationPaths);
WriterContext writerContext = new WriterContext("GerritDestination", "TEST", false, new DummyRevision("test"), Glob.ALL_FILES.roots());
List<DestinationEffect> result = destination.newWriter(writerContext).write(TransformResults.of(workdir, originRef).withSummary("Test message").withIdentity(originRef.asString()).withLabelFinder(e -> e.equals("SOME_REVIEWER") ? ImmutableList.of("foo@example.com") : ImmutableList.of()), glob, console);
assertThat(result).hasSize(1);
assertThat(result.get(0).getErrors()).isEmpty();
assertPushRef("refs/for/master%hashtag=copybara_id_origin_ref_commiter@email," + "r=foo@example.com");
}
use of com.google.copybara.util.console.Message in project copybara by google.
the class GerritDestinationTest method testCc.
@Test
public void testCc() throws Exception {
pushToRefsFor = "master";
writeFile(workdir, "file", "some content");
fetch = "master";
options.gerrit.gerritTopic = "testTopic";
options.setForce(true);
url = "https://localhost:33333/foo/bar";
mockNoChangesFound();
DummyRevision originRef = new DummyRevision("origin_ref");
GerritDestination destination = destination("submit = False", "cc = [\"${SOME_REVIEWER}\"]");
Glob glob = Glob.createGlob(ImmutableList.of("**"), excludedDestinationPaths);
WriterContext writerContext = new WriterContext("GerritDestination", "TEST", false, new DummyRevision("test"), Glob.ALL_FILES.roots());
List<DestinationEffect> result = destination.newWriter(writerContext).write(TransformResults.of(workdir, originRef).withSummary("Test message").withIdentity(originRef.asString()).withLabelFinder(e -> e.equals("SOME_REVIEWER") ? ImmutableList.of("foo@example.com") : ImmutableList.of()), glob, console);
assertThat(result).hasSize(1);
assertThat(result.get(0).getErrors()).isEmpty();
assertPushRef("refs/for/master%topic=testTopic,hashtag=copybara_id_origin_ref_commiter@email," + "cc=foo@example.com");
}
use of com.google.copybara.util.console.Message in project copybara by google.
the class GerritDestinationTest method testLabels.
@Test
public void testLabels() throws Exception {
pushToRefsFor = "master";
writeFile(workdir, "file", "some content");
fetch = "master";
options.gerrit.gerritTopic = "testTopic";
options.setForce(true);
url = "https://localhost:33333/foo/bar";
mockNoChangesFound();
DummyRevision originRef = new DummyRevision("origin_ref");
GerritDestination destination = destination("submit = False", "labels = [\"${SOME_LABELS}\"]");
Glob glob = Glob.createGlob(ImmutableList.of("**"), excludedDestinationPaths);
WriterContext writerContext = new WriterContext("GerritDestination", "TEST", false, new DummyRevision("test"), Glob.ALL_FILES.roots());
List<DestinationEffect> result = destination.newWriter(writerContext).write(TransformResults.of(workdir, originRef).withSummary("Test message").withIdentity(originRef.asString()).withLabelFinder(e -> e.equals("SOME_LABELS") ? ImmutableList.of("Foo+1", "Bar-1") : ImmutableList.of()), glob, console);
assertThat(result).hasSize(1);
assertThat(result.get(0).getErrors()).isEmpty();
assertPushRef("refs/for/master%topic=testTopic,hashtag=copybara_id_origin_ref_commiter@email," + "label=Foo\\+1,label=Bar-1");
}
Aggregations