use of com.google.copybara.LabelFinder in project copybara by google.
the class GerritDestinationTest method reuseChangeId.
@Test
public void reuseChangeId() throws Exception {
fetch = "master";
Files.write(workdir.resolve("file"), "some content".getBytes());
options.setForce(true);
options.gerrit.gerritChangeId = null;
url = "https://localhost:33333/foo/bar";
GitRepository repo = localGerritRepo("localhost:33333/foo/bar");
gitApiMockHttpTransport = NO_CHANGE_FOUND_MOCK;
process(new DummyRevision("origin_ref"));
String changeId = lastCommitChangeIdLine("origin_ref", repo);
assertThat(changeId).matches(GerritDestination.CHANGE_ID_LABEL + ": I[a-z0-9]+");
LabelFinder labelFinder = new LabelFinder(changeId);
Files.write(workdir.resolve("file"), "some different content".getBytes());
gitApiMockHttpTransport = new GitApiMockHttpTransport() {
@Override
protected byte[] getContent(String method, String url, MockLowLevelHttpRequest request) throws IOException {
String expected = "https://localhost:33333/changes/?q=change:%20" + labelFinder.getValue() + "%20AND%20project:foo/bar";
if (method.equals("GET") && url.equals(expected)) {
String result = "[" + "{" + " change_id : \"" + labelFinder.getValue() + "\"," + " status : \"NEW\"" + "}]";
return result.getBytes(UTF_8);
}
throw new IllegalArgumentException(method + " " + url);
}
};
// Allow to push again in a non-fastforward way.
repo.simpleCommand("update-ref", "-d", "refs/for/master");
process(new DummyRevision("origin_ref"));
assertThat(lastCommitChangeIdLine("origin_ref", repo)).isEqualTo(changeId);
GitTesting.assertThatCheckout(repo, "refs/for/master").containsFile("file", "some different content").containsNoMoreFiles();
}
use of com.google.copybara.LabelFinder in project copybara by google.
the class GerritDestinationTest method lastCommitChangeIdLineForRef.
private static String lastCommitChangeIdLineForRef(String gitRef, String originRef, GitRepository repo) throws RepoException {
GitLogEntry log = Iterables.getOnlyElement(repo.log(gitRef).withLimit(1).run());
assertThat(log.getBody()).contains("\n" + DummyOrigin.LABEL_NAME + ": " + originRef + "\n");
String line = null;
for (LabelFinder label : ChangeMessage.parseMessage(log.getBody()).getLabels()) {
if (label.isLabel(GerritDestination.CHANGE_ID_LABEL)) {
assertThat(label.getValue()).matches("I[0-9a-f]{40}$");
// Multiple Change-Ids are not allowed.
assertThat(line).isNull();
line = label.getLine();
}
}
assertWithMessage("Cannot find " + GerritDestination.CHANGE_ID_LABEL + " in:\n" + log.getBody()).that(line).isNotNull();
return line;
}
use of com.google.copybara.LabelFinder in project copybara by google.
the class ExposeLabelInMessage method transform.
@Override
public void transform(TransformWork work) throws IOException, ValidationException {
ImmutableList<LabelFinder> labelInMessage = work.getLabelInMessage(label);
String value;
if (!labelInMessage.isEmpty()) {
LabelFinder last = Iterables.getLast(labelInMessage);
value = last.getValue();
if (!label.equals(newLabelName) || !separator.equals(last.getSeparator())) {
// Remove the old label since we want it with different name/separator.
work.removeLabel(label, /*wholeMessage=*/
true);
}
} else {
value = work.getLabel(label);
}
if (value == null) {
checkCondition(ignoreNotFound, "Cannot find label %s", label);
return;
}
work.addOrReplaceLabel(newLabelName, value, separator);
}
use of com.google.copybara.LabelFinder in project copybara by google.
the class GitIntegrateChanges method doIntegrate.
private void doIntegrate(GitRepository repository, GeneralOptions generalOptions, Predicate<String> externalFiles, TransformResult result, MessageInfo messageInfo) throws CannotIntegrateException, RepoException {
for (LabelFinder label : result.findAllLabels()) {
if (!label.isLabel() || !this.label.equals(label.getName())) {
continue;
}
if (label.getValue().isEmpty()) {
throw new CannotIntegrateException("Found an empty value for label %s", this.label);
}
try (ProfilerTask ignore = generalOptions.profiler().start("integrate", ImmutableMap.of("URL", label.getValue()))) {
generalOptions.console().progress("Integrating change from " + label.getValue());
IntegrateLabel integrateLabel = GithubPRIntegrateLabel.parse(label.getValue(), repository, generalOptions);
if (integrateLabel == null) {
integrateLabel = GerritIntegrateLabel.parse(label.getValue(), repository, generalOptions);
if (integrateLabel == null) {
GitRevision gitRevision = GitRepoType.GIT.resolveRef(repository, /*repoUrl=*/
null, label.getValue(), generalOptions);
integrateLabel = IntegrateLabel.genericGitRevision(gitRevision);
}
}
strategy.integrate(repository, integrateLabel, externalFiles, label, messageInfo, generalOptions.console(), generalOptions.getDirFactory());
} catch (ValidationException e) {
throw new CannotIntegrateException(e, "Error resolving %s", label.getValue());
}
}
}
Aggregations