Search in sources :

Example 1 with UpdatePullRequest

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

the class GitHubPrDestination method newWriter.

@Override
public Writer<GitRevision> newWriter(WriterContext writerContext) throws ValidationException {
    String prBranch = getPullRequestBranchName(writerContext.getOriginalRevision(), writerContext.getWorkflowName(), writerContext.getWorkflowIdentityUser());
    GitHubPrWriteHook gitHubPrWriteHook = writeHook.withUpdatedPrBranch(prBranch);
    GitHubWriterState state = new GitHubWriterState(localRepo, destinationOptions.localRepoPath != null ? prBranch : "copybara/push-" + UUID.randomUUID() + (writerContext.isDryRun() ? "-dryrun" : ""));
    return new WriterImpl<GitHubWriterState>(writerContext.isDryRun(), url, getDestinationRef(), prBranch, partialFetch, /*tagName*/
    null, /*tagMsg*/
    null, generalOptions, gitHubPrWriteHook, state, /*nonFastForwardPush=*/
    true, integrates, destinationOptions.lastRevFirstParent, destinationOptions.ignoreIntegrationErrors, destinationOptions.localRepoPath, destinationOptions.committerName, destinationOptions.committerEmail, destinationOptions.rebaseWhenBaseline(), gitOptions.visitChangePageSize, gitOptions.gitTagOverwrite, checker) {

        @Override
        public ImmutableList<DestinationEffect> write(TransformResult transformResult, Glob destinationFiles, Console console) throws ValidationException, RepoException, IOException {
            ImmutableList.Builder<DestinationEffect> result = ImmutableList.<DestinationEffect>builder().addAll(super.write(transformResult, destinationFiles, console));
            if (writerContext.isDryRun() || state.pullRequestNumber != null) {
                return result.build();
            }
            if (!gitHubDestinationOptions.createPullRequest) {
                console.infoFmt("Please create a PR manually following this link: %s/compare/%s...%s" + " (Only needed once)", asHttpsUrl(), getDestinationRef(), prBranch);
                state.pullRequestNumber = -1L;
                return result.build();
            }
            GitHubApi api = gitHubOptions.newGitHubApi(getProjectName());
            ImmutableList<PullRequest> pullRequests = api.getPullRequests(getProjectName(), PullRequestListParams.DEFAULT.withHead(String.format("%s:%s", ghHost.getUserNameFromUrl(url), prBranch)));
            ChangeMessage msg = ChangeMessage.parseMessage(transformResult.getSummary().trim());
            String title = GitHubPrDestination.this.title == null ? msg.firstLine() : SkylarkUtil.mapLabels(transformResult.getLabelFinder(), GitHubPrDestination.this.title, "title");
            String prBody = GitHubPrDestination.this.body == null ? msg.toString() : SkylarkUtil.mapLabels(transformResult.getLabelFinder(), GitHubPrDestination.this.body, "body");
            for (PullRequest pr : pullRequests) {
                if (pr.getHead().getRef().equals(prBranch)) {
                    if (!pr.isOpen()) {
                        console.warnFmt("Pull request for branch %s already exists as %s/pull/%s, but is closed - " + "reopening.", prBranch, asHttpsUrl(), pr.getNumber());
                        api.updatePullRequest(getProjectName(), pr.getNumber(), new UpdatePullRequest(null, null, OPEN));
                    } else {
                        console.infoFmt("Pull request for branch %s already exists as %s/pull/%s", prBranch, asHttpsUrl(), pr.getNumber());
                    }
                    if (!pr.getBase().getRef().equals(getDestinationRef())) {
                        // TODO(malcon): Update PR or create a new one?
                        console.warnFmt("Current base branch '%s' is different from the PR base branch '%s'", getDestinationRef(), pr.getBase().getRef());
                    }
                    if (updateDescription) {
                        checkCondition(!Strings.isNullOrEmpty(title), "Pull Request title cannot be empty. Either use 'title' field in" + " git.github_pr_destination or modify the message to not be empty");
                        api.updatePullRequest(getProjectName(), pr.getNumber(), new UpdatePullRequest(title, prBody, /*state=*/
                        null));
                    }
                    result.add(new DestinationEffect(DestinationEffect.Type.UPDATED, String.format("Pull Request %s updated", pr.getHtmlUrl()), transformResult.getChanges().getCurrent(), new DestinationEffect.DestinationRef(Long.toString(pr.getNumber()), "pull_request", pr.getHtmlUrl())));
                    return result.build();
                }
            }
            checkCondition(!Strings.isNullOrEmpty(title), "Pull Request title cannot be empty. Either use 'title' field in" + " git.github_pr_destination or modify the message to not be empty");
            PullRequest pr = api.createPullRequest(getProjectName(), new CreatePullRequest(title, prBody, prBranch, getDestinationRef()));
            console.infoFmt("Pull Request %s/pull/%s created using branch '%s'.", asHttpsUrl(), pr.getNumber(), prBranch);
            state.pullRequestNumber = pr.getNumber();
            result.add(new DestinationEffect(DestinationEffect.Type.CREATED, String.format("Pull Request %s created", pr.getHtmlUrl()), transformResult.getChanges().getCurrent(), new DestinationEffect.DestinationRef(Long.toString(pr.getNumber()), "pull_request", pr.getHtmlUrl())));
            return result.build();
        }

        @Override
        public Endpoint getFeedbackEndPoint(Console console) throws ValidationException {
            gitHubOptions.validateEndpointChecker(endpointChecker);
            return new GitHubEndPoint(gitHubOptions.newGitHubApiSupplier(url, endpointChecker, ghHost), url, console, ghHost);
        }
    };
}
Also used : TransformResult(com.google.copybara.TransformResult) ImmutableList(com.google.common.collect.ImmutableList) CreatePullRequest(com.google.copybara.git.github.api.CreatePullRequest) UpdatePullRequest(com.google.copybara.git.github.api.UpdatePullRequest) PullRequest(com.google.copybara.git.github.api.PullRequest) CreatePullRequest(com.google.copybara.git.github.api.CreatePullRequest) WriterImpl(com.google.copybara.git.GitDestination.WriterImpl) GitHubApi(com.google.copybara.git.github.api.GitHubApi) DestinationEffect(com.google.copybara.DestinationEffect) Console(com.google.copybara.util.console.Console) ChangeMessage(com.google.copybara.ChangeMessage) UpdatePullRequest(com.google.copybara.git.github.api.UpdatePullRequest) Glob(com.google.copybara.util.Glob)

