use of com.google.copybara.profiler.Profiler.ProfilerTask in project copybara by google.
the class Feedback method run.
@Override
public void run(Path workdir, @Nullable String sourceRef) throws RepoException, ValidationException {
// TODO(danielromero): Handle correctly null sourceRefs
SkylarkConsole console = new SkylarkConsole(generalOptions.console());
Profiler profiler = generalOptions.profiler();
try (ProfilerTask ignore = profiler.start("run/" + name)) {
for (Action action : actions) {
try (ProfilerTask ignore2 = profiler.start(action.getName())) {
action.run(new FeedbackContext(origin, destination, sourceRef, console));
}
}
}
ValidationException.checkCondition(console.getErrorCount() == 0, "%d errors executing the feedback migration", console.getErrorCount());
}
use of com.google.copybara.profiler.Profiler.ProfilerTask in project copybara by google.
the class GitHubPrOrigin method getRevisionForPR.
private GitRevision getRevisionForPR(String project, PullRequest prData) throws RepoException, ValidationException {
GitHubApi api = gitHubOptions.newGitHubApi(project);
int prNumber = (int) prData.getNumber();
boolean actuallyUseMerge = this.useMerge;
ImmutableListMultimap.Builder<String, String> labels = ImmutableListMultimap.builder();
checkPrState(prData);
checkPrBranch(project, prData);
checkRequiredLabels(api, project, prData);
checkRequiredStatusContextNames(api, project, prData);
checkRequiredCheckRuns(api, project, prData);
checkReviewApprovers(api, project, prData, labels);
// Fetch also the baseline branch. It is almost free and doing a roundtrip later would hurt
// latency.
console.progressFmt("Fetching Pull Request %d and branch '%s'", prNumber, prData.getBase().getRef());
ImmutableList.Builder<String> refSpecBuilder = ImmutableList.<String>builder().add(String.format("%s:%s", asHeadRef(prNumber), LOCAL_PR_HEAD_REF)).add(String.format("refs/heads/%s:" + LOCAL_PR_BASE_BRANCH, prData.getBase().getRef()));
if (actuallyUseMerge) {
if (!Boolean.FALSE.equals(prData.isMergeable())) {
refSpecBuilder.add(String.format("%s:%s", asMergeRef(prNumber), LOCAL_PR_MERGE_REF));
} else if (forceImport()) {
console.warnFmt("PR %d is not mergeable, but continuing with PR Head instead because of %s", prNumber, GeneralOptions.FORCE);
actuallyUseMerge = false;
} else {
throw new CannotResolveRevisionException(String.format("Cannot find a merge reference for Pull Request %d." + " It might have a conflict with head.", prNumber));
}
}
ImmutableList<String> refspec = refSpecBuilder.build();
try (ProfilerTask ignore = generalOptions.profiler().start("fetch")) {
getRepository().fetch(ghHost.projectAsUrl(project), /*prune=*/
false, /*force=*/
true, refspec, partialFetch);
} catch (CannotResolveRevisionException e) {
if (actuallyUseMerge && prData.isMergeable() == null && forceImport()) {
// We can perhaps recover by fetching without the merge ref
actuallyUseMerge = false;
refspec = refspec.subList(0, refspec.size() - 1);
try (ProfilerTask ignore = generalOptions.profiler().start("fetch")) {
getRepository().fetch(ghHost.projectAsUrl(project), /*prune=*/
false, /*force=*/
true, refspec, partialFetch);
e = null;
} catch (CannotResolveRevisionException e2) {
// Report the error from the second fetch instead of the original fetch
e = e2;
}
}
if (e != null) {
if (actuallyUseMerge) {
String msg = String.format("Cannot find a merge reference for Pull Request %d.", prNumber);
if (Boolean.TRUE.equals(prData.isMergeable())) {
msg += " GitHub reported that this merge reference should exist.";
}
throw new CannotResolveRevisionException(msg, e);
} else {
throw new CannotResolveRevisionException(String.format("Cannot find Pull Request %d.", prNumber), e);
}
}
}
String refForMigration = actuallyUseMerge ? LOCAL_PR_MERGE_REF : LOCAL_PR_HEAD_REF;
GitRevision gitRevision = getRepository().resolveReference(refForMigration);
String headPrSha1 = getRepository().resolveReference(LOCAL_PR_HEAD_REF).getSha1();
String integrateLabel = new GitHubPrIntegrateLabel(getRepository(), generalOptions, project, prNumber, prData.getHead().getLabel(), // The integrate SHA has to be HEAD of the PR not the merge ref, even if use_merge = True
headPrSha1).toString();
labels.putAll(GITHUB_PR_REQUESTED_REVIEWER, prData.getRequestedReviewers().stream().map(User::getLogin).collect(toImmutableList()));
labels.put(GITHUB_PR_NUMBER_LABEL, Integer.toString(prNumber));
labels.put(GitModule.DEFAULT_INTEGRATE_LABEL, integrateLabel);
labels.put(GITHUB_BASE_BRANCH, prData.getBase().getRef());
labels.put(GITHUB_PR_HEAD_SHA, headPrSha1);
labels.put(GITHUB_PR_USE_MERGE, Boolean.toString(actuallyUseMerge));
String mergeBase = getRepository().mergeBase(refForMigration, LOCAL_PR_BASE_BRANCH);
labels.put(GITHUB_BASE_BRANCH_SHA1, mergeBase);
labels.put(GITHUB_PR_TITLE, prData.getTitle());
labels.put(GITHUB_PR_BODY, prData.getBody());
labels.put(GITHUB_PR_URL, prData.getHtmlUrl());
labels.put(GITHUB_PR_USER, prData.getUser().getLogin());
labels.putAll(GITHUB_PR_ASSIGNEE, prData.getAssignees().stream().map(User::getLogin).collect(Collectors.toList()));
GitRevision result = new GitRevision(getRepository(), gitRevision.getSha1(), /*reviewReference=*/
null, actuallyUseMerge ? asMergeRef(prNumber) : asHeadRef(prNumber), labels.build(), url);
return describeVersion ? getRepository().addDescribeVersion(result) : result;
}
use of com.google.copybara.profiler.Profiler.ProfilerTask in project copybara by google.
the class GitHubPrOrigin method checkRequiredCheckRuns.
/**
* Check that the PR has a conclusion of "success" for each check_run whose name is in the list
* provided in the `required_check_runs` param
*/
private void checkRequiredCheckRuns(GitHubApi api, String project, PullRequest prData) throws ValidationException, RepoException {
Set<String> requiredCheckRuns = getRequiredCheckRuns();
if (forceImport() || requiredCheckRuns.isEmpty()) {
return;
}
try (ProfilerTask ignore = generalOptions.profiler().start("github_api_get_combined_status")) {
CheckRuns checkRuns = api.getCheckRuns(project, prData.getHead().getSha());
Set<String> requiredButNotPresent = Sets.newHashSet(requiredCheckRuns);
List<CheckRun> passedCheckRuns = checkRuns.getCheckRuns().stream().filter(e -> e.getConclusion().equals("success")).collect(Collectors.toList());
requiredButNotPresent.removeAll(Collections2.transform(passedCheckRuns, CheckRun::getName));
if (!requiredButNotPresent.isEmpty()) {
throw new EmptyChangeException(String.format("Cannot migrate http://github.com/%s/pull/%d because the following check runs " + "have not been passed: %s", project, prData.getNumber(), requiredButNotPresent));
}
}
}
use of com.google.copybara.profiler.Profiler.ProfilerTask in project copybara by google.
the class GitHubPrOrigin method checkRequiredStatusContextNames.
/**
* Check that the PR has a state of "success" for each status whose context is in the list
* provided in the `required_status_context_names` param
*/
private void checkRequiredStatusContextNames(GitHubApi api, String project, PullRequest prData) throws ValidationException, RepoException {
Set<String> requiredStatusContextNames = getRequiredStatusContextNames();
if (forceImport() || requiredStatusContextNames.isEmpty()) {
return;
}
try (ProfilerTask ignore = generalOptions.profiler().start("github_api_get_combined_status")) {
CombinedStatus combinedStatus = api.getCombinedStatus(project, prData.getHead().getSha());
Set<String> requiredButNotPresent = Sets.newHashSet(requiredStatusContextNames);
List<Status> successStatuses = combinedStatus.getStatuses().stream().filter(e -> e.getState() == State.SUCCESS).collect(Collectors.toList());
requiredButNotPresent.removeAll(Collections2.transform(successStatuses, Status::getContext));
if (!requiredButNotPresent.isEmpty()) {
throw new EmptyChangeException(String.format("Cannot migrate http://github.com/%s/pull/%d because the following ci labels " + "have not been passed: %s", project, prData.getNumber(), requiredButNotPresent));
}
}
}
use of com.google.copybara.profiler.Profiler.ProfilerTask in project copybara by google.
the class Mirror method run.
@Override
public void run(Path workdir, ImmutableList<String> sourceRefs) throws RepoException, IOException, ValidationException {
try (ProfilerTask ignore = generalOptions.profiler().start("run/" + name)) {
GitRepository repo = gitOptions.cachedBareRepoForUrl(origin);
if (Iterables.isEmpty(actions)) {
defaultMirror(repo);
} else {
ImmutableList.Builder<ActionResult> allResultsBuilder = ImmutableList.builder();
for (Action action : actions) {
GitMirrorContext context = new GitMirrorContext(action, new SkylarkConsole(generalOptions.console()), sourceRefs, refspec, origin, destination, generalOptions.isForced(), repo, generalOptions.getDirFactory(), Dict.empty());
try {
action.run(context);
ActionResult actionResult = context.getActionResult();
allResultsBuilder.add(actionResult);
// First error aborts the execution of the other actions unless --force is used
ValidationException.checkCondition(generalOptions.isForced() || actionResult.getResult() != Result.ERROR, "Feedback migration '%s' action '%s' returned error: %s. Aborting execution.", name, action.getName(), actionResult.getMsg());
} catch (NonFastForwardRepositoryException e) {
allResultsBuilder.add(ActionResult.error(action.getName() + ": " + e.getMessage()));
if (!generalOptions.isForced()) {
throw e;
}
logger.atWarning().withCause(e).log();
} finally {
generalOptions.eventMonitors().dispatchEvent(m -> m.onChangeMigrationFinished(new ChangeMigrationFinishedEvent(ImmutableList.copyOf(context.getNewDestinationEffects()), getOriginDescription(), getDestinationDescription())));
}
}
ImmutableList<ActionResult> allResults = allResultsBuilder.build();
if (allResults.stream().anyMatch(a -> a.getResult() == Result.ERROR)) {
String errors = allResults.stream().filter(a -> a.getResult() == Result.ERROR).map(ActionResult::getMsg).collect(Collectors.joining("\n - "));
throw new ValidationException("One or more errors happened during the migration:\n" + " - " + errors);
}
// 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("git.mirror migration '%s' was noop. Detailed messages: %s", name, detailedMessage));
}
}
}
// More fine grain events based on the references created/updated/deleted:
ChangeMigrationFinishedEvent event = new ChangeMigrationFinishedEvent(ImmutableList.of(new DestinationEffect(generalOptions.dryRunMode ? DestinationEffect.Type.NOOP : DestinationEffect.Type.UPDATED, generalOptions.dryRunMode ? "Refspecs " + refspec + " can be mirrored" : "Refspecs " + refspec + " mirrored successfully", // TODO(danielromero): Populate OriginRef here
ImmutableList.of(), new DestinationRef(getOriginDestinationRef(destination), "mirror", /*url=*/
null))), getOriginDescription(), getDestinationDescription());
generalOptions.eventMonitors().dispatchEvent(m -> m.onChangeMigrationFinished(event));
}
Aggregations