Search in sources :

Example 1 with LabelNotFoundException

use of com.google.copybara.templatetoken.LabelTemplate.LabelNotFoundException in project copybara by google.

the class TemplateMessage method transform.

@Override
public TransformationStatus transform(TransformWork work) throws IOException, ValidationException {
    String newMsg;
    try {
        newMsg = labelTemplate.resolve(work::getLabel);
    } catch (LabelNotFoundException e) {
        if (ignoreIfLabelNotFound) {
            return TransformationStatus.success();
        }
        throw new ValidationException(String.format("Cannot find label '%s' in message:\n %s\nor any of the original commit messages", e.getLabel(), work.getMessage()));
    }
    if (!replaceMessage) {
        newMsg += (newLine ? "\n" : "") + work.getMessage();
    }
    work.setMessage(newMsg);
    return TransformationStatus.success();
}
Also used : ValidationException(com.google.copybara.exception.ValidationException) LabelNotFoundException(com.google.copybara.templatetoken.LabelTemplate.LabelNotFoundException)

Example 2 with LabelNotFoundException

use of com.google.copybara.templatetoken.LabelTemplate.LabelNotFoundException in project copybara by google.

the class LabelTemplateTest method testNotFound.

@Test
public void testNotFound() {
    LabelNotFoundException exception = assertThrows(LabelNotFoundException.class, () -> new LabelTemplate("Foo${LABEL}${OTHER}").resolve(e -> e.equals("LABEL") ? "VAL" : null));
    assertThat(exception.getLabel()).isEqualTo("OTHER");
}
Also used : Assert.assertThrows(org.junit.Assert.assertThrows) LabelNotFoundException(com.google.copybara.templatetoken.LabelTemplate.LabelNotFoundException) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) LabelNotFoundException(com.google.copybara.templatetoken.LabelTemplate.LabelNotFoundException) Test(org.junit.Test)

Example 3 with LabelNotFoundException

use of com.google.copybara.templatetoken.LabelTemplate.LabelNotFoundException in project copybara by google.

the class MetadataSquashNotes method transform.

@Override
public TransformationStatus transform(TransformWork work) throws IOException, ValidationException {
    StringBuilder sb;
    try {
        sb = new StringBuilder(prefixTemplate.resolve(work::getLabel));
    } catch (LabelNotFoundException e) {
        throw new ValidationException(String.format("Cannot find label '%s' in message:\n %s\nor any of the original commit messages", e.getLabel(), work.getMessage()));
    }
    if (max == 0) {
        // Don't force changes to be computed if we don't want any change back.
        work.setMessage(sb.toString());
        return TransformationStatus.success();
    }
    int counter = 0;
    List<? extends Change<?>> changes = work.getChanges().getCurrent();
    if (oldestFirst) {
        changes = Lists.reverse(changes);
    }
    if (!useMerge) {
        changes = changes.stream().filter(e -> !e.isMerge()).collect(Collectors.toList());
    }
    for (int i = 0; i < changes.size(); i++) {
        Change<?> c = changes.get(i);
        if (counter == max) {
            break;
        }
        ArrayList<String> summary = new ArrayList<>();
        if (compact) {
            sb.append("  - ");
            if (showRef) {
                summary.add(c.getRef());
            }
            if (showDescription) {
                summary.add(cutIfLong(c.firstLineMessage()));
            }
            if (showAuthor) {
                summary.add("by " + c.getMappedAuthor().toString());
            }
            sb.append(summary.stream().collect(Collectors.joining(" ")));
            sb.append("\n");
        } else {
            sb.append("--\n");
            if (showRef) {
                summary.add(c.getRef());
            } else {
                summary.add(String.format("Change %s of %s", i + 1, changes.size()));
            }
            if (showAuthor) {
                summary.add("by " + c.getAuthor().toString());
            }
            sb.append(summary.stream().collect(Collectors.joining(" ")));
            if (showDescription) {
                sb.append(":\n\n");
                sb.append(c.getMessage());
            }
            sb.append("\n");
        }
        counter++;
    }
    if (changes.size() > max) {
        sb.append("  (And ").append(changes.size() - max).append(" more changes)\n");
    }
    work.setMessage(sb.toString());
    return TransformationStatus.success();
}
Also used : ValidationException(com.google.copybara.exception.ValidationException) NonReversibleValidationException(com.google.copybara.exception.NonReversibleValidationException) LabelNotFoundException(com.google.copybara.templatetoken.LabelTemplate.LabelNotFoundException) ArrayList(java.util.ArrayList)

Aggregations

LabelNotFoundException (com.google.copybara.templatetoken.LabelTemplate.LabelNotFoundException)3 ValidationException (com.google.copybara.exception.ValidationException)2 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 NonReversibleValidationException (com.google.copybara.exception.NonReversibleValidationException)1 ArrayList (java.util.ArrayList)1 Assert.assertThrows (org.junit.Assert.assertThrows)1 Test (org.junit.Test)1 RunWith (org.junit.runner.RunWith)1 JUnit4 (org.junit.runners.JUnit4)1