Search in sources :

Example 21 with ProfilerTask

use of com.google.copybara.profiler.Profiler.ProfilerTask in project copybara by google.

the class Feedback method run.

@Override
public void run(Path workdir, ImmutableList<String> sourceRefs) throws RepoException, ValidationException {
    ImmutableList.Builder<ActionResult> allResultsBuilder = ImmutableList.builder();
    String suffix = Joiner.on('_').join(sourceRefs).replaceAll("([/ ])", "_");
    String root = "run/" + name + "/" + suffix.substring(0, Math.min(suffix.length(), 20));
    try (ProfilerTask ignore = profiler().start(root)) {
        for (Action action : actions) {
            ArrayList<DestinationEffect> effects = new ArrayList<>();
            try (ProfilerTask ignore2 = profiler().start(action.getName())) {
                SkylarkConsole console = new SkylarkConsole(generalOptions.console());
                eventMonitors().dispatchEvent(m -> m.onChangeMigrationStarted(new ChangeMigrationStartedEvent()));
                FeedbackMigrationContext context = new FeedbackMigrationContext(this, action, generalOptions.cliLabels(), sourceRefs, console);
                action.run(context);
                effects.addAll(context.getNewDestinationEffects());
                ActionResult actionResult = context.getActionResult();
                allResultsBuilder.add(actionResult);
                // First error aborts the execution of the other actions
                ValidationException.checkCondition(actionResult.getResult() != Result.ERROR, "Feedback migration '%s' action '%s' returned error: %s. Aborting execution.", name, action.getName(), actionResult.getMsg());
            } finally {
                eventMonitors().dispatchEvent(m -> m.onChangeMigrationFinished(new ChangeMigrationFinishedEvent(ImmutableList.copyOf(effects), getOriginDescription(), getDestinationDescription())));
            }
        }
    }
    ImmutableList<ActionResult> allResults = allResultsBuilder.build();
    // This check also returns true if there are no actions
    if (allResults.stream().allMatch(a -> a.getResult() == Result.NO_OP)) {
        String detailedMessage = allResults.isEmpty() ? "actions field is empty" : allResults.stream().map(ActionResult::getMsg).collect(ImmutableList.toImmutableList()).toString();
        throw new EmptyChangeException(String.format("Feedback migration '%s' was noop. Detailed messages: %s", name, detailedMessage));
    }
}
Also used : Action(com.google.copybara.action.Action) ChangeMigrationFinishedEvent(com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) SkylarkConsole(com.google.copybara.transform.SkylarkConsole) ActionResult(com.google.copybara.action.ActionResult) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) DestinationEffect(com.google.copybara.DestinationEffect) ChangeMigrationStartedEvent(com.google.copybara.monitor.EventMonitor.ChangeMigrationStartedEvent) EmptyChangeException(com.google.copybara.exception.EmptyChangeException)

Example 22 with ProfilerTask

use of com.google.copybara.profiler.Profiler.ProfilerTask 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 23 with ProfilerTask

use of com.google.copybara.profiler.Profiler.ProfilerTask 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 " + this.label);
        }
        try (ProfilerTask ignore = generalOptions.profiler().start("integrate", ImmutableMap.of("URL", label.getValue()))) {
            generalOptions.console().progressFmt("Integrating change from '%s'", 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, /*describeVersion=*/
                    false, /*partialFetch*/
                    false);
                    integrateLabel = IntegrateLabel.genericGitRevision(gitRevision);
                }
            }
            strategy.integrate(repository, integrateLabel, externalFiles, label, messageInfo, generalOptions.console(), generalOptions.getDirFactory());
        } catch (ValidationException e) {
            throw new CannotIntegrateException("Error resolving " + label.getValue(), e);
        }
    }
}
Also used : ValidationException(com.google.copybara.exception.ValidationException) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) LabelFinder(com.google.copybara.LabelFinder)

Example 24 with ProfilerTask

use of com.google.copybara.profiler.Profiler.ProfilerTask in project copybara by google.

the class Mirror method defaultMirror.

