Search in sources :

Example 21 with EmptyChangeException

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

the class GitRepositoryTest method testEmptyCommitNoBaseline.

@Test
public void testEmptyCommitNoBaseline() throws Exception {
    GitRepository bare = GitRepository.newBareRepo(gitDir, getGitEnv(), /*verbose=*/
    true, DEFAULT_TIMEOUT, /*noVerify=*/
    false);
    EmptyChangeException expected = assertThrows(EmptyChangeException.class, () -> bare.commit("Copybara", ZonedDateTime.now(ZoneId.of("-07:00")), "this should be empty"));
    assertThat(expected).hasMessageThat().contains("Migration of the revision resulted in an empty change from baseline 'unknown'.");
}
Also used : EmptyChangeException(com.google.copybara.exception.EmptyChangeException) Test(org.junit.Test)

Example 22 with EmptyChangeException

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

the class GithubPROrigin method getRevisionForPR.

private GitRevision getRevisionForPR(String project, int prNumber) throws RepoException, ValidationException {
    if (!requiredLabels.isEmpty()) {
        int retryCount = 0;
        Set<String> requiredButNotPresent;
        do {
            Issue issue;
            try (ProfilerTask ignore = generalOptions.profiler().start("github_api_get_issue")) {
                issue = githubOptions.getApi(project).getIssue(project, prNumber);
            }
            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, prNumber, requiredButNotPresent));
        }
    }
    PullRequest prData;
    try (ProfilerTask ignore = generalOptions.profiler().start("github_api_get_pr")) {
        prData = githubOptions.getApi(project).getPullRequest(project, prNumber);
    }
    if (requiredState == StateFilter.OPEN && !prData.isOpen()) {
        throw new EmptyChangeException(String.format("Pull Request %d is not open", prNumber));
    }
    if (requiredState == StateFilter.CLOSED && prData.isOpen()) {
        throw new EmptyChangeException(String.format("Pull Request %d is open", prNumber));
    }
    String stableRef = useMerge ? GithubUtil.asMergeRef(prNumber) : GithubUtil.asHeadRef(prNumber);
    // 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());
    try {
        getRepository().fetch(asGithubUrl(project), /*prune=*/
        false, /*force=*/
        true, ImmutableList.of(stableRef + ":refs/PR_HEAD", // GitRepository need the whole reference name.
        "refs/heads/" + prData.getBase().getRef() + ":refs/PR_BASE_BRANCH"));
    } catch (CannotResolveRevisionException e) {
        if (useMerge) {
            throw new CannotResolveRevisionException(String.format("Cannot find a merge reference for Pull Request %d." + " It might have a conflict with head.", prNumber), e);
        } else {
            throw new CannotResolveRevisionException(String.format("Cannot find Pull Request %d.", prNumber), e);
        }
    }
    GitRevision gitRevision = getRepository().resolveReference("PR_HEAD");
    String integrateLabel = new GithubPRIntegrateLabel(getRepository(), generalOptions, project, prNumber, prData.getHead().getLabel(), gitRevision.getSha1()).toString();
    ImmutableMap.Builder<String, String> labels = ImmutableMap.builder();
    labels.put(GITHUB_PR_NUMBER_LABEL, Integer.toString(prNumber));
    labels.put(GitModule.DEFAULT_INTEGRATE_LABEL, integrateLabel);
    labels.put(GITHUB_BASE_BRANCH, prData.getBase().getRef());
    String mergeBase = getRepository().mergeBase("refs/PR_HEAD", "refs/PR_BASE_BRANCH");
    labels.put(GITHUB_BASE_BRANCH_SHA1, mergeBase);
    labels.put(GITHUB_PR_TITLE, prData.getTitle());
    labels.put(GITHUB_PR_BODY, prData.getBody());
    return new GitRevision(getRepository(), gitRevision.getSha1(), /*reviewReference=*/
    null, stableRef, labels.build(), url);
}
Also used : Issue(com.google.copybara.git.github.api.Issue) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) PullRequest(com.google.copybara.git.github.api.PullRequest) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) Endpoint(com.google.copybara.Endpoint) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 23 with EmptyChangeException

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

the class GithubPROrigin method newReader.

