use of com.google.copybara.git.github.api.CreatePullRequest in project copybara by google.
the class GithubPrDestination method newWriter.
@Override
public Writer<GitRevision> newWriter(Glob destinationFiles, boolean dryRun, @Nullable String groupId, @Nullable Writer<GitRevision> oldWriter) throws ValidationException {
WriterImpl gitOldWriter = (WriterImpl) oldWriter;
boolean effectiveSkipPush = GithubPrDestination.this.effectiveSkipPush || dryRun;
GithubWriterState state;
String pushBranchName = branchFromGroupId(groupId);
if (oldWriter != null && gitOldWriter.skipPush == effectiveSkipPush) {
state = (GithubWriterState) ((WriterImpl) oldWriter).state;
} else {
state = new GithubWriterState(localRepo, destinationOptions.localRepoPath != null ? pushBranchName : "copybara/push-" + UUID.randomUUID() + (dryRun ? "-dryrun" : ""));
}
return new WriterImpl<GithubWriterState>(destinationFiles, effectiveSkipPush, url, destinationRef, pushBranchName, generalOptions, commitGenerator, processPushOutput, state, /*nonFastForwardPush=*/
true, integrates, destinationOptions.lastRevFirstParent, destinationOptions.ignoreIntegrationErrors, destinationOptions.localRepoPath, destinationOptions.committerName, destinationOptions.committerEmail, destinationOptions.rebaseWhenBaseline(), gitOptions.visitChangePageSize) {
@Override
public ImmutableList<DestinationEffect> write(TransformResult transformResult, Console console) throws ValidationException, RepoException, IOException {
ImmutableList.Builder<DestinationEffect> result = ImmutableList.<DestinationEffect>builder().addAll(super.write(transformResult, console));
if (effectiveSkipPush || 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(), destinationRef, pushBranchName);
state.pullRequestNumber = -1L;
return result.build();
}
GithubApi api = githubOptions.getApi(GithubUtil.getProjectNameFromUrl(url));
for (PullRequest pr : api.getPullRequests(getProjectName())) {
if (pr.isOpen() && pr.getHead().getRef().equals(pushBranchName)) {
console.infoFmt("Pull request for branch %s already exists as %s/pull/%s", pushBranchName, asHttpsUrl(), pr.getNumber());
if (!pr.getBase().getRef().equals(destinationRef)) {
// TODO(malcon): Update PR or create a new one?
console.warnFmt("Current base branch '%s' is different from the PR base branch '%s'", destinationRef, pr.getBase().getRef());
}
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()), ImmutableList.of()));
return result.build();
}
}
ChangeMessage msg = ChangeMessage.parseMessage(transformResult.getSummary());
PullRequest pr = api.createPullRequest(getProjectName(), new CreatePullRequest(title == null ? msg.firstLine() : title, body == null ? msg.getText() : body, pushBranchName, destinationRef));
console.infoFmt("Pull Request %s/pull/%s created using branch '%s'.", asHttpsUrl(), pr.getNumber(), pushBranchName);
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()), ImmutableList.of()));
return result.build();
}
};
}
use of com.google.copybara.git.github.api.CreatePullRequest in project copybara by google.
the class AbstractGithubApiTest method testCreatePullRequest.
@Test
public void testCreatePullRequest() throws Exception {
trainMockPost("/repos/example/project/pulls", createValidator(TestCreatePullRequest.class, (cpr) -> cpr.getTitle().equals("title") && cpr.getBase().equals("aabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb") && cpr.getBody().equals("[TEST] example pull request one") && cpr.getHead().equals("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")), getResource("pulls_12345_testdata.json"));
// The test does not actually use the data in the CreatePullRequest
PullRequest pullRequest = api.createPullRequest("example/project", new CreatePullRequest("title", "[TEST] example pull request one", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", "aabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"));
assertThat(pullRequest.getNumber()).isEqualTo(12345);
assertThat(pullRequest.getState()).isEqualTo("open");
assertThat(pullRequest.getTitle()).isEqualTo("[TEST] example pull request one");
}
Aggregations