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");
}
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.");
}
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();
}
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");
}
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"));
}
Aggregations