Repository: hub4j/github-api (java)

Issue:
401 when trying to extract GHUser#type
Hi, a code like this

```
new GitHubBuilder()
        .withJwtToken("token")
        .build()
        .getApp()
        .getInstallationById(githubInstallationId)
        .getAccount()
        .getType()
```

leads to 401:
```
Caused by: org.kohsuke.github.HttpException: {"message":"Bad credentials","documentation_url":"https://docs.github.com/rest"}
	at org.kohsuke.github.GitHubConnectorResponseErrorHandler$1.onError(GitHubConnectorResponseErrorHandler.java:62) ~[github-api-unbridged-1.318.jar:na]
```

There are a few issues here:
1. The field is already initialized as "Organization" after calling `getAccount()`, so populating here is not necessary, perhaps.
2. `installation.getAccount().getUrl()` returns `https://api.github.com/users/<ORGANIZATION_LOGIN>`, which perhaps is invalid, because it's an organization, not a user.


Pull request description:
GHPerson#getType() should call populate() only if type is null
# Description
While calling the app or app installations endpoints, `type` is returned in the response from GitHub for users and organizations. Do not call `populate()` if `type` is already set. Perhaps we can fix this at the design level to prevent these kinds of bugs (caused because we do not know what fields have been populated already) from occurring altogether, but making a quick fix for this in the meantime.
Fixes #1816 
<!-- Describe your change here -->

# Before submitting a PR:

- [x] Changes must not break binary backwards compatibility. If you are unclear on how to make the change you think is needed while maintaining backward compatibility, [CONTRIBUTING.md](CONTRIBUTING.md) for details.
- [x] Add JavaDocs and other comments explaining the behavior. 
- [x] When adding or updating methods that fetch entities, add `@link` JavaDoc entries to the relevant documentation on https://docs.github.com/en/rest . 
- [x] Add tests that cover any added or changed code. This generally requires capturing snapshot test data. See [CONTRIBUTING.md](CONTRIBUTING.md) for details.
- [x] Run `mvn -D enable-ci clean install site` locally. If this command doesn't succeed, your change will not pass CI.
- [x] Push your changes to a branch other than `main`. You will create your PR from that branch.

# When creating a PR: 

- [x] Fill in the "Description" above with clear summary of the changes. This includes:
  - [x] If this PR fixes one or more issues, include "Fixes #<issue number>" lines for each issue. 
  - [x] Provide links to relevant documentation on https://docs.github.com/en/rest where possible. If not including links, explain why not.
- [x] All lines of new code should be covered by tests as reported by code coverage. Any lines that are not covered must have PR comments explaining why they cannot be covered. For example, "Reaching this particular exception is hard and is not a particular common scenario."
- [x] Enable "Allow edits from maintainers".


