Search in sources :

Example 1 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, @Nullable String sourceRef) throws RepoException, ValidationException {
    // TODO(danielromero): Handle correctly null sourceRefs
    SkylarkConsole console = new SkylarkConsole(generalOptions.console());
    Profiler profiler = generalOptions.profiler();
    try (ProfilerTask ignore = profiler.start("run/" + name)) {
        for (Action action : actions) {
            try (ProfilerTask ignore2 = profiler.start(action.getName())) {
                action.run(new FeedbackContext(origin, destination, sourceRef, console));
            }
        }
    }
    ValidationException.checkCondition(console.getErrorCount() == 0, "%d errors executing the feedback migration", console.getErrorCount());
}
Also used : SkylarkConsole(com.google.copybara.transform.SkylarkConsole) Profiler(com.google.copybara.profiler.Profiler) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask)

Example 2 with ProfilerTask

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

the class GitHubPrOrigin method getRevisionForPR.

private GitRevision getRevisionForPR(String project, PullRequest prData) throws RepoException, ValidationException {
    GitHubApi api = gitHubOptions.newGitHubApi(project);
    int prNumber = (int) prData.getNumber();
    boolean actuallyUseMerge = this.useMerge;
    ImmutableListMultimap.Builder<String, String> labels = ImmutableListMultimap.builder();
    checkPrState(prData);
    checkPrBranch(project, prData);
    checkRequiredLabels(api, project, prData);
    checkRequiredStatusContextNames(api, project, prData);
    checkRequiredCheckRuns(api, project, prData);
    checkReviewApprovers(api, project, prData, labels);
    // Fetch also the baseline branch. It is almost free and doing a roundtrip later would hurt
    // latency.
    console.progressFmt("Fetching Pull Request %d and branch '%s'", prNumber, prData.getBase().getRef());
    ImmutableList.Builder<String> refSpecBuilder = ImmutableList.<String>builder().add(String.format("%s:%s", asHeadRef(prNumber), LOCAL_PR_HEAD_REF)).add(String.format("refs/heads/%s:" + LOCAL_PR_BASE_BRANCH, prData.getBase().getRef()));
    if (actuallyUseMerge) {
        if (!Boolean.FALSE.equals(prData.isMergeable())) {
            refSpecBuilder.add(String.format("%s:%s", asMergeRef(prNumber), LOCAL_PR_MERGE_REF));
        } else if (forceImport()) {
            console.warnFmt("PR %d is not mergeable, but continuing with PR Head instead because of %s", prNumber, GeneralOptions.FORCE);
            actuallyUseMerge = false;
        } else {
            throw new CannotResolveRevisionException(String.format("Cannot find a merge reference for Pull Request %d." + " It might have a conflict with head.", prNumber));
        }
    }
    ImmutableList<String> refspec = refSpecBuilder.build();
    try (ProfilerTask ignore = generalOptions.profiler().start("fetch")) {
        getRepository().fetch(ghHost.projectAsUrl(project), /*prune=*/
        false, /*force=*/
        true, refspec, partialFetch);
    } catch (CannotResolveRevisionException e) {
        if (actuallyUseMerge && prData.isMergeable() == null && forceImport()) {
            // We can perhaps recover by fetching without the merge ref
            actuallyUseMerge = false;
            refspec = refspec.subList(0, refspec.size() - 1);
            try (ProfilerTask ignore = generalOptions.profiler().start("fetch")) {
                getRepository().fetch(ghHost.projectAsUrl(project), /*prune=*/
                false, /*force=*/
                true, refspec, partialFetch);
                e = null;
            } catch (CannotResolveRevisionException e2) {
                // Report the error from the second fetch instead of the original fetch
                e = e2;
            }
        }
        if (e != null) {
            if (actuallyUseMerge) {
                String msg = String.format("Cannot find a merge reference for Pull Request %d.", prNumber);
                if (Boolean.TRUE.equals(prData.isMergeable())) {
                    msg += " GitHub reported that this merge reference should exist.";
                }
                throw new CannotResolveRevisionException(msg, e);
            } else {
                throw new CannotResolveRevisionException(String.format("Cannot find Pull Request %d.", prNumber), e);
            }
        }
    }
    String refForMigration = actuallyUseMerge ? LOCAL_PR_MERGE_REF : LOCAL_PR_HEAD_REF;
    GitRevision gitRevision = getRepository().resolveReference(refForMigration);
    String headPrSha1 = getRepository().resolveReference(LOCAL_PR_HEAD_REF).getSha1();
    String integrateLabel = new GitHubPrIntegrateLabel(getRepository(), generalOptions, project, prNumber, prData.getHead().getLabel(), // The integrate SHA has to be HEAD of the PR not the merge ref, even if use_merge = True
    headPrSha1).toString();
    labels.putAll(GITHUB_PR_REQUESTED_REVIEWER, prData.getRequestedReviewers().stream().map(User::getLogin).collect(toImmutableList()));
    labels.put(GITHUB_PR_NUMBER_LABEL, Integer.toString(prNumber));
    labels.put(GitModule.DEFAULT_INTEGRATE_LABEL, integrateLabel);
    labels.put(GITHUB_BASE_BRANCH, prData.getBase().getRef());
    labels.put(GITHUB_PR_HEAD_SHA, headPrSha1);
    labels.put(GITHUB_PR_USE_MERGE, Boolean.toString(actuallyUseMerge));
    String mergeBase = getRepository().mergeBase(refForMigration, LOCAL_PR_BASE_BRANCH);
    labels.put(GITHUB_BASE_BRANCH_SHA1, mergeBase);
    labels.put(GITHUB_PR_TITLE, prData.getTitle());
    labels.put(GITHUB_PR_BODY, prData.getBody());
    labels.put(GITHUB_PR_URL, prData.getHtmlUrl());
    labels.put(GITHUB_PR_USER, prData.getUser().getLogin());
    labels.putAll(GITHUB_PR_ASSIGNEE, prData.getAssignees().stream().map(User::getLogin).collect(Collectors.toList()));
    GitRevision result = new GitRevision(getRepository(), gitRevision.getSha1(), /*reviewReference=*/
    null, actuallyUseMerge ? asMergeRef(prNumber) : asHeadRef(prNumber), labels.build(), url);
    return describeVersion ? getRepository().addDescribeVersion(result) : result;
}
Also used : User(com.google.copybara.git.github.api.User) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) Endpoint(com.google.copybara.Endpoint) GitHubApi(com.google.copybara.git.github.api.GitHubApi) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) ImmutableListMultimap.toImmutableListMultimap(com.google.common.collect.ImmutableListMultimap.toImmutableListMultimap) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException)

