use of com.google.copybara.profiler.Profiler.ProfilerTask in project copybara by google.
the class Feedback method run.
@Override
public void run(Path workdir, ImmutableList<String> sourceRefs) throws RepoException, ValidationException {
ImmutableList.Builder<ActionResult> allResultsBuilder = ImmutableList.builder();
String suffix = Joiner.on('_').join(sourceRefs).replaceAll("([/ ])", "_");
String root = "run/" + name + "/" + suffix.substring(0, Math.min(suffix.length(), 20));
try (ProfilerTask ignore = profiler().start(root)) {
for (Action action : actions) {
ArrayList<DestinationEffect> effects = new ArrayList<>();
try (ProfilerTask ignore2 = profiler().start(action.getName())) {
SkylarkConsole console = new SkylarkConsole(generalOptions.console());
eventMonitors().dispatchEvent(m -> m.onChangeMigrationStarted(new ChangeMigrationStartedEvent()));
FeedbackMigrationContext context = new FeedbackMigrationContext(this, action, generalOptions.cliLabels(), sourceRefs, console);
action.run(context);
effects.addAll(context.getNewDestinationEffects());
ActionResult actionResult = context.getActionResult();
allResultsBuilder.add(actionResult);
// First error aborts the execution of the other actions
ValidationException.checkCondition(actionResult.getResult() != Result.ERROR, "Feedback migration '%s' action '%s' returned error: %s. Aborting execution.", name, action.getName(), actionResult.getMsg());
} finally {
eventMonitors().dispatchEvent(m -> m.onChangeMigrationFinished(new ChangeMigrationFinishedEvent(ImmutableList.copyOf(effects), getOriginDescription(), getDestinationDescription())));
}
}
}
ImmutableList<ActionResult> allResults = allResultsBuilder.build();
// This check also returns true if there are no actions
if (allResults.stream().allMatch(a -> a.getResult() == Result.NO_OP)) {
String detailedMessage = allResults.isEmpty() ? "actions field is empty" : allResults.stream().map(ActionResult::getMsg).collect(ImmutableList.toImmutableList()).toString();
throw new EmptyChangeException(String.format("Feedback migration '%s' was noop. Detailed messages: %s", name, detailedMessage));
}
}
use of com.google.copybara.profiler.Profiler.ProfilerTask in project copybara by google.
the class GitHubPrOrigin method checkRequiredLabels.
/**
* Check that the PR has all the labels provided in the `required_labels` param
*/
private void checkRequiredLabels(GitHubApi api, String project, PullRequest prData) throws ValidationException, RepoException {
Set<String> requiredLabels = getRequiredLabels();
Set<String> retryableLabels = getRetryableLabels();
if (forceImport() || requiredLabels.isEmpty()) {
return;
}
int retryCount = 0;
Set<String> requiredButNotPresent;
do {
Issue issue;
try (ProfilerTask ignore = generalOptions.profiler().start("github_api_get_issue")) {
issue = api.getIssue(project, prData.getNumber());
}
requiredButNotPresent = Sets.newHashSet(requiredLabels);
requiredButNotPresent.removeAll(Collections2.transform(issue.getLabels(), Label::getName));
// If we got all the labels we want or none of the ones we didn't get are retryable, return.
if (requiredButNotPresent.isEmpty() || Collections.disjoint(requiredButNotPresent, retryableLabels)) {
break;
}
Uninterruptibles.sleepUninterruptibly(2, TimeUnit.SECONDS);
retryCount++;
} while (retryCount < RETRY_COUNT);
if (!requiredButNotPresent.isEmpty()) {
throw new EmptyChangeException(String.format("Cannot migrate http://github.com/%s/pull/%d because it is missing the following" + " labels: %s", project, prData.getNumber(), requiredButNotPresent));
}
}
use of com.google.copybara.profiler.Profiler.ProfilerTask 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 " + this.label);
}
try (ProfilerTask ignore = generalOptions.profiler().start("integrate", ImmutableMap.of("URL", label.getValue()))) {
generalOptions.console().progressFmt("Integrating change from '%s'", 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, /*describeVersion=*/
false, /*partialFetch*/
false);
integrateLabel = IntegrateLabel.genericGitRevision(gitRevision);
}
}
strategy.integrate(repository, integrateLabel, externalFiles, label, messageInfo, generalOptions.console(), generalOptions.getDirFactory());
} catch (ValidationException e) {
throw new CannotIntegrateException("Error resolving " + label.getValue(), e);
}
}
}
use of com.google.copybara.profiler.Profiler.ProfilerTask in project copybara by google.
the class Mirror method defaultMirror.
private void defaultMirror(GitRepository repo) throws RepoException, ValidationException {
List<String> fetchRefspecs = refspec.stream().map(r -> r.originToOrigin().toString()).collect(Collectors.toList());
generalOptions.console().progressFmt("Fetching from %s", origin);
Profiler profiler = generalOptions.profiler();
try (ProfilerTask ignore1 = profiler.start("fetch")) {
repo.fetch(origin, /*prune=*/
true, /*force=*/
true, fetchRefspecs, partialFetch);
}
if (generalOptions.dryRunMode) {
generalOptions.console().progressFmt("Skipping push to %s. You can check the" + " commits to push in: %s", destination, repo.getGitDir());
} else {
generalOptions.console().progressFmt("Pushing to %s", destination);
List<Refspec> pushRefspecs = mirrorOptions.forcePush || generalOptions.isForced() ? refspec.stream().map(Refspec::withAllowNoFastForward).collect(Collectors.toList()) : refspec;
try (ProfilerTask ignore1 = profiler.start("push")) {
repo.push().prune(prune).withRefspecs(destination, pushRefspecs).run();
} catch (NonFastForwardRepositoryException e) {
// multiple refs, and that mirrors, it is better to just fail and tell the user.
throw new ValidationException("Error pushing some refs because origin is behind:" + e.getMessage(), e);
}
}
}
use of com.google.copybara.profiler.Profiler.ProfilerTask in project copybara by google.
the class GitVisitorUtil method visitChanges.
/**
* Visits
*/
static void visitChanges(GitRevision start, ChangesVisitor visitor, ChangeReader.Builder queryChanges, GeneralOptions generalOptions, String type, int visitChangePageSize) throws RepoException, ValidationException {
Preconditions.checkNotNull(start);
int skip = 0;
boolean finished = false;
try (ProfilerTask ignore = generalOptions.profiler().start(type + "/visit_changes")) {
while (!finished) {
ImmutableList<Change<GitRevision>> result;
try (ProfilerTask ignore2 = generalOptions.profiler().start("git_log_" + skip + "_" + visitChangePageSize)) {
result = queryChanges.setSkip(skip).setLimit(visitChangePageSize).build().run(start.getSha1()).reverse();
}
if (result.isEmpty()) {
break;
}
skip += result.size();
for (Change<GitRevision> current : result) {
if (visitor.visit(current) == VisitResult.TERMINATE) {
finished = true;
break;
}
}
}
}
if (skip == 0) {
throw new CannotResolveRevisionException("Cannot resolve reference " + start.getSha1());
}
}
Aggregations