Search in sources :

Example 61 with DummyRevision

use of com.google.copybara.testing.DummyRevision in project copybara by google.

the class GitHubPrDestinationTest method testWriteNomain.

@Test
public void testWriteNomain() throws ValidationException, IOException, RepoException {
    GitHubPrDestination d = skylark.eval("r", "r = git.github_pr_destination(" + "    url = 'https://github.com/foo'," + "    destination_ref = 'other'," + ")");
    DummyRevision dummyRevision = new DummyRevision("dummyReference", "feature");
    WriterContext writerContext = new WriterContext("piper_to_github_pr", "TEST", false, dummyRevision, Glob.ALL_FILES.roots());
    String branchName = Identity.computeIdentity("OriginGroupIdentity", dummyRevision.contextReference(), writerContext.getWorkflowName(), "copy.bara.sky", writerContext.getWorkflowIdentityUser());
    mockNoPullRequestsGet(branchName);
    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\":\"" + branchName + "\",\"title\":\"test summary\"}")));
    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(branchName)).isTrue();
    assertThat(Iterables.transform(remote.log(branchName).run(), GitLogEntry::getBody)).containsExactly("first change\n", "second change\n", "test summary\n" + "\n" + "DummyOrigin-RevId: one\n");
}
Also used : WriterContext(com.google.copybara.WriterContext) DummyRevision(com.google.copybara.testing.DummyRevision) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry) Test(org.junit.Test)

Example 62 with DummyRevision

use of com.google.copybara.testing.DummyRevision in project copybara by google.

the class GitHubPrDestinationTest method emptyChange.

@Test
public void emptyChange() throws Exception {
    Writer<GitRevision> writer = getWriterForTestEmptyDiff();
    GitRepository remote = gitUtil.mockRemoteRepo("github.com/foo");
    addFiles(remote, null, "first change", ImmutableMap.<String, String>builder().put("foo.txt", "").buildOrThrow());
    String baseline = remote.resolveReference("HEAD").getSha1();
    addFiles(remote, "test_feature", "second change", ImmutableMap.<String, String>builder().put("foo.txt", "test").buildOrThrow());
    String changeHead = remote.resolveReference("HEAD").getSha1();
    gitUtil.mockApi("GET", getPullRequestsUrl("test_feature"), mockResponse("[{" + "  \"id\": 1,\n" + "  \"number\": 12345,\n" + "  \"state\": \"closed\",\n" + "  \"title\": \"test summary\",\n" + "  \"body\": \"test summary\"," + "  \"head\": {\"sha\": \"" + changeHead + "\"}," + "  \"base\": {\"sha\": \"" + baseline + "\"}" + "}]"));
    writeFile(this.workdir, "foo.txt", "test");
    RedundantChangeException e = assertThrows(RedundantChangeException.class, () -> writer.write(TransformResults.of(this.workdir, new DummyRevision("one")).withBaseline(baseline).withChanges(new Changes(ImmutableList.of(toChange(new DummyRevision("feature"), new Author("Foo Bar", "foo@bar.com"))), ImmutableList.of())).withLabelFinder(Functions.forMap(ImmutableMap.of("aaa", ImmutableList.of("first a", "second a")))), Glob.ALL_FILES, console));
    assertThat(e).hasMessageThat().contains("Skipping push to the existing pr https://github.com/foo/pull/12345 " + "as the change feature is empty.");
}
Also used : Changes(com.google.copybara.Changes) DummyRevision(com.google.copybara.testing.DummyRevision) RedundantChangeException(com.google.copybara.exception.RedundantChangeException) Author(com.google.copybara.authoring.Author) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 63 with DummyRevision

use of com.google.copybara.testing.DummyRevision in project copybara by google.

the class GitHubPrDestinationTest method testDestinationStatus.