Example 3 with ProfilerTask

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

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

Example 5 with ProfilerTask

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

the class Mirror method run.

@Override
public void run(Path workdir, ImmutableList<String> sourceRefs) throws RepoException, IOException, ValidationException {
    try (ProfilerTask ignore = generalOptions.profiler().start("run/" + name)) {
        GitRepository repo = gitOptions.cachedBareRepoForUrl(origin);
        if (Iterables.isEmpty(actions)) {
            defaultMirror(repo);
        } else {
            ImmutableList.Builder<ActionResult> allResultsBuilder = ImmutableList.builder();
            for (Action action : actions) {
                GitMirrorContext context = new GitMirrorContext(action, new SkylarkConsole(generalOptions.console()), sourceRefs, refspec, origin, destination, generalOptions.isForced(), repo, generalOptions.getDirFactory(), Dict.empty());
                try {
                    action.run(context);
                    ActionResult actionResult = context.getActionResult();
                    allResultsBuilder.add(actionResult);
                    // First error aborts the execution of the other actions unless --force is used
                    ValidationException.checkCondition(generalOptions.isForced() || actionResult.getResult() != Result.ERROR, "Feedback migration '%s' action '%s' returned error: %s. Aborting execution.", name, action.getName(), actionResult.getMsg());
                } catch (NonFastForwardRepositoryException e) {
                    allResultsBuilder.add(ActionResult.error(action.getName() + ": " + e.getMessage()));
                    if (!generalOptions.isForced()) {
                        throw e;
                    }
                    logger.atWarning().withCause(e).log();
                } finally {
                    generalOptions.eventMonitors().dispatchEvent(m -> m.onChangeMigrationFinished(new ChangeMigrationFinishedEvent(ImmutableList.copyOf(context.getNewDestinationEffects()), getOriginDescription(), getDestinationDescription())));
                }
            }
            ImmutableList<ActionResult> allResults = allResultsBuilder.build();
            if (allResults.stream().anyMatch(a -> a.getResult() == Result.ERROR)) {
                String errors = allResults.stream().filter(a -> a.getResult() == Result.ERROR).map(ActionResult::getMsg).collect(Collectors.joining("\n - "));
                throw new ValidationException("One or more errors happened during the migration:\n" + " - " + errors);
            }
            // 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("git.mirror migration '%s' was noop. Detailed messages: %s", name, detailedMessage));
            }
        }
    }
    // More fine grain events based on the references created/updated/deleted:
    ChangeMigrationFinishedEvent event = new ChangeMigrationFinishedEvent(ImmutableList.of(new DestinationEffect(generalOptions.dryRunMode ? DestinationEffect.Type.NOOP : DestinationEffect.Type.UPDATED, generalOptions.dryRunMode ? "Refspecs " + refspec + " can be mirrored" : "Refspecs " + refspec + " mirrored successfully", // TODO(danielromero): Populate OriginRef here
    ImmutableList.of(), new DestinationRef(getOriginDestinationRef(destination), "mirror", /*url=*/
    null))), getOriginDescription(), getDestinationDescription());
    generalOptions.eventMonitors().dispatchEvent(m -> m.onChangeMigrationFinished(event));
}
Also used : Action(com.google.copybara.action.Action) ValidationException(com.google.copybara.exception.ValidationException) ChangeMigrationFinishedEvent(com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent) ImmutableList(com.google.common.collect.ImmutableList) SkylarkConsole(com.google.copybara.transform.SkylarkConsole) ActionResult(com.google.copybara.action.ActionResult) DestinationRef(com.google.copybara.DestinationEffect.DestinationRef) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) DestinationEffect(com.google.copybara.DestinationEffect) EmptyChangeException(com.google.copybara.exception.EmptyChangeException)

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