Search in sources :

Example 1 with TransformResult

use of com.google.copybara.TransformResult in project copybara by google.

the class GithubPrDestination method newWriter.

@Override
public Writer<GitRevision> newWriter(Glob destinationFiles, boolean dryRun, @Nullable String groupId, @Nullable Writer<GitRevision> oldWriter) throws ValidationException {
    WriterImpl gitOldWriter = (WriterImpl) oldWriter;
    boolean effectiveSkipPush = GithubPrDestination.this.effectiveSkipPush || dryRun;
    GithubWriterState state;
    String pushBranchName = branchFromGroupId(groupId);
    if (oldWriter != null && gitOldWriter.skipPush == effectiveSkipPush) {
        state = (GithubWriterState) ((WriterImpl) oldWriter).state;
    } else {
        state = new GithubWriterState(localRepo, destinationOptions.localRepoPath != null ? pushBranchName : "copybara/push-" + UUID.randomUUID() + (dryRun ? "-dryrun" : ""));
    }
    return new WriterImpl<GithubWriterState>(destinationFiles, effectiveSkipPush, url, destinationRef, pushBranchName, generalOptions, commitGenerator, processPushOutput, state, /*nonFastForwardPush=*/
    true, integrates, destinationOptions.lastRevFirstParent, destinationOptions.ignoreIntegrationErrors, destinationOptions.localRepoPath, destinationOptions.committerName, destinationOptions.committerEmail, destinationOptions.rebaseWhenBaseline(), gitOptions.visitChangePageSize) {

        @Override
        public ImmutableList<DestinationEffect> write(TransformResult transformResult, Console console) throws ValidationException, RepoException, IOException {
            ImmutableList.Builder<DestinationEffect> result = ImmutableList.<DestinationEffect>builder().addAll(super.write(transformResult, console));
            if (effectiveSkipPush || state.pullRequestNumber != null) {
                return result.build();
            }
            if (!githubDestinationOptions.createPullRequest) {
                console.infoFmt("Please create a PR manually following this link: %s/compare/%s...%s" + " (Only needed once)", asHttpsUrl(), destinationRef, pushBranchName);
                state.pullRequestNumber = -1L;
                return result.build();
            }
            GithubApi api = githubOptions.getApi(GithubUtil.getProjectNameFromUrl(url));
            for (PullRequest pr : api.getPullRequests(getProjectName())) {
                if (pr.isOpen() && pr.getHead().getRef().equals(pushBranchName)) {
                    console.infoFmt("Pull request for branch %s already exists as %s/pull/%s", pushBranchName, asHttpsUrl(), pr.getNumber());
                    if (!pr.getBase().getRef().equals(destinationRef)) {
                        // TODO(malcon): Update PR or create a new one?
                        console.warnFmt("Current base branch '%s' is different from the PR base branch '%s'", destinationRef, pr.getBase().getRef());
                    }
                    result.add(new DestinationEffect(DestinationEffect.Type.UPDATED, String.format("Pull Request %s updated", pr.getHtmlUrl()), transformResult.getChanges().getCurrent(), new DestinationEffect.DestinationRef(Long.toString(pr.getNumber()), "pull_request", pr.getHtmlUrl()), ImmutableList.of()));
                    return result.build();
                }
            }
            ChangeMessage msg = ChangeMessage.parseMessage(transformResult.getSummary());
            PullRequest pr = api.createPullRequest(getProjectName(), new CreatePullRequest(title == null ? msg.firstLine() : title, body == null ? msg.getText() : body, pushBranchName, destinationRef));
            console.infoFmt("Pull Request %s/pull/%s created using branch '%s'.", asHttpsUrl(), pr.getNumber(), pushBranchName);
            state.pullRequestNumber = pr.getNumber();
            result.add(new DestinationEffect(DestinationEffect.Type.CREATED, String.format("Pull Request %s created", pr.getHtmlUrl()), transformResult.getChanges().getCurrent(), new DestinationEffect.DestinationRef(Long.toString(pr.getNumber()), "pull_request", pr.getHtmlUrl()), ImmutableList.of()));
            return result.build();
        }
    };
}
Also used : TransformResult(com.google.copybara.TransformResult) ImmutableList(com.google.common.collect.ImmutableList) CreatePullRequest(com.google.copybara.git.github.api.CreatePullRequest) PullRequest(com.google.copybara.git.github.api.PullRequest) GithubApi(com.google.copybara.git.github.api.GithubApi) CreatePullRequest(com.google.copybara.git.github.api.CreatePullRequest) WriterImpl(com.google.copybara.git.GitDestination.WriterImpl) DestinationEffect(com.google.copybara.DestinationEffect) Console(com.google.copybara.util.console.Console) ChangeMessage(com.google.copybara.ChangeMessage)