@Test
public void testDestinationStatus() throws ValidationException, IOException, RepoException {
    options.githubDestination.createPullRequest = false;
    gitUtil.mockApi(anyString(), anyString(), new MockLowLevelHttpRequest() {

        @Override
        public LowLevelHttpResponse execute() throws IOException {
            throw new AssertionError("No API calls allowed for this test");
        }
    });
    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\n\nDummyOrigin-RevId: baseline", ImmutableMap.<String, String>builder().put("foo.txt", "").buildOrThrow());
    DestinationStatus status = writer.getDestinationStatus(Glob.ALL_FILES, "DummyOrigin-RevId");
    assertThat(status.getBaseline()).isEqualTo("baseline");
    assertThat(status.getPendingChanges()).isEmpty();
    writeFile(this.workdir, "test.txt", "some content");
    writer.write(TransformResults.of(this.workdir, new DummyRevision("one")), Glob.ALL_FILES, console);
    // New writer since after changes it keeps state internally for ITERATIVE mode
    status = d.newWriter(writerContext).getDestinationStatus(Glob.ALL_FILES, "DummyOrigin-RevId");
    assertThat(status.getBaseline()).isEqualTo("baseline");
    // Not supported for now as we rewrite the whole branch history.
    assertThat(status.getPendingChanges()).isEmpty();
}
Also used : WriterContext(com.google.copybara.WriterContext) DestinationStatus(com.google.copybara.Destination.DestinationStatus) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) DummyRevision(com.google.copybara.testing.DummyRevision) IOException(java.io.IOException) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Test(org.junit.Test)

Example 64 with DummyRevision

use of com.google.copybara.testing.DummyRevision in project copybara by google.

the class GitHubPrDestinationTest method testPrExistsButIsClosed.

@Test
public void testPrExistsButIsClosed() throws ValidationException, IOException, RepoException {
    options.githubDestination.destinationPrBranch = "feature";
    gitUtil.mockApi("GET", getPullRequestsUrl("feature"), mockResponse("[{" + "  \"id\": 1,\n" + "  \"number\": 12345,\n" + "  \"state\": \"closed\",\n" + "  \"title\": \"test summary\",\n" + "  \"body\": \"test summary\"," + "  \"head\": {\"ref\": \"feature\"}," + "  \"base\": {\"ref\": \"base\"}" + "}]"));
    gitUtil.mockApi("POST", "https://api.github.com/repos/foo/pulls/12345", mockResponseAndValidateRequest("{\n" + "  \"id\": 1,\n" + "  \"number\": 12345,\n" + "  \"state\": \"open\",\n" + "  \"title\": \"test summary\",\n" + "  \"body\": \"test summary\"" + "}", MockRequestAssertion.equals("{\"state\":\"open\"}")));
    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/12345");
}
Also used : WriterContext(com.google.copybara.WriterContext) DummyRevision(com.google.copybara.testing.DummyRevision) Test(org.junit.Test)

Example 65 with DummyRevision

use of com.google.copybara.testing.DummyRevision in project copybara by google.

the class GitHubPrDestinationTest method testWrite_primaryBranchMode.

@Test
public void testWrite_primaryBranchMode() throws ValidationException, IOException, RepoException {
    primaryBranchMigration = "True";
    options.githubDestination.destinationPrBranch = "feature";
    checkWrite(new DummyRevision("dummyReference"));
}
Also used : DummyRevision(com.google.copybara.testing.DummyRevision) Test(org.junit.Test)

Aggregations

DummyRevision (com.google.copybara.testing.DummyRevision)144 Test (org.junit.Test)124 WriterContext (com.google.copybara.WriterContext)58 Path (java.nio.file.Path)29 DestinationEffect (com.google.copybara.DestinationEffect)28 Author (com.google.copybara.authoring.Author)21 Changes (com.google.copybara.Changes)19 Glob (com.google.copybara.util.Glob)19 ValidationException (com.google.copybara.exception.ValidationException)18 IOException (java.io.IOException)16 GitLogEntry (com.google.copybara.git.GitRepository.GitLogEntry)14 TransformResult (com.google.copybara.TransformResult)13 TestingConsole (com.google.copybara.util.console.testing.TestingConsole)13 ZonedDateTime (java.time.ZonedDateTime)13 TransformWork (com.google.copybara.TransformWork)12 RepoException (com.google.copybara.exception.RepoException)12 CheckerException (com.google.copybara.checks.CheckerException)11 DummyChecker (com.google.copybara.testing.DummyChecker)11 FileSubjects.assertThatPath (com.google.copybara.testing.FileSubjects.assertThatPath)11 SkylarkTestExecutor (com.google.copybara.testing.SkylarkTestExecutor)11