Search in sources :

Example 51 with WriterContext

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

the class GitHubDestinationTest method testChecker.

@Test
public void testChecker() throws Exception {
    GitDestination d = skylark.eval("r", "r = git.github_destination(" + "    url = 'http://github.com/example/example', \n" + "    api_checker = testing.dummy_checker(),\n" + ")");
    WriterContext writerContext = new WriterContext("piper_to_github", "test", false, new DummyRevision("origin_ref1"), Glob.ALL_FILES.roots());
    Writer<GitRevision> writer = d.newWriter(writerContext);
    GitHubEndPoint endpoint = (GitHubEndPoint) writer.getFeedbackEndPoint(console);
    EvalException e = assertThrows(EvalException.class, () -> endpoint.getCombinedStatus("bad_word"));
    assertThat(e).hasMessageThat().contains("Bad word 'bad_word' found: field 'path'");
}
Also used : WriterContext(com.google.copybara.WriterContext) DummyRevision(com.google.copybara.testing.DummyRevision) EvalException(net.starlark.java.eval.EvalException) Test(org.junit.Test)

Example 52 with WriterContext

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

the class GitHubDestinationTest method checkRefNotFound.

private void checkRefNotFound() throws IOException, RepoException, ValidationException {
    addFiles(remote, primaryBranch, "first change", ImmutableMap.<String, String>builder().put("foo.txt", "foo").buildOrThrow());
    GitTesting.assertThatCheckout(remote, primaryBranch).containsFile("foo.txt", "foo").containsNoMoreFiles();
    writeFile(this.workdir, "test.txt", "some content");
    WriterContext writerContext = new WriterContext("piper_to_github", "test", false, new DummyRevision("origin_ref1"), Glob.ALL_FILES.roots());
    Writer<GitRevision> writer = destinationWithExistingPrBranch("other_${my_label}", /*deletePrBranch=*/
    "None").newWriter(writerContext);
    process(writer, new DummyRevision("origin_ref1"));
    GitTesting.assertThatCheckout(remote, primaryBranch).containsFile("test.txt", "some content").containsNoMoreFiles();
    console.assertThat().onceInLog(MessageType.VERBOSE, "Branch other_12345 does not exist");
    console.assertThat().onceInLog(MessageType.VERBOSE, "Branch other_6789 does not exist");
}
Also used : WriterContext(com.google.copybara.WriterContext) DummyRevision(com.google.copybara.testing.DummyRevision)

Example 53 with WriterContext

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

the class GitHubDestinationTest method testWithGitHubApiError.

@Test
public void testWithGitHubApiError() throws Exception {
    gitUtil.mockApi("GET", "https://api.github.com/repos/foo/git/refs/heads/other_12345", mockResponseWithStatus("", 403));
    gitUtil.mockApi("GET", "https://api.github.com/repos/foo/git/refs/other_6789", mockResponseWithStatus("", 403));
    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());
    Writer<GitRevision> writer = destinationWithExistingPrBranch("other_${my_label}", /*deletePrBranch=*/
    "None").newWriter(writerContext);
    GitHubApiException e = assertThrows(GitHubApiException.class, () -> process(writer, new DummyRevision("origin_ref1")));
    Assert.assertSame(e.getResponseCode(), FORBIDDEN);
}
Also used : WriterContext(com.google.copybara.WriterContext) DummyRevision(com.google.copybara.testing.DummyRevision) GitHubApiException(com.google.copybara.git.github.api.GitHubApiException) Test(org.junit.Test)

Example 54 with WriterContext

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

the class GitHubPrDestinationTest method testWrite_noContextReference.

@Test
public void testWrite_noContextReference() throws ValidationException {
    WriterContext writerContext = new WriterContext("piper_to_github_pr", "TEST", false, new DummyRevision("feature", null), Glob.ALL_FILES.roots());
    GitHubPrDestination d = skylark.eval("r", "r = git.github_pr_destination(" + "    url = 'https://github.com/foo'" + ")");
    ValidationException thrown = assertThrows(ValidationException.class, () -> d.newWriter(writerContext));
    assertThat(thrown).hasMessageThat().contains("git.github_pr_destination is incompatible with the current origin." + " Origin has to be able to provide the contextReference or use" + " '--github-destination-pr-branch' flag");
}
Also used : WriterContext(com.google.copybara.WriterContext) ValidationException(com.google.copybara.exception.ValidationException) DummyRevision(com.google.copybara.testing.DummyRevision) Test(org.junit.Test)

Example 55 with WriterContext

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

the class GitHubPrDestinationTest method testCustomTitleAndBody_withUpdate.

@Test
public void testCustomTitleAndBody_withUpdate() 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\":\"Body first a\"," + "\"head\":\"feature\"," + "\"title\":\"Title first a\"}")));
    GitHubPrDestination d = skylark.eval("r", "r = git.github_pr_destination(" + "    url = 'https://github.com/foo', \n" + "    title = 'Title ${aaa}',\n" + "    body = 'Body ${aaa}',\n" + "    destination_ref = 'main',\n" + "    update_description = True,\n" + ")");
    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")).withLabelFinder(Functions.forMap(ImmutableMap.of("aaa", ImmutableList.of("first a", "second a")))), 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");
    reset(gitUtil.httpTransport());
    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\"}" + "}]"));
    gitUtil.mockApi("POST", "https://api.github.com/repos/foo/pulls/12345", mockResponseAndValidateRequest("{\n" + "  \"id\": 1,\n" + "  \"number\": 12345,\n" + "  \"state\": \"open\",\n" + "  \"title\": \"custom title\",\n" + "  \"body\": \"custom body\"" + "}", MockRequestAssertion.equals("{\"body\":\"Body first b\"," + "\"title\":\"Title first b\"}")));
    writeFile(this.workdir, "test.txt", "some other content");
    writer = d.newWriter(writerContext);
    writer.write(TransformResults.of(this.workdir, new DummyRevision("two")).withLabelFinder(Functions.forMap(ImmutableMap.of("aaa", ImmutableList.of("first b", "second b")))), 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/12345");
}
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