Example 2 with TransformResult

use of com.google.copybara.TransformResult in project copybara by google.

the class GitDestinationTest method processWithBaselineAndConfirmation.

private void processWithBaselineAndConfirmation(Writer<GitRevision> writer, DummyRevision originRef, String baseline, boolean askForConfirmation) throws ValidationException, RepoException, IOException {
    TransformResult result = TransformResults.of(workdir, originRef);
    if (baseline != null) {
        result = result.withBaseline(baseline);
    }
    if (askForConfirmation) {
        result = result.withAskForConfirmation(true);
    }
    ImmutableList<DestinationEffect> destinationResult = writer.write(result, console);
    assertThat(destinationResult).hasSize(1);
    assertThat(destinationResult.get(0).getErrors()).isEmpty();
    assertThat(destinationResult.get(0).getType()).isEqualTo(Type.CREATED);
    assertThat(destinationResult.get(0).getDestinationRef().getType()).isEqualTo("commit");
    assertThat(destinationResult.get(0).getDestinationRef().getId()).matches("[0-9a-f]{40}");
}
Also used : TransformResult(com.google.copybara.TransformResult) DestinationEffect(com.google.copybara.DestinationEffect)

Example 3 with TransformResult

use of com.google.copybara.TransformResult in project copybara by google.

the class GitHubPrDestination method newWriter.

