Search in sources :

Example 56 with WriterContext

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

the class GitHubPrDestinationTest method checkWrite.

private void checkWrite(Revision revision) throws ValidationException, RepoException, IOException {
    mockNoPullRequestsGet("feature");
    gitUtil.mockApi("POST", "https://api.github.com/repos/foo/pulls", mockResponseAndValidateRequest("{\n" + "  \"id\": 1,\n" + "  \"number\": 12345,\n" + "  \"state\": \"open\",\n" + "  \"title\": \"test summary\",\n" + "  \"body\": \"test summary\"\n" + "}", MockRequestAssertion.equals("{\"base\":\"main\",\"body\":\"test summary\\n\",\"head\":\"" + "feature" + "\",\"title\":\"test summary\"}")));
    GitHubPrDestination d = skylark.eval("r", "r = git.github_pr_destination(" + "    url = 'https://github.com/foo'," + "    destination_ref = 'main'" + ")");
    Writer<GitRevision> writer = d.newWriter(new WriterContext("piper_to_github_pr", "TEST", false, revision, Glob.ALL_FILES.roots()));
    GitRepository remote = gitUtil.mockRemoteRepo("github.com/foo");
    addFiles(remote, null, "first change", ImmutableMap.<String, String>builder().put("foo.txt", "").buildOrThrow());
    writeFile(this.workdir, "test.txt", "some content");
    writer.write(TransformResults.of(this.workdir, new DummyRevision("one")), Glob.ALL_FILES, console);
    writeFile(this.workdir, "test.txt", "other content");
    writer.write(TransformResults.of(this.workdir, new DummyRevision("two")), Glob.ALL_FILES, console);
    writeFile(this.workdir, "test.txt", "and content");
    writer.write(TransformResults.of(this.workdir, new DummyRevision("three")), Glob.ALL_FILES, console);
    console.assertThat().timesInLog(1, MessageType.INFO, "Pull Request https://github.com/foo/pull/12345 created using branch 'feature'.");
    assertThat(remote.refExists("feature")).isTrue();
    assertThat(Iterables.transform(remote.log("feature").run(), GitLogEntry::getBody)).containsExactly("first change\n", "test summary\n" + "\n" + "DummyOrigin-RevId: one\n", "test summary\n" + "\n" + "DummyOrigin-RevId: two\n", "test summary\n" + "\n" + "DummyOrigin-RevId: three\n");
    // If we don't keep writer state (same as a new migration). We do a rebase of
    // all the changes.
    writer = d.newWriter(new WriterContext("piper_to_github_pr", "TEST", false, revision, Glob.ALL_FILES.roots()));
    writeFile(this.workdir, "test.txt", "and content");
    gitUtil.mockApi("GET", getPullRequestsUrl("feature"), mockResponse("" + "[{\n" + "  \"id\": 1,\n" + "  \"number\": 12345,\n" + "  \"state\": \"open\",\n" + "  \"title\": \"test summary\",\n" + "  \"body\": \"test summary\"," + "  \"head\": { \"ref\": \"feature\"}," + "  \"base\": { \"ref\": \"feature\"}" + "}]"));
    writer.write(TransformResults.of(this.workdir, new DummyRevision("four")), Glob.ALL_FILES, console);
    assertThat(Iterables.transform(remote.log("feature").run(), GitLogEntry::getBody)).containsExactly("first change\n", "test summary\n" + "\n" + "DummyOrigin-RevId: four\n");
}
Also used : WriterContext(com.google.copybara.WriterContext) DummyRevision(com.google.copybara.testing.DummyRevision) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry)

Example 57 with WriterContext

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

the class GitHubPrDestinationTest method testCustomTitleAndBody.

