Search in sources :

Example 21 with WriterContext

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

the class GitHubDestinationTest method testCheckerOnPush.

@Test
public void testCheckerOnPush() throws Exception {
    options.workflowOptions.initHistory = true;
    addFiles(remote, primaryBranch, "first change", ImmutableMap.of("foo.txt", "hello"));
    options.testingOptions.checker = new DummyChecker(ImmutableSet.of("BAD"));
    GitDestination d = skylark.eval("r", "r = git.github_destination(" + "    url = '" + url + "', \n" + "    push = '" + primaryBranch + "',\n" + "    checker = testing.dummy_checker(),\n" + ")");
    WriterContext writerContext = new WriterContext("piper_to_github", "test", false, new DummyRevision("origin_ref1"), Glob.ALL_FILES.roots());
    writeFile(this.workdir, "test.txt", "BAD");
    Writer<GitRevision> writer = d.newWriter(writerContext);
    DummyRevision ref = new DummyRevision("origin_ref1");
    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());
    TransformResult result = TransformResults.of(workdir, ref).withChanges(changes);
    assertThat(assertThrows(CheckerException.class, () -> writer.write(result, destinationFiles, console))).hasMessageThat().contains("Bad word 'bad' found");
}
Also used : Changes(com.google.copybara.Changes) WriterContext(com.google.copybara.WriterContext) TransformResult(com.google.copybara.TransformResult) CheckerException(com.google.copybara.checks.CheckerException) DummyRevision(com.google.copybara.testing.DummyRevision) Author(com.google.copybara.authoring.Author) Change(com.google.copybara.Change) DummyChecker(com.google.copybara.testing.DummyChecker) Test(org.junit.Test)

Example 22 with WriterContext

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

the class GitHubDestinationTest method testDryRun.

@Test
public void testDryRun() throws Exception {
    fetch = primaryBranch;
    push = primaryBranch;
    addFiles(remote, primaryBranch, "first change", ImmutableMap.<String, String>builder().put("foo.txt", "foo").buildOrThrow());
    remote.simpleCommand("branch", "other");
    WriterContext writerContext = new WriterContext("piper_to_github", "test", true, new DummyRevision("origin_ref1"), Glob.ALL_FILES.roots());
    Writer<GitRevision> writer = destination().newWriter(writerContext);
    process(writer, new DummyRevision("origin_ref1"));
    GitTesting.assertThatCheckout(remote, primaryBranch).containsFile("foo.txt", "foo").containsNoMoreFiles();
    Files.write(workdir.resolve("test.txt"), "some content".getBytes(UTF_8));
    // Run again without dry run
    writer = newWriter();
    process(writer, new DummyRevision("origin_ref1"));
    GitTesting.assertThatCheckout(remote, primaryBranch).containsFile("test.txt", "some content").containsNoMoreFiles();
}
Also used : WriterContext(com.google.copybara.WriterContext) DummyRevision(com.google.copybara.testing.DummyRevision) Test(org.junit.Test)

Example 23 with WriterContext

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

the class GitHubDestinationTest method testPrToUpdateWithLabel.

@Test
public void testPrToUpdateWithLabel() throws Exception {
    gitUtil.mockApi("GET", "https://api.github.com/repos/foo/git/refs/heads/other_12345", mockResponse("{\n" + "\"ref\" : \"refs/heads/test_existing_12345_pr\",\n" + "\"node_id\" : \"MDM6UmVmcmVmcy9oZWFkcy9mZWF0dXJlQQ==\",\n" + "\"url\" :" + " \"https://api.github.com/repos/octocat/Hello-World/git/refs/heads/test_existing_12345_pr\",\n" + "\"object\" : {\n" + "         \"type\" : \"commit\",\n" + "         \"sha\" : \"aa218f56b14c9653891f9e74264a383fa43fefbd\",\n" + "         \"url\" :" + " \"https://api.github.com/repos/octocat/Hello-World/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd\"\n" + "       }\n" + "}"));
    gitUtil.mockApi("GET", "https://api.github.com/repos/foo/git/refs/heads/other_6789", mockResponse("{\n" + "\"ref\" : \"refs/heads/test_existing_6789_pr\",\n" + "\"node_id\" : \"MDM6UmVmcmVmcy9oZWFkcy9mZWF0dXJlQQ==\",\n" + "\"url\" :" + " \"https://api.github.com/repos/octocat/Hello-World/git/refs/heads/test_existing_6789_pr\",\n" + "\"object\" : {\n" + "         \"type\" : \"commit\",\n" + "         \"sha\" : \"aa218f56b14c9653891f9e74264a383fa43fefbd\",\n" + "         \"url\" :" + " \"https://api.github.com/repos/octocat/Hello-World/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd\"\n" + "       }\n" + "}"));
    addFiles(remote, primaryBranch, "first change", ImmutableMap.<String, String>builder().put("foo.txt", "foo").buildOrThrow());
    remote.simpleCommand("branch", "other_12345");
    remote.simpleCommand("branch", "other_6789");
    GitTesting.assertThatCheckout(remote, primaryBranch).containsFile("foo.txt", "foo").containsNoMoreFiles();
    GitTesting.assertThatCheckout(remote, "other_12345").containsFile("foo.txt", "foo").containsNoMoreFiles();
    GitTesting.assertThatCheckout(remote, "other_6789").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();
    GitTesting.assertThatCheckout(remote, "other_12345").containsFile("test.txt", "some content").containsNoMoreFiles();
    GitTesting.assertThatCheckout(remote, "other_6789").containsFile("test.txt", "some content").containsNoMoreFiles();
}
Also used : WriterContext(com.google.copybara.WriterContext) DummyRevision(com.google.copybara.testing.DummyRevision) Test(org.junit.Test)

