Repository: hub4j/github-api
Pull request #1832 (2024-04-12 10:18:08)

Add GHOrganization.listSecurityManagers()
# Description

Add support to list [Security Managers](https://docs.github.com/en/organizations/managing-peoples-access-to-your-organization-with-roles/managing-security-managers-in-your-organization) in a GitHub Organization. See [GitHub's API doc for this API](https://docs.github.com/en/rest/orgs/security-managers?apiVersion=2022-11-28#list-security-manager-teams) for more details about this API.

## Tests:
If I'm given access to the `hub4j-test-org` Org, including permission to create a team and configure that team as a Security Manager team (or someone else can set this  up for me), I can include an integration test (I didn't find a step-by-step process in CONTRIBUTING.md to get this access. 

# 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. 
- [ ] 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.
- [ ] 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:
  - [ ] If this PR fixes one or more issues, include "Fixes #<issue number>" lines for each issue. 
  - [ ] Provide links to relevant documentation on https://docs.github.com/en/rest where possible. If not including links, explain why not.
- [ ] 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/GHOrganization.java b/src/main/java/org/kohsuke/github/GHOrganization.java
index 864057e1c..45711792e 100644
--- a/src/main/java/org/kohsuke/github/GHOrganization.java
+++ b/src/main/java/org/kohsuke/github/GHOrganization.java
@@ -450,6 +450,19 @@ public class GHOrganization extends GHPerson {
                 .toIterable(GHUser[].class, null);
     }
 
+    /**
+     * List up all the security managers.
+     *
+     * @return the paged iterable
+     * @throws IOException
+     *             the io exception
+     */
+    public PagedIterable<GHTeam> listSecurityManagers() throws IOException {
+        return root().createRequest()
+                .withUrlPath(String.format("/orgs/%s/security-managers", login))
+                .toIterable(GHTeam[].class, item -> item.wrapUp(this));
+    }
+
     /**
      * Conceals the membership.
      *


Test patch:
diff --git a/src/test/java/org/kohsuke/github/GHOrganizationTest.java b/src/test/java/org/kohsuke/github/GHOrganizationTest.java
index 6cd384e09..668c42f88 100644
--- a/src/test/java/org/kohsuke/github/GHOrganizationTest.java
+++ b/src/test/java/org/kohsuke/github/GHOrganizationTest.java
@@ -335,6 +335,25 @@ public class GHOrganizationTest extends AbstractGitHubWireMockTest {
                         "timja"));
     }
 
+    /**
+     * Test list security managers.
+     *
+     * @throws IOException
+     *             Signals that an I/O exception has occurred.
+     */
+    @Test
+    public void testListSecurityManagers() throws IOException {
+        GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
+
+        List<GHTeam> securityManagers = org.listSecurityManagers().toList();
+
+        assertThat(securityManagers, notNullValue());
+        // In case more are added in the future
+        assertThat(securityManagers.size(), greaterThanOrEqualTo(1));
+        assertThat(securityManagers.stream().map(GHTeam::getName).collect(Collectors.toList()),
+                hasItems("security team"));
+    }
+
     /**
      * Test list outside collaborators.
      *
diff --git a/src/test/resources/org/kohsuke/github/GHOrganizationTest/wiremock/testListSecurityManagers/__files/1-security-managers.json b/src/test/resources/org/kohsuke/github/GHOrganizationTest/wiremock/testListSecurityManagers/__files/1-security-managers.json
new file mode 100644
index 000000000..20585daf2
--- /dev/null
+++ b/src/test/resources/org/kohsuke/github/GHOrganizationTest/wiremock/testListSecurityManagers/__files/1-security-managers.json
@@ -0,0 +1,16 @@
+[
+    {
+      "name": "security team",
+      "id": 31337,
+      "node_id": "MDQ6VGVhbTMxMzM3",
+      "slug": "security-team",
+      "description": "Security manager role access to all git repositories",
+      "privacy": "closed",
+      "notification_setting": "notifications_enabled",
+      "url": "https://api.github.com/organizations/7544739/team/31337",
+      "html_url": "https://api.github.com/orgs/hub4j-test-org/teams/schibsted-data-security-team",
+      "members_url": "https://api.github.com/api/v3/organizations/7544739/team/31337/members{/member}",
+      "repositories_url": "https://api.github.com/api/v3/organizations/7544739/team/31337/repos",
+      "permission": "pull"
+    }
+]
\ No newline at end of file
diff --git a/src/test/resources/org/kohsuke/github/GHOrganizationTest/wiremock/testListSecurityManagers/__files/2-orgs_hub4j-test-org.json b/src/test/resources/org/kohsuke/github/GHOrganizationTest/wiremock/testListSecurityManagers/__files/2-orgs_hub4j-test-org.json
new file mode 100644
index 000000000..731f70709
--- /dev/null
+++ b/src/test/resources/org/kohsuke/github/GHOrganizationTest/wiremock/testListSecurityManagers/__files/2-orgs_hub4j-test-org.json
@@ -0,0 +1,41 @@
+{
+  "login": "hub4j-test-org",
+  "id": 7544739,
+  "node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
+  "url": "https://api.github.com/orgs/hub4j-test-org",
+  "repos_url": "https://api.github.com/orgs/hub4j-test-org/repos",
+  "events_url": "https://api.github.com/orgs/hub4j-test-org/events",
+  "hooks_url": "https://api.github.com/orgs/hub4j-test-org/hooks",
+  "issues_url": "https://api.github.com/orgs/hub4j-test-org/issues",
+  "members_url": "https://api.github.com/orgs/hub4j-test-org/members{/member}",
+  "public_members_url": "https://api.github.com/orgs/hub4j-test-org/public_members{/member}",
+  "avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
+  "description": null,
+  "is_verified": false,
+  "has_organization_projects": true,
+  "has_repository_projects": true,
+  "public_repos": 11,
+  "public_gists": 0,
+  "followers": 0,
+  "following": 0,
+  "html_url": "https://github.com/hub4j-test-org",
+  "created_at": "2014-05-10T19:39:11Z",
+  "updated_at": "2015-04-20T00:42:30Z",
+  "type": "Organization",
+  "total_private_repos": 0,
+  "owned_private_repos": 0,
+  "private_gists": 0,
+  "disk_usage": 147,
+  "collaborators": 0,
+  "billing_email": "kk@kohsuke.org",
+  "default_repository_permission": "none",
+  "members_can_create_repositories": false,
+  "two_factor_requirement_enabled": false,
+  "plan": {
+    "name": "free",
+    "space": 976562499,
+    "private_repos": 0,
+    "filled_seats": 12,
+    "seats": 0
+  }
+}
\ No newline at end of file
diff --git a/src/test/resources/org/kohsuke/github/GHOrganizationTest/wiremock/testListSecurityManagers/mappings/1-security-managers.json b/src/test/resources/org/kohsuke/github/GHOrganizationTest/wiremock/testListSecurityManagers/mappings/1-security-managers.json
new file mode 100644
index 000000000..27c4dbb24
--- /dev/null
+++ b/src/test/resources/org/kohsuke/github/GHOrganizationTest/wiremock/testListSecurityManagers/mappings/1-security-managers.json
@@ -0,0 +1,48 @@
+{
+  "id": "a3604d73-d76d-4f3e-8f7f-cecee94c5cef",
+  "name": "user",
+  "request": {
+    "url": "/orgs/hub4j-test-org/security-managers",
+    "method": "GET",
+    "headers": {
+      "Accept": {
+        "equalTo": "application/vnd.github.v3+json"
+      }
+    }
+  },
+  "response": {
+    "status": 200,
+    "bodyFileName": "1-security-managers.json",
+    "headers": {
+      "Server": "GitHub.com",
+      "Date": "Tue, 25 Feb 2020 14:27:54 GMT",
+      "Content-Type": "application/json; charset=utf-8",
+      "Status": "200 OK",
+      "X-RateLimit-Limit": "5000",
+      "X-RateLimit-Remaining": "4999",
+      "X-RateLimit-Reset": "1582644474",
+      "Cache-Control": "private, max-age=60, s-maxage=60",
+      "Vary": [
+        "Accept, Authorization, Cookie, X-GitHub-OTP",
+        "Accept-Encoding, Accept, X-Requested-With"
+      ],
+      "ETag": "W/\"9adfa44fe91fb698b5fd807d9471afc3\"",
+      "Last-Modified": "Tue, 18 Feb 2020 13:29:56 GMT",
+      "X-OAuth-Scopes": "delete_repo, repo, user",
+      "X-Accepted-OAuth-Scopes": "",
+      "X-GitHub-Media-Type": "unknown, github.v3",
+      "Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type",
+      "Access-Control-Allow-Origin": "*",
+      "Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
+      "X-Frame-Options": "deny",
+      "X-Content-Type-Options": "nosniff",
+      "X-XSS-Protection": "1; mode=block",
+      "Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
+      "Content-Security-Policy": "default-src 'none'",
+      "X-GitHub-Request-Id": "E686:35E3:5882BF:ABE338:5E552EEA"
+    }
+  },
+  "uuid": "a3604d73-d76d-4f3e-8f7f-cecee94c5cef",
+  "persistent": true,
+  "insertionIndex": 1
+}
\ No newline at end of file
diff --git a/src/test/resources/org/kohsuke/github/GHOrganizationTest/wiremock/testListSecurityManagers/mappings/2-orgs_hub4j-test-org.json b/src/test/resources/org/kohsuke/github/GHOrganizationTest/wiremock/testListSecurityManagers/mappings/2-orgs_hub4j-test-org.json
new file mode 100644
index 000000000..e337b8d47
--- /dev/null
+++ b/src/test/resources/org/kohsuke/github/GHOrganizationTest/wiremock/testListSecurityManagers/mappings/2-orgs_hub4j-test-org.json
@@ -0,0 +1,48 @@
+{
+  "id": "6682f58b-c93b-4525-9e99-485e631aaaab",
+  "name": "orgs_hub4j-test-org",
+  "request": {
+    "url": "/orgs/hub4j-test-org",
+    "method": "GET",
+    "headers": {
+      "Accept": {
+        "equalTo": "application/vnd.github.v3+json"
+      }
+    }
+  },
+  "response": {
+    "status": 200,
+    "bodyFileName": "2-orgs_hub4j-test-org.json",
+    "headers": {
+      "Server": "GitHub.com",
+      "Date": "Tue, 25 Feb 2020 14:29:12 GMT",
+      "Content-Type": "application/json; charset=utf-8",
+      "Status": "200 OK",
+      "X-RateLimit-Limit": "5000",
+      "X-RateLimit-Remaining": "4991",
+      "X-RateLimit-Reset": "1582644474",
+      "Cache-Control": "private, max-age=60, s-maxage=60",
+      "Vary": [
+        "Accept, Authorization, Cookie, X-GitHub-OTP",
+        "Accept-Encoding, Accept, X-Requested-With"
+      ],
+      "ETag": "W/\"712644daa44df3089a27d6ef60979929\"",
+      "Last-Modified": "Mon, 20 Apr 2015 00:42:30 GMT",
+      "X-OAuth-Scopes": "delete_repo, repo, user",
+      "X-Accepted-OAuth-Scopes": "admin:org, read:org, repo, user, write:org",
+      "X-GitHub-Media-Type": "unknown, github.v3",
+      "Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type",
+      "Access-Control-Allow-Origin": "*",
+      "Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
+      "X-Frame-Options": "deny",
+      "X-Content-Type-Options": "nosniff",
+      "X-XSS-Protection": "1; mode=block",
+      "Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
+      "Content-Security-Policy": "default-src 'none'",
+      "X-GitHub-Request-Id": "E7D4:4A52:336FA3:797A80:5E552F38"
+    }
+  },
+  "uuid": "6682f58b-c93b-4525-9e99-485e631aaaab",
+  "persistent": true,
+  "insertionIndex": 2
+}
\ No newline at end of file