Search in sources :

Example 1 with EmptyChangeException

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

the class WorkflowTest method testIterativeModeProducesNoop.

@Test
public void testIterativeModeProducesNoop() throws Exception {
    assertThat(checkIterativeModeWithError(new EmptyChangeException("This was an empty change!"))).hasMessage("Iterative workflow produced no changes in the destination for resolved ref: 3");
    console().assertThat().onceInLog(MessageType.WARNING, "Migration of origin revision '2' resulted in an empty change.*").onceInLog(MessageType.WARNING, "Migration of origin revision '3' resulted in an empty change.*");
}
Also used : EmptyChangeException(com.google.copybara.exception.EmptyChangeException) Test(org.junit.Test)

Example 2 with EmptyChangeException

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

the class Main method runInternal.

/**
 * Runs the command and returns the {@link ExitCode}.
 *
 * <p>This method is also responsible for the exception handling/logging.
 */
private CommandResult runInternal(String[] args, Console console, FileSystem fs) {
    CommandEnv commandEnv = null;
    CopybaraCmd subcommand = null;
    try {
        ModuleSet moduleSet = newModuleSet(environment, fs, console);
        final MainArguments mainArgs = new MainArguments();
        Options options = moduleSet.getOptions();
        jCommander = new JCommander(ImmutableList.builder().addAll(options.getAll()).add(mainArgs).build());
        jCommander.setProgramName("copybara");
        String version = getVersion();
        logger.atInfo().log("Copybara version: %s", version);
        jCommander.parse(args);
        ConfigLoaderProvider configLoaderProvider = newConfigLoaderProvider(moduleSet);
        ImmutableMap<String, CopybaraCmd> commands = Maps.uniqueIndex(getCommands(moduleSet, configLoaderProvider, jCommander), CopybaraCmd::name);
        // generating the usage info.
        for (Map.Entry<String, CopybaraCmd> cmd : commands.entrySet()) {
            jCommander.addCommand(cmd.getKey(), cmd.getValue());
        }
        CommandWithArgs cmdToRun = mainArgs.parseCommand(commands, commands.get("migrate"));
        subcommand = cmdToRun.getSubcommand();
        initEnvironment(options, cmdToRun.getSubcommand(), ImmutableList.copyOf(args));
        GeneralOptions generalOptions = options.get(GeneralOptions.class);
        Path baseWorkdir = mainArgs.getBaseWorkdir(generalOptions, generalOptions.getFileSystem());
        commandEnv = new CommandEnv(baseWorkdir, options, cmdToRun.getArgs());
        generalOptions.console().progressFmt("Running %s", subcommand.name());
        // TODO(malcon): Remove this after 2019-09-15, once tested that temp features work.
        logger.atInfo().log("Temporary features test: %s", options.get(GeneralOptions.class).isTemporaryFeature("TEST_TEMP_FEATURES", true));
        ExitCode exitCode = subcommand.run(commandEnv);
        return new CommandResult(exitCode, subcommand, commandEnv);
    } catch (CommandLineException | ParameterException e) {
        printCauseChain(Level.WARNING, console, args, e);
        console.error("Try 'copybara help'.");
        return new CommandResult(ExitCode.COMMAND_LINE_ERROR, subcommand, commandEnv);
    } catch (RepoException e) {
        printCauseChain(Level.SEVERE, console, args, e);
        // have to do this hack.
        if (e.getCause() instanceof InterruptedException) {
            return new CommandResult(ExitCode.INTERRUPTED, subcommand, commandEnv);
        }
        return new CommandResult(ExitCode.REPOSITORY_ERROR, subcommand, commandEnv);
    } catch (EmptyChangeException e) {
        // This is not necessarily an error. Maybe the tool was run previously and there are no new
        // changes to import.
        console.warn(e.getMessage());
        return new CommandResult(ExitCode.NO_OP, subcommand, commandEnv);
    } catch (ValidationException e) {
        printCauseChain(Level.WARNING, console, args, e);
        return new CommandResult(ExitCode.CONFIGURATION_ERROR, subcommand, commandEnv);
    } catch (IOException e) {
        handleUnexpectedError(console, e.getMessage(), args, e);
        return new CommandResult(ExitCode.ENVIRONMENT_ERROR, subcommand, commandEnv);
    } catch (RuntimeException e) {
        // This usually indicates a serious programming error that will require Copybara team
        // intervention. Print stack trace without concern for presentation.
        e.printStackTrace();
        handleUnexpectedError(console, "Unexpected error (please file a bug against copybara): " + e.getMessage(), args, e);
        return new CommandResult(ExitCode.INTERNAL_ERROR, subcommand, commandEnv);
    }
}
Also used : ValidationException(com.google.copybara.exception.ValidationException) ExitCode(com.google.copybara.util.ExitCode) CommandLineException(com.google.copybara.exception.CommandLineException) JCommander(com.beust.jcommander.JCommander) ParameterException(com.beust.jcommander.ParameterException) Path(java.nio.file.Path) RepoException(com.google.copybara.exception.RepoException) IOException(java.io.IOException) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) CommandWithArgs(com.google.copybara.MainArguments.CommandWithArgs) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with EmptyChangeException

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

