Skip to content

Commit

Permalink
Don't pull avatar if remote GitHub Enterprise uses private mode. (#532)
Browse files Browse the repository at this point in the history
* Don't pull avatar if remote GitHub Enterprise uses private mode.

* Apply suggestions from code review

Co-authored-by: James Nord <[email protected]>

* Apply suggestions from code review

Co-authored-by: James Nord <[email protected]>

* Update src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMNavigator.java

* Fix spotless

Co-authored-by: James Nord <[email protected]>
  • Loading branch information
Vlatombe and jtnord committed May 3, 2022
1 parent 91207e9 commit 9fb798a
Showing 1 changed file with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import hudson.util.ListBoxModel;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
Expand Down Expand Up @@ -1574,15 +1575,21 @@ public List<Action> retrieveActions(
// trusted source
listener.getLogger().printf("Looking up details of %s...%n", getRepoOwner());
List<Action> result = new ArrayList<>();
String apiUri = Util.fixEmptyAndTrim(getApiUri());
StandardCredentials credentials =
Connector.lookupScanCredentials((Item) owner, getApiUri(), credentialsId);
GitHub hub = Connector.connect(getApiUri(), credentials);
Connector.lookupScanCredentials((Item) owner, apiUri, credentialsId);
GitHub hub = Connector.connect(apiUri, credentials);
boolean privateMode = determinePrivateMode(apiUri);
try {
Connector.configureLocalRateLimitChecker(listener, hub);
GHUser u = hub.getUser(getRepoOwner());
String objectUrl = u.getHtmlUrl() == null ? null : u.getHtmlUrl().toExternalForm();
result.add(new ObjectMetadataAction(Util.fixEmpty(u.getName()), null, objectUrl));
result.add(new GitHubOrgMetadataAction(u));
if (privateMode) {
result.add(new GitHubOrgMetadataAction((String) null));
} else {
result.add(new GitHubOrgMetadataAction(u));
}
result.add(new GitHubLink("icon-github-logo", u.getHtmlUrl()));
if (objectUrl == null) {
listener.getLogger().println("Organization URL: unspecified");
Expand All @@ -1600,6 +1607,23 @@ public List<Action> retrieveActions(
}
}

private static boolean determinePrivateMode(String apiUri) {
if (apiUri == null || apiUri.equals(GitHubServerConfig.GITHUB_URL)) {
return false;
}
try {
GitHub.connectToEnterpriseAnonymously(apiUri).checkApiUrlValidity();
} catch (MalformedURLException e) {
// URL is bogus so there is never going to be an avatar - or anything else come to think of it
return true;
} catch (IOException e) {
if (e.getMessage().contains("private mode enabled")) {
return true;
}
}
return false;
}

/** {@inheritDoc} */
@Override
public void afterSave(@NonNull SCMNavigatorOwner owner) {
Expand Down

0 comments on commit 9fb798a

Please sign in to comment.