Repository: hub4j/github-api
Pull request #1183 (2021-06-10 20:53:25)

Add list projects method for GHUser
# Description

As was mentioned in https://github.com/hub4j/github-api/issues/924, there is no method in GHUser to retrieve user project boards.
In scope of this PR listProjects method was added in GHUser.


Patch:
diff --git a/src/main/java/org/kohsuke/github/GHUser.java b/src/main/java/org/kohsuke/github/GHUser.java
index 459d5a9bf..348ef055d 100644
--- a/src/main/java/org/kohsuke/github/GHUser.java
+++ b/src/main/java/org/kohsuke/github/GHUser.java
@@ -28,6 +28,8 @@ import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
 import java.io.IOException;
 import java.util.*;
 
+import static org.kohsuke.github.internal.Previews.INERTIA;
+
 /**
  * Represents an user of GitHub.
  *
@@ -134,6 +136,22 @@ public class GHUser extends GHPerson {
         return listRepositories("starred");
     }
 
+    /**
+     * Lists all the projects.
+     * <p>
+     * https://docs.github.com/en/rest/reference/projects#list-user-projects
+     *
+     * @return the paged iterable
+     */
+    @Deprecated
+    @Preview(INERTIA)
+    public PagedIterable<GHProject> listProjects() {
+        return root.createRequest()
+                .withPreview(INERTIA)
+                .withUrlPath(getApiTailUrl("projects"))
+                .toIterable(GHProject[].class, item -> item.wrap(root));
+    }
+
     private PagedIterable<GHRepository> listRepositories(final String suffix) {
         return root.createRequest()
                 .withUrlPath(getApiTailUrl(suffix))


Test patch:
diff --git a/src/test/java/org/kohsuke/github/GHUserTest.java b/src/test/java/org/kohsuke/github/GHUserTest.java
index 715b98daf..c60baa2ef 100644
--- a/src/test/java/org/kohsuke/github/GHUserTest.java
+++ b/src/test/java/org/kohsuke/github/GHUserTest.java
@@ -81,6 +81,17 @@ public class GHUserTest extends AbstractGitHubWireMockTest {
         assertThat(i, equalTo(115));
     }
 
+    @Test
+    public void listProjects() throws IOException {
+        GHUser user = gitHub.getUser("t0m4uk1991");
+        List<GHProject> projects = user.listProjects().toList();
+        assertThat(projects, notNullValue());
+        assertThat(projects.size(), is(3));
+        assertThat(projects.get(0).getName(), is("Project 1"));
+        assertThat(projects.get(1).getName(), is("Project 2"));
+        assertThat(projects.get(2).getName(), is("Project 3"));
+    }
+
     @Test
     public void listPublicRepositoriesPageSize62() throws IOException {
         GHUser user = gitHub.getUser("kohsuke");
diff --git a/src/test/resources/org/kohsuke/github/GHUserTest/wiremock/listProjects/__files/user-1.json b/src/test/resources/org/kohsuke/github/GHUserTest/wiremock/listProjects/__files/user-1.json
new file mode 100644
index 000000000..e08b554c1
--- /dev/null
+++ b/src/test/resources/org/kohsuke/github/GHUserTest/wiremock/listProjects/__files/user-1.json
@@ -0,0 +1,46 @@
+{
+  "login": "t0m4uk1991",
+  "id": 6698785,
+  "node_id": "MDQ6VXNlcjY2OTg3ODU=",
+  "avatar_url": "https://avatars.githubusercontent.com/u/6698785?v=4",
+  "gravatar_id": "",
+  "url": "https://api.github.com/users/t0m4uk1991",
+  "html_url": "https://github.com/t0m4uk1991",
+  "followers_url": "https://api.github.com/users/t0m4uk1991/followers",
+  "following_url": "https://api.github.com/users/t0m4uk1991/following{/other_user}",
+  "gists_url": "https://api.github.com/users/t0m4uk1991/gists{/gist_id}",
+  "starred_url": "https://api.github.com/users/t0m4uk1991/starred{/owner}{/repo}",
+  "subscriptions_url": "https://api.github.com/users/t0m4uk1991/subscriptions",
+  "organizations_url": "https://api.github.com/users/t0m4uk1991/orgs",
+  "repos_url": "https://api.github.com/users/t0m4uk1991/repos",
+  "events_url": "https://api.github.com/users/t0m4uk1991/events{/privacy}",
+  "received_events_url": "https://api.github.com/users/t0m4uk1991/received_events",
+  "type": "User",
+  "site_admin": false,
+  "name": null,
+  "company": null,
+  "blog": "https://t0m4uk1991.github.io",
+  "location": "Ukraine",
+  "email": "t0m4uk1991@gmail.com",
+  "hireable": true,
+  "bio": null,
+  "twitter_username": null,
+  "public_repos": 14,
+  "public_gists": 10,
+  "followers": 2,
+  "following": 2,
+  "created_at": "2014-02-16T20:43:03Z",
+  "updated_at": "2021-06-05T08:38:35Z",
+  "private_gists": 2,
+  "total_private_repos": 16,
+  "owned_private_repos": 16,
+  "disk_usage": 23717,
+  "collaborators": 0,
+  "two_factor_authentication": false,
+  "plan": {
+    "name": "free",
+    "space": 976562499,
+    "collaborators": 0,
+    "private_repos": 10000
+  }
+}
\ No newline at end of file
diff --git a/src/test/resources/org/kohsuke/github/GHUserTest/wiremock/listProjects/__files/users_t0m4uk1991-2.json b/src/test/resources/org/kohsuke/github/GHUserTest/wiremock/listProjects/__files/users_t0m4uk1991-2.json
new file mode 100644
index 000000000..e08b554c1
--- /dev/null
+++ b/src/test/resources/org/kohsuke/github/GHUserTest/wiremock/listProjects/__files/users_t0m4uk1991-2.json
@@ -0,0 +1,46 @@
+{
+  "login": "t0m4uk1991",
+  "id": 6698785,
+  "node_id": "MDQ6VXNlcjY2OTg3ODU=",
+  "avatar_url": "https://avatars.githubusercontent.com/u/6698785?v=4",
+  "gravatar_id": "",
+  "url": "https://api.github.com/users/t0m4uk1991",
+  "html_url": "https://github.com/t0m4uk1991",
+  "followers_url": "https://api.github.com/users/t0m4uk1991/followers",
+  "following_url": "https://api.github.com/users/t0m4uk1991/following{/other_user}",
+  "gists_url": "https://api.github.com/users/t0m4uk1991/gists{/gist_id}",
+  "starred_url": "https://api.github.com/users/t0m4uk1991/starred{/owner}{/repo}",
+  "subscriptions_url": "https://api.github.com/users/t0m4uk1991/subscriptions",
+  "organizations_url": "https://api.github.com/users/t0m4uk1991/orgs",
+  "repos_url": "https://api.github.com/users/t0m4uk1991/repos",
+  "events_url": "https://api.github.com/users/t0m4uk1991/events{/privacy}",
+  "received_events_url": "https://api.github.com/users/t0m4uk1991/received_events",
+  "type": "User",
+  "site_admin": false,
+  "name": null,
+  "company": null,
+  "blog": "https://t0m4uk1991.github.io",
+  "location": "Ukraine",
+  "email": "t0m4uk1991@gmail.com",
+  "hireable": true,
+  "bio": null,
+  "twitter_username": null,
+  "public_repos": 14,
+  "public_gists": 10,
+  "followers": 2,
+  "following": 2,
+  "created_at": "2014-02-16T20:43:03Z",
+  "updated_at": "2021-06-05T08:38:35Z",
+  "private_gists": 2,
+  "total_private_repos": 16,
+  "owned_private_repos": 16,
+  "disk_usage": 23717,
+  "collaborators": 0,
+  "two_factor_authentication": false,
+  "plan": {
+    "name": "free",
+    "space": 976562499,
+    "collaborators": 0,
+    "private_repos": 10000
+  }
+}
\ No newline at end of file
diff --git a/src/test/resources/org/kohsuke/github/GHUserTest/wiremock/listProjects/__files/users_t0m4uk1991_projects-3.json b/src/test/resources/org/kohsuke/github/GHUserTest/wiremock/listProjects/__files/users_t0m4uk1991_projects-3.json
new file mode 100644
index 000000000..02235d11d
--- /dev/null
+++ b/src/test/resources/org/kohsuke/github/GHUserTest/wiremock/listProjects/__files/users_t0m4uk1991_projects-3.json
@@ -0,0 +1,104 @@
+[
+  {
+    "owner_url": "https://api.github.com/users/t0m4uk1991",
+    "url": "https://api.github.com/projects/12651120",
+    "html_url": "https://github.com/users/t0m4uk1991/projects/3",
+    "columns_url": "https://api.github.com/projects/12651120/columns",
+    "id": 12651120,
+    "node_id": "MDc6UHJvamVjdDEyNjUxMTIw",
+    "name": "Project 1",
+    "body": "Project 1",
+    "number": 3,
+    "state": "open",
+    "creator": {
+      "login": "t0m4uk1991",
+      "id": 6698785,
+      "node_id": "MDQ6VXNlcjY2OTg3ODU=",
+      "avatar_url": "https://avatars.githubusercontent.com/u/6698785?u=dcfcdb0aa9e86943f3783dfdbe83be39de599a5d&v=4",
+      "gravatar_id": "",
+      "url": "https://api.github.com/users/t0m4uk1991",
+      "html_url": "https://github.com/t0m4uk1991",
+      "followers_url": "https://api.github.com/users/t0m4uk1991/followers",
+      "following_url": "https://api.github.com/users/t0m4uk1991/following{/other_user}",
+      "gists_url": "https://api.github.com/users/t0m4uk1991/gists{/gist_id}",
+      "starred_url": "https://api.github.com/users/t0m4uk1991/starred{/owner}{/repo}",
+      "subscriptions_url": "https://api.github.com/users/t0m4uk1991/subscriptions",
+      "organizations_url": "https://api.github.com/users/t0m4uk1991/orgs",
+      "repos_url": "https://api.github.com/users/t0m4uk1991/repos",
+      "events_url": "https://api.github.com/users/t0m4uk1991/events{/privacy}",
+      "received_events_url": "https://api.github.com/users/t0m4uk1991/received_events",
+      "type": "User",
+      "site_admin": false
+    },
+    "created_at": "2021-06-10T17:58:40Z",
+    "updated_at": "2021-06-10T17:58:40Z"
+  },
+  {
+    "owner_url": "https://api.github.com/users/t0m4uk1991",
+    "url": "https://api.github.com/projects/12651122",
+    "html_url": "https://github.com/users/t0m4uk1991/projects/4",
+    "columns_url": "https://api.github.com/projects/12651122/columns",
+    "id": 12651122,
+    "node_id": "MDc6UHJvamVjdDEyNjUxMTIy",
+    "name": "Project 2",
+    "body": "Project 2",
+    "number": 4,
+    "state": "open",
+    "creator": {
+      "login": "t0m4uk1991",
+      "id": 6698785,
+      "node_id": "MDQ6VXNlcjY2OTg3ODU=",
+      "avatar_url": "https://avatars.githubusercontent.com/u/6698785?u=dcfcdb0aa9e86943f3783dfdbe83be39de599a5d&v=4",
+      "gravatar_id": "",
+      "url": "https://api.github.com/users/t0m4uk1991",
+      "html_url": "https://github.com/t0m4uk1991",
+      "followers_url": "https://api.github.com/users/t0m4uk1991/followers",
+      "following_url": "https://api.github.com/users/t0m4uk1991/following{/other_user}",
+      "gists_url": "https://api.github.com/users/t0m4uk1991/gists{/gist_id}",
+      "starred_url": "https://api.github.com/users/t0m4uk1991/starred{/owner}{/repo}",
+      "subscriptions_url": "https://api.github.com/users/t0m4uk1991/subscriptions",
+      "organizations_url": "https://api.github.com/users/t0m4uk1991/orgs",
+      "repos_url": "https://api.github.com/users/t0m4uk1991/repos",
+      "events_url": "https://api.github.com/users/t0m4uk1991/events{/privacy}",
+      "received_events_url": "https://api.github.com/users/t0m4uk1991/received_events",
+      "type": "User",
+      "site_admin": false
+    },
+    "created_at": "2021-06-10T17:58:59Z",
+    "updated_at": "2021-06-10T17:58:59Z"
+  },
+  {
+    "owner_url": "https://api.github.com/users/t0m4uk1991",
+    "url": "https://api.github.com/projects/12651123",
+    "html_url": "https://github.com/users/t0m4uk1991/projects/5",
+    "columns_url": "https://api.github.com/projects/12651123/columns",
+    "id": 12651123,
+    "node_id": "MDc6UHJvamVjdDEyNjUxMTIz",
+    "name": "Project 3",
+    "body": "Project 3",
+    "number": 5,
+    "state": "open",
+    "creator": {
+      "login": "t0m4uk1991",
+      "id": 6698785,
+      "node_id": "MDQ6VXNlcjY2OTg3ODU=",
+      "avatar_url": "https://avatars.githubusercontent.com/u/6698785?u=dcfcdb0aa9e86943f3783dfdbe83be39de599a5d&v=4",
+      "gravatar_id": "",
+      "url": "https://api.github.com/users/t0m4uk1991",
+      "html_url": "https://github.com/t0m4uk1991",
+      "followers_url": "https://api.github.com/users/t0m4uk1991/followers",
+      "following_url": "https://api.github.com/users/t0m4uk1991/following{/other_user}",
+      "gists_url": "https://api.github.com/users/t0m4uk1991/gists{/gist_id}",
+      "starred_url": "https://api.github.com/users/t0m4uk1991/starred{/owner}{/repo}",
+      "subscriptions_url": "https://api.github.com/users/t0m4uk1991/subscriptions",
+      "organizations_url": "https://api.github.com/users/t0m4uk1991/orgs",
+      "repos_url": "https://api.github.com/users/t0m4uk1991/repos",
+      "events_url": "https://api.github.com/users/t0m4uk1991/events{/privacy}",
+      "received_events_url": "https://api.github.com/users/t0m4uk1991/received_events",
+      "type": "User",
+      "site_admin": false
+    },
+    "created_at": "2021-06-10T17:59:15Z",
+    "updated_at": "2021-06-10T17:59:15Z"
+  }
+]
\ No newline at end of file
diff --git a/src/test/resources/org/kohsuke/github/GHUserTest/wiremock/listProjects/mappings/user-1.json b/src/test/resources/org/kohsuke/github/GHUserTest/wiremock/listProjects/mappings/user-1.json
new file mode 100644
index 000000000..c631dd20d
--- /dev/null
+++ b/src/test/resources/org/kohsuke/github/GHUserTest/wiremock/listProjects/mappings/user-1.json
@@ -0,0 +1,47 @@
+{
+  "id": "a20bf0ec-d676-4f9e-aaa0-b64e16887305",
+  "name": "user",
+  "request": {
+    "url": "/user",
+    "method": "GET",
+    "headers": {
+      "Accept": {
+        "equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
+      }
+    }
+  },
+  "response": {
+    "status": 200,
+    "bodyFileName": "user-1.json",
+    "headers": {
+      "Server": "GitHub.com",
+      "Date": "Thu, 10 Jun 2021 18:03:47 GMT",
+      "Content-Type": "application/json; charset=utf-8",
+      "Cache-Control": "private, max-age=60, s-maxage=60",
+      "Vary": [
+        "Accept, Authorization, Cookie, X-GitHub-OTP",
+        "Accept-Encoding, Accept, X-Requested-With"
+      ],
+      "ETag": "W/\"b7fb71c9f9723032eb235ddd346b34ab754f2b41d8a907c802131f8a771d71bb\"",
+      "Last-Modified": "Sat, 05 Jun 2021 08:38:35 GMT",
+      "X-OAuth-Scopes": "admin:org, repo, user",
+      "X-Accepted-OAuth-Scopes": "",
+      "X-GitHub-Media-Type": "unknown, github.v3",
+      "X-RateLimit-Limit": "5000",
+      "X-RateLimit-Remaining": "4992",
+      "X-RateLimit-Reset": "1623349987",
+      "X-RateLimit-Used": "8",
+      "X-RateLimit-Resource": "core",
+      "Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
+      "X-Frame-Options": "deny",
+      "X-Content-Type-Options": "nosniff",
+      "X-XSS-Protection": "0",
+      "Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
+      "Content-Security-Policy": "default-src 'none'",
+      "X-GitHub-Request-Id": "8DED:76B6:1A5518:1AEA33:60C25402"
+    }
+  },
+  "uuid": "a20bf0ec-d676-4f9e-aaa0-b64e16887305",
+  "persistent": true,
+  "insertionIndex": 1
+}
\ No newline at end of file
diff --git a/src/test/resources/org/kohsuke/github/GHUserTest/wiremock/listProjects/mappings/users_t0m4uk1991-2.json b/src/test/resources/org/kohsuke/github/GHUserTest/wiremock/listProjects/mappings/users_t0m4uk1991-2.json
new file mode 100644
index 000000000..ce6a5ceda
--- /dev/null
+++ b/src/test/resources/org/kohsuke/github/GHUserTest/wiremock/listProjects/mappings/users_t0m4uk1991-2.json
@@ -0,0 +1,47 @@
+{
+  "id": "50b95df9-b365-4f47-a298-756999a88046",
+  "name": "users_t0m4uk1991",
+  "request": {
+    "url": "/users/t0m4uk1991",
+    "method": "GET",
+    "headers": {
+      "Accept": {
+        "equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
+      }
+    }
+  },
+  "response": {
+    "status": 200,
+    "bodyFileName": "users_t0m4uk1991-2.json",
+    "headers": {
+      "Server": "GitHub.com",
+      "Date": "Thu, 10 Jun 2021 18:03:48 GMT",
+      "Content-Type": "application/json; charset=utf-8",
+      "Cache-Control": "private, max-age=60, s-maxage=60",
+      "Vary": [
+        "Accept, Authorization, Cookie, X-GitHub-OTP",
+        "Accept-Encoding, Accept, X-Requested-With"
+      ],
+      "ETag": "W/\"b7fb71c9f9723032eb235ddd346b34ab754f2b41d8a907c802131f8a771d71bb\"",
+      "Last-Modified": "Sat, 05 Jun 2021 08:38:35 GMT",
+      "X-OAuth-Scopes": "admin:org, repo, user",
+      "X-Accepted-OAuth-Scopes": "",
+      "X-GitHub-Media-Type": "unknown, github.v3",
+      "X-RateLimit-Limit": "5000",
+      "X-RateLimit-Remaining": "4990",
+      "X-RateLimit-Reset": "1623349987",
+      "X-RateLimit-Used": "10",
+      "X-RateLimit-Resource": "core",
+      "Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
+      "X-Frame-Options": "deny",
+      "X-Content-Type-Options": "nosniff",
+      "X-XSS-Protection": "0",
+      "Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
+      "Content-Security-Policy": "default-src 'none'",
+      "X-GitHub-Request-Id": "1F30:2A5E:63945:69C68:60C25404"
+    }
+  },
+  "uuid": "50b95df9-b365-4f47-a298-756999a88046",
+  "persistent": true,
+  "insertionIndex": 2
+}
\ No newline at end of file
diff --git a/src/test/resources/org/kohsuke/github/GHUserTest/wiremock/listProjects/mappings/users_t0m4uk1991_projects-3.json b/src/test/resources/org/kohsuke/github/GHUserTest/wiremock/listProjects/mappings/users_t0m4uk1991_projects-3.json
new file mode 100644
index 000000000..8de2be7ba
--- /dev/null
+++ b/src/test/resources/org/kohsuke/github/GHUserTest/wiremock/listProjects/mappings/users_t0m4uk1991_projects-3.json
@@ -0,0 +1,46 @@
+{
+  "id": "a9d6c6bb-6be3-486a-9beb-17f1270fbfa9",
+  "name": "users_t0m4uk1991_projects",
+  "request": {
+    "url": "/users/t0m4uk1991/projects",
+    "method": "GET",
+    "headers": {
+      "Accept": {
+        "equalTo": "application/vnd.github.inertia-preview+json"
+      }
+    }
+  },
+  "response": {
+    "status": 200,
+    "bodyFileName": "users_t0m4uk1991_projects-3.json",
+    "headers": {
+      "Server": "GitHub.com",
+      "Date": "Thu, 10 Jun 2021 18:03:48 GMT",
+      "Content-Type": "application/json; charset=utf-8",
+      "Cache-Control": "private, max-age=60, s-maxage=60",
+      "Vary": [
+        "Accept, Authorization, Cookie, X-GitHub-OTP",
+        "Accept-Encoding, Accept, X-Requested-With"
+      ],
+      "ETag": "W/\"ca6ae9200bb8d394dfb5a5f588e60071af37a4c8f23daf2e3e0ee8c305ab1e91\"",
+      "X-OAuth-Scopes": "admin:org, repo, user",
+      "X-Accepted-OAuth-Scopes": "repo",
+      "X-GitHub-Media-Type": "github.inertia-preview; format=json",
+      "X-RateLimit-Limit": "5000",
+      "X-RateLimit-Remaining": "4989",
+      "X-RateLimit-Reset": "1623349987",
+      "X-RateLimit-Used": "11",
+      "X-RateLimit-Resource": "core",
+      "Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
+      "X-Frame-Options": "deny",
+      "X-Content-Type-Options": "nosniff",
+      "X-XSS-Protection": "0",
+      "Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
+      "Content-Security-Policy": "default-src 'none'",
+      "X-GitHub-Request-Id": "AC93:2A60:1A7CBA:1B1351:60C25404"
+    }
+  },
+  "uuid": "a9d6c6bb-6be3-486a-9beb-17f1270fbfa9",
+  "persistent": true,
+  "insertionIndex": 3
+}
\ No newline at end of file