use of com.google.copybara.WriterContext 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.WriterContext 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.WriterContext 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.WriterContext in project copybara by google.
the class SubmodulesInDestinationTest method writeWithSubmoduleInDestination.
private void writeWithSubmoduleInDestination() throws Exception {
fetch = primaryBranch;
push = primaryBranch;
Path scratchTree = Files.createTempDirectory("SubmodulesInDestinationTest-scratchTree");
GitRepository scratchRepo = repo().withWorkTree(scratchTree);
scratchRepo.simpleCommand("submodule", "add", "file://" + submodule.getGitDir(), "submodule");
scratchRepo.simpleCommand("commit", "-m", "commit submodule");
Files.write(workdir.resolve("test42"), new byte[] { 42 });
WriterContext writerContext = new WriterContext("SubmodulesInDestinationTest", "Test", false, new DummyRevision("test"), Glob.ALL_FILES.roots());
Destination.Writer<GitRevision> writer = destination().newWriter(writerContext);
ImmutableList<DestinationEffect> result = writer.write(TransformResults.of(workdir, new DummyRevision("ref1")), destinationFiles, console);
assertThat(result).hasSize(1);
assertThat(result.get(0).getErrors()).isEmpty();
assertThat(result.get(0).getType()).isEqualTo(Type.CREATED);
assertThat(result.get(0).getDestinationRef().getType()).isEqualTo("commit");
assertThat(result.get(0).getDestinationRef().getId()).matches("[0-9a-f]{40}");
}
use of com.google.copybara.WriterContext in project copybara by google.
the class SubmodulesInDestinationTest method submoduleInSubdirectoryWithSiblingFiles.
@Test
public void submoduleInSubdirectoryWithSiblingFiles() throws Exception {
destinationFiles = Glob.createGlob(ImmutableList.of("foo/a", "foo/c"));
fetch = primaryBranch;
push = primaryBranch;
Path scratchTree = Files.createTempDirectory("SubmodulesInDestinationTest-scratchTree");
GitRepository scratchRepo = repo().withWorkTree(scratchTree);
Files.createDirectories(scratchTree.resolve("foo"));
Files.write(scratchTree.resolve("foo/a"), new byte[] { 1 });
scratchRepo.add().files("foo/a").run();
scratchRepo.simpleCommand("submodule", "add", "file://" + submodule.getGitDir(), "foo/b");
scratchRepo.simpleCommand("commit", "-m", "commit submodule and foo/a");
// Create a commit that removes foo/a and adds foo/c
Files.createDirectories(workdir.resolve("foo"));
Files.write(workdir.resolve("foo/c"), new byte[] { 1 });
WriterContext writerContext = new WriterContext("SubmodulesInDestinationTest", "Test", false, new DummyRevision("test"), Glob.ALL_FILES.roots());
Destination.Writer<GitRevision> writer = destination().newWriter(writerContext);
ImmutableList<DestinationEffect> result = writer.write(TransformResults.of(workdir, new DummyRevision("ref1")), destinationFiles, console);
assertThat(result).hasSize(1);
assertThat(result.get(0).getErrors()).isEmpty();
assertThat(result.get(0).getType()).isEqualTo(Type.CREATED);
assertThat(result.get(0).getDestinationRef().getType()).isEqualTo("commit");
assertThat(result.get(0).getDestinationRef().getId()).matches("[0-9a-f]{40}");
GitTesting.assertThatCheckout(repo(), primaryBranch).containsFiles("foo/c", "foo/b").containsNoFiles("foo/a");
}
Aggregations