Skip to content

Commit e20a6b1

Browse files
committed
feat: implement get PR id of GraphQL feature and enabling auto merge for PR feature
1 parent 18e5d72 commit e20a6b1

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

src/main/java/org/kohsuke/github/GHPullRequest.java

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
*/
2424
package org.kohsuke.github;
2525

26+
import com.fasterxml.jackson.annotation.JsonCreator;
27+
import com.fasterxml.jackson.annotation.JsonProperty;
2628
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2729
import org.apache.commons.lang3.StringUtils;
2830

@@ -629,6 +631,132 @@ public enum MergeMethod {
629631
REBASE
630632
}
631633

634+
/**
635+
* Get pull request id for GraphQL
636+
*
637+
* @return The pull request id for GraphQL
638+
* @throws IOException
639+
* the io exception
640+
*/
641+
public String getGraphqlPullRequestId() throws IOException {
642+
if (owner == null) {
643+
throw new IllegalStateException("Repository owner is required to get the pull request ID");
644+
}
645+
String repositoryName = owner.getName();
646+
String ownerName = owner.getOwnerName();
647+
648+
String graphqlBody = String.format(
649+
"query GetPullRequestID { repository(name: \"%s\", owner: \"%s\") { pullRequest(number: %d) { id } } }",
650+
repositoryName,
651+
ownerName,
652+
number);
653+
654+
GetGraphqlPullRequestIdResponse response = root().createGraphQLRequest(graphqlBody)
655+
.fetchGraphQLResponse(GetGraphqlPullRequestIdResponse.class);
656+
657+
return response.getId();
658+
}
659+
660+
/**
661+
* The response from the GraphQL query to get the pull request ID. Minimum required fields are included.
662+
*/
663+
private static class GetGraphqlPullRequestIdResponse {
664+
private final PullRequestOwnerRepository repository;
665+
666+
@JsonCreator
667+
public GetGraphqlPullRequestIdResponse(@JsonProperty("repository") PullRequestOwnerRepository repository) {
668+
this.repository = repository;
669+
}
670+
671+
public String getId() {
672+
return repository.getId();
673+
}
674+
675+
private static class PullRequestOwnerRepository {
676+
private final GraphQLPullRequest pullRequest;
677+
678+
@JsonCreator
679+
public PullRequestOwnerRepository(@JsonProperty("pullRequest") GraphQLPullRequest pullRequest) {
680+
this.pullRequest = pullRequest;
681+
}
682+
683+
public String getId() {
684+
return pullRequest.getId();
685+
}
686+
687+
private static class GraphQLPullRequest {
688+
private final String id;
689+
690+
@JsonCreator
691+
public GraphQLPullRequest(@JsonProperty("id") String id) {
692+
this.id = id;
693+
}
694+
695+
public String getId() {
696+
return id;
697+
}
698+
}
699+
}
700+
}
701+
702+
/**
703+
* Request to enable auto merge for a pull request.
704+
*
705+
* @param authorEmail
706+
* The email address to associate with this merge.
707+
* @param clientMutationId
708+
* A unique identifier for the client performing the mutation.
709+
* @param commitBody
710+
* Commit body to use for the commit when the PR is mergable; if omitted, a default message will be used.
711+
* NOTE: when merging with a merge queue any input value for commit message is ignored.
712+
* @param commitHeadline
713+
* Commit headline to use for the commit when the PR is mergable; if omitted, a default message will be
714+
* used. NOTE: when merging with a merge queue any input value for commit headline is ignored.
715+
* @param expectedHeadOid
716+
* The expected head OID of the pull request.
717+
* @param mergeMethod
718+
* The merge method to use. If omitted, defaults to `MERGE`. NOTE: when merging with a merge queue any
719+
* input value for merge method is ignored.
720+
* @throws IOException
721+
* the io exception
722+
*/
723+
public void requestEnableAutoMerge(String authorEmail,
724+
String clientMutationId,
725+
String commitBody,
726+
String commitHeadline,
727+
String expectedHeadOid,
728+
MergeMethod mergeMethod) throws IOException {
729+
730+
StringBuilder inputBuilder = new StringBuilder();
731+
inputBuilder.append(" pullRequestId: \"").append(getGraphqlPullRequestId()).append("\"");
732+
733+
if (authorEmail != null) {
734+
inputBuilder.append(" authorEmail: \"").append(authorEmail).append("\"");
735+
}
736+
if (clientMutationId != null) {
737+
inputBuilder.append(" clientMutationId: \"").append(clientMutationId).append("\"");
738+
}
739+
if (commitBody != null) {
740+
inputBuilder.append(" commitBody: \"").append(commitBody).append("\"");
741+
}
742+
if (commitHeadline != null) {
743+
inputBuilder.append(" commitHeadline: \"").append(commitHeadline).append("\"");
744+
}
745+
if (expectedHeadOid != null) {
746+
inputBuilder.append(" expectedHeadOid: \"").append(expectedHeadOid).append("\"");
747+
}
748+
if (mergeMethod != null) {
749+
inputBuilder.append(" mergeMethod: ").append(mergeMethod);
750+
}
751+
752+
String graphqlBody = "mutation EnableAutoMerge { enablePullRequestAutoMerge(input: {" + inputBuilder + "}) { "
753+
+ "pullRequest { id } } }";
754+
755+
root().createGraphQLRequest(graphqlBody).sendGraphQL();
756+
757+
refresh();
758+
}
759+
632760
/**
633761
* The status of auto merging a {@linkplain GHPullRequest}.
634762
*

0 commit comments

Comments
 (0)