|
23 | 23 | */
|
24 | 24 | package org.kohsuke.github;
|
25 | 25 |
|
| 26 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 27 | +import com.fasterxml.jackson.annotation.JsonProperty; |
26 | 28 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
27 | 29 | import org.apache.commons.lang3.StringUtils;
|
| 30 | +import org.kohsuke.github.graphql.response.GHGraphQLResponse; |
28 | 31 |
|
29 | 32 | import java.io.IOException;
|
30 | 33 | import java.net.URL;
|
@@ -629,6 +632,187 @@ public enum MergeMethod {
|
629 | 632 | REBASE
|
630 | 633 | }
|
631 | 634 |
|
| 635 | + /** |
| 636 | + * Get pull request id for GraphQL |
| 637 | + * |
| 638 | + * @return The pull request id for GraphQL |
| 639 | + * |
| 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 | + GHGraphQLResponse<GetGraphqlPullRequestIdResponse> response = root().createGraphQLRequest(graphqlBody) |
| 655 | + .fetchGraphQLResponse(GetGraphqlPullRequestIdResponse.class); |
| 656 | + |
| 657 | + if (!response.isSuccessful()) { |
| 658 | + throw new IOException("Failed to get graphql pull request id: " + response.getErrorMessages()); |
| 659 | + } |
| 660 | + |
| 661 | + return response.getData().getId(); |
| 662 | + } |
| 663 | + |
| 664 | + /** |
| 665 | + * The response from the GraphQL query to get the pull request ID. Minimum required fields are included. |
| 666 | + */ |
| 667 | + private static class GetGraphqlPullRequestIdResponse { |
| 668 | + private final PullRequestOwnerRepository repository; |
| 669 | + |
| 670 | + @JsonCreator |
| 671 | + public GetGraphqlPullRequestIdResponse(@JsonProperty("repository") PullRequestOwnerRepository repository) { |
| 672 | + this.repository = repository; |
| 673 | + } |
| 674 | + |
| 675 | + public String getId() { |
| 676 | + return repository.getId(); |
| 677 | + } |
| 678 | + |
| 679 | + private static class PullRequestOwnerRepository { |
| 680 | + private final GraphQLPullRequest pullRequest; |
| 681 | + |
| 682 | + @JsonCreator |
| 683 | + public PullRequestOwnerRepository(@JsonProperty("pullRequest") GraphQLPullRequest pullRequest) { |
| 684 | + this.pullRequest = pullRequest; |
| 685 | + } |
| 686 | + |
| 687 | + public String getId() { |
| 688 | + return pullRequest.getId(); |
| 689 | + } |
| 690 | + |
| 691 | + private static class GraphQLPullRequest { |
| 692 | + private final String id; |
| 693 | + |
| 694 | + @JsonCreator |
| 695 | + public GraphQLPullRequest(@JsonProperty("id") String id) { |
| 696 | + this.id = id; |
| 697 | + } |
| 698 | + |
| 699 | + public String getId() { |
| 700 | + return id; |
| 701 | + } |
| 702 | + } |
| 703 | + } |
| 704 | + } |
| 705 | + |
| 706 | + /** |
| 707 | + * Request to enable auto merge for a pull request. |
| 708 | + * |
| 709 | + * @param authorEmail |
| 710 | + * The email address to associate with this merge. |
| 711 | + * @param clientMutationId |
| 712 | + * A unique identifier for the client performing the mutation. |
| 713 | + * @param commitBody |
| 714 | + * Commit body to use for the commit when the PR is mergable; if omitted, a default message will be used. |
| 715 | + * NOTE: when merging with a merge queue any input value for commit message is ignored. |
| 716 | + * @param commitHeadline |
| 717 | + * Commit headline to use for the commit when the PR is mergable; if omitted, a default message will be |
| 718 | + * used. NOTE: when merging with a merge queue any input value for commit headline is ignored. |
| 719 | + * @param expectedHeadOid |
| 720 | + * The expected head OID of the pull request. |
| 721 | + * @param mergeMethod |
| 722 | + * The merge method to use. If omitted, defaults to `MERGE`. NOTE: when merging with a merge queue any |
| 723 | + * input value for merge method is ignored. |
| 724 | + * @throws IOException |
| 725 | + * the io exception |
| 726 | + */ |
| 727 | + public void requestEnableAutoMerge(String authorEmail, |
| 728 | + String clientMutationId, |
| 729 | + String commitBody, |
| 730 | + String commitHeadline, |
| 731 | + Integer expectedHeadOid, |
| 732 | + MergeMethod mergeMethod) throws IOException { |
| 733 | + |
| 734 | + StringBuilder inputBuilder = new StringBuilder(); |
| 735 | + inputBuilder.append(" pullRequestId: \"").append(getGraphqlPullRequestId()).append("\""); |
| 736 | + |
| 737 | + if (authorEmail != null) { |
| 738 | + inputBuilder.append(" authorEmail: \"").append(authorEmail).append("\""); |
| 739 | + } |
| 740 | + if (clientMutationId != null) { |
| 741 | + inputBuilder.append(" clientMutationId: \"").append(clientMutationId).append("\""); |
| 742 | + } |
| 743 | + if (commitBody != null) { |
| 744 | + inputBuilder.append(" commitBody: \"").append(commitBody).append("\""); |
| 745 | + } |
| 746 | + if (commitHeadline != null) { |
| 747 | + inputBuilder.append(" commitHeadline: \"").append(commitHeadline).append("\""); |
| 748 | + } |
| 749 | + if (expectedHeadOid != null) { |
| 750 | + inputBuilder.append(" expectedHeadOid: ").append(expectedHeadOid); |
| 751 | + } |
| 752 | + if (mergeMethod != null) { |
| 753 | + inputBuilder.append(" mergeMethod: ").append(mergeMethod); |
| 754 | + } |
| 755 | + |
| 756 | + String graphqlBody = "mutation EnableAutoMerge { enablePullRequestAutoMerge(input: {" + inputBuilder + "}) { " |
| 757 | + + "pullRequest { id } } }"; |
| 758 | + |
| 759 | + GHGraphQLResponse<EnablePullRequestAutoMergeResponse> response = root().createGraphQLRequest(graphqlBody) |
| 760 | + .fetchGraphQLResponse(EnablePullRequestAutoMergeResponse.class); |
| 761 | + |
| 762 | + if (!response.isSuccessful()) { |
| 763 | + throw new IOException("Failed to enable auto merge: " + response.getErrorMessages()); |
| 764 | + } |
| 765 | + |
| 766 | + refresh(); |
| 767 | + } |
| 768 | + |
| 769 | + /** |
| 770 | + * The response from the GraphQL query to enable auto merge for a pull request. Minimum required fields are |
| 771 | + * included. |
| 772 | + */ |
| 773 | + private static class EnablePullRequestAutoMergeResponse { |
| 774 | + private final EnablePullRequestAutoMerge enablePullRequestAutoMerge; |
| 775 | + |
| 776 | + @JsonCreator |
| 777 | + public EnablePullRequestAutoMergeResponse( |
| 778 | + @JsonProperty("enablePullRequestAutoMerge") EnablePullRequestAutoMerge enablePullRequestAutoMerge) { |
| 779 | + this.enablePullRequestAutoMerge = enablePullRequestAutoMerge; |
| 780 | + } |
| 781 | + |
| 782 | + public String getId() { |
| 783 | + return enablePullRequestAutoMerge.getId(); |
| 784 | + } |
| 785 | + |
| 786 | + private static class EnablePullRequestAutoMerge { |
| 787 | + |
| 788 | + private final EnablePullRequestAutoMergePullRequest pullRequest; |
| 789 | + |
| 790 | + @JsonCreator |
| 791 | + public EnablePullRequestAutoMerge( |
| 792 | + @JsonProperty("pullRequest") EnablePullRequestAutoMergePullRequest autoMergeRequest) { |
| 793 | + this.pullRequest = autoMergeRequest; |
| 794 | + } |
| 795 | + |
| 796 | + public String getId() { |
| 797 | + return pullRequest.getId(); |
| 798 | + } |
| 799 | + |
| 800 | + private static class EnablePullRequestAutoMergePullRequest { |
| 801 | + |
| 802 | + private final String id; |
| 803 | + |
| 804 | + @JsonCreator |
| 805 | + public EnablePullRequestAutoMergePullRequest(@JsonProperty("id") String id) { |
| 806 | + this.id = id; |
| 807 | + } |
| 808 | + |
| 809 | + public String getId() { |
| 810 | + return id; |
| 811 | + } |
| 812 | + } |
| 813 | + } |
| 814 | + } |
| 815 | + |
632 | 816 | /**
|
633 | 817 | * The status of auto merging a {@linkplain GHPullRequest}.
|
634 | 818 | *
|
|
0 commit comments