Search in sources :

Example 1 with Issue

use of com.google.copybara.git.github.api.Issue in project copybara by google.

the class AbstractGithubApiTest method testGetIssue.

@Test
public void testGetIssue() throws Exception {
    trainMockGet("/repos/example/project/issues/12345", getResource("issues_12345_testdata.json"));
    Issue issue = api.getIssue("example/project", 12345);
    assertThat(issue.getNumber()).isEqualTo(12345);
    assertThat(issue.getState()).isEqualTo("open");
    assertThat(issue.getTitle()).isEqualTo("[TEST] example pull request one");
    assertThat(issue.getBody()).isEqualTo("Example body.\r\n");
    assertThat(Lists.transform(issue.getLabels(), Label::getName)).containsExactly("cla: yes");
}
Also used : Issue(com.google.copybara.git.github.api.Issue) Label(com.google.copybara.git.github.api.Issue.Label) Test(org.junit.Test)

Example 2 with Issue

use of com.google.copybara.git.github.api.Issue 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);
}
Also used : Issue(com.google.copybara.git.github.api.Issue) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) PullRequest(com.google.copybara.git.github.api.PullRequest) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) Endpoint(com.google.copybara.Endpoint) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

Issue (com.google.copybara.git.github.api.Issue)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Endpoint (com.google.copybara.Endpoint)1 CannotResolveRevisionException (com.google.copybara.exception.CannotResolveRevisionException)1 EmptyChangeException (com.google.copybara.exception.EmptyChangeException)1 Label (com.google.copybara.git.github.api.Issue.Label)1 PullRequest (com.google.copybara.git.github.api.PullRequest)1 ProfilerTask (com.google.copybara.profiler.Profiler.ProfilerTask)1 Test (org.junit.Test)1