use of com.google.copybara.revision.Changes in project copybara by google.
the class WorkflowRunHelper method maybeValidateRepoInLastRevState.
void maybeValidateRepoInLastRevState(@Nullable Metadata metadata) throws RepoException, ValidationException, IOException {
if (!workflow.isCheckLastRevState() || isForce()) {
return;
}
workflow.getGeneralOptions().ioRepoTask("validate_last_rev", () -> {
O lastRev = workflow.getGeneralOptions().repoTask("get_last_rev", this::maybeGetLastRev);
if (lastRev == null) {
// Not the job of this function to check for lastrev status.
return null;
}
Change<O> change = originReader.change(lastRev);
Changes changes = new Changes(ImmutableList.of(change), ImmutableList.of());
// Create a new writer so that state is not shared with the regular writer.
// The current writer might have state from previous migrations, etc.
ChangeMigrator<O, D> migrator = getMigratorForChangeAndWriter(change, workflow.createDryRunWriter(resolvedRef));
try {
workflow.getGeneralOptions().ioRepoTask("migrate", () -> migrator.doMigrate(lastRev, lastRev, new PrefixConsole("Validating last migration: ", workflow.getConsole()), metadata == null ? new Metadata(change.getMessage(), change.getAuthor(), ImmutableSetMultimap.of()) : metadata, changes, /*destinationBaseline=*/
null, lastRev));
throw new ValidationException("Migration of last-rev '" + lastRev.asString() + "' didn't" + " result in an empty change. This means that the result change of that" + " migration was modified ouside of Copybara or that new changes happened" + " later in the destination without using Copybara. Use --force if you" + " really want to do the migration.");
} catch (EmptyChangeException ignored) {
// EmptyChangeException ignored
}
return null;
});
}
use of com.google.copybara.revision.Changes in project copybara by google.
the class GitDestinationTest method testTagWithMsg.
@Test
public void testTagWithMsg() throws Exception {
fetch = primaryBranch;
push = primaryBranch;
Files.write(workdir.resolve("test.txt"), "some content".getBytes(UTF_8));
options.setForce(true);
WriterContext writerContext = new WriterContext("piper_to_github", "TEST", false, new DummyRevision("test"), Glob.ALL_FILES.roots());
evalDestination().newWriter(writerContext).write(TransformResults.of(workdir, new DummyRevision("ref1")), destinationFiles, console);
options.setForce(false);
Changes changes = new Changes(ImmutableList.of(new Change<>(new DummyRevision("ref2"), new Author("foo", "foo@foo.com"), "message", ZonedDateTime.now(ZoneOffset.UTC), ImmutableListMultimap.of("my_label", "12345"))), ImmutableList.of());
Files.write(workdir.resolve("test.txt"), "some content 2".getBytes(UTF_8));
evalDestinationWithTag(tagMsg).newWriter(writerContext).write(TransformResults.of(workdir, new DummyRevision("ref2")).withChanges(changes), destinationFiles, console);
CommandOutput commandOutput = repo().simpleCommand("tag", "-n9");
assertThat(commandOutput.getStdout()).matches(".*test_v1.*" + tagMsg + "\n");
}
use of com.google.copybara.revision.Changes in project copybara by google.
the class GerritDestinationTest method setup.
@Before
public void setup() throws Exception {
workdir = Files.createTempDirectory("workdir");
console = new TestingConsole();
options = new OptionsBuilder().setConsole(console).setOutputRootToTmpDir();
gitUtil = new GitTestUtil(options);
Path credentialsFile = Files.createTempFile("credentials", "test");
Files.write(credentialsFile, BASE_URL.getBytes(UTF_8));
GitRepository repo = newBareRepo(Files.createTempDirectory("test_repo"), getGitEnv(), /*verbose=*/
true, DEFAULT_TIMEOUT, /*noVerify=*/
false).init().withCredentialHelper("store --file=" + credentialsFile);
gitUtil.mockRemoteGitRepos(new Validator(), repo);
options.gitDestination = new GitDestinationOptions(options.general, options.git);
options.gitDestination.committerEmail = "commiter@email";
options.gitDestination.committerName = "Bara Kopi";
excludedDestinationPaths = ImmutableList.of();
repoGitDir = gitUtil.mockRemoteRepo("localhost:33333/foo/bar").getGitDir();
url = "https://localhost:33333/foo/bar";
repo().init();
skylark = new SkylarkTestExecutor(options);
when(gitUtil.httpTransport().buildRequest(eq("GET"), startsWith("https://localhost:33333/changes/"))).then((Answer<LowLevelHttpRequest>) invocation -> {
String change = changeIdFromRequest((String) invocation.getArguments()[1]);
return mockResponse("[" + "{" + " change_id : \"" + change + "\"," + " status : \"NEW\"" + "}]");
});
gitUtil.mockApi(eq("GET"), eq("https://localhost:33333/changes/?q=" + "hashtag:%22copybara_id_origin_ref_commiter@email%22%20AND" + "%20project:foo/bar%20AND%20status:NEW"), mockResponse("[]"));
}
use of com.google.copybara.revision.Changes in project copybara by google.
the class GitOriginTest method testChanges.
@Test
public void testChanges() throws Exception {
// Need to "round" it since git doesn't store the milliseconds
ZonedDateTime beforeTime = ZonedDateTime.now(ZoneId.systemDefault()).minusSeconds(1);
String author = "John Name <john@name.com>";
singleFileCommit(author, "change2", "test.txt", "some content2");
git("tag", "-m", "This is a tag", "0.1");
singleFileCommit(author, "change3", "test.txt", "some content3");
singleFileCommit(author, "change4", "test.txt", "some content4");
ImmutableList<Change<GitRevision>> changes = newReader().changes(origin.resolve(firstCommitRef), origin.resolve("HEAD")).getChanges();
assertThat(changes).hasSize(3);
assertThat(changes.stream().map(c -> c.getRevision().getUrl()).allMatch(c -> c.startsWith("file://"))).isTrue();
assertThat(changes.get(0).getMessage()).isEqualTo("change2\n");
assertThat(changes.get(0).getRevision().associatedLabel("GIT_DESCRIBE_CHANGE_VERSION")).contains("0.1");
assertThat(changes.get(1).getMessage()).isEqualTo("change3\n");
assertThat(changes.get(1).getRevision().associatedLabel("GIT_DESCRIBE_CHANGE_VERSION")).contains("0.1-1-g" + changes.get(1).getRevision().asString().substring(0, 7));
assertThat(changes.get(2).getMessage()).isEqualTo("change4\n");
assertThat(changes.get(2).getRevision().associatedLabel("GIT_DESCRIBE_CHANGE_VERSION")).contains("0.1-2-g" + changes.get(2).getRevision().asString().substring(0, 7));
TransformWork work = TransformWorks.of(Paths.get(""), "some msg", console).withChanges(new Changes(changes.reverse(), ImmutableList.of()));
assertThat(work.getLabel("GIT_DESCRIBE_CHANGE_VERSION")).isEqualTo("0.1-2-g" + changes.get(2).getRevision().asString().substring(0, 7));
assertThat(work.getAllLabels("GIT_DESCRIBE_CHANGE_VERSION").getImmutableList()).isEqualTo(ImmutableList.of("0.1-2-g" + changes.get(2).getRevision().asString().substring(0, 7), "0.1-1-g" + changes.get(1).getRevision().asString().substring(0, 7), "0.1"));
for (Change<GitRevision> change : changes) {
assertThat(change.getAuthor().getEmail()).isEqualTo("john@name.com");
assertThat(change.getDateTime()).isAtLeast(beforeTime);
assertThat(change.getDateTime()).isAtMost(ZonedDateTime.now(ZoneId.systemDefault()).plusSeconds(1));
}
}
use of com.google.copybara.revision.Changes in project copybara by google.
the class GitHubDestinationTest method testPrToUpdateIngoredForInitHistory.
@Test
public void testPrToUpdateIngoredForInitHistory() throws Exception {
options.workflowOptions.initHistory = true;
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());
writeFile(this.workdir, "test.txt", "some content");
Writer<GitRevision> writer = destinationWithExistingPrBranch("other", "True").newWriter(writerContext);
DummyRevision ref = new DummyRevision("origin_ref1");
TransformResult result = TransformResults.of(workdir, ref);
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());
result = result.withChanges(changes);
ImmutableList<DestinationEffect> destinationResult = writer.write(result, destinationFiles, console);
assertThat(destinationResult).hasSize(1);
assertThat(destinationResult.get(0).getErrors()).isEmpty();
assertThat(destinationResult.get(0).getType()).isEqualTo(Type.CREATED);
assertThat(destinationResult.get(0).getDestinationRef().getType()).isEqualTo("commit");
assertThat(destinationResult.get(0).getDestinationRef().getId()).matches("[0-9a-f]{40}");
// This is a migration of two changes (use the same ref because mocks)
verifyNoInteractions(gitUtil.httpTransport());
GitTesting.assertThatCheckout(remote, primaryBranch).containsFile("test.txt", "some content").containsNoMoreFiles();
assertThat(remote.simpleCommand("show-ref").getStdout()).doesNotContain("other");
}
Aggregations