Patch:
diff --git a/src/main/java/org/kohsuke/github/GHPerson.java b/src/main/java/org/kohsuke/github/GHPerson.java
index 175883b3b..1ef07032a 100644
--- a/src/main/java/org/kohsuke/github/GHPerson.java
+++ b/src/main/java/org/kohsuke/github/GHPerson.java
@@ -316,7 +316,9 @@ public abstract class GHPerson extends GHObject {
      *             the io exception
      */
     public String getType() throws IOException {
-        populate();
+        if (type == null) {
+            populate();
+        }
         return type;
     }
 


Test patch:
diff --git a/src/test/java/org/kohsuke/github/GHAppTest.java b/src/test/java/org/kohsuke/github/GHAppTest.java
index 8b7418514..b0369c4b9 100644
--- a/src/test/java/org/kohsuke/github/GHAppTest.java
+++ b/src/test/java/org/kohsuke/github/GHAppTest.java
@@ -57,6 +57,7 @@ public class GHAppTest extends AbstractGHAppInstallationTest {
         assertThat(app.getId(), is((long) 82994));
         assertThat(app.getOwner().getId(), is((long) 7544739));
         assertThat(app.getOwner().getLogin(), is("hub4j-test-org"));
+        assertThat(app.getOwner().getType(), is("Organization"));
         assertThat(app.getName(), is("GHApi Test app 1"));
         assertThat(app.getSlug(), is("ghapi-test-app-1"));
         assertThat(app.getDescription(), is(""));
@@ -85,8 +86,10 @@ public class GHAppTest extends AbstractGHAppInstallationTest {
         assertThat(appInstallation.getId(), is((long) 1037204));
         assertThat(appInstallation.getAccount().getId(), is((long) 195438329));
         assertThat(appInstallation.getAccount().getLogin(), is("approval-test"));
+        assertThat(appInstallation.getAccount().getType(), is("Organization"));
         assertThat(appInstallation.getRequester().getId(), is((long) 195437694));
         assertThat(appInstallation.getRequester().getLogin(), is("kaladinstormblessed2"));
+        assertThat(appInstallation.getRequester().getType(), is("User"));
         assertThat(appInstallation.getCreatedAt(), is(GitHubClient.parseDate("2025-01-17T15:50:51Z")));
         assertThat(appInstallation.getNodeId(), is("MDMwOkludGVncmF0aW9uSW5zdGFsbGF0aW9uUmVxdWVzdDEwMzcyMDQ="));
     }
@@ -275,6 +278,7 @@ public class GHAppTest extends AbstractGHAppInstallationTest {
         assertThat(appInstallation.getId(), is((long) 11111111));
         assertThat(appAccount.getId(), is((long) 111111111));
         assertThat(appAccount.login, is("bogus"));
+        assertThat(appAccount.getType(), is("Organization"));
         assertThat(appInstallation.getRepositorySelection(), is(GHRepositorySelection.SELECTED));
         assertThat(appInstallation.getAccessTokenUrl(), endsWith("/app/installations/11111111/access_tokens"));
         assertThat(appInstallation.getRepositoriesUrl(), endsWith("/installation/repositories"));
diff --git a/src/test/java/org/kohsuke/github/GitHubTest.java b/src/test/java/org/kohsuke/github/GitHubTest.java
index fe277899e..f8f352179 100644
--- a/src/test/java/org/kohsuke/github/GitHubTest.java
+++ b/src/test/java/org/kohsuke/github/GitHubTest.java
@@ -96,6 +96,22 @@ public class GitHubTest extends AbstractGitHubWireMockTest {
         assertThat(org, not(sameInstance(org2)));
     }
 
+    /**
+     * Verifies that the `type` field is correctly fetched when listing organizations.
+     * <p>
+     * Since the `type` field is not included by default in the list of organizations, this test ensures that calling
+     * {@code getType()} retrieves the expected value.
+     * </p>
+     *
+     * @throws IOException
+     *             if an I/O error occurs while fetching the organizations.
+     */
+    @Test
+    public void listOrganizationsFetchesType() throws IOException {
+        String type = gitHub.listOrganizations().withPageSize(1).iterator().next().getType();
+        assertThat(type, equalTo("Organization"));
+    }
+
     /**
      * Search users.
      *
diff --git a/src/test/resources/org/kohsuke/github/GitHubTest/wiremock/listOrganizationsFetchesType/__files/1-user.json b/src/test/resources/org/kohsuke/github/GitHubTest/wiremock/listOrganizationsFetchesType/__files/1-user.json
new file mode 100644
index 000000000..bc76bc73d
--- /dev/null
+++ b/src/test/resources/org/kohsuke/github/GitHubTest/wiremock/listOrganizationsFetchesType/__files/1-user.json
@@ -0,0 +1,48 @@
+{
+  "login": "anujhydrabadi",
+  "id": 129152617,
+  "node_id": "U_kgDOB7K2aQ",
+  "avatar_url": "https://avatars.githubusercontent.com/u/129152617?v=4",
+  "gravatar_id": "",
+  "url": "https://api.github.com/users/anujhydrabadi",
+  "html_url": "https://github.com/anujhydrabadi",
+  "followers_url": "https://api.github.com/users/anujhydrabadi/followers",
+  "following_url": "https://api.github.com/users/anujhydrabadi/following{/other_user}",
+  "gists_url": "https://api.github.com/users/anujhydrabadi/gists{/gist_id}",
+  "starred_url": "https://api.github.com/users/anujhydrabadi/starred{/owner}{/repo}",
+  "subscriptions_url": "https://api.github.com/users/anujhydrabadi/subscriptions",
+  "organizations_url": "https://api.github.com/users/anujhydrabadi/orgs",
+  "repos_url": "https://api.github.com/users/anujhydrabadi/repos",
+  "events_url": "https://api.github.com/users/anujhydrabadi/events{/privacy}",
+  "received_events_url": "https://api.github.com/users/anujhydrabadi/received_events",
+  "type": "User",
+  "user_view_type": "private",
+  "site_admin": false,
+  "name": "Anuj Hydrabadi",
+  "company": "@Facets-cloud",
+  "blog": "",
+  "location": null,
+  "email": null,
+  "hireable": null,
+  "bio": null,
+  "twitter_username": null,
+  "notification_email": null,
+  "public_repos": 6,
+  "public_gists": 1,
+  "followers": 0,
+  "following": 0,
+  "created_at": "2023-03-28T07:02:48Z",
+  "updated_at": "2025-01-31T11:08:30Z",
+  "private_gists": 0,
+  "total_private_repos": 28,
+  "owned_private_repos": 28,
+  "disk_usage": 1269,
+  "collaborators": 2,
+  "two_factor_authentication": true,
+  "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/GitHubTest/wiremock/listOrganizationsFetchesType/__files/2-organizations.json b/src/test/resources/org/kohsuke/github/GitHubTest/wiremock/listOrganizationsFetchesType/__files/2-organizations.json
new file mode 100644
index 000000000..3c98fb9ce
--- /dev/null
+++ b/src/test/resources/org/kohsuke/github/GitHubTest/wiremock/listOrganizationsFetchesType/__files/2-organizations.json
@@ -0,0 +1,16 @@
+[
+  {
+    "login": "errfree",
+    "id": 44,
+    "node_id": "MDEyOk9yZ2FuaXphdGlvbjQ0",
+    "url": "https://api.github.com/orgs/errfree",
+    "repos_url": "https://api.github.com/orgs/errfree/repos",
+    "events_url": "https://api.github.com/orgs/errfree/events",
+    "hooks_url": "https://api.github.com/orgs/errfree/hooks",
+    "issues_url": "https://api.github.com/orgs/errfree/issues",
+    "members_url": "https://api.github.com/orgs/errfree/members{/member}",
+    "public_members_url": "https://api.github.com/orgs/errfree/public_members{/member}",
+    "avatar_url": "https://avatars.githubusercontent.com/u/44?v=4",
+    "description": null
+  }
+]
\ No newline at end of file
diff --git a/src/test/resources/org/kohsuke/github/GitHubTest/wiremock/listOrganizationsFetchesType/__files/3-orgs_errfree.json b/src/test/resources/org/kohsuke/github/GitHubTest/wiremock/listOrganizationsFetchesType/__files/3-orgs_errfree.json
new file mode 100644
index 000000000..1f6b52cf4
--- /dev/null
+++ b/src/test/resources/org/kohsuke/github/GitHubTest/wiremock/listOrganizationsFetchesType/__files/3-orgs_errfree.json
@@ -0,0 +1,26 @@
+{
+  "login": "errfree",
+  "id": 44,
+  "node_id": "MDEyOk9yZ2FuaXphdGlvbjQ0",
+  "url": "https://api.github.com/orgs/errfree",
+  "repos_url": "https://api.github.com/orgs/errfree/repos",
+  "events_url": "https://api.github.com/orgs/errfree/events",
+  "hooks_url": "https://api.github.com/orgs/errfree/hooks",
+  "issues_url": "https://api.github.com/orgs/errfree/issues",
+  "members_url": "https://api.github.com/orgs/errfree/members{/member}",
+  "public_members_url": "https://api.github.com/orgs/errfree/public_members{/member}",
+  "avatar_url": "https://avatars.githubusercontent.com/u/44?v=4",
+  "description": null,
+  "is_verified": false,
+  "has_organization_projects": true,
+  "has_repository_projects": true,
+  "public_repos": 2,
+  "public_gists": 0,
+  "followers": 14,
+  "following": 0,
+  "html_url": "https://github.com/errfree",
+  "created_at": "2008-01-24T02:08:37Z",
+  "updated_at": "2020-05-13T06:35:19Z",
+  "archived_at": null,
+  "type": "Organization"
+}
\ No newline at end of file
diff --git a/src/test/resources/org/kohsuke/github/GitHubTest/wiremock/listOrganizationsFetchesType/mappings/1-user.json b/src/test/resources/org/kohsuke/github/GitHubTest/wiremock/listOrganizationsFetchesType/mappings/1-user.json
new file mode 100644
index 000000000..e1a7e928a
--- /dev/null
+++ b/src/test/resources/org/kohsuke/github/GitHubTest/wiremock/listOrganizationsFetchesType/mappings/1-user.json
@@ -0,0 +1,47 @@
+{
+  "id": "cd7530db-bd05-4525-9136-0bf80e609c23",
+  "name": "user",
+  "request": {
+    "url": "/user",
+    "method": "GET",
+    "headers": {
+      "Accept": {
+        "equalTo": "application/vnd.github+json"
+      }
+    }
+  },
+  "response": {
+    "status": 200,
+    "bodyFileName": "1-user.json",
+    "headers": {
+      "Date": "Sun, 16 Feb 2025 09:01:24 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/\"2288274ffc402f5864177c99f29e71d41d774342ce2b6fc9d2b12db0df22c6ef\"",
+      "Last-Modified": "Fri, 31 Jan 2025 11:08:30 GMT",
+      "X-OAuth-Scopes": "admin:enterprise, admin:gpg_key, admin:org, admin:org_hook, admin:public_key, admin:repo_hook, admin:ssh_signing_key, audit_log, codespace, delete:packages, delete_repo, gist, notifications, project, repo, user, workflow, write:discussion, write:packages",
+      "X-Accepted-OAuth-Scopes": "",
+      "X-GitHub-Media-Type": "github.v3; format=json",
+      "x-github-api-version-selected": "2022-11-28",
+      "X-RateLimit-Limit": "5000",
+      "X-RateLimit-Remaining": "4962",
+      "X-RateLimit-Reset": "1739697044",
+      "X-RateLimit-Used": "38",
+      "X-RateLimit-Resource": "core",
+      "Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset",
+      "Access-Control-Allow-Origin": "*",
+      "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'",
+      "Server": "github.com",
+      "X-GitHub-Request-Id": "415A:2933E2:783482:ABFF69:67B1A964"
+    }
+  },
+  "uuid": "cd7530db-bd05-4525-9136-0bf80e609c23",
+  "persistent": true,
+  "insertionIndex": 1
+}
\ No newline at end of file
diff --git a/src/test/resources/org/kohsuke/github/GitHubTest/wiremock/listOrganizationsFetchesType/mappings/2-organizations.json b/src/test/resources/org/kohsuke/github/GitHubTest/wiremock/listOrganizationsFetchesType/mappings/2-organizations.json
new file mode 100644
index 000000000..e53cbf176
--- /dev/null
+++ b/src/test/resources/org/kohsuke/github/GitHubTest/wiremock/listOrganizationsFetchesType/mappings/2-organizations.json
@@ -0,0 +1,47 @@
+{
+  "id": "87f5d76b-1169-4caf-ab76-04ac6b1e78b9",
+  "name": "organizations",
+  "request": {
+    "url": "/organizations?per_page=1",
+    "method": "GET",
+    "headers": {
+      "Accept": {
+        "equalTo": "application/vnd.github+json"
+      }
+    }
+  },
+  "response": {
+    "status": 200,
+    "bodyFileName": "2-organizations.json",
+    "headers": {
+      "Date": "Sun, 16 Feb 2025 09:01:25 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/\"575d7c16fc728d88180cb71aecfb2c215cdf28f0259a6eac1d93c607816f3722\"",
+      "X-OAuth-Scopes": "admin:enterprise, admin:gpg_key, admin:org, admin:org_hook, admin:public_key, admin:repo_hook, admin:ssh_signing_key, audit_log, codespace, delete:packages, delete_repo, gist, notifications, project, repo, user, workflow, write:discussion, write:packages",
+      "X-Accepted-OAuth-Scopes": "",
+      "X-GitHub-Media-Type": "github.v3; format=json",
+      "x-github-api-version-selected": "2022-11-28",
+      "X-RateLimit-Limit": "5000",
+      "X-RateLimit-Remaining": "4960",
+      "X-RateLimit-Reset": "1739697044",
+      "X-RateLimit-Used": "40",
+      "X-RateLimit-Resource": "core",
+      "Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset",
+      "Access-Control-Allow-Origin": "*",
+      "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'",
+      "Server": "github.com",
+      "X-GitHub-Request-Id": "58BE:2FA953:5F36F5:9301D2:67B1A965",
+      "Link": "<https://api.github.com/organizations?per_page=1&since=44>; rel=\"next\", <https://api.github.com/organizations{?since}>; rel=\"first\""
+    }
+  },
+  "uuid": "87f5d76b-1169-4caf-ab76-04ac6b1e78b9",
+  "persistent": true,
+  "insertionIndex": 2
+}
\ No newline at end of file
diff --git a/src/test/resources/org/kohsuke/github/GitHubTest/wiremock/listOrganizationsFetchesType/mappings/3-orgs_errfree.json b/src/test/resources/org/kohsuke/github/GitHubTest/wiremock/listOrganizationsFetchesType/mappings/3-orgs_errfree.json
new file mode 100644
index 000000000..13a81e17a
--- /dev/null
+++ b/src/test/resources/org/kohsuke/github/GitHubTest/wiremock/listOrganizationsFetchesType/mappings/3-orgs_errfree.json
@@ -0,0 +1,47 @@
+{
+  "id": "950bf65f-6dda-43d1-9ded-907e269024df",
+  "name": "orgs_errfree",
+  "request": {
+    "url": "/orgs/errfree",
+    "method": "GET",
+    "headers": {
+      "Accept": {
+        "equalTo": "application/vnd.github+json"
+      }
+    }
+  },
+  "response": {
+    "status": 200,
+    "bodyFileName": "3-orgs_errfree.json",
+    "headers": {
+      "Date": "Sun, 16 Feb 2025 09:01:25 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/\"fdf38082b2dec2c4293a820e9f615c582c00ca7ab2365a5db1a7f7a3a1905dbd\"",
+      "Last-Modified": "Wed, 13 May 2020 06:35:19 GMT",
+      "X-OAuth-Scopes": "admin:enterprise, admin:gpg_key, admin:org, admin:org_hook, admin:public_key, admin:repo_hook, admin:ssh_signing_key, audit_log, codespace, delete:packages, delete_repo, gist, notifications, project, repo, user, workflow, write:discussion, write:packages",
+      "X-Accepted-OAuth-Scopes": "admin:org, read:org, repo, user, write:org",
+      "X-GitHub-Media-Type": "github.v3; format=json",
+      "x-github-api-version-selected": "2022-11-28",
+      "X-RateLimit-Limit": "5000",
+      "X-RateLimit-Remaining": "4959",
+      "X-RateLimit-Reset": "1739697044",
+      "X-RateLimit-Used": "41",
+      "X-RateLimit-Resource": "core",
+      "Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset",
+      "Access-Control-Allow-Origin": "*",
+      "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'",
+      "Server": "github.com",
+      "X-GitHub-Request-Id": "1EF0:22ABA6:638768:974F6C:67B1A965"
+    }
+  },
+  "uuid": "950bf65f-6dda-43d1-9ded-907e269024df",
+  "persistent": true,
+  "insertionIndex": 3
+}
\ No newline at end of file