Search in sources :

Example 26 with EmptyChangeException

use of com.google.copybara.exception.EmptyChangeException in project copybara by google.

the class GerritOrigin method resolve.

@Override
public GitRevision resolve(@Nullable String reference) throws RepoException, ValidationException {
    generalOptions.console().progress("Git Origin: Initializing local repo");
    checkCondition(!Strings.isNullOrEmpty(reference), "Expecting a change number as reference");
    GerritChange change = GerritChange.resolve(getRepository(), repoUrl, reference, this.generalOptions);
    if (change == null) {
        GitRevision gitRevision = GitRepoType.GIT.resolveRef(getRepository(), repoUrl, reference, this.generalOptions, describeVersion, partialFetch);
        return describeVersion ? getRepository().addDescribeVersion(gitRevision) : gitRevision;
    }
    GerritApi api = gerritOptions.newGerritApi(repoUrl);
    ChangeInfo response = api.getChange(Integer.toString(change.getChange()), new GetChangeInput(ImmutableSet.of(DETAILED_ACCOUNTS, DETAILED_LABELS)));
    if (branch != null && !branch.equals(response.getBranch())) {
        throw new EmptyChangeException(String.format("Skipping import of change %s for branch %s. Only tracking changes for branch %s", change.getChange(), response.getBranch(), branch));
    }
    ImmutableMultimap.Builder<String, String> labels = ImmutableMultimap.builder();
    labels.put(GerritChange.GERRIT_CHANGE_BRANCH, response.getBranch());
    if (response.getTopic() != null) {
        labels.put(GerritChange.GERRIT_CHANGE_TOPIC, response.getTopic());
    }
    labels.put(GerritChange.GERRIT_COMPLETE_CHANGE_ID_LABEL, response.getId());
    for (Entry<String, List<AccountInfo>> e : response.getReviewers().entrySet()) {
        for (AccountInfo info : e.getValue()) {
            if (info.getEmail() != null) {
                labels.put("GERRIT_" + e.getKey() + "_EMAIL", info.getEmail());
            }
        }
    }
    if (response.getOwner().getEmail() != null) {
        labels.put(GerritChange.GERRIT_OWNER_EMAIL_LABEL, response.getOwner().getEmail());
    }
    GitRevision gitRevision = change.fetch(labels.build());
    return describeVersion ? getRepository().addDescribeVersion(gitRevision) : gitRevision;
}
Also used : GetChangeInput(com.google.copybara.git.gerritapi.GetChangeInput) ChangeInfo(com.google.copybara.git.gerritapi.ChangeInfo) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) GerritApi(com.google.copybara.git.gerritapi.GerritApi) AccountInfo(com.google.copybara.git.gerritapi.AccountInfo)

Example 27 with EmptyChangeException

use of com.google.copybara.exception.EmptyChangeException in project copybara by google.

the class GitHubPrOrigin method checkRequiredLabels.

/**
 * Check that the PR has all the labels provided in the `required_labels` param
 */
private void checkRequiredLabels(GitHubApi api, String project, PullRequest prData) throws ValidationException, RepoException {
    Set<String> requiredLabels = getRequiredLabels();
    Set<String> retryableLabels = getRetryableLabels();
    if (forceImport() || requiredLabels.isEmpty()) {
        return;
    }
    int retryCount = 0;
    Set<String> requiredButNotPresent;
    do {
        Issue issue;
        try (ProfilerTask ignore = generalOptions.profiler().start("github_api_get_issue")) {
            issue = api.getIssue(project, prData.getNumber());
        }
        requiredButNotPresent = Sets.newHashSet(requiredLabels);
        requiredButNotPresent.removeAll(Collections2.transform(issue.getLabels(), Label::getName));
        // If we got all the labels we want or none of the ones we didn't get are retryable, return.
        if (requiredButNotPresent.isEmpty() || Collections.disjoint(requiredButNotPresent, retryableLabels)) {
            break;
        }
        Uninterruptibles.sleepUninterruptibly(2, TimeUnit.SECONDS);
        retryCount++;
    } while (retryCount < RETRY_COUNT);
    if (!requiredButNotPresent.isEmpty()) {
        throw new EmptyChangeException(String.format("Cannot migrate http://github.com/%s/pull/%d because it is missing the following" + " labels: %s", project, prData.getNumber(), requiredButNotPresent));
    }
}
Also used : Issue(com.google.copybara.git.github.api.Issue) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) Endpoint(com.google.copybara.Endpoint)