Example 2 with UpdatePullRequest

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

the class AbstractGitHubApiTest method testUpdatePullRequest.

@Test
public void testUpdatePullRequest() throws Exception {
    JsonValidator<UpdatePullRequest> validator = createValidator(UpdatePullRequest.class, (request) -> request.getTitle().equals("title") && request.getBody().equals("body") && request.getState().equals(UpdatePullRequest.State.CLOSED));
    trainMockPost("/repos/example/project/pulls/12345", validator, getResource("pulls_12345_testdata.json"));
    api.updatePullRequest("example/project", 12345, new UpdatePullRequest("title", "body", UpdatePullRequest.State.CLOSED));
    assertThat(validator.wasCalled()).isTrue();
}
Also used : UpdatePullRequest(com.google.copybara.git.github.api.UpdatePullRequest) Test(org.junit.Test)

Aggregations

UpdatePullRequest (com.google.copybara.git.github.api.UpdatePullRequest)2 ImmutableList (com.google.common.collect.ImmutableList)1 ChangeMessage (com.google.copybara.ChangeMessage)1 DestinationEffect (com.google.copybara.DestinationEffect)1 TransformResult (com.google.copybara.TransformResult)1 WriterImpl (com.google.copybara.git.GitDestination.WriterImpl)1 CreatePullRequest (com.google.copybara.git.github.api.CreatePullRequest)1 GitHubApi (com.google.copybara.git.github.api.GitHubApi)1 PullRequest (com.google.copybara.git.github.api.PullRequest)1 Glob (com.google.copybara.util.Glob)1 Console (com.google.copybara.util.console.Console)1 Test (org.junit.Test)1