forked from spotify/github-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGithubAppClient.java
More file actions
189 lines (167 loc) · 6.31 KB
/
GithubAppClient.java
File metadata and controls
189 lines (167 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*-
* -\-\-
* github-api
* --
* Copyright (C) 2016 - 2020 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/
package com.spotify.github.v3.clients;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.ImmutableMap;
import com.spotify.github.v3.apps.InstallationRepositoriesResponse;
import com.spotify.github.v3.checks.AccessToken;
import com.spotify.github.v3.checks.App;
import com.spotify.github.v3.checks.Installation;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import javax.ws.rs.core.HttpHeaders;
/** Apps API client */
public class GithubAppClient {
private static final String GET_AUTHENTICATED_APP_URL = "/app";
private static final String GET_INSTALLATION_BY_ID_URL = "/app/installations/%s";
private static final String GET_ACCESS_TOKEN_URL = "/app/installations/%s/access_tokens";
private static final String GET_INSTALLATIONS_URL = "/app/installations?per_page=100";
private static final String GET_INSTALLATION_REPO_URL = "/repos/%s/%s/installation";
private static final String LIST_ACCESSIBLE_REPOS_URL = "/installation/repositories";
/*
Owner and org are interchangeable and therefore "owner" is used to
refer to the organisation in the installation endpoint
*/
private static final String GET_INSTALLATION_ORG_URL = "/orgs/%s/installation";
private static final String GET_INSTALLATION_USER_URL = "/users/%s/installation";
private final GitHubClient github;
private final Optional<String> maybeOwner;
private final Optional<String> maybeRepo;
private final Map<String, String> extraHeaders =
ImmutableMap.of(HttpHeaders.ACCEPT, "application/vnd.github.machine-man-preview+json");
private static final TypeReference<List<Installation>> INSTALLATION_LIST_TYPE_REFERENCE =
new TypeReference<>() {};
GithubAppClient(final GitHubClient github, final String owner, final String repo) {
this.github = github;
this.maybeOwner = Optional.of(owner);
this.maybeRepo = Optional.of(repo);
}
GithubAppClient(final GitHubClient github, final String owner) {
this.github = github;
this.maybeOwner = Optional.of(owner);
this.maybeRepo = Optional.empty();
}
GithubAppClient(final GitHubClient github) {
this.github = github;
this.maybeOwner = Optional.empty();
this.maybeRepo = Optional.empty();
}
/**
* Gets the owner, throwing a descriptive exception if not present.
*
* @return the owner string
* @throws IllegalStateException if owner is not present
*/
private String requireOwner() {
return maybeOwner.orElseThrow(
() ->
new IllegalStateException(
"This operation requires an owner context. "
+ "Use GitHubClient.createOrganisationClient(owner).createGithubAppClient() "
+ "or GitHubClient.createRepositoryClient(owner, repo).createGithubAppClient() "
+ "instead of GitHubClient.createGithubAppClient()"));
}
/**
* List Installations of an app.
*
* @return a list of Installation
*/
public CompletableFuture<List<Installation>> getInstallations() {
return github.request(GET_INSTALLATIONS_URL, INSTALLATION_LIST_TYPE_REFERENCE, extraHeaders);
}
/**
* Get Installation of repo or org
*
* @return an Installation
*/
public CompletableFuture<Installation> getInstallation() {
return maybeRepo.map(this::getRepoInstallation).orElseGet(this::getOrgInstallation);
}
/**
* Get Installation identified by its installation id
*
* @return an Installation
*/
public CompletableFuture<Installation> getInstallation(final Integer installationId) {
return github.request(
String.format(GET_INSTALLATION_BY_ID_URL, installationId), Installation.class);
}
/**
* Get an installation of a repo
*
* @return an Installation
*/
private CompletableFuture<Installation> getRepoInstallation(final String repo) {
return github.request(
String.format(GET_INSTALLATION_REPO_URL, requireOwner(), repo), Installation.class);
}
/**
* Get an installation of an org
*
* @return an Installation
*/
private CompletableFuture<Installation> getOrgInstallation() {
return github.request(
String.format(GET_INSTALLATION_ORG_URL, requireOwner()), Installation.class);
}
/**
* Get an installation of a user
*
* @return an Installation
*/
public CompletableFuture<Installation> getUserInstallation() {
return github.request(
String.format(GET_INSTALLATION_USER_URL, requireOwner()), Installation.class);
}
/**
* Authenticates as an installation
*
* @return an Installation Token
*/
public CompletableFuture<AccessToken> getAccessToken(final Integer installationId) {
final String path = String.format(GET_ACCESS_TOKEN_URL, installationId);
return github.post(path, "", AccessToken.class, extraHeaders);
}
/**
* Lists the repositories that an app installation can access.
*
* <p>see
* https://docs.github.com/en/free-pro-team@latest/rest/reference/apps#list-repositories-accessible-to-the-app-installation
*/
public CompletableFuture<InstallationRepositoriesResponse> listAccessibleRepositories(
final int installationId) {
return GitHubClient.scopeForInstallationId(github, installationId)
.request(LIST_ACCESSIBLE_REPOS_URL, InstallationRepositoriesResponse.class, extraHeaders);
}
/**
* Get the authenticated GitHub App.
*
* <p>Returns the authenticated app. You must use a JWT to access this endpoint.
*
* <p>see https://docs.github.com/en/rest/apps/apps#get-the-authenticated-app
*
* @return the authenticated App
*/
public CompletableFuture<App> getAuthenticatedApp() {
return github.request(GET_AUTHENTICATED_APP_URL, App.class);
}
}