From f4810bdaae1aa53d3b1ef1a0dafbb15124da995d Mon Sep 17 00:00:00 2001 From: Andy Neebel Date: Thu, 3 Mar 2016 10:28:36 -0600 Subject: [PATCH 1/2] Allow anon credentials when retrieving commit Uses the same logic as when scanning the repository for branches to find the commit to build for a given branch. --- .../github_branch_source/GitHubSCMSource.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMSource.java b/src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMSource.java index 0adc75a76..66df2416d 100644 --- a/src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMSource.java +++ b/src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMSource.java @@ -347,12 +347,17 @@ protected SCMSourceCriteria.Probe getProbe(final String branch, final String thi @CheckForNull protected SCMRevision retrieve(SCMHead head, 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 null; } - listener.getLogger().format("Connecting to %s using %s%n", getDescriptor().getDisplayName(), CredentialsNameProvider.name(credentials)); - GitHub github = Connector.connect(apiUri, credentials); + 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()); + } String fullName = repoOwner + "/" + repository; GHRepository repo = github.getRepository(fullName); return doRetrieve(head, listener, repo); From 5b64de60f07bfef2b14d29e8cb1b8f5714fd54ce Mon Sep 17 00:00:00 2001 From: Andy Neebel Date: Fri, 4 Mar 2016 10:32:08 -0600 Subject: [PATCH 2/2] Wrap new credentials logic in try-catch Handles a rate-limit exception then --- .../github_branch_source/GitHubSCMSource.java | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMSource.java b/src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMSource.java index 66df2416d..bdf39b759 100644 --- a/src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMSource.java +++ b/src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMSource.java @@ -348,19 +348,24 @@ protected SCMSourceCriteria.Probe getProbe(final String branch, final String thi protected SCMRevision retrieve(SCMHead head, TaskListener listener) throws IOException, InterruptedException { StandardCredentials credentials = Connector.lookupScanCredentials(getOwner(), apiUri, scanCredentialsId); GitHub github = Connector.connect(apiUri, credentials); - if (credentials != null && !github.isCredentialValid()) { - listener.getLogger().format("Invalid scan credentials, skipping%n"); - return null; - } - 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()); + try { + if (credentials != null && !github.isCredentialValid()) { + listener.getLogger().format("Invalid scan credentials, skipping%n"); + return null; + } + 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()); + } + String fullName = repoOwner + "/" + repository; + GHRepository repo = github.getRepository(fullName); + return doRetrieve(head, listener, repo); + } catch (RateLimitExceededException rle) { + listener.getLogger().format("%n%s%n%n", rle.getMessage()); + throw new InterruptedException(); } - String fullName = repoOwner + "/" + repository; - GHRepository repo = github.getRepository(fullName); - return doRetrieve(head, listener, repo); } protected SCMRevision doRetrieve(SCMHead head, TaskListener listener, GHRepository repo) throws IOException, InterruptedException {