Repository: hub4j/github-api (java)

Issue:
Provide Auto Merge information in pull request?
Hey all ✋🏻 ,

unfortunately, it's not possible to enable the auto merge via the https://docs.github.com/en/rest/reference/pulls API.
But for a current project, I need at least the auto merge information about a `GHPullRequest`. 

I prepared a suggestion, but since it would be my first contribution, I just want to hear your opinion about  adding the information to the `GHPullRequest`. I made a suggestion here:
https://github.com/jeskosda/github-api/commit/7622293b2e4731019a20c3098a0b93347f276aa6

Since it's hard to prepare the auto merge in the test, I extended a current snapshot. 

Pull request:
feat: add auto merge information to pull request
# Description

Hey ✋🏻 , as already clarified in https://github.com/hub4j/github-api/issues/1245, this PR will add the auto merge object to the `GHPullRequest`. 

Closes #1245 

```yaml
auto_merge:
      title: Auto merge
      description: The status of auto merging a pull request.
      type: object
      properties:
        enabled_by:
          "$ref": "#/components/schemas/simple-user"
        merge_method:
          type: string
          description: The merge method to use.
          enum:
            - merge
            - squash
            - rebase
        commit_title:
          type: string
          description: Title for the merge commit message.
        commit_message:
          type: string
          description: Commit message for the merge commit.
      required:
        - enabled_by
        - merge_method
        - commit_title
        - commit_message
      nullable: true
```

The structure is double-checked against a request against the `/pulls` API:

```json
{
...,
"auto_merge": {
    "enabled_by": {
      "login": "jeskosda",
      "id": 1,
      "node_id": "xxx",
      "avatar_url": "https://avatars.githubusercontent.com/u/61695677?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/jeskosda",
      "html_url": "https://github.com/jeskosda",
      "followers_url": "https://api.github.com/users/jeskosda/followers",
      "following_url": "https://api.github.com/users/jeskosda/following{/other_user}",
      "gists_url": "https://api.github.com/users/jeskosda/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/jeskosda/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/jeskosda/subscriptions",
      "organizations_url": "https://api.github.com/users/jeskosda/orgs",
      "repos_url": "https://api.github.com/users/jeskosda/repos",
      "events_url": "https://api.github.com/users/jeskosda/events{/privacy}",
      "received_events_url": "https://api.github.com/users/jeskosda/received_events",
      "type": "User",
      "site_admin": false
    },
    "merge_method": "squash",
    "commit_title": "Test",
    "commit_message": "Test"
  }
}
```


Patch:
diff --git a/src/main/java/org/kohsuke/github/GHPullRequest.java b/src/main/java/org/kohsuke/github/GHPullRequest.java
index f0a3e8875..c476784c5 100644
--- a/src/main/java/org/kohsuke/github/GHPullRequest.java
+++ b/src/main/java/org/kohsuke/github/GHPullRequest.java
@@ -68,8 +68,10 @@ public class GHPullRequest extends GHIssue implements Refreshable {
     private String mergeable_state;
     private int changed_files;
     private String merge_commit_sha;
+    private AutoMerge auto_merge;
 
     // pull request reviewers
+
     private GHUser[] requested_reviewers;
     private GHTeam[] requested_teams;
 
@@ -84,11 +86,19 @@ public class GHPullRequest extends GHIssue implements Refreshable {
             // Issues returned from search to do not have an owner. Attempt to use url.
             final URL url = Objects.requireNonNull(getUrl(), "Missing instance URL!");
             return StringUtils.prependIfMissing(url.toString().replace(root().getApiUrl(), ""), "/");
-
         }
         return "/repos/" + owner.getOwnerName() + "/" + owner.getName() + "/pulls/" + number;
     }
 