Example 24 with WriterContext

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

the class GitHubPrDestinationTest method testBranchNameFromUser.

private void testBranchNameFromUser(String branchNameFromUser, String expectedBranchName, String contextReference) throws ValidationException, IOException, RepoException {
    GitHubPrDestination d = skylark.eval("r", "r = git.github_pr_destination(" + "    url = 'https://github.com/foo'," + "    destination_ref = 'other'," + "    pr_branch = " + "\'" + branchNameFromUser + "\'," + ")");
    DummyRevision dummyRevision = new DummyRevision("dummyReference", contextReference);
    mockNoPullRequestsGet(expectedBranchName);
    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\":\"other\",\"body\":\"test summary\\n\",\"head\":\"" + expectedBranchName + "\",\"title\":\"test summary\"}")));
    WriterContext writerContext = new WriterContext("piper_to_github_pr", "TEST", false, dummyRevision, Glob.ALL_FILES.roots());
    Writer<GitRevision> writer = d.newWriter(writerContext);
    GitRepository remote = gitUtil.mockRemoteRepo("github.com/foo");
    addFiles(remote, "main", "first change", ImmutableMap.<String, String>builder().put("foo.txt", "").buildOrThrow());
    addFiles(remote, "other", "second change", ImmutableMap.<String, String>builder().put("foo.txt", "test").buildOrThrow());
    writeFile(this.workdir, "test.txt", "some content");
    writer.write(TransformResults.of(this.workdir, new DummyRevision("one")), Glob.ALL_FILES, console);
    assertThat(remote.refExists(expectedBranchName)).isTrue();
}
Also used : WriterContext(com.google.copybara.WriterContext) DummyRevision(com.google.copybara.testing.DummyRevision)

Example 25 with WriterContext

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

the class GitHubPrDestinationTest method getWriterForTestEmptyDiff.

private Writer<GitRevision> getWriterForTestEmptyDiff() throws Exception {
    gitUtil = new GitTestUtil(options);
    gitUtil.mockRemoteGitRepos();
    options.gitDestination = new GitDestinationOptions(options.general, options.git);
    options.gitDestination.committerEmail = "commiter@email";
    options.gitDestination.committerName = "Bara Kopi";
    skylark = new SkylarkTestExecutor(options);
    options.githubDestination.destinationPrBranch = "test_feature";
    GitHubPrDestination d = skylark.eval("r", "r = git.github_pr_destination(" + "    url = 'https://github.com/foo', \n" + "    title = 'Title ${aaa}',\n" + "    body = 'Body ${aaa}',\n" + "    allow_empty_diff = False,\n" + "    destination_ref = 'main',\n" + "    pr_branch = 'test_${CONTEXT_REFERENCE}',\n" + "    primary_branch_migration = " + primaryBranchMigration + ",\n" + ")");
    WriterContext writerContext = new WriterContext("piper_to_github", "TEST", false, new DummyRevision("feature", "feature"), Glob.ALL_FILES.roots());
    return d.newWriter(writerContext);
}
Also used : WriterContext(com.google.copybara.WriterContext) DummyRevision(com.google.copybara.testing.DummyRevision) GitTestUtil(com.google.copybara.testing.git.GitTestUtil) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor)

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