@Test
public void testCustomTitleAndBody() throws ValidationException, IOException, RepoException {
    options.githubDestination.destinationPrBranch = "feature";
    mockNoPullRequestsGet("feature");
    gitUtil.mockApi("POST", "https://api.github.com/repos/foo/pulls", mockResponseAndValidateRequest("{\n" + "  \"id\": 1,\n" + "  \"number\": 12345,\n" + "  \"state\": \"open\",\n" + "  \"title\": \"custom title\",\n" + "  \"body\": \"custom body\"" + "}", MockRequestAssertion.equals("{\"base\":\"main\"," + "\"body\":\"custom body\"," + "\"head\":\"feature\"," + "\"title\":\"custom title\"}")));
    GitHubPrDestination d = skylark.eval("r", "r = git.github_pr_destination(" + "    url = 'https://github.com/foo', \n" + "    title = 'custom title',\n" + "    body = 'custom body',\n" + "    destination_ref = 'main'" + ")");
    WriterContext writerContext = new WriterContext("piper_to_github", "TEST", false, new DummyRevision("feature", "feature"), Glob.ALL_FILES.roots());
    Writer<GitRevision> writer = d.newWriter(writerContext);
    GitRepository remote = gitUtil.mockRemoteRepo("github.com/foo");
    addFiles(remote, null, "first change", ImmutableMap.<String, String>builder().put("foo.txt", "").buildOrThrow());
    writeFile(this.workdir, "test.txt", "some content");
    writer.write(TransformResults.of(this.workdir, new DummyRevision("one")), Glob.ALL_FILES, console);
    verify(gitUtil.httpTransport(), times(1)).buildRequest("GET", getPullRequestsUrl("feature"));
    verify(gitUtil.httpTransport(), times(1)).buildRequest("POST", "https://api.github.com/repos/foo/pulls");
}
Also used : WriterContext(com.google.copybara.WriterContext) DummyRevision(com.google.copybara.testing.DummyRevision) Test(org.junit.Test)

Example 58 with WriterContext

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

the class GitHubPrDestinationTest method testTrimMessageForPrTitle.

@Test
public void testTrimMessageForPrTitle() throws ValidationException, IOException, RepoException {
    options.githubDestination.destinationPrBranch = "feature";
    mockNoPullRequestsGet("feature");
    gitUtil.mockApi("POST", "https://api.github.com/repos/foo/pulls", mockResponseAndValidateRequest("{\n" + "  \"id\": 1,\n" + "  \"number\": 12345,\n" + "  \"state\": \"open\",\n" + "  \"title\": \"test summary\",\n" + "  \"body\": \"test summary\"" + "}", MockRequestAssertion.equals("{\"base\":\"main\",\"body\":\"Internal change.\\n\",\"head\":\"feature\"," + "\"title\":\"Internal change.\"}")));
    GitHubPrDestination d = skylark.eval("r", "r = git.github_pr_destination(" + "    url = 'https://github.com/foo'," + "    destination_ref = 'main'" + ")");
    WriterContext writerContext = new WriterContext("piper_to_github", "test", false, new DummyRevision("feature", "feature"), Glob.ALL_FILES.roots());
    Writer<GitRevision> writer = d.newWriter(writerContext);
    GitRepository remote = gitUtil.mockRemoteRepo("github.com/foo");
    addFiles(remote, null, "first change", ImmutableMap.<String, String>builder().put("foo.txt", "").buildOrThrow());
    writeFile(this.workdir, "test.txt", "some content");
    writer.write(TransformResults.of(this.workdir, new DummyRevision("one")).withSummary("\n\n\n\n\nInternal change."), Glob.ALL_FILES, console);
    verify(gitUtil.httpTransport(), times(1)).buildRequest("GET", getPullRequestsUrl("feature"));
    verify(gitUtil.httpTransport(), times(1)).buildRequest("POST", "https://api.github.com/repos/foo/pulls");
}
Also used : WriterContext(com.google.copybara.WriterContext) DummyRevision(com.google.copybara.testing.DummyRevision) Test(org.junit.Test)

Aggregations

WriterContext (com.google.copybara.WriterContext)58 DummyRevision (com.google.copybara.testing.DummyRevision)58 Test (org.junit.Test)46 DestinationEffect (com.google.copybara.DestinationEffect)21 Glob (com.google.copybara.util.Glob)17 Path (java.nio.file.Path)15 Author (com.google.copybara.authoring.Author)14 Changes (com.google.copybara.Changes)13 ValidationException (com.google.copybara.exception.ValidationException)12 GitLogEntry (com.google.copybara.git.GitRepository.GitLogEntry)11 CheckerException (com.google.copybara.checks.CheckerException)10 DummyChecker (com.google.copybara.testing.DummyChecker)10 MockRequestAssertion (com.google.copybara.testing.git.GitTestUtil.MockRequestAssertion)10 CommandOutput (com.google.copybara.util.CommandOutput)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 ImmutableList (com.google.common.collect.ImmutableList)8 ImmutableSet (com.google.common.collect.ImmutableSet)8 Truth.assertThat (com.google.common.truth.Truth.assertThat)8 ChangeMessage (com.google.copybara.ChangeMessage)8 Writer (com.google.copybara.Destination.Writer)8