Search in sources :

Example 1 with GithubPrUrl

use of com.google.copybara.git.github.util.GithubUtil.GithubPrUrl 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));
}
Also used : GithubPrUrl(com.google.copybara.git.github.util.GithubUtil.GithubPrUrl) Matcher(java.util.regex.Matcher) CharMatcher(com.google.common.base.CharMatcher) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException)

Aggregations

CharMatcher (com.google.common.base.CharMatcher)1 CannotResolveRevisionException (com.google.copybara.exception.CannotResolveRevisionException)1 GithubPrUrl (com.google.copybara.git.github.util.GithubUtil.GithubPrUrl)1 Matcher (java.util.regex.Matcher)1