@Override
public Writer<GitRevision> newWriter(WriterContext writerContext) throws ValidationException {
    String prBranch = getPullRequestBranchName(writerContext.getOriginalRevision(), writerContext.getWorkflowName(), writerContext.getWorkflowIdentityUser());
    GitHubPrWriteHook gitHubPrWriteHook = writeHook.withUpdatedPrBranch(prBranch);
    GitHubWriterState state = new GitHubWriterState(localRepo, destinationOptions.localRepoPath != null ? prBranch : "copybara/push-" + UUID.randomUUID() + (writerContext.isDryRun() ? "-dryrun" : ""));
    return new WriterImpl<GitHubWriterState>(writerContext.isDryRun(), url, getDestinationRef(), prBranch, partialFetch, /*tagName*/
    null, /*tagMsg*/
    null, generalOptions, gitHubPrWriteHook, state, /*nonFastForwardPush=*/
    true, integrates, destinationOptions.lastRevFirstParent, destinationOptions.ignoreIntegrationErrors, destinationOptions.localRepoPath, destinationOptions.committerName, destinationOptions.committerEmail, destinationOptions.rebaseWhenBaseline(), gitOptions.visitChangePageSize, gitOptions.gitTagOverwrite, checker) {

        @Override
        public ImmutableList<DestinationEffect> write(TransformResult transformResult, Glob destinationFiles, Console console) throws ValidationException, RepoException, IOException {
            ImmutableList.Builder<DestinationEffect> result = ImmutableList.<DestinationEffect>builder().addAll(super.write(transformResult, destinationFiles, console));
            if (writerContext.isDryRun() || state.pullRequestNumber != null) {
                return result.build();
            }
            if (!gitHubDestinationOptions.createPullRequest) {
                console.infoFmt("Please create a PR manually following this link: %s/compare/%s...%s" + " (Only needed once)", asHttpsUrl(), getDestinationRef(), prBranch);
                state.pullRequestNumber = -1L;
                return result.build();
            }
            GitHubApi api = gitHubOptions.newGitHubApi(getProjectName());
            ImmutableList<PullRequest> pullRequests = api.getPullRequests(getProjectName(), PullRequestListParams.DEFAULT.withHead(String.format("%s:%s", ghHost.getUserNameFromUrl(url), prBranch)));
            ChangeMessage msg = ChangeMessage.parseMessage(transformResult.getSummary().trim());
            String title = GitHubPrDestination.this.title == null ? msg.firstLine() : SkylarkUtil.mapLabels(transformResult.getLabelFinder(), GitHubPrDestination.this.title, "title");
            String prBody = GitHubPrDestination.this.body == null ? msg.toString() : SkylarkUtil.mapLabels(transformResult.getLabelFinder(), GitHubPrDestination.this.body, "body");
            for (PullRequest pr : pullRequests) {
                if (pr.getHead().getRef().equals(prBranch)) {
                    if (!pr.isOpen()) {
                        console.warnFmt("Pull request for branch %s already exists as %s/pull/%s, but is closed - " + "reopening.", prBranch, asHttpsUrl(), pr.getNumber());
                        api.updatePullRequest(getProjectName(), pr.getNumber(), new UpdatePullRequest(null, null, OPEN));
                    } else {
                        console.infoFmt("Pull request for branch %s already exists as %s/pull/%s", prBranch, asHttpsUrl(), pr.getNumber());
                    }
                    if (!pr.getBase().getRef().equals(getDestinationRef())) {
                        // TODO(malcon): Update PR or create a new one?
                        console.warnFmt("Current base branch '%s' is different from the PR base branch '%s'", getDestinationRef(), pr.getBase().getRef());
                    }
                    if (updateDescription) {
                        checkCondition(!Strings.isNullOrEmpty(title), "Pull Request title cannot be empty. Either use 'title' field in" + " git.github_pr_destination or modify the message to not be empty");
                        api.updatePullRequest(getProjectName(), pr.getNumber(), new UpdatePullRequest(title, prBody, /*state=*/
                        null));
                    }
                    result.add(new DestinationEffect(DestinationEffect.Type.UPDATED, String.format("Pull Request %s updated", pr.getHtmlUrl()), transformResult.getChanges().getCurrent(), new DestinationEffect.DestinationRef(Long.toString(pr.getNumber()), "pull_request", pr.getHtmlUrl())));
                    return result.build();
                }
            }
            checkCondition(!Strings.isNullOrEmpty(title), "Pull Request title cannot be empty. Either use 'title' field in" + " git.github_pr_destination or modify the message to not be empty");
            PullRequest pr = api.createPullRequest(getProjectName(), new CreatePullRequest(title, prBody, prBranch, getDestinationRef()));
            console.infoFmt("Pull Request %s/pull/%s created using branch '%s'.", asHttpsUrl(), pr.getNumber(), prBranch);
            state.pullRequestNumber = pr.getNumber();
            result.add(new DestinationEffect(DestinationEffect.Type.CREATED, String.format("Pull Request %s created", pr.getHtmlUrl()), transformResult.getChanges().getCurrent(), new DestinationEffect.DestinationRef(Long.toString(pr.getNumber()), "pull_request", pr.getHtmlUrl())));
            return result.build();
        }

        @Override
        public Endpoint getFeedbackEndPoint(Console console) throws ValidationException {
            gitHubOptions.validateEndpointChecker(endpointChecker);
            return new GitHubEndPoint(gitHubOptions.newGitHubApiSupplier(url, endpointChecker, ghHost), url, console, ghHost);
        }
    };
}
Also used : TransformResult(com.google.copybara.TransformResult) ImmutableList(com.google.common.collect.ImmutableList) CreatePullRequest(com.google.copybara.git.github.api.CreatePullRequest) UpdatePullRequest(com.google.copybara.git.github.api.UpdatePullRequest) PullRequest(com.google.copybara.git.github.api.PullRequest) CreatePullRequest(com.google.copybara.git.github.api.CreatePullRequest) WriterImpl(com.google.copybara.git.GitDestination.WriterImpl) GitHubApi(com.google.copybara.git.github.api.GitHubApi) DestinationEffect(com.google.copybara.DestinationEffect) Console(com.google.copybara.util.console.Console) ChangeMessage(com.google.copybara.ChangeMessage) UpdatePullRequest(com.google.copybara.git.github.api.UpdatePullRequest) Glob(com.google.copybara.util.Glob)

Example 4 with TransformResult

use of com.google.copybara.TransformResult in project copybara by google.

the class GitDestinationTest method processWithBaselineAndConfirmation.

private void processWithBaselineAndConfirmation(Writer<GitRevision> writer, Glob destinationFiles, DummyRevision originRef, String baseline, boolean askForConfirmation) throws ValidationException, RepoException, IOException {
    TransformResult result = TransformResults.of(workdir, originRef);
    if (baseline != null) {
        result = result.withBaseline(baseline);
    }
    if (askForConfirmation) {
        result = result.withAskForConfirmation(true);
    }
    ImmutableList<DestinationEffect> destinationResult = writer.write(result, destinationFiles, console);
    assertThat(destinationResult).hasSize(1);
    assertThat(destinationResult.get(0).getErrors()).isEmpty();
    assertThat(destinationResult.get(0).getType()).isEqualTo(Type.CREATED);
    assertThat(destinationResult.get(0).getDestinationRef().getType()).isEqualTo("commit");
    assertThat(destinationResult.get(0).getDestinationRef().getId()).matches("[0-9a-f]{40}");
}
Also used : TransformResult(com.google.copybara.TransformResult) DestinationEffect(com.google.copybara.DestinationEffect)

