Skip to content

Commit

Permalink
[JENKINS-31574] Anonymous access is allowed for scan credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
recena committed Nov 30, 2015
1 parent 425820b commit ffd52fc
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 45 deletions.
Expand Up @@ -197,13 +197,17 @@ public String getRemote() {

@Override protected final void retrieve(SCMHeadObserver observer, final TaskListener listener) throws IOException, InterruptedException {
StandardCredentials credentials = Connector.lookupScanCredentials(getOwner(), apiUri, scanCredentialsId);
if (credentials == null) {
listener.getLogger().println("No scan credentials, skipping");
GitHub github = Connector.connect(apiUri, credentials);
if (credentials != null && !github.isCredentialValid()) {
listener.getLogger().format("Invalid scan credentials, skipping%n");
return;
}
listener.getLogger().format("Connecting to %s using %s%n", getDescriptor().getDisplayName(), CredentialsNameProvider.name(credentials));
GitHub github = Connector.connect(apiUri, credentials);
String fullName = repoOwner + "/" + repository;
if (!github.isAnonymous()) {
listener.getLogger().format("Connecting to %s using %s%n", getDescriptor().getDisplayName(),
CredentialsNameProvider.name(credentials));
} else {
listener.getLogger().format("Connecting to %s using anonymous access%n", getDescriptor().getDisplayName());
}
/* TODO call GitHubBuilder withRateLimitHandler to notify listener so we do not get stuck without messages in something like
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
Expand All @@ -213,6 +217,7 @@ public String getRemote() {
at org.kohsuke.github.Requester.to(Requester.java:191)
at org.kohsuke.github.GitHub.getRepository(GitHub.java:320)
*/
String fullName = repoOwner + "/" + repository;
final GHRepository repo = github.getRepository(fullName);
listener.getLogger().format("Looking up %s%n", HyperlinkNote.encodeTo(repo.getHtmlUrl().toString(), fullName));
doRetrieve(observer, listener, repo);
Expand Down Expand Up @@ -327,21 +332,24 @@ public ListBoxModel doFillRepositoryItems(@AncestorInPath SCMSourceOwner context
return result;
}
try {
GitHub github = Connector.connect(apiUri, Connector.lookupScanCredentials(context, apiUri, scanCredentialsId));

GHMyself myself = null;
try {
myself = github.getMyself();
} catch (IllegalStateException e) {
LOGGER.log(Level.WARNING, e.getMessage());
} catch (IOException e) {
LOGGER.log(Level.WARNING, e.getMessage());
}
if (myself != null && repoOwner.equals(myself.getLogin())) {
for (String name : myself.getAllRepositories().keySet()) {
result.add(name);
StandardCredentials credentials = Connector.lookupScanCredentials(context, apiUri, scanCredentialsId);
GitHub github = Connector.connect(apiUri, credentials);

if (!github.isAnonymous()) {
GHMyself myself = null;
try {
myself = github.getMyself();
} catch (IllegalStateException e) {
LOGGER.log(Level.WARNING, e.getMessage());
} catch (IOException e) {
LOGGER.log(Level.WARNING, e.getMessage());
}
if (myself != null && repoOwner.equals(myself.getLogin())) {
for (String name : myself.getAllRepositories().keySet()) {
result.add(name);
}
return result;
}
return result;
}

GHOrganization org = null;
Expand Down
Expand Up @@ -105,50 +105,57 @@ public String getApiUri() {
return;
}
StandardCredentials credentials = Connector.lookupScanCredentials(observer.getContext(), apiUri, scanCredentialsId);
if (credentials == null) {
listener.getLogger().format("No scan credentials, skipping%n");
return;
}
GitHub github = Connector.connect(apiUri, credentials);
if (!github.isCredentialValid()) {
if (credentials != null && !github.isCredentialValid()) {
listener.getLogger().format("Invalid scan credentials, skipping%n");
return;
}
listener.getLogger().format("Connecting to GitHub using %s%n", CredentialsNameProvider.name(credentials));
GHMyself myself = null;
try {
myself = github.getMyself();
} catch (IllegalStateException e) {
// may be anonymous... ok to ignore
} catch (IOException e) {
// may be anonymous... ok to ignore
}
if (myself != null && repoOwner.equals(myself.getLogin())) {
listener.getLogger().format("Looking up repositories of myself %s%n", repoOwner);
for (GHRepository repo : myself.listRepositories()) {
if (!repo.getOwnerName().equals(repoOwner)) {
continue; // ignore repos in other orgs when using GHMyself

if (!github.isAnonymous()) {
listener.getLogger().format("Connecting to GitHub using %s%n", CredentialsNameProvider.name(credentials));
GHMyself myself = null;
try {
// Requires an authenticated access
myself = github.getMyself();
} catch (IOException e) {
// Something wrong happened, maybe java.net.ConnectException?
}
if (myself != null && repoOwner.equals(myself.getLogin())) {
listener.getLogger().format("Looking up repositories of myself %s%n%n", repoOwner);
for (GHRepository repo : myself.listRepositories()) {
if (!repo.getOwnerName().equals(repoOwner)) {
continue; // ignore repos in other orgs when using GHMyself
}
add(listener, observer, repo);
}
add(listener, observer, repo);
return;
}
return;
} else {
listener.getLogger().format("Connecting to GitHub using anonymous access%n");
}

GHOrganization org = null;
try {
org = github.getOrganization(repoOwner);
} catch (IOException e) {
// may be a user... ok to ignore
}
GHOrganization org = github.getOrganization(repoOwner);
if (org != null && repoOwner.equals(org.getLogin())) {
listener.getLogger().format("Looking up repositories of organization %s%n", repoOwner);
listener.getLogger().format("Looking up repositories of organization %s%n%n", repoOwner);
for (GHRepository repo : org.listRepositories()) {
add(listener, observer, repo);
}
return;
}

GHUser user = null;
try {
user = github.getUser(repoOwner);
} catch (IOException e) {
// may be organization... ok to ignore
// Something wrong happened, maybe java.net.ConnectException?
}
if (user != null && repoOwner.equals(user.getLogin())) {
listener.getLogger().format("Looking up repositories of user %s%n", repoOwner);
listener.getLogger().format("Looking up repositories of user %s%n%n", repoOwner);
for (GHRepository repo : user.listRepositories()) {
add(listener, observer, repo);
}
Expand Down Expand Up @@ -207,7 +214,7 @@ public FormValidation doCheckScanCredentialsId(@AncestorInPath SCMSourceOwner co
}
return FormValidation.error("Invalid credentials");
} else {
return FormValidation.warning("Credentials are required");
return FormValidation.warning("Credentials are recommended");
}
}

Expand Down

0 comments on commit ffd52fc

Please sign in to comment.