+    /**
+     * The status of auto merging a pull request.
+     *
+     * @return the {@linkplain AutoMerge} or {@code null} if no auto merge is set.
+     */
+    public AutoMerge getAutoMerge() {
+        return auto_merge;
+    }
+
     /**
      * The URL of the patch file. like https://github.com/jenkinsci/jenkins/pull/100.patch
      *
@@ -268,9 +278,7 @@ public class GHPullRequest extends GHIssue implements Refreshable {
         return mergeable;
     }
 
-    /**
-     * for test purposes only
-     */
+    /** for test purposes only */
     @Deprecated
     Boolean getMergeableNoRefresh() throws IOException {
         return mergeable;
@@ -351,6 +359,7 @@ public class GHPullRequest extends GHIssue implements Refreshable {
     /**
      * Fully populate the data by retrieving missing data.
      *
+     * <p>
      * Depending on the original API call where this object is created, it may not contain everything.
      */
     private void populate() throws IOException {
@@ -359,9 +368,7 @@ public class GHPullRequest extends GHIssue implements Refreshable {
         refresh();
     }
 
-    /**
-     * Repopulates this object.
-     */
+    /** Repopulates this object. */
     public void refresh() throws IOException {
         if (isOffline()) {
             return; // cannot populate, will have to live with what we have
@@ -378,7 +385,6 @@ public class GHPullRequest extends GHIssue implements Refreshable {
      * default.
      *
      * @return the paged iterable
-     *
      * @see <a href="https://docs.github.com/en/rest/reference/pulls#list-pull-requests-files">List pull requests
      *      files</a>
      */
@@ -577,6 +583,7 @@ public class GHPullRequest extends GHIssue implements Refreshable {
 
     /**
      * Merge this pull request.
+     *
      * <p>
      * The equivalent of the big green "Merge pull request" button.
      *
@@ -591,6 +598,7 @@ public class GHPullRequest extends GHIssue implements Refreshable {
 
     /**
      * Merge this pull request.
+     *
      * <p>
      * The equivalent of the big green "Merge pull request" button.
      *
@@ -607,6 +615,7 @@ public class GHPullRequest extends GHIssue implements Refreshable {
 
     /**
      * Merge this pull request, using the specified merge method.
+     *
      * <p>
      * The equivalent of the big green "Merge pull request" button.
      *
@@ -629,11 +638,58 @@ public class GHPullRequest extends GHIssue implements Refreshable {
                 .send();
     }
 
-    /**
-     * The enum MergeMethod.
-     */
+    /** The enum MergeMethod. */
     public enum MergeMethod {
         MERGE, SQUASH, REBASE
     }
 
+    /**
+     * The status of auto merging a {@linkplain GHPullRequest}.
+     *
+     */
+    @SuppressFBWarnings(value = "UWF_UNWRITTEN_FIELD", justification = "Field comes from JSON deserialization")
+    public static class AutoMerge {
+
+        private GHUser enabled_by;
+        private MergeMethod merge_method;
+        private String commit_title;
+        private String commit_message;
+
+        /**
+         * The user who enabled the auto merge of the pull request.
+         *
+         * @return the {@linkplain GHUser}
+         */
+        @SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected behavior")
+        public GHUser getEnabledBy() {
+            return enabled_by;
+        }
+
+        /**
+         * The merge method of the auto merge.
+         *
+         * @return the {@linkplain MergeMethod}
+         */
+        public MergeMethod getMergeMethod() {
+            return merge_method;
+        }
+
+        /**
+         * the title of the commit, if e.g. {@linkplain MergeMethod#SQUASH} is used for the auto merge.
+         *
+         * @return the title of the commit
+         */
+        public String getCommitTitle() {
+            return commit_title;
+        }
+
+        /**
+         * the message of the commit, if e.g. {@linkplain MergeMethod#SQUASH} is used for the auto merge.
+         *
+         * @return the message of the commit
+         */
+        public String getCommitMessage() {
+            return commit_message;
+        }
+    }
 }


Test patch:
diff --git a/src/test/java/org/kohsuke/github/EnumTest.java b/src/test/java/org/kohsuke/github/EnumTest.java
index 7c5441367..1d36b5e03 100644
--- a/src/test/java/org/kohsuke/github/EnumTest.java
+++ b/src/test/java/org/kohsuke/github/EnumTest.java
@@ -1,6 +1,7 @@
 package org.kohsuke.github;
 
 import org.junit.Test;
+import org.kohsuke.github.GHPullRequest.MergeMethod;
 
 import static org.hamcrest.CoreMatchers.*;
 
@@ -55,7 +56,7 @@ public class EnumTest extends AbstractGitHubWireMockTest {
         assertThat(GHProject.ProjectState.values().length, equalTo(2));
         assertThat(GHProject.ProjectStateFilter.values().length, equalTo(3));
 
-        assertThat(GHPullRequest.MergeMethod.values().length, equalTo(3));
+        assertThat(MergeMethod.values().length, equalTo(3));
 
         assertThat(GHPullRequestQueryBuilder.Sort.values().length, equalTo(4));
 
diff --git a/src/test/java/org/kohsuke/github/GHPullRequestTest.java b/src/test/java/org/kohsuke/github/GHPullRequestTest.java
index 9ede99d36..956fddeb7 100644
--- a/src/test/java/org/kohsuke/github/GHPullRequestTest.java
+++ b/src/test/java/org/kohsuke/github/GHPullRequestTest.java
@@ -3,6 +3,7 @@ package org.kohsuke.github;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
+import org.kohsuke.github.GHPullRequest.AutoMerge;
 
 import java.io.IOException;
 import java.util.Collection;
@@ -37,6 +38,14 @@ public class GHPullRequestTest extends AbstractGitHubWireMockTest {
         assertThat(p.getTitle(), equalTo(name));
         assertThat(p.canMaintainerModify(), is(false));
         assertThat(p.isDraft(), is(false));
+
+        // Check auto merge status of the pull request
+        final AutoMerge autoMerge = p.getAutoMerge();
+        assertThat(autoMerge, is(notNullValue()));
+        assertThat(autoMerge.getCommitMessage(), equalTo("This is a auto merged squash commit message"));
+        assertThat(autoMerge.getCommitTitle(), equalTo("This is a auto merged squash commit"));
+        assertThat(autoMerge.getMergeMethod(), equalTo(GHPullRequest.MergeMethod.SQUASH));
+        assertThat(autoMerge.getEnabledBy(), is(notNullValue()));
     }
 
     @Test
@@ -60,7 +69,6 @@ public class GHPullRequestTest extends AbstractGitHubWireMockTest {
         p = repo.queryPullRequests().state(GHIssueState.OPEN).head("test/stable").list().toList().get(0);
         assertThat(p2.getNumber(), is(p.getNumber()));
         assertThat(p.isDraft(), is(true));
-
     }
 
     @Test
diff --git a/src/test/resources/org/kohsuke/github/GHPullRequestTest/wiremock/createPullRequest/__files/repos_hub4j-test-org_github-api_pulls-4.json b/src/test/resources/org/kohsuke/github/GHPullRequestTest/wiremock/createPullRequest/__files/repos_hub4j-test-org_github-api_pulls-4.json
index 7b4f8dda5..40101121d 100644
--- a/src/test/resources/org/kohsuke/github/GHPullRequestTest/wiremock/createPullRequest/__files/repos_hub4j-test-org_github-api_pulls-4.json
+++ b/src/test/resources/org/kohsuke/github/GHPullRequestTest/wiremock/createPullRequest/__files/repos_hub4j-test-org_github-api_pulls-4.json
@@ -328,6 +328,31 @@
   "mergeable": null,
   "rebaseable": null,
   "mergeable_state": "unknown",
+  "auto_merge": {
+    "enabled_by": {
+      "login": "bitwiseman",
+      "id": 1958953,
+      "node_id": "MDQ6VXNlcjE5NTg5NTM=",
+      "avatar_url": "https://avatars3.githubusercontent.com/u/1958953?v=4",
+      "gravatar_id": "",
+      "url": "http://localhost:51456/users/bitwiseman",
+      "html_url": "https://github.com/bitwiseman",
+      "followers_url": "http://localhost:51456/users/bitwiseman/followers",
+      "following_url": "http://localhost:51456/users/bitwiseman/following{/other_user}",
+      "gists_url": "http://localhost:51456/users/bitwiseman/gists{/gist_id}",
+      "starred_url": "http://localhost:51456/users/bitwiseman/starred{/owner}{/repo}",
+      "subscriptions_url": "http://localhost:51456/users/bitwiseman/subscriptions",
+      "organizations_url": "http://localhost:51456/users/bitwiseman/orgs",
+      "repos_url": "http://localhost:51456/users/bitwiseman/repos",
+      "events_url": "http://localhost:51456/users/bitwiseman/events{/privacy}",
+      "received_events_url": "http://localhost:51456/users/bitwiseman/received_events",
+      "type": "User",
+      "site_admin": false
+    },
+    "merge_method": "squash",
+    "commit_title": "This is a auto merged squash commit",
+    "commit_message": "This is a auto merged squash commit message"
+  },
   "merged_by": null,
   "comments": 0,
   "review_comments": 0,
@@ -336,4 +361,4 @@
   "additions": 1,
   "deletions": 1,
   "changed_files": 1
-}
\ No newline at end of file
+}