the class GitHubPrOrigin method checkRequiredCheckRuns.

/**
 * Check that the PR has a conclusion of "success" for each check_run whose name is in the list
 * provided in the `required_check_runs` param
 */
private void checkRequiredCheckRuns(GitHubApi api, String project, PullRequest prData) throws ValidationException, RepoException {
    Set<String> requiredCheckRuns = getRequiredCheckRuns();
    if (forceImport() || requiredCheckRuns.isEmpty()) {
        return;
    }
    try (ProfilerTask ignore = generalOptions.profiler().start("github_api_get_combined_status")) {
        CheckRuns checkRuns = api.getCheckRuns(project, prData.getHead().getSha());
        Set<String> requiredButNotPresent = Sets.newHashSet(requiredCheckRuns);
        List<CheckRun> passedCheckRuns = checkRuns.getCheckRuns().stream().filter(e -> e.getConclusion().equals("success")).collect(Collectors.toList());
        requiredButNotPresent.removeAll(Collections2.transform(passedCheckRuns, CheckRun::getName));
        if (!requiredButNotPresent.isEmpty()) {
            throw new EmptyChangeException(String.format("Cannot migrate http://github.com/%s/pull/%d because the following check runs " + "have not been passed: %s", project, prData.getNumber(), requiredButNotPresent));
        }
    }
}
Also used : GitHubUtil.asHeadRef(com.google.copybara.git.github.util.GitHubUtil.asHeadRef) Origin(com.google.copybara.Origin) CombinedStatus(com.google.copybara.git.github.api.CombinedStatus) Collections2(com.google.common.collect.Collections2) Review(com.google.copybara.git.github.api.Review) ImmutableListMultimap.toImmutableListMultimap(com.google.common.collect.ImmutableListMultimap.toImmutableListMultimap) Matcher(java.util.regex.Matcher) Change(com.google.copybara.Change) BaselinesWithoutLabelVisitor(com.google.copybara.BaselinesWithoutLabelVisitor) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) Endpoint(com.google.copybara.Endpoint) Splitter(com.google.common.base.Splitter) GeneralOptions(com.google.copybara.GeneralOptions) Path(java.nio.file.Path) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) User(com.google.copybara.git.github.api.User) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) Collectors(java.util.stream.Collectors) Collectors.joining(java.util.stream.Collectors.joining) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) List(java.util.List) GitHubApi(com.google.copybara.git.github.api.GitHubApi) PullRequest(com.google.copybara.git.github.api.PullRequest) GitHubUtil.asMergeRef(com.google.copybara.git.github.util.GitHubUtil.asMergeRef) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) AutoValue(com.google.auto.value.AutoValue) Optional(java.util.Optional) Joiner(com.google.common.base.Joiner) AuthorAssociation(com.google.copybara.git.github.api.AuthorAssociation) Iterables(com.google.common.collect.Iterables) CheckRuns(com.google.copybara.git.github.api.CheckRuns) ValidationException.checkCondition(com.google.copybara.exception.ValidationException.checkCondition) RepoException(com.google.copybara.exception.RepoException) SubmoduleStrategy(com.google.copybara.git.GitOrigin.SubmoduleStrategy) HashSet(java.util.HashSet) GitHubUtil(com.google.copybara.git.github.util.GitHubUtil) Label(com.google.copybara.git.github.api.Label) State(com.google.copybara.git.github.api.Status.State) ImmutableList(com.google.common.collect.ImmutableList) Issue(com.google.copybara.git.github.api.Issue) Nullable(javax.annotation.Nullable) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) CharMatcher(com.google.common.base.CharMatcher) ValidationException(com.google.copybara.exception.ValidationException) ReaderImpl(com.google.copybara.git.GitOrigin.ReaderImpl) PatchTransformation(com.google.copybara.transform.patch.PatchTransformation) Console(com.google.copybara.util.console.Console) TimeUnit(java.util.concurrent.TimeUnit) Authoring(com.google.copybara.authoring.Authoring) Checker(com.google.copybara.checks.Checker) Glob(com.google.copybara.util.Glob) CheckRun(com.google.copybara.git.github.api.CheckRun) Preconditions(com.google.common.base.Preconditions) Status(com.google.copybara.git.github.api.Status) GitHubHost(com.google.copybara.git.github.util.GitHubHost) GitHubPrUrl(com.google.copybara.git.github.util.GitHubHost.GitHubPrUrl) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) CheckRuns(com.google.copybara.git.github.api.CheckRuns) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) CheckRun(com.google.copybara.git.github.api.CheckRun) EmptyChangeException(com.google.copybara.exception.EmptyChangeException)

