Repository: hub4j/github-api
Pull request #1748 (2023-11-21 16:26:29)

Allow query parameters for listInstallations query
# Description

Added a change to allow the option to pass in the `since` parameter per https://developer.github.com/v3/apps/#list-installations. 


# 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/GHApp.java b/src/main/java/org/kohsuke/github/GHApp.java
index 91b24b275..2da38b79f 100644
--- a/src/main/java/org/kohsuke/github/GHApp.java
+++ b/src/main/java/org/kohsuke/github/GHApp.java
@@ -6,6 +6,7 @@ import org.kohsuke.github.internal.EnumUtils;
 import java.io.IOException;
 import java.net.URL;
 import java.util.Collections;
+import java.util.Date;
 import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
@@ -209,10 +210,26 @@ public class GHApp extends GHObject {
      */
     @Preview(MACHINE_MAN)
     public PagedIterable<GHAppInstallation> listInstallations() {
-        return root().createRequest()
-                .withPreview(MACHINE_MAN)
-                .withUrlPath("/app/installations")
-                .toIterable(GHAppInstallation[].class, null);
+        return listInstallations(null);
+    }
+
+    /**
+     * Obtains all the installations associated with this app since a given date.
+     * <p>
+     * You must use a JWT to access this endpoint.
+     *
+     * @param since
+     *            - Allows users to get installations that have been updated since a given date.
+     * @return a list of App installations since a given time.
+     * @see <a href="https://developer.github.com/v3/apps/#list-installations">List installations</a>
+     */
+    @Preview(MACHINE_MAN)
+    public PagedIterable<GHAppInstallation> listInstallations(final Date since) {
+        Requester requester = root().createRequest().withPreview(MACHINE_MAN).withUrlPath("/app/installations");
+        if (since != null) {
+            requester.with("since", GitHubClient.printDate(since));
+        }
+        return requester.toIterable(GHAppInstallation[].class, null);
     }
 
     /**


Test patch:
diff --git a/src/test/java/org/kohsuke/github/GHAppTest.java b/src/test/java/org/kohsuke/github/GHAppTest.java
index 91c1bb9d1..2ceb479e4 100644
--- a/src/test/java/org/kohsuke/github/GHAppTest.java
+++ b/src/test/java/org/kohsuke/github/GHAppTest.java
@@ -3,11 +3,15 @@ package org.kohsuke.github;
 import org.junit.Test;
 
 import java.io.IOException;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.TimeZone;
 
 import static org.hamcrest.Matchers.*;
 import static org.junit.Assert.assertThrows;
@@ -85,6 +89,28 @@ public class GHAppTest extends AbstractGHAppInstallationTest {
         testAppInstallation(appInstallation);
     }
 
+    /**
+     * List installations that have been updated since a given date.
+     *
+     * @throws IOException
+     *             Signals that an I/O exception has occurred.
+     *
+     * @throws ParseException
+     *             Issue parsing date string.
+     */
+    @Test
+    public void listInstallationsSince() throws IOException, ParseException {
+        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
+        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
+        Date localDate = simpleDateFormat.parse("2023-11-01");
+        GHApp app = gitHub.getApp();
+        List<GHAppInstallation> installations = app.listInstallations(localDate).toList();
+        assertThat(installations.size(), is(1));
+
+        GHAppInstallation appInstallation = installations.get(0);
+        testAppInstallation(appInstallation);
+    }
+
     /**
      * Gets the installation by id.
      *
diff --git a/src/test/resources/org/kohsuke/github/GHAppTest/wiremock/listInstallationsSince/__files/body-mapping-githubapp-app.json b/src/test/resources/org/kohsuke/github/GHAppTest/wiremock/listInstallationsSince/__files/body-mapping-githubapp-app.json
new file mode 100644
index 000000000..d36be086d
--- /dev/null
+++ b/src/test/resources/org/kohsuke/github/GHAppTest/wiremock/listInstallationsSince/__files/body-mapping-githubapp-app.json
@@ -0,0 +1,41 @@
+{
+  "id": 11111,
+  "node_id": "MDM6QXBwMzI2MTY=",
+  "owner": {
+    "login": "bogus",
+    "id": 111111111,
+    "node_id": "asdfasdfasdf",
+    "avatar_url": "https://avatars2.githubusercontent.com/u/111111111?v=4",
+    "gravatar_id": "",
+    "url": "https://api.github.com/users/bogus",
+    "html_url": "https://github.com/bogus",
+    "followers_url": "https://api.github.com/users/bogus/followers",
+    "following_url": "https://api.github.com/users/bogus/following{/other_user}",
+    "gists_url": "https://api.github.com/users/bogus/gists{/gist_id}",
+    "starred_url": "https://api.github.com/users/bogus/starred{/owner}{/repo}",
+    "subscriptions_url": "https://api.github.com/users/bogus/subscriptions",
+    "organizations_url": "https://api.github.com/users/bogus/orgs",
+    "repos_url": "https://api.github.com/users/bogus/repos",
+    "events_url": "https://api.github.com/users/bogus/events{/privacy}",
+    "received_events_url": "https://api.github.com/users/bogus/received_events",
+    "type": "Organization",
+    "site_admin": false
+  },
+  "name": "Bogus-Development",
+  "description": "",
+  "external_url": "https://bogus.domain.com",
+  "html_url": "https://github.com/apps/bogus-development",
+  "created_at": "2019-06-10T04:21:41Z",
+  "updated_at": "2019-06-10T04:21:41Z",
+  "permissions": {
+    "checks": "write",
+    "contents": "read",
+    "metadata": "read",
+    "pull_requests": "write"
+  },
+  "events": [
+    "pull_request",
+    "push"
+  ],
+  "installations_count": 1
+}
\ No newline at end of file
diff --git a/src/test/resources/org/kohsuke/github/GHAppTest/wiremock/listInstallationsSince/__files/body-mapping-githubapp-installations.json b/src/test/resources/org/kohsuke/github/GHAppTest/wiremock/listInstallationsSince/__files/body-mapping-githubapp-installations.json
new file mode 100644
index 000000000..4ca1c46c8
--- /dev/null
+++ b/src/test/resources/org/kohsuke/github/GHAppTest/wiremock/listInstallationsSince/__files/body-mapping-githubapp-installations.json
@@ -0,0 +1,45 @@
+[
+  {
+    "id": 11111111,
+    "account": {
+      "login": "bogus",
+      "id": 111111111,
+      "node_id": "asdfasdfasdf",
+      "avatar_url": "https://avatars2.githubusercontent.com/u/111111111?v=4",
+      "gravatar_id": "",
+      "url": "https://api.github.com/users/bogus",
+      "html_url": "https://github.com/bogus",
+      "followers_url": "https://api.github.com/users/bogus/followers",
+      "following_url": "https://api.github.com/users/bogus/following{/other_user}",
+      "gists_url": "https://api.github.com/users/bogus/gists{/gist_id}",
+      "starred_url": "https://api.github.com/users/bogus/starred{/owner}{/repo}",
+      "subscriptions_url": "https://api.github.com/users/bogus/subscriptions",
+      "organizations_url": "https://api.github.com/users/bogus/orgs",
+      "repos_url": "https://api.github.com/users/bogus/repos",
+      "events_url": "https://api.github.com/users/bogus/events{/privacy}",
+      "received_events_url": "https://api.github.com/users/bogus/received_events",
+      "type": "Organization",
+      "site_admin": false
+    },
+    "repository_selection": "selected",
+    "access_tokens_url": "https://api.github.com/app/installations/11111111/access_tokens",
+    "repositories_url": "https://api.github.com/installation/repositories",
+    "html_url": "https://github.com/organizations/bogus/settings/installations/11111111",
+    "app_id": 11111,
+    "target_id": 111111111,
+    "target_type": "Organization",
+    "permissions": {
+      "checks": "write",
+      "pull_requests": "write",
+      "contents": "read",
+      "metadata": "read"
+    },
+    "events": [
+      "pull_request",
+      "push"
+    ],
+    "created_at": "2019-07-04T01:19:36.000Z",
+    "updated_at": "2019-07-30T22:48:09.000Z",
+    "single_file_name": null
+  }
+]
\ No newline at end of file
diff --git a/src/test/resources/org/kohsuke/github/GHAppTest/wiremock/listInstallationsSince/mappings/mapping-githubapp-app.json b/src/test/resources/org/kohsuke/github/GHAppTest/wiremock/listInstallationsSince/mappings/mapping-githubapp-app.json
new file mode 100644
index 000000000..2e7a553f6
--- /dev/null
+++ b/src/test/resources/org/kohsuke/github/GHAppTest/wiremock/listInstallationsSince/mappings/mapping-githubapp-app.json
@@ -0,0 +1,37 @@
+{
+  "request": {
+    "url": "/app",
+    "method": "GET",
+    "headers": {
+      "Accept": {
+        "equalTo": "application/vnd.github.machine-man-preview+json"
+      }
+    }
+  },
+  "response": {
+    "status": 200,
+    "bodyFileName": "body-mapping-githubapp-app.json",
+    "headers": {
+      "Server": "GitHub.com",
+      "Date": "Fri, 09 Aug 2019 05:36:38 GMT",
+      "Content-Type": "application/json; charset=utf-8",
+      "Status": "200 OK",
+      "Cache-Control": "public, max-age=60, s-maxage=60",
+      "Vary": [
+        "Accept",
+        "Accept-Encoding"
+      ],
+      "ETag": "W/\"01163b1a237898d328ed56cd0e9aefca\"",
+      "X-GitHub-Media-Type": "github.machine-man-preview; format=json",
+      "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": "*",
+      "Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
+      "Content-Security-Policy": "default-src 'none'",
+      "Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
+      "X-Content-Type-Options": "nosniff",
+      "X-Frame-Options": "deny",
+      "X-XSS-Protection": "1; mode=block",
+      "X-GitHub-Request-Id": "E0C4:3088:300C54:3ACB77:5D4D0666"
+    }
+  }
+}
\ No newline at end of file
diff --git a/src/test/resources/org/kohsuke/github/GHAppTest/wiremock/listInstallationsSince/mappings/mapping-githubapp-installations.json b/src/test/resources/org/kohsuke/github/GHAppTest/wiremock/listInstallationsSince/mappings/mapping-githubapp-installations.json
new file mode 100644
index 000000000..cb402b4a4
--- /dev/null
+++ b/src/test/resources/org/kohsuke/github/GHAppTest/wiremock/listInstallationsSince/mappings/mapping-githubapp-installations.json
@@ -0,0 +1,42 @@
+{
+  "request": {
+    "urlPathPattern": "/app/installations",
+    "method": "GET",
+    "headers": {
+      "Accept": {
+        "equalTo": "application/vnd.github.machine-man-preview+json"
+      }
+    },
+    "queryParameters": {
+      "since": {
+        "equalTo": "2023-11-01T00:00:00Z"
+      }
+    }
+  },
+  "response": {
+    "status": 200,
+    "bodyFileName": "body-mapping-githubapp-installations.json",
+    "headers": {
+      "Server": "GitHub.com",
+      "Date": "Fri, 09 Aug 2019 05:36:38 GMT",
+      "Content-Type": "application/json; charset=utf-8",
+      "Status": "200 OK",
+      "Cache-Control": "public, max-age=60, s-maxage=60",
+      "Vary": [
+        "Accept",
+        "Accept-Encoding"
+      ],
+      "ETag": "W/\"01163b1a237898d328ed56cd0e9aefca\"",
+      "X-GitHub-Media-Type": "github.machine-man-preview; format=json",
+      "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": "*",
+      "Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
+      "Content-Security-Policy": "default-src 'none'",
+      "Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
+      "X-Content-Type-Options": "nosniff",
+      "X-Frame-Options": "deny",
+      "X-XSS-Protection": "1; mode=block",
+      "X-GitHub-Request-Id": "E0C4:3088:300C54:3ACB77:5D4D0666"
+    }
+  }
+}
\ No newline at end of file