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,16 +61,38 @@ 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 ) ;
6971 this .maybeRepo = Optional .empty ();
7072 }
7173
74+ GithubAppClient (final GitHubClient github ) {
75+ this .github = github ;
76+ this .maybeOwner = Optional .empty ();
77+ this .maybeRepo = Optional .empty ();
78+ }
79+
80+ /**
81+ * Gets the owner, throwing a descriptive exception if not present.
82+ *
83+ * @return the owner string
84+ * @throws IllegalStateException if owner is not present
85+ */
86+ private String requireOwner () {
87+ return maybeOwner .orElseThrow (
88+ () ->
89+ new IllegalStateException (
90+ "This operation requires an owner context. "
91+ + "Use GitHubClient.createOrganisationClient(owner).createGithubAppClient() "
92+ + "or GitHubClient.createRepositoryClient(owner, repo).createGithubAppClient() "
93+ + "instead of GitHubClient.createGithubAppClient()" ));
94+ }
95+
7296 /**
7397 * List Installations of an app.
7498 *
@@ -99,29 +123,32 @@ public CompletableFuture<Installation> getInstallation(final Integer installatio
99123
100124 /**
101125 * Get an installation of a repo
126+ *
102127 * @return an Installation
103128 */
104129 private CompletableFuture <Installation > getRepoInstallation (final String repo ) {
105130 return github .request (
106- String .format (GET_INSTALLATION_REPO_URL , owner , repo ), Installation .class );
131+ String .format (GET_INSTALLATION_REPO_URL , requireOwner () , repo ), Installation .class );
107132 }
108133
109134 /**
110135 * Get an installation of an org
136+ *
111137 * @return an Installation
112138 */
113139 private CompletableFuture <Installation > getOrgInstallation () {
114140 return github .request (
115- String .format (GET_INSTALLATION_ORG_URL , owner ), Installation .class );
141+ String .format (GET_INSTALLATION_ORG_URL , requireOwner () ), Installation .class );
116142 }
117143
118- /**
144+ /**
119145 * Get an installation of a user
146+ *
120147 * @return an Installation
121148 */
122149 public CompletableFuture <Installation > getUserInstallation () {
123150 return github .request (
124- String .format (GET_INSTALLATION_USER_URL , owner ), Installation .class );
151+ String .format (GET_INSTALLATION_USER_URL , requireOwner () ), Installation .class );
125152 }
126153
127154 /**
@@ -146,4 +173,17 @@ public CompletableFuture<InstallationRepositoriesResponse> listAccessibleReposit
146173 return GitHubClient .scopeForInstallationId (github , installationId )
147174 .request (LIST_ACCESSIBLE_REPOS_URL , InstallationRepositoriesResponse .class , extraHeaders );
148175 }
176+
177+ /**
178+ * Get the authenticated GitHub App.
179+ *
180+ * <p>Returns the authenticated app. You must use a JWT to access this endpoint.
181+ *
182+ * <p>see https://docs.github.com/en/rest/apps/apps#get-the-authenticated-app
183+ *
184+ * @return the authenticated App
185+ */
186+ public CompletableFuture <App > getAuthenticatedApp () {
187+ return github .request (GET_AUTHENTICATED_APP_URL , App .class );
188+ }
149189}
0 commit comments