Example 4 with EmptyChangeException

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

the class GitHubPrOrigin method checkReviewApprovers.

/**
 * Check that the PR has been approved by sufficient reviewers of the correct types, in accordance
 * with the values provided in the `review_state` and `review_approvers` params
 */
private void checkReviewApprovers(GitHubApi api, String project, PullRequest prData, ImmutableListMultimap.Builder<String, String> labelsBuilder) throws ValidationException, RepoException {
    if (reviewState == null) {
        return;
    }
    ImmutableList<Review> reviews = api.getReviews(project, prData.getNumber());
    ApproverState approverState = reviewState.shouldMigrate(reviews, reviewApprovers, prData.getHead().getSha());
    if (!forceImport() && !approverState.shouldMigrate()) {
        String rejected = "";
        if (!approverState.rejectedReviews().isEmpty()) {
            rejected = String.format("\nThe following reviews were ignored because they don't meet " + "the association requirement of %s:\n%s", Joiner.on(", ").join(reviewApprovers), approverState.rejectedReviews().entries().stream().map(e -> String.format("User %s - Association: %s", e.getKey(), e.getValue())).collect(joining("\n")));
        }
        throw new EmptyChangeException(String.format("Cannot migrate http://github.com/%s/pull/%d because it is missing the required" + " approvals (origin is configured as %s).%s", project, prData.getNumber(), reviewState, rejected));
    }
    Set<String> approvers = new HashSet<>();
    Set<String> others = new HashSet<>();
    for (Review review : reviews) {
        if (reviewApprovers.contains(review.getAuthorAssociation())) {
            approvers.add(review.getUser().getLogin());
        } else {
            others.add(review.getUser().getLogin());
        }
    }
    labelsBuilder.putAll(GITHUB_PR_REVIEWER_APPROVER, approvers);
    labelsBuilder.putAll(GITHUB_PR_REVIEWER_OTHER, others);
}
Also used : GitHubUtil.asHeadRef(com.google.copybara.git.github.util.GitHubUtil.asHeadRef) Origin(com.google.copybara.Origin) CombinedStatus(com.google.copybara.git.github.api.CombinedStatus) Collections2(com.google.common.collect.Collections2) Review(com.google.copybara.git.github.api.Review) ImmutableListMultimap.toImmutableListMultimap(com.google.common.collect.ImmutableListMultimap.toImmutableListMultimap) Matcher(java.util.regex.Matcher) Change(com.google.copybara.Change) BaselinesWithoutLabelVisitor(com.google.copybara.BaselinesWithoutLabelVisitor) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) Endpoint(com.google.copybara.Endpoint) Splitter(com.google.common.base.Splitter) GeneralOptions(com.google.copybara.GeneralOptions) Path(java.nio.file.Path) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) User(com.google.copybara.git.github.api.User) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) Collectors(java.util.stream.Collectors) Collectors.joining(java.util.stream.Collectors.joining) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) List(java.util.List) GitHubApi(com.google.copybara.git.github.api.GitHubApi) PullRequest(com.google.copybara.git.github.api.PullRequest) GitHubUtil.asMergeRef(com.google.copybara.git.github.util.GitHubUtil.asMergeRef) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) AutoValue(com.google.auto.value.AutoValue) Optional(java.util.Optional) Joiner(com.google.common.base.Joiner) AuthorAssociation(com.google.copybara.git.github.api.AuthorAssociation) Iterables(com.google.common.collect.Iterables) CheckRuns(com.google.copybara.git.github.api.CheckRuns) ValidationException.checkCondition(com.google.copybara.exception.ValidationException.checkCondition) RepoException(com.google.copybara.exception.RepoException) SubmoduleStrategy(com.google.copybara.git.GitOrigin.SubmoduleStrategy) HashSet(java.util.HashSet) GitHubUtil(com.google.copybara.git.github.util.GitHubUtil) Label(com.google.copybara.git.github.api.Label) State(com.google.copybara.git.github.api.Status.State) ImmutableList(com.google.common.collect.ImmutableList) Issue(com.google.copybara.git.github.api.Issue) Nullable(javax.annotation.Nullable) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) CharMatcher(com.google.common.base.CharMatcher) ValidationException(com.google.copybara.exception.ValidationException) ReaderImpl(com.google.copybara.git.GitOrigin.ReaderImpl) PatchTransformation(com.google.copybara.transform.patch.PatchTransformation) Console(com.google.copybara.util.console.Console) TimeUnit(java.util.concurrent.TimeUnit) Authoring(com.google.copybara.authoring.Authoring) Checker(com.google.copybara.checks.Checker) Glob(com.google.copybara.util.Glob) CheckRun(com.google.copybara.git.github.api.CheckRun) Preconditions(com.google.common.base.Preconditions) Status(com.google.copybara.git.github.api.Status) GitHubHost(com.google.copybara.git.github.util.GitHubHost) GitHubPrUrl(com.google.copybara.git.github.util.GitHubHost.GitHubPrUrl) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) Review(com.google.copybara.git.github.api.Review) HashSet(java.util.HashSet)