private void defaultMirror(GitRepository repo) throws RepoException, ValidationException {
    List<String> fetchRefspecs = refspec.stream().map(r -> r.originToOrigin().toString()).collect(Collectors.toList());
    generalOptions.console().progressFmt("Fetching from %s", origin);
    Profiler profiler = generalOptions.profiler();
    try (ProfilerTask ignore1 = profiler.start("fetch")) {
        repo.fetch(origin, /*prune=*/
        true, /*force=*/
        true, fetchRefspecs, partialFetch);
    }
    if (generalOptions.dryRunMode) {
        generalOptions.console().progressFmt("Skipping push to %s. You can check the" + " commits to push in: %s", destination, repo.getGitDir());
    } else {
        generalOptions.console().progressFmt("Pushing to %s", destination);
        List<Refspec> pushRefspecs = mirrorOptions.forcePush || generalOptions.isForced() ? refspec.stream().map(Refspec::withAllowNoFastForward).collect(Collectors.toList()) : refspec;
        try (ProfilerTask ignore1 = profiler.start("push")) {
            repo.push().prune(prune).withRefspecs(destination, pushRefspecs).run();
        } catch (NonFastForwardRepositoryException e) {
            // multiple refs, and that mirrors, it is better to just fail and tell the user.
            throw new ValidationException("Error pushing some refs because origin is behind:" + e.getMessage(), e);
        }
    }
}
Also used : ConfigFile(com.google.copybara.config.ConfigFile) ActionResult(com.google.copybara.action.ActionResult) Iterables(com.google.common.collect.Iterables) GITHUB_COM(com.google.copybara.git.github.util.GitHubHost.GITHUB_COM) RepoException(com.google.copybara.exception.RepoException) DestinationEffect(com.google.copybara.DestinationEffect) ImmutableList(com.google.common.collect.ImmutableList) Result(com.google.copybara.action.ActionResult.Result) GeneralOptions(com.google.copybara.GeneralOptions) Path(java.nio.file.Path) Profiler(com.google.copybara.profiler.Profiler) Nullable(javax.annotation.Nullable) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) DestinationRef(com.google.copybara.DestinationEffect.DestinationRef) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) Migration(com.google.copybara.config.Migration) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) Action(com.google.copybara.action.Action) ValidationException(com.google.copybara.exception.ValidationException) SkylarkConsole(com.google.copybara.transform.SkylarkConsole) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Dict(net.starlark.java.eval.Dict) List(java.util.List) ChangeMigrationFinishedEvent(com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) FluentLogger(com.google.common.flogger.FluentLogger) ValidationException(com.google.copybara.exception.ValidationException) Profiler(com.google.copybara.profiler.Profiler) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask)

Example 25 with ProfilerTask

use of com.google.copybara.profiler.Profiler.ProfilerTask in project copybara by google.

the class GitVisitorUtil method visitChanges.

/**
 * Visits
 */
static void visitChanges(GitRevision start, ChangesVisitor visitor, ChangeReader.Builder queryChanges, GeneralOptions generalOptions, String type, int visitChangePageSize) throws RepoException, ValidationException {
    Preconditions.checkNotNull(start);
    int skip = 0;
    boolean finished = false;
    try (ProfilerTask ignore = generalOptions.profiler().start(type + "/visit_changes")) {
        while (!finished) {
            ImmutableList<Change<GitRevision>> result;
            try (ProfilerTask ignore2 = generalOptions.profiler().start("git_log_" + skip + "_" + visitChangePageSize)) {
                result = queryChanges.setSkip(skip).setLimit(visitChangePageSize).build().run(start.getSha1()).reverse();
            }
            if (result.isEmpty()) {
                break;
            }
            skip += result.size();
            for (Change<GitRevision> current : result) {
                if (visitor.visit(current) == VisitResult.TERMINATE) {
                    finished = true;
                    break;
                }
            }
        }
    }
    if (skip == 0) {
        throw new CannotResolveRevisionException("Cannot resolve reference " + start.getSha1());
    }
}
Also used : ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) Change(com.google.copybara.Change)

Aggregations

ProfilerTask (com.google.copybara.profiler.Profiler.ProfilerTask)29 ValidationException (com.google.copybara.exception.ValidationException)10 ImmutableList (com.google.common.collect.ImmutableList)8 EmptyChangeException (com.google.copybara.exception.EmptyChangeException)8 RepoException (com.google.copybara.exception.RepoException)7 Test (org.junit.Test)6 Change (com.google.copybara.Change)5 Endpoint (com.google.copybara.Endpoint)5 CannotResolveRevisionException (com.google.copybara.exception.CannotResolveRevisionException)5 SkylarkConsole (com.google.copybara.transform.SkylarkConsole)5 IOException (java.io.IOException)5 VisibleForTesting (com.google.common.annotations.VisibleForTesting)4 Preconditions (com.google.common.base.Preconditions)4 ImmutableSetMultimap (com.google.common.collect.ImmutableSetMultimap)4 Action (com.google.copybara.action.Action)4 ChangeMigrationFinishedEvent (com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent)4 Path (java.nio.file.Path)4 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)3 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)3 ImmutableListMultimap.toImmutableListMultimap (com.google.common.collect.ImmutableListMultimap.toImmutableListMultimap)3