Skip to content

Commit 06804f7

Browse files
Support GitHub Enterprise.
BUG=477915865 GWSQ_IGNORE: vcarbonaro@google.com PiperOrigin-RevId: 874716222 Change-Id: I5766381792022f125c580615fcc12dbd2983cc78
1 parent fa21eb1 commit 06804f7

File tree

3 files changed

+69
-9
lines changed

3 files changed

+69
-9
lines changed

java/com/google/copybara/git/GitHubOptions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.google.api.client.http.javanet.NetHttpTransport;
2323
import com.google.common.annotations.VisibleForTesting;
2424
import com.google.common.base.Preconditions;
25-
import com.google.common.base.VerifyException;
2625
import com.google.common.collect.ImmutableList;
2726
import com.google.copybara.GeneralOptions;
2827
import com.google.copybara.LazyResourceLoader;
@@ -227,7 +226,8 @@ protected GitHubApiTransport newTransport(
227226
if (gitHubHostName.equals("github.com")) {
228227
return newTransport(repo, storePath, console);
229228
}
230-
throw new VerifyException("Non-mygithub.libinneed.workers.dev Rest API is not yet supported.");
229+
return new GitHubApiTransportImpl(
230+
repo, newHttpTransport(), storePath, gitHubApiBearerAuth, console, gitHubHostName);
231231
}
232232