Example 5 with EmptyChangeException

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

the class GitHubPrOrigin method checkRequiredStatusContextNames.

/**
 * Check that the PR has a state of "success" for each status whose context is in the list
 * provided in the `required_status_context_names` param
 */
private void checkRequiredStatusContextNames(GitHubApi api, String project, PullRequest prData) throws ValidationException, RepoException {
    Set<String> requiredStatusContextNames = getRequiredStatusContextNames();
    if (forceImport() || requiredStatusContextNames.isEmpty()) {
        return;
    }
    try (ProfilerTask ignore = generalOptions.profiler().start("github_api_get_combined_status")) {
        CombinedStatus combinedStatus = api.getCombinedStatus(project, prData.getHead().getSha());
        Set<String> requiredButNotPresent = Sets.newHashSet(requiredStatusContextNames);
        List<Status> successStatuses = combinedStatus.getStatuses().stream().filter(e -> e.getState() == State.SUCCESS).collect(Collectors.toList());
        requiredButNotPresent.removeAll(Collections2.transform(successStatuses, Status::getContext));
        if (!requiredButNotPresent.isEmpty()) {
            throw new EmptyChangeException(String.format("Cannot migrate http://github.com/%s/pull/%d because the following ci labels " + "have not been passed: %s", project, prData.getNumber(), requiredButNotPresent));
        }
    }
}
Also used : CombinedStatus(com.google.copybara.git.github.api.CombinedStatus) Status(com.google.copybara.git.github.api.Status) GitHubUtil.asHeadRef(com.google.copybara.git.github.util.GitHubUtil.asHeadRef) Origin(com.google.copybara.Origin) CombinedStatus(com.google.copybara.git.github.api.CombinedStatus) Collections2(com.google.common.collect.Collections2) Review(com.google.copybara.git.github.api.Review) ImmutableListMultimap.toImmutableListMultimap(com.google.common.collect.ImmutableListMultimap.toImmutableListMultimap) Matcher(java.util.regex.Matcher) Change(com.google.copybara.Change) BaselinesWithoutLabelVisitor(com.google.copybara.BaselinesWithoutLabelVisitor) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) Endpoint(com.google.copybara.Endpoint) Splitter(com.google.common.base.Splitter) GeneralOptions(com.google.copybara.GeneralOptions) Path(java.nio.file.Path) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) User(com.google.copybara.git.github.api.User) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) Collectors(java.util.stream.Collectors) Collectors.joining(java.util.stream.Collectors.joining) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) List(java.util.List) GitHubApi(com.google.copybara.git.github.api.GitHubApi) PullRequest(com.google.copybara.git.github.api.PullRequest) GitHubUtil.asMergeRef(com.google.copybara.git.github.util.GitHubUtil.asMergeRef) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) AutoValue(com.google.auto.value.AutoValue) Optional(java.util.Optional) Joiner(com.google.common.base.Joiner) AuthorAssociation(com.google.copybara.git.github.api.AuthorAssociation) Iterables(com.google.common.collect.Iterables) CheckRuns(com.google.copybara.git.github.api.CheckRuns) ValidationException.checkCondition(com.google.copybara.exception.ValidationException.checkCondition) RepoException(com.google.copybara.exception.RepoException) SubmoduleStrategy(com.google.copybara.git.GitOrigin.SubmoduleStrategy) HashSet(java.util.HashSet) GitHubUtil(com.google.copybara.git.github.util.GitHubUtil) Label(com.google.copybara.git.github.api.Label) State(com.google.copybara.git.github.api.Status.State) ImmutableList(com.google.common.collect.ImmutableList) Issue(com.google.copybara.git.github.api.Issue) Nullable(javax.annotation.Nullable) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) CharMatcher(com.google.common.base.CharMatcher) ValidationException(com.google.copybara.exception.ValidationException) ReaderImpl(com.google.copybara.git.GitOrigin.ReaderImpl) PatchTransformation(com.google.copybara.transform.patch.PatchTransformation) Console(com.google.copybara.util.console.Console) TimeUnit(java.util.concurrent.TimeUnit) Authoring(com.google.copybara.authoring.Authoring) Checker(com.google.copybara.checks.Checker) Glob(com.google.copybara.util.Glob) CheckRun(com.google.copybara.git.github.api.CheckRun) Preconditions(com.google.common.base.Preconditions) Status(com.google.copybara.git.github.api.Status) GitHubHost(com.google.copybara.git.github.util.GitHubHost) GitHubPrUrl(com.google.copybara.git.github.util.GitHubHost.GitHubPrUrl) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) CombinedStatus(com.google.copybara.git.github.api.CombinedStatus)

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