@Override
public Reader<GitRevision> newReader(Glob originFiles, Authoring authoring) throws ValidationException {
    return new ReaderImpl(url, originFiles, authoring, gitOptions, gitOriginOptions, generalOptions, /*includeBranchCommitLogs=*/
    false, submoduleStrategy, firstParent) {

        /**
         * Disable rebase since this is controlled by useMerge field.
         */
        @Override
        protected void maybeRebase(GitRepository repo, GitRevision ref, Path workdir) throws RepoException, CannotResolveRevisionException {
        }

        @Override
        public Optional<Baseline<GitRevision>> findBaseline(GitRevision startRevision, String label) throws RepoException, ValidationException {
            if (!baselineFromBranch) {
                return super.findBaseline(startRevision, label);
            }
            return findBaselinesWithoutLabel(startRevision, /*limit=*/
            1).stream().map(e -> new Baseline<>(e.getSha1(), e)).findFirst();
        }

        @Override
        public ImmutableList<GitRevision> findBaselinesWithoutLabel(GitRevision startRevision, int limit) throws RepoException, ValidationException {
            String baseline = startRevision.associatedLabels().get(GITHUB_BASE_BRANCH_SHA1);
            Preconditions.checkNotNull(baseline, "%s label should be present in %s", GITHUB_BASE_BRANCH_SHA1, startRevision);
            GitRevision baselineRev = getRepository().resolveReference(baseline);
            // Don't skip the first change as it is already the baseline
            BaselinesWithoutLabelVisitor<GitRevision> visitor = new BaselinesWithoutLabelVisitor<>(originFiles, limit, /*skipFirst=*/
            false);
            visitChanges(baselineRev, visitor);
            return visitor.getResult();
        }

        @Override
        public Endpoint getFeedbackEndPoint() {
            return new GitHubEndPoint(githubOptions, url);
        }

        /**
         * Deal with the case of useMerge. We have a new commit (the merge) and first-parent from that
         * commit doesn't work for this case.
         */
        @Override
        public ChangesResponse<GitRevision> changes(@Nullable GitRevision fromRef, GitRevision toRef) throws RepoException {
            if (!useMerge) {
                return super.changes(fromRef, toRef);
            }
            GitLogEntry merge = Iterables.getOnlyElement(getRepository().log(toRef.getSha1()).withLimit(1).run());
            // Fast-forward merge
            if (merge.getParents().size() == 1) {
                return super.changes(fromRef, toRef);
            }
            // HEAD of the Pull Request
            GitRevision gitRevision = merge.getParents().get(1);
            ChangesResponse<GitRevision> prChanges = super.changes(fromRef, gitRevision);
            // origin_files
            if (prChanges.isEmpty()) {
                return prChanges;
            }
            try {
                return ChangesResponse.forChanges(ImmutableList.<Change<GitRevision>>builder().addAll(prChanges.getChanges()).add(change(merge.getCommit())).build());
            } catch (EmptyChangeException e) {
                throw new RepoException("Error getting the merge commit information: " + merge, e);
            }
        }

        @Nullable
        @Override
        public String getGroupIdentity(GitRevision rev) throws RepoException {
            return rev.associatedLabels().get(GITHUB_PR_NUMBER_LABEL);
        }
    };
}
Also used : Path(java.nio.file.Path) Iterables(com.google.common.collect.Iterables) Origin(com.google.copybara.Origin) ValidationException.checkCondition(com.google.copybara.exception.ValidationException.checkCondition) RepoException(com.google.copybara.exception.RepoException) Collections2(com.google.common.collect.Collections2) SubmoduleStrategy(com.google.copybara.git.GitOrigin.SubmoduleStrategy) Matcher(java.util.regex.Matcher) ImmutableList(com.google.common.collect.ImmutableList) Change(com.google.copybara.Change) BaselinesWithoutLabelVisitor(com.google.copybara.BaselinesWithoutLabelVisitor) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) Endpoint(com.google.copybara.Endpoint) Issue(com.google.copybara.git.github.api.Issue) Splitter(com.google.common.base.Splitter) GeneralOptions(com.google.copybara.GeneralOptions) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry) Label(com.google.copybara.git.github.api.Issue.Label) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) ImmutableMap(com.google.common.collect.ImmutableMap) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) CharMatcher(com.google.common.base.CharMatcher) ValidationException(com.google.copybara.exception.ValidationException) ReaderImpl(com.google.copybara.git.GitOrigin.ReaderImpl) Set(java.util.Set) Console(com.google.copybara.util.console.Console) Sets(com.google.common.collect.Sets) GithubUtil.getProjectNameFromUrl(com.google.copybara.git.github.util.GithubUtil.getProjectNameFromUrl) TimeUnit(java.util.concurrent.TimeUnit) Authoring(com.google.copybara.authoring.Authoring) Glob(com.google.copybara.util.Glob) PullRequest(com.google.copybara.git.github.api.PullRequest) GithubUtil.asGithubUrl(com.google.copybara.git.github.util.GithubUtil.asGithubUrl) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) GithubUtil(com.google.copybara.git.github.util.GithubUtil) Collections(java.util.Collections) GithubPrUrl(com.google.copybara.git.github.util.GithubUtil.GithubPrUrl) ReaderImpl(com.google.copybara.git.GitOrigin.ReaderImpl) Change(com.google.copybara.Change) RepoException(com.google.copybara.exception.RepoException) BaselinesWithoutLabelVisitor(com.google.copybara.BaselinesWithoutLabelVisitor) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) Nullable(javax.annotation.Nullable) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry)

Example 24 with EmptyChangeException

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

the class WorkflowRunHelper method migrate.

/**
 * Performs a full migration, including checking out files from the origin, deleting excluded
 * files, transforming the code, and writing to the destination. This writes to the destination
 * exactly once.
 *
 * @param rev revision to the version which will be written to the destination
 * @param lastRev last revision that was migrated
 * @param processConsole console to use to print progress messages
 * @param metadata metadata of the change to be migrated
 * @param changes changes included in this migration
 * @param destinationBaseline it not null, use this baseline in the destination
 * @param changeIdentityRevision the revision to be used for computing the change identity
 */
ImmutableList<DestinationEffect> migrate(O rev, @Nullable O lastRev, Console processConsole, Metadata metadata, Changes changes, @Nullable String destinationBaseline, @Nullable O changeIdentityRevision) throws IOException, RepoException, ValidationException {
    ImmutableList<DestinationEffect> effects = ImmutableList.of();
    boolean callPerMigrationHook = true;
    try {
        eventMonitor().onChangeMigrationStarted(new ChangeMigrationStartedEvent());
        effects = doMigrate(rev, lastRev, processConsole, metadata, changes, destinationBaseline, changeIdentityRevision);
        return effects;
    } catch (EmptyChangeException empty) {
        effects = ImmutableList.of(new DestinationEffect(Type.NOOP, empty.getMessage(), changes.getCurrent(), /*destinationRef=*/
        null, ImmutableList.of()));
        throw empty;
    } catch (ValidationException | IOException | RepoException | RuntimeException e) {
        effects = ImmutableList.of(new DestinationEffect(Type.ERROR, "Errors happened during the migration", changes.getCurrent(), /*destinationRef=*/
        null, ImmutableList.of(e.getMessage() != null ? e.getMessage() : e.toString())));
        callPerMigrationHook = e instanceof ValidationException;
        throw e;
    } finally {
        eventMonitor().onChangeMigrationFinished(new ChangeMigrationFinishedEvent(effects));
        if (callPerMigrationHook) {
            FinishHookContext finishHookContext = new FinishHookContext(getOriginReader().getFeedbackEndPoint(), getDestinationWriter().getFeedbackEndPoint(), effects, resolvedRef, new SkylarkConsole(getConsole()));
            try (ProfilerTask ignored = profiler().start("finish_hooks")) {
                for (Action action : workflow.getAfterMigrationActions()) {
                    try (ProfilerTask ignored2 = profiler().start(action.getName())) {
                        logger.log(Level.INFO, "Running after migration hook: " + action.getName());
                        action.run(finishHookContext);
                    }
                }
            }
        }
    }
}
Also used : Action(com.google.copybara.feedback.Action) ValidationException(com.google.copybara.exception.ValidationException) ChangeMigrationFinishedEvent(com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent) FinishHookContext(com.google.copybara.feedback.FinishHookContext) IOException(java.io.IOException) RepoException(com.google.copybara.exception.RepoException) SkylarkConsole(com.google.copybara.transform.SkylarkConsole) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) ChangeMigrationStartedEvent(com.google.copybara.monitor.EventMonitor.ChangeMigrationStartedEvent) EmptyChangeException(com.google.copybara.exception.EmptyChangeException)

Example 25 with EmptyChangeException

use of com.google.copybara.exception.EmptyChangeException 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)

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