Example 28 with EmptyChangeException

use of com.google.copybara.exception.EmptyChangeException in project copybara by google.

the class FeedbackTest method testNoopResultThrowsEmptyChangeException.

@Test
public void testNoopResultThrowsEmptyChangeException() throws Exception {
    Feedback feedback = feedback("" + "def test_action_1(ctx):\n" + "    return ctx.noop('No effect 1')\n" + "\n" + "def test_action_2(ctx):\n" + "    return ctx.noop('No effect 2')\n" + "\n", "test_action_1", "test_action_2");
    EmptyChangeException expected = assertThrows(EmptyChangeException.class, () -> feedback.run(workdir, ImmutableList.of()));
    assertThat(expected).hasMessageThat().contains("Feedback migration 'default' was noop. " + "Detailed messages: [No effect 1, No effect 2]");
    console.assertThat().equalsNext(MessageType.INFO, "Action 'test_action_1' returned noop: No effect 1").equalsNext(MessageType.INFO, "Action 'test_action_2' returned noop: No effect 2");
}
Also used : Feedback(com.google.copybara.feedback.Feedback) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) Test(org.junit.Test)

Example 29 with EmptyChangeException

use of com.google.copybara.exception.EmptyChangeException in project copybara by google.

the class FeedbackTest method testNoActionsThrowsEmptyChangeException.

@Test
public void testNoActionsThrowsEmptyChangeException() throws Exception {
    Feedback feedback = feedback("");
    EmptyChangeException expected = assertThrows(EmptyChangeException.class, () -> feedback.run(workdir, ImmutableList.of()));
    assertThat(expected).hasMessageThat().contains("Feedback migration 'default' was noop. Detailed messages: actions field is empty");
}
Also used : Feedback(com.google.copybara.feedback.Feedback) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) Test(org.junit.Test)

Example 30 with EmptyChangeException

use of com.google.copybara.exception.EmptyChangeException in project copybara by google.

the class WorkflowTest method testSquashAlreadyMigratedSameChange.

@Test
public void testSquashAlreadyMigratedSameChange() throws Exception {
    origin.addSimpleChange(/*timestamp*/
    1);
    skylarkWorkflow("default", SQUASH).run(workdir, ImmutableList.of(HEAD));
    // Disable force so that we get an error
    options.setForce(false);
    EmptyChangeException thrown = assertThrows(EmptyChangeException.class, () -> skylarkWorkflow("default", SQUASH).run(workdir, ImmutableList.of(HEAD)));
    assertThat(thrown).hasMessageThat().contains("'0' has been already migrated");
}
Also used : EmptyChangeException(com.google.copybara.exception.EmptyChangeException) Test(org.junit.Test)

Aggregations

EmptyChangeException (com.google.copybara.exception.EmptyChangeException)42 Test (org.junit.Test)27 ImmutableList (com.google.common.collect.ImmutableList)10 RepoException (com.google.copybara.exception.RepoException)10 ValidationException (com.google.copybara.exception.ValidationException)10 ProfilerTask (com.google.copybara.profiler.Profiler.ProfilerTask)10 Path (java.nio.file.Path)10 CannotResolveRevisionException (com.google.copybara.exception.CannotResolveRevisionException)8 Endpoint (com.google.copybara.Endpoint)7 VisibleForTesting (com.google.common.annotations.VisibleForTesting)5 CharMatcher (com.google.common.base.CharMatcher)5 Preconditions (com.google.common.base.Preconditions)5 Splitter (com.google.common.base.Splitter)5 Collections2 (com.google.common.collect.Collections2)5 ImmutableSetMultimap (com.google.common.collect.ImmutableSetMultimap)5 Iterables (com.google.common.collect.Iterables)5 Sets (com.google.common.collect.Sets)5 Uninterruptibles (com.google.common.util.concurrent.Uninterruptibles)5 BaselinesWithoutLabelVisitor (com.google.copybara.BaselinesWithoutLabelVisitor)5 Change (com.google.copybara.Change)5