use of com.google.copybara.exception.CannotResolveRevisionException in project copybara by google.
the class DummyOriginTest method exceptionWhenParsingOutOfRangeReference.
@Test
public void exceptionWhenParsingOutOfRangeReference() throws Exception {
DummyOrigin origin = new DummyOrigin().addSimpleChange(/*timestamp*/
9).addSimpleChange(/*timestamp*/
98);
CannotResolveRevisionException thrown = assertThrows(CannotResolveRevisionException.class, () -> origin.resolve("42"));
assertThat(thrown).hasMessageThat().contains("Cannot find any change for 42. Only 2 changes exist");
}
use of com.google.copybara.exception.CannotResolveRevisionException in project copybara by google.
the class GithubPROrigin method getRevisionForPR.
private GitRevision getRevisionForPR(String project, int prNumber) throws RepoException, ValidationException {
if (!requiredLabels.isEmpty()) {
int retryCount = 0;
Set<String> requiredButNotPresent;
do {
Issue issue;
try (ProfilerTask ignore = generalOptions.profiler().start("github_api_get_issue")) {
issue = githubOptions.getApi(project).getIssue(project, prNumber);
}
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, prNumber, requiredButNotPresent));
}
}
PullRequest prData;
try (ProfilerTask ignore = generalOptions.profiler().start("github_api_get_pr")) {
prData = githubOptions.getApi(project).getPullRequest(project, prNumber);
}
if (requiredState == StateFilter.OPEN && !prData.isOpen()) {
throw new EmptyChangeException(String.format("Pull Request %d is not open", prNumber));
}
if (requiredState == StateFilter.CLOSED && prData.isOpen()) {
throw new EmptyChangeException(String.format("Pull Request %d is open", prNumber));
}
String stableRef = useMerge ? GithubUtil.asMergeRef(prNumber) : GithubUtil.asHeadRef(prNumber);
// 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());
try {
getRepository().fetch(asGithubUrl(project), /*prune=*/
false, /*force=*/
true, ImmutableList.of(stableRef + ":refs/PR_HEAD", // GitRepository need the whole reference name.
"refs/heads/" + prData.getBase().getRef() + ":refs/PR_BASE_BRANCH"));
} catch (CannotResolveRevisionException e) {
if (useMerge) {
throw new CannotResolveRevisionException(String.format("Cannot find a merge reference for Pull Request %d." + " It might have a conflict with head.", prNumber), e);
} else {
throw new CannotResolveRevisionException(String.format("Cannot find Pull Request %d.", prNumber), e);
}
}
GitRevision gitRevision = getRepository().resolveReference("PR_HEAD");
String integrateLabel = new GithubPRIntegrateLabel(getRepository(), generalOptions, project, prNumber, prData.getHead().getLabel(), gitRevision.getSha1()).toString();
ImmutableMap.Builder<String, String> labels = ImmutableMap.builder();
labels.put(GITHUB_PR_NUMBER_LABEL, Integer.toString(prNumber));
labels.put(GitModule.DEFAULT_INTEGRATE_LABEL, integrateLabel);
labels.put(GITHUB_BASE_BRANCH, prData.getBase().getRef());
String mergeBase = getRepository().mergeBase("refs/PR_HEAD", "refs/PR_BASE_BRANCH");
labels.put(GITHUB_BASE_BRANCH_SHA1, mergeBase);
labels.put(GITHUB_PR_TITLE, prData.getTitle());
labels.put(GITHUB_PR_BODY, prData.getBody());
return new GitRevision(getRepository(), gitRevision.getSha1(), /*reviewReference=*/
null, stableRef, labels.build(), url);
}
use of com.google.copybara.exception.CannotResolveRevisionException in project copybara by google.
the class GithubPROrigin method resolve.
@Override
public GitRevision resolve(String reference) throws RepoException, ValidationException {
checkCondition(reference != null, "" + "A pull request reference is expected as argument in the command line." + " Invoke copybara as:\n" + " copybara copy.bara.sky workflow_name 12345");
console.progress("GitHub PR Origin: Resolving reference " + reference);
// A whole https pull request url
Optional<GithubPrUrl> githubPrUrl = GithubUtil.maybeParseGithubPrUrl(reference);
String configProjectName = getProjectNameFromUrl(url);
if (githubPrUrl.isPresent()) {
checkCondition(githubPrUrl.get().getProject().equals(configProjectName), "Project name should be '%s' but it is '%s' instead", configProjectName, githubPrUrl.get().getProject());
return getRevisionForPR(configProjectName, githubPrUrl.get().getPrNumber());
}
// A Pull request number
if (CharMatcher.digit().matchesAllOf(reference)) {
return getRevisionForPR(getProjectNameFromUrl(url), Integer.parseInt(reference));
}
// refs/pull/12345/head
Optional<Integer> prNumber = GithubUtil.maybeParseGithubPrFromHeadRef(reference);
if (prNumber.isPresent()) {
return getRevisionForPR(configProjectName, prNumber.get());
}
String sha1Part = Splitter.on(" ").split(reference).iterator().next();
Matcher matcher = GitRevision.COMPLETE_SHA1_PATTERN.matcher(sha1Part);
// the destination. But in this case we cannot do that much apart from --force.
if (matcher.matches()) {
return new GitRevision(getRepository(), getRepository().parseRef(sha1Part));
}
throw new CannotResolveRevisionException(String.format("'%s' is not a valid reference for a GitHub Pull Request. Valid formats:" + "'https://github.com/project/pull/1234', 'refs/pull/1234/head' or '1234'", reference));
}
use of com.google.copybara.exception.CannotResolveRevisionException in project copybara by google.
the class GithubPROrigin method newReader.
@Override
public Reader<GitRevision> newReader(Glob originFiles, Authoring authoring) throws ValidationException {
return new ReaderImpl(url, originFiles, authoring, gitOptions, gitOriginOptions, generalOptions, /*includeBranchCommitLogs=*/
false, submoduleStrategy, firstParent) {
/**
* Disable rebase since this is controlled by useMerge field.
*/
@Override
protected void maybeRebase(GitRepository repo, GitRevision ref, Path workdir) throws RepoException, CannotResolveRevisionException {
}
@Override
public Optional<Baseline<GitRevision>> findBaseline(GitRevision startRevision, String label) throws RepoException, ValidationException {
if (!baselineFromBranch) {
return super.findBaseline(startRevision, label);
}
return findBaselinesWithoutLabel(startRevision, /*limit=*/
1).stream().map(e -> new Baseline<>(e.getSha1(), e)).findFirst();
}
@Override
public ImmutableList<GitRevision> findBaselinesWithoutLabel(GitRevision startRevision, int limit) throws RepoException, ValidationException {
String baseline = startRevision.associatedLabels().get(GITHUB_BASE_BRANCH_SHA1);
Preconditions.checkNotNull(baseline, "%s label should be present in %s", GITHUB_BASE_BRANCH_SHA1, startRevision);
GitRevision baselineRev = getRepository().resolveReference(baseline);
// Don't skip the first change as it is already the baseline
BaselinesWithoutLabelVisitor<GitRevision> visitor = new BaselinesWithoutLabelVisitor<>(originFiles, limit, /*skipFirst=*/
false);
visitChanges(baselineRev, visitor);
return visitor.getResult();
}
@Override
public Endpoint getFeedbackEndPoint() {
return new GitHubEndPoint(githubOptions, url);
}
/**
* Deal with the case of useMerge. We have a new commit (the merge) and first-parent from that
* commit doesn't work for this case.
*/
@Override
public ChangesResponse<GitRevision> changes(@Nullable GitRevision fromRef, GitRevision toRef) throws RepoException {
if (!useMerge) {
return super.changes(fromRef, toRef);
}
GitLogEntry merge = Iterables.getOnlyElement(getRepository().log(toRef.getSha1()).withLimit(1).run());
// Fast-forward merge
if (merge.getParents().size() == 1) {
return super.changes(fromRef, toRef);
}
// HEAD of the Pull Request
GitRevision gitRevision = merge.getParents().get(1);
ChangesResponse<GitRevision> prChanges = super.changes(fromRef, gitRevision);
// origin_files
if (prChanges.isEmpty()) {
return prChanges;
}
try {
return ChangesResponse.forChanges(ImmutableList.<Change<GitRevision>>builder().addAll(prChanges.getChanges()).add(change(merge.getCommit())).build());
} catch (EmptyChangeException e) {
throw new RepoException("Error getting the merge commit information: " + merge, e);
}
}
@Nullable
@Override
public String getGroupIdentity(GitRevision rev) throws RepoException {
return rev.associatedLabels().get(GITHUB_PR_NUMBER_LABEL);
}
};
}
use of com.google.copybara.exception.CannotResolveRevisionException in project copybara by google.
the class GitHubPrOrigin method resolveLastRev.
@Override
public GitRevision resolveLastRev(String reference) throws RepoException, ValidationException {
String sha1Part = Splitter.on(" ").split(reference).iterator().next();
Matcher matcher = GitRevision.COMPLETE_SHA1_PATTERN.matcher(sha1Part);
// the destination. But in this case we cannot do that much apart from --force.
if (matcher.matches()) {
return new GitRevision(getRepository(), getRepository().parseRef(sha1Part));
}
throw new CannotResolveRevisionException(String.format("'%s' is not a valid SHA.", reference));
}
Aggregations