77 * Licensed under the Apache License, Version 2.0 (the "License");
88 * you may not use this file except in compliance with the License.
99 * You may obtain a copy of the License at
10- *
10+ *
1111 * http://www.apache.org/licenses/LICENSE-2.0
12- *
12+ *
1313 * Unless required by applicable law or agreed to in writing, software
1414 * distributed under the License is distributed on an "AS IS" BASIS,
1515 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2424import com .google .common .collect .ImmutableMap ;
2525import com .spotify .github .v3 .apps .InstallationRepositoriesResponse ;
2626import com .spotify .github .v3 .checks .AccessToken ;
27+ import com .spotify .github .v3 .checks .App ;
2728import com .spotify .github .v3 .checks .Installation ;
2829import java .util .List ;
2930import java .util .Map ;
3435/** Apps API client */
3536public class GithubAppClient {
3637
38+ private static final String GET_AUTHENTICATED_APP_URL = "/app" ;
3739 private static final String GET_INSTALLATION_BY_ID_URL = "/app/installations/%s" ;
3840 private static final String GET_ACCESS_TOKEN_URL = "/app/installations/%s/access_tokens" ;
3941 private static final String GET_INSTALLATIONS_URL = "/app/installations?per_page=100" ;
@@ -48,7 +50,7 @@ public class GithubAppClient {
4850 private static final String GET_INSTALLATION_USER_URL = "/users/%s/installation" ;
4951
5052 private final GitHubClient github ;
51- private final String owner ;
53+ private final Optional < String > maybeOwner ;
5254 private final Optional <String > maybeRepo ;
5355
5456 private final Map <String , String > extraHeaders =
@@ -59,13 +61,19 @@ public class GithubAppClient {
5961
6062 GithubAppClient (final GitHubClient github , final String owner , final String repo ) {
6163 this .github = github ;
62- this .owner = owner ;
64+ this .maybeOwner = Optional . of ( owner ) ;
6365 this .maybeRepo = Optional .of (repo );
6466 }
6567
6668 GithubAppClient (final GitHubClient github , final String owner ) {
6769 this .github = github ;
68- this .owner = owner ;
70+ this .maybeOwner = Optional .of (owner );
71+ this .maybeRepo = Optional .empty ();
72+ }
73+
74+ GithubAppClient (final GitHubClient github ) {
75+ this .github = github ;
76+ this .maybeOwner = Optional .empty ();
6977 this .maybeRepo = Optional .empty ();
7078 }
7179
@@ -99,29 +107,32 @@ public CompletableFuture<Installation> getInstallation(final Integer installatio
99107
100108 /**
101109 * Get an installation of a repo
110+ *
102111 * @return an Installation
103112 */
104113 private CompletableFuture <Installation > getRepoInstallation (final String repo ) {
105114 return github .request (
106- String .format (GET_INSTALLATION_REPO_URL , owner , repo ), Installation .class );
115+ String .format (GET_INSTALLATION_REPO_URL , maybeOwner . get () , repo ), Installation .class );
107116 }
108117
109118 /**
110119 * Get an installation of an org
120+ *
111121 * @return an Installation
112122 */
113123 private CompletableFuture <Installation > getOrgInstallation () {
114124 return github .request (
115- String .format (GET_INSTALLATION_ORG_URL , owner ), Installation .class );
125+ String .format (GET_INSTALLATION_ORG_URL , maybeOwner . get () ), Installation .class );
116126 }
117127
118- /**
128+ /**
119129 * Get an installation of a user
130+ *
120131 * @return an Installation
121132 */
122133 public CompletableFuture <Installation > getUserInstallation () {
123134 return github .request (
124- String .format (GET_INSTALLATION_USER_URL , owner ), Installation .class );
135+ String .format (GET_INSTALLATION_USER_URL , maybeOwner . get () ), Installation .class );
125136 }
126137
127138 /**
@@ -146,4 +157,17 @@ public CompletableFuture<InstallationRepositoriesResponse> listAccessibleReposit
146157 return GitHubClient .scopeForInstallationId (github , installationId )
147158 .request (LIST_ACCESSIBLE_REPOS_URL , InstallationRepositoriesResponse .class , extraHeaders );
148159 }
160+
161+ /**
162+ * Get the authenticated GitHub App.
163+ *
164+ * <p>Returns the authenticated app. You must use a JWT to access this endpoint.
165+ *
166+ * <p>see https://docs.github.com/en/rest/apps/apps#get-the-authenticated-app
167+ *
168+ * @return the authenticated App
169+ */
170+ public CompletableFuture <App > getAuthenticatedApp () {
171+ return github .request (GET_AUTHENTICATED_APP_URL , App .class );
172+ }
149173}
0 commit comments