233233
protected HttpTransport newHttpTransport() {

java/com/google/copybara/git/github/api/GitHubApiTransportImpl.java

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.google.api.client.json.JsonFactory;
2828
import com.google.api.client.json.JsonObjectParser;
2929
import com.google.api.client.json.gson.GsonFactory;
30+
import com.google.common.annotations.VisibleForTesting;
3031
import com.google.common.base.Preconditions;
3132
import com.google.common.collect.ImmutableListMultimap;
3233
import com.google.common.collect.Iterables;
@@ -55,9 +56,11 @@ public class GitHubApiTransportImpl implements GitHubApiTransport {
5556
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
5657

5758
private static final JsonFactory JSON_FACTORY = new GsonFactory();
58-
private static final String API_URL = "https://api.github.com";
59-
private static final String GITHUB_WEB_URL = "https://github.com";
59+
private static final String GITHUB_DOT_COM_API_URL = "https://api.github.com";
60+
private static final String GITHUB_DOT_COM_WEB_URL = "https://github.com";
6061

62+
private final String apiUrl;
63+
private final String webUrl;
6164
private final GitRepository repo;
6265
private final HttpTransport httpTransport;
6366
private final String storePath;
@@ -71,6 +74,24 @@ public GitHubApiTransportImpl(GitRepository repo, HttpTransport httpTransport,
7174
this.storePath = storePath;
7275
this.console = Preconditions.checkNotNull(console);
7376
this.bearerAuth = bearerAuth;
77+
this.apiUrl = GITHUB_DOT_COM_API_URL;
78+
this.webUrl = GITHUB_DOT_COM_WEB_URL;
79+
}
80+
81+
public GitHubApiTransportImpl(
82+
GitRepository repo,
83+
HttpTransport httpTransport,
84+
String storePath,
85+
boolean bearerAuth,
86+
Console console,
87+
String webUrl) {
88+
this.repo = Preconditions.checkNotNull(repo);
89+
this.httpTransport = Preconditions.checkNotNull(httpTransport);
90+
this.storePath = storePath;
91+
this.console = Preconditions.checkNotNull(console);
92+
this.bearerAuth = bearerAuth;
93+
this.webUrl = buildGhesWebUrl(Preconditions.checkNotNull(webUrl));
94+
this.apiUrl = buildGhesApiUrl(this.webUrl);
7495
}
7596

7697
@SuppressWarnings("unchecked")
@@ -87,7 +108,7 @@ public <T> T get(String path, Type responseType, ImmutableListMultimap<String, S
87108
Object responseObj = GsonParserUtil.parseHttpResponse(response, responseType, false);
88109
if (responseObj instanceof PaginatedPayload) {
89110
return (T)
90-
((PaginatedPayload) responseObj).annotatePayload(API_URL, maybeGetLinkHeader(response));
111+
((PaginatedPayload) responseObj).annotatePayload(apiUrl, maybeGetLinkHeader(response));
91112
}
92113
return (T) responseObj;
93114
} catch (HttpResponseException e) {
@@ -152,7 +173,7 @@ public <T> T post(String path, Object request, Type responseType, String request
152173
response, responseType, false);
153174
if (responseObj instanceof PaginatedPayload) {
154175
return (T)
155-
((PaginatedPayload) responseObj).annotatePayload(API_URL, maybeGetLinkHeader(response));
176+
((PaginatedPayload) responseObj).annotatePayload(apiUrl, maybeGetLinkHeader(response));
156177
}
157178
return (T) responseObj;
158179

@@ -221,7 +242,7 @@ private HttpRequestFactory getHttpRequestFactory(
221242

222243
private GenericUrl getFullEndpointURL(String path) {
223244
String maybePrefix = path.startsWith("/") ? "" : "/";
224-
return new GenericUrl(URI.create(API_URL + maybePrefix + path));
245+
return new GenericUrl(URI.create(apiUrl + maybePrefix + path));
225246
}
226247

227248
/**
@@ -231,10 +252,10 @@ private GenericUrl getFullEndpointURL(String path) {
231252
*/
232253
private UserPassword getCredentials() throws RepoException, ValidationException {
233254
try {
234-
return repo.credentialFill(API_URL);
255+
return repo.credentialFill(apiUrl);
235256
} catch (ValidationException e) {
236257
try {
237-
return repo.credentialFill(GITHUB_WEB_URL);
258+
return repo.credentialFill(webUrl);
238259
} catch (ValidationException e1) {
239260
// Ugly, but helpful...
240261
throw new ValidationException(String.format(
@@ -252,4 +273,22 @@ private UserPassword getCredentials() throws RepoException, ValidationException
252273
}
253274
}
254275
}
276+
277+
private static String buildGhesWebUrl(String hostName) {
278+
return "https://" + hostName;
279+
}
280+
281+
private static String buildGhesApiUrl(String hostName) {
282+
return hostName + "/api/v3";
283+
}
284+
285+
@VisibleForTesting
286+
public String getApiUrl() {
287+
return apiUrl;
288+
}
289+
290+
@VisibleForTesting
291+
public String getWebUrl() {
292+
return webUrl;
293+
}
255294
}

javatests/com/google/copybara/git/github/api/GitHubApiTransportImplTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,27 @@ public LowLevelHttpResponse execute() throws IOException {
133133
assertThat(headers).containsEntry("authorization", ImmutableList.of("Bearer SECRET"));
134134
}
135135

136+
@Test
137+
public void testDefaultUrlsPopulated() throws Exception {
138+
httpTransport = createMockHttpTransport(new IOException());
139+
GitHubApiTransportImpl transport =
140+
new GitHubApiTransportImpl(repo, httpTransport, "store", false, new TestingConsole());
141+
142+
assertThat(transport.getApiUrl()).isEqualTo("https://api.github.com");
143+
assertThat(transport.getWebUrl()).isEqualTo("https://github.com");
144+
}
145+
146+
@Test
147+
public void testGhesUrlsPopulated() throws Exception {
148+
httpTransport = createMockHttpTransport(new IOException());
149+
GitHubApiTransportImpl transport =
150+
new GitHubApiTransportImpl(
151+
repo, httpTransport, "store", false, new TestingConsole(), "github.enterprise.com");
152+
153+
assertThat(transport.getApiUrl()).isEqualTo("https://github.enterprise.com/api/v3");
154+
assertThat(transport.getWebUrl()).isEqualTo("https://github.enterprise.com");
155+
}
156+
136157
private void runTestThrowsHttpResponseException(Callable<?> c) throws Exception {
137158
HttpResponseException ex =
138159
new HttpResponseException.Builder(STATUS_CODE, ERROR_MESSAGE, new HttpHeaders()).build();

0 commit comments

Comments
 (0)