Search in sources :

Example 1 with LabelFinder

use of com.google.copybara.LabelFinder in project copybara by google.

the class GerritDestinationTest method reuseChangeId.

@Test
public void reuseChangeId() throws Exception {
    fetch = "master";
    Files.write(workdir.resolve("file"), "some content".getBytes());
    options.setForce(true);
    options.gerrit.gerritChangeId = null;
    url = "https://localhost:33333/foo/bar";
    GitRepository repo = localGerritRepo("localhost:33333/foo/bar");
    gitApiMockHttpTransport = NO_CHANGE_FOUND_MOCK;
    process(new DummyRevision("origin_ref"));
    String changeId = lastCommitChangeIdLine("origin_ref", repo);
    assertThat(changeId).matches(GerritDestination.CHANGE_ID_LABEL + ": I[a-z0-9]+");
    LabelFinder labelFinder = new LabelFinder(changeId);
    Files.write(workdir.resolve("file"), "some different content".getBytes());
    gitApiMockHttpTransport = new GitApiMockHttpTransport() {

        @Override
        protected byte[] getContent(String method, String url, MockLowLevelHttpRequest request) throws IOException {
            String expected = "https://localhost:33333/changes/?q=change:%20" + labelFinder.getValue() + "%20AND%20project:foo/bar";
            if (method.equals("GET") && url.equals(expected)) {
                String result = "[" + "{" + "  change_id : \"" + labelFinder.getValue() + "\"," + "  status : \"NEW\"" + "}]";
                return result.getBytes(UTF_8);
            }
            throw new IllegalArgumentException(method + " " + url);
        }
    };
    // Allow to push again in a non-fastforward way.
    repo.simpleCommand("update-ref", "-d", "refs/for/master");
    process(new DummyRevision("origin_ref"));
    assertThat(lastCommitChangeIdLine("origin_ref", repo)).isEqualTo(changeId);
    GitTesting.assertThatCheckout(repo, "refs/for/master").containsFile("file", "some different content").containsNoMoreFiles();
}
Also used : DummyRevision(com.google.copybara.testing.DummyRevision) GitApiMockHttpTransport(com.google.copybara.testing.OptionsBuilder.GitApiMockHttpTransport) LabelFinder(com.google.copybara.LabelFinder) IOException(java.io.IOException) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Test(org.junit.Test)

Example 2 with LabelFinder

use of com.google.copybara.LabelFinder in project copybara by google.

the class GerritDestinationTest method lastCommitChangeIdLineForRef.

private static String lastCommitChangeIdLineForRef(String gitRef, String originRef, GitRepository repo) throws RepoException {
    GitLogEntry log = Iterables.getOnlyElement(repo.log(gitRef).withLimit(1).run());
    assertThat(log.getBody()).contains("\n" + DummyOrigin.LABEL_NAME + ": " + originRef + "\n");
    String line = null;
    for (LabelFinder label : ChangeMessage.parseMessage(log.getBody()).getLabels()) {
        if (label.isLabel(GerritDestination.CHANGE_ID_LABEL)) {
            assertThat(label.getValue()).matches("I[0-9a-f]{40}$");
            // Multiple Change-Ids are not allowed.
            assertThat(line).isNull();
            line = label.getLine();
        }
    }
    assertWithMessage("Cannot find " + GerritDestination.CHANGE_ID_LABEL + " in:\n" + log.getBody()).that(line).isNotNull();
    return line;
}
Also used : LabelFinder(com.google.copybara.LabelFinder) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry)

Example 3 with LabelFinder

use of com.google.copybara.LabelFinder in project copybara by google.

the class ExposeLabelInMessage method transform.

@Override
public void transform(TransformWork work) throws IOException, ValidationException {
    ImmutableList<LabelFinder> labelInMessage = work.getLabelInMessage(label);
    String value;
    if (!labelInMessage.isEmpty()) {
        LabelFinder last = Iterables.getLast(labelInMessage);
        value = last.getValue();
        if (!label.equals(newLabelName) || !separator.equals(last.getSeparator())) {
            // Remove the old label since we want it with different name/separator.
            work.removeLabel(label, /*wholeMessage=*/
            true);
        }
    } else {
        value = work.getLabel(label);
    }
    if (value == null) {
        checkCondition(ignoreNotFound, "Cannot find label %s", label);
        return;
    }
    work.addOrReplaceLabel(newLabelName, value, separator);
}
Also used : LabelFinder(com.google.copybara.LabelFinder)

Example 4 with LabelFinder

use of com.google.copybara.LabelFinder in project copybara by google.

the class GitIntegrateChanges method doIntegrate.

private void doIntegrate(GitRepository repository, GeneralOptions generalOptions, Predicate<String> externalFiles, TransformResult result, MessageInfo messageInfo) throws CannotIntegrateException, RepoException {
    for (LabelFinder label : result.findAllLabels()) {
        if (!label.isLabel() || !this.label.equals(label.getName())) {
            continue;
        }
        if (label.getValue().isEmpty()) {
            throw new CannotIntegrateException("Found an empty value for label %s", this.label);
        }
        try (ProfilerTask ignore = generalOptions.profiler().start("integrate", ImmutableMap.of("URL", label.getValue()))) {
            generalOptions.console().progress("Integrating change from " + label.getValue());
            IntegrateLabel integrateLabel = GithubPRIntegrateLabel.parse(label.getValue(), repository, generalOptions);
            if (integrateLabel == null) {
                integrateLabel = GerritIntegrateLabel.parse(label.getValue(), repository, generalOptions);
                if (integrateLabel == null) {
                    GitRevision gitRevision = GitRepoType.GIT.resolveRef(repository, /*repoUrl=*/
                    null, label.getValue(), generalOptions);
                    integrateLabel = IntegrateLabel.genericGitRevision(gitRevision);
                }
            }
            strategy.integrate(repository, integrateLabel, externalFiles, label, messageInfo, generalOptions.console(), generalOptions.getDirFactory());
        } catch (ValidationException e) {
            throw new CannotIntegrateException(e, "Error resolving %s", label.getValue());
        }
    }
}
Also used : ValidationException(com.google.copybara.exception.ValidationException) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) LabelFinder(com.google.copybara.LabelFinder)

Aggregations

LabelFinder (com.google.copybara.LabelFinder)4 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)1 ValidationException (com.google.copybara.exception.ValidationException)1 GitLogEntry (com.google.copybara.git.GitRepository.GitLogEntry)1 ProfilerTask (com.google.copybara.profiler.Profiler.ProfilerTask)1 DummyRevision (com.google.copybara.testing.DummyRevision)1 GitApiMockHttpTransport (com.google.copybara.testing.OptionsBuilder.GitApiMockHttpTransport)1 IOException (java.io.IOException)1 Test (org.junit.Test)1