Search in sources :

Example 1 with DestinationEffect

use of com.google.copybara.DestinationEffect 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 DestinationEffect

use of com.google.copybara.DestinationEffect 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 DestinationEffect

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

the class GerritDestinationTest method testGerritSubmit.

@Test
public void testGerritSubmit() throws Exception {
    options.gerrit.gerritChangeId = null;
    fetch = "master";
    pushToRefsFor = "master";
    Files.write(workdir.resolve("file"), "some content".getBytes());
    options.setForce(true);
    url = "https://localhost:33333/foo/bar";
    GitRepository repo = localGerritRepo("localhost:33333/foo/bar");
    gitApiMockHttpTransport = NO_CHANGE_FOUND_MOCK;
    DummyRevision originRef = new DummyRevision("origin_ref");
    GerritDestination destination = destination("submit = True");
    Glob glob = Glob.createGlob(ImmutableList.of("**"), excludedDestinationPaths);
    List<DestinationEffect> result = destination.newWriter(glob, /*dryRun=*/
    false, /*groupId=*/
    null, /*oldWriter=*/
    null).write(TransformResults.of(workdir, originRef).withSummary("Test message").withIdentity(originRef.asString()), console);
    assertThat(result).hasSize(1);
    assertThat(result.get(0).getErrors()).isEmpty();
    String changeId = lastCommitChangeIdLineForRef("master", "origin_ref", repo).replace("Change-Id: ", "").trim();
    assertThat(changeId).isNotNull();
    assertThat(destination.getType()).isEqualTo("git.destination");
    assertThat(destination.describe(glob).get("fetch")).isEqualTo(ImmutableSet.of("master"));
    assertThat(destination.describe(glob).get("push")).isEqualTo(ImmutableSet.of("master"));
}
Also used : DestinationEffect(com.google.copybara.DestinationEffect) DummyRevision(com.google.copybara.testing.DummyRevision) Glob(com.google.copybara.util.Glob) Test(org.junit.Test)

Example 4 with DestinationEffect

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

the class SubmodulesInDestinationTest method writeWithSubmoduleInDestination.

private void writeWithSubmoduleInDestination() throws Exception {
    fetch = "master";
    push = "master";
    Path scratchTree = Files.createTempDirectory("SubmodulesInDestinationTest-scratchTree");
    GitRepository scratchRepo = repo().withWorkTree(scratchTree);
    scratchRepo.simpleCommand("submodule", "add", "file://" + submodule.getGitDir(), "submodule");
    scratchRepo.simpleCommand("commit", "-m", "commit submodule");
    Files.write(workdir.resolve("test42"), new byte[] { 42 });
    Destination.Writer<?> writer = destination().newWriter(destinationFiles, /*dryRun=*/
    false, /*groupId=*/
    null, /*oldWriter=*/
    null);
    ImmutableList<DestinationEffect> result = writer.write(TransformResults.of(workdir, new DummyRevision("ref1")), console);
    assertThat(result).hasSize(1);
    assertThat(result.get(0).getErrors()).isEmpty();
    assertThat(result.get(0).getType()).isEqualTo(Type.CREATED);
    assertThat(result.get(0).getDestinationRef().getType()).isEqualTo("commit");
    assertThat(result.get(0).getDestinationRef().getId()).matches("[0-9a-f]{40}");
}
Also used : Path(java.nio.file.Path) Destination(com.google.copybara.Destination) DestinationEffect(com.google.copybara.DestinationEffect) DummyRevision(com.google.copybara.testing.DummyRevision)

Example 5 with DestinationEffect

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

the class SubmodulesInDestinationTest method submoduleInSubdirectoryWithSiblingFiles.

@Test
public void submoduleInSubdirectoryWithSiblingFiles() throws Exception {
    destinationFiles = Glob.createGlob(ImmutableList.of("foo/a", "foo/c"));
    fetch = "master";
    push = "master";
    Path scratchTree = Files.createTempDirectory("SubmodulesInDestinationTest-scratchTree");
    GitRepository scratchRepo = repo().withWorkTree(scratchTree);
    Files.createDirectories(scratchTree.resolve("foo"));
    Files.write(scratchTree.resolve("foo/a"), new byte[] { 1 });
    scratchRepo.add().files("foo/a").run();
    scratchRepo.simpleCommand("submodule", "add", "file://" + submodule.getGitDir(), "foo/b");
    scratchRepo.simpleCommand("commit", "-m", "commit submodule and foo/a");
    // Create a commit that removes foo/a and adds foo/c
    Files.createDirectories(workdir.resolve("foo"));
    Files.write(workdir.resolve("foo/c"), new byte[] { 1 });
    Destination.Writer<?> writer = destination().newWriter(destinationFiles, /*dryRun=*/
    false, /*groupId=*/
    null, /*oldWriter=*/
    null);
    ImmutableList<DestinationEffect> result = writer.write(TransformResults.of(workdir, new DummyRevision("ref1")), console);
    assertThat(result).hasSize(1);
    assertThat(result.get(0).getErrors()).isEmpty();
    assertThat(result.get(0).getType()).isEqualTo(Type.CREATED);
    assertThat(result.get(0).getDestinationRef().getType()).isEqualTo("commit");
    assertThat(result.get(0).getDestinationRef().getId()).matches("[0-9a-f]{40}");
    GitTesting.assertThatCheckout(repo(), "master").containsFiles("foo/c", "foo/b").containsNoFiles("foo/a");
}
Also used : Path(java.nio.file.Path) Destination(com.google.copybara.Destination) DestinationEffect(com.google.copybara.DestinationEffect) DummyRevision(com.google.copybara.testing.DummyRevision) Test(org.junit.Test)

Aggregations

DestinationEffect (com.google.copybara.DestinationEffect)10 DummyRevision (com.google.copybara.testing.DummyRevision)5 Test (org.junit.Test)5 Destination (com.google.copybara.Destination)2 TransformResult (com.google.copybara.TransformResult)2 GerritProcessPushOutput (com.google.copybara.git.GerritDestination.GerritProcessPushOutput)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 PrintStream (java.io.PrintStream)2 Path (java.nio.file.Path)2 ImmutableList (com.google.common.collect.ImmutableList)1 ChangeMessage (com.google.copybara.ChangeMessage)1 DestinationRef (com.google.copybara.DestinationEffect.DestinationRef)1 WriterImpl (com.google.copybara.git.GitDestination.WriterImpl)1 CreatePullRequest (com.google.copybara.git.github.api.CreatePullRequest)1 GithubApi (com.google.copybara.git.github.api.GithubApi)1 PullRequest (com.google.copybara.git.github.api.PullRequest)1 ChangeMigrationFinishedEvent (com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent)1 Glob (com.google.copybara.util.Glob)1 Console (com.google.copybara.util.console.Console)1