Example 5 with TransformResult

use of com.google.copybara.TransformResult in project copybara by google.

the class GitHubDestinationTest method testPrToUpdateIngoredForInitHistory.

@Test
public void testPrToUpdateIngoredForInitHistory() throws Exception {
    options.workflowOptions.initHistory = true;
    addFiles(remote, primaryBranch, "first change", ImmutableMap.<String, String>builder().put("foo.txt", "foo").buildOrThrow());
    WriterContext writerContext = new WriterContext("piper_to_github", "test", false, new DummyRevision("origin_ref1"), Glob.ALL_FILES.roots());
    writeFile(this.workdir, "test.txt", "some content");
    Writer<GitRevision> writer = destinationWithExistingPrBranch("other", "True").newWriter(writerContext);
    DummyRevision ref = new DummyRevision("origin_ref1");
    TransformResult result = TransformResults.of(workdir, ref);
    Changes changes = new Changes(ImmutableList.of(new Change<>(ref, new Author("foo", "foo@foo.com"), "message", ZonedDateTime.now(ZoneOffset.UTC), ImmutableListMultimap.of("my_label", "12345")), new Change<>(ref, new Author("foo", "foo@foo.com"), "message", ZonedDateTime.now(ZoneOffset.UTC), ImmutableListMultimap.of("my_label", "6789"))), ImmutableList.of());
    result = result.withChanges(changes);
    ImmutableList<DestinationEffect> destinationResult = writer.write(result, destinationFiles, console);
    assertThat(destinationResult).hasSize(1);
    assertThat(destinationResult.get(0).getErrors()).isEmpty();
    assertThat(destinationResult.get(0).getType()).isEqualTo(Type.CREATED);
    assertThat(destinationResult.get(0).getDestinationRef().getType()).isEqualTo("commit");
    assertThat(destinationResult.get(0).getDestinationRef().getId()).matches("[0-9a-f]{40}");
    // This is a migration of two changes (use the same ref because mocks)
    verifyNoInteractions(gitUtil.httpTransport());
    GitTesting.assertThatCheckout(remote, primaryBranch).containsFile("test.txt", "some content").containsNoMoreFiles();
    assertThat(remote.simpleCommand("show-ref").getStdout()).doesNotContain("other");
}
Also used : Changes(com.google.copybara.Changes) WriterContext(com.google.copybara.WriterContext) TransformResult(com.google.copybara.TransformResult) DestinationEffect(com.google.copybara.DestinationEffect) DummyRevision(com.google.copybara.testing.DummyRevision) Author(com.google.copybara.authoring.Author) Change(com.google.copybara.Change) Test(org.junit.Test)

Aggregations

TransformResult (com.google.copybara.TransformResult)16 DummyRevision (com.google.copybara.testing.DummyRevision)11 DestinationEffect (com.google.copybara.DestinationEffect)10 Test (org.junit.Test)8 Author (com.google.copybara.authoring.Author)5 Change (com.google.copybara.Change)4 Changes (com.google.copybara.Changes)4 WriterContext (com.google.copybara.WriterContext)4 ImmutableList (com.google.common.collect.ImmutableList)2 ChangeMessage (com.google.copybara.ChangeMessage)2 WriterImpl (com.google.copybara.git.GitDestination.WriterImpl)2 CreatePullRequest (com.google.copybara.git.github.api.CreatePullRequest)2 PullRequest (com.google.copybara.git.github.api.PullRequest)2 HgLogEntry (com.google.copybara.hg.HgRepository.HgLogEntry)2 Console (com.google.copybara.util.console.Console)2 CheckerException (com.google.copybara.checks.CheckerException)1 EmptyChangeException (com.google.copybara.exception.EmptyChangeException)1 RepoException (com.google.copybara.exception.RepoException)1 GitHubApi (com.google.copybara.git.github.api.GitHubApi)1 GithubApi (com.google.copybara.git.github.api.GithubApi)1