From 14d7eb316ddeb9b8b9c376a2129b53a2ea57f222 Mon Sep 17 00:00:00 2001 From: David Tanner Date: Thu, 25 Jun 2015 21:56:06 -0600 Subject: [PATCH 1/2] Work for fixing credentials issues --- .../org/jenkinsci/plugins/ghprb/Ghprb.java | 9 ++- .../plugins/ghprb/GhprbGitHubAuth.java | 70 +++++++++++++++---- .../ghprb/GhprbGitHubAuth/config.groovy | 12 ++-- 3 files changed, 70 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/ghprb/Ghprb.java b/src/main/java/org/jenkinsci/plugins/ghprb/Ghprb.java index 41a71f726..4503971ae 100644 --- a/src/main/java/org/jenkinsci/plugins/ghprb/Ghprb.java +++ b/src/main/java/org/jenkinsci/plugins/ghprb/Ghprb.java @@ -8,6 +8,7 @@ import com.cloudbees.plugins.credentials.common.StandardCredentials; import com.cloudbees.plugins.credentials.domains.Domain; import com.cloudbees.plugins.credentials.domains.DomainSpecification; +import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder; import com.cloudbees.plugins.credentials.domains.HostnamePortSpecification; import com.cloudbees.plugins.credentials.domains.HostnameSpecification; import com.cloudbees.plugins.credentials.domains.PathSpecification; @@ -30,10 +31,10 @@ import org.apache.commons.collections.Predicate; import org.apache.commons.collections.PredicateUtils; import org.apache.commons.collections.functors.InstanceofPredicate; +import org.codehaus.plexus.util.StringUtils; import org.jenkinsci.plugins.ghprb.extensions.GhprbExtension; import org.jenkinsci.plugins.ghprb.extensions.GhprbExtensionDescriptor; import org.jenkinsci.plugins.ghprb.extensions.GhprbProjectExtension; -import org.jenkinsci.plugins.gitclient.GitURIRequirementsBuilder; import org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl; import org.kohsuke.github.GHCommitState; import org.kohsuke.github.GHUser; @@ -369,7 +370,7 @@ public static void addIfMissing(DescribableList permissions = new ArrayList(3); + if (repository.hasAdminAccess()) { + permissions.add("Admin"); + } + if (repository.hasPushAccess()) { + permissions.add("Push"); + } + if (repository.hasPullAccess()) { + permissions.add("Pull"); + } + sb.append(Joiner.on(", ").join(permissions)); + + return FormValidation.ok(sb.toString()); + } catch (Exception ex) { + return FormValidation.error("Unable to connect to GitHub API: " + ex); + } + } + + private GitHubBuilder getBuilder(String serverAPIUrl, String credentialsId) { + GitHubBuilder builder = new GitHubBuilder() + .withEndpoint(serverAPIUrl) + .withConnector(new HttpConnectorWithJenkinsProxy()); + + StandardCredentials credentials = Ghprb.lookupCredentials(null, credentialsId, serverAPIUrl); + if (credentials instanceof StandardUsernamePasswordCredentials) { + StandardUsernamePasswordCredentials upCredentials = (StandardUsernamePasswordCredentials) credentials; + builder.withPassword(upCredentials.getUsername(), upCredentials.getPassword().getPlainText()); + + } else if (credentials instanceof StringCredentials) { + StringCredentials tokenCredentials = (StringCredentials) credentials; + builder.withOAuthToken(tokenCredentials.getSecret().getPlainText()); + } else { + return null; + } + return builder; + } public FormValidation doTestGithubAccess( @QueryParameter("serverAPIUrl") final String serverAPIUrl, @QueryParameter("credentialsId") final String credentialsId) { try { - - GitHubBuilder builder = new GitHubBuilder() - .withEndpoint(serverAPIUrl) - .withConnector(new HttpConnectorWithJenkinsProxy()); - - StandardCredentials credentials = Ghprb.lookupCredentials(null, credentialsId, serverAPIUrl); - if (credentials instanceof StandardUsernamePasswordCredentials) { - StandardUsernamePasswordCredentials upCredentials = (StandardUsernamePasswordCredentials) credentials; - builder.withPassword(upCredentials.getUsername(), upCredentials.getPassword().getPlainText()); - - } else if (credentials instanceof StringCredentials) { - StringCredentials tokenCredentials = (StringCredentials) credentials; - builder.withOAuthToken(tokenCredentials.getSecret().getPlainText()); - } else { - return FormValidation.error("No credentials provided"); + GitHubBuilder builder = getBuilder(serverAPIUrl, credentialsId); + if (builder == null) { + return FormValidation.error("No credentials ID provided!!"); } GitHub gh = builder.build(); GHMyself me = gh.getMyself(); diff --git a/src/main/resources/org/jenkinsci/plugins/ghprb/GhprbGitHubAuth/config.groovy b/src/main/resources/org/jenkinsci/plugins/ghprb/GhprbGitHubAuth/config.groovy index 5b234d11d..6aefeca56 100644 --- a/src/main/resources/org/jenkinsci/plugins/ghprb/GhprbGitHubAuth/config.groovy +++ b/src/main/resources/org/jenkinsci/plugins/ghprb/GhprbGitHubAuth/config.groovy @@ -19,8 +19,14 @@ f.entry(title:_("Credentials"), field:"credentialsId") { }""" /* workaround for JENKINS-19124 */) } -f.entry(title:_("Test Credentials")) { - f.validateButton(title:_("Connect to API"), progress:_("Connecting..."), with:"serverAPIUrl,credentialsId", method:"testGithubAccess") +f.advanced(title:_("Test Credentials")) { + f.entry(title:_("Test Credentials")) { + f.validateButton(title:_("Connect to API"), progress:_("Connecting..."), with:"serverAPIUrl,credentialsId", method:"testGithubAccess") + f.entry(title:_("Repository owner/name"), field:"repo") { + f.textbox() + } + f.validateButton(title:_("Check repo permissions"), progress:_("Connecting..."), with:"serverAPIUrl,credentialsId,repo", method:"checkRepoAccess") + } } f.advanced(title:_("Create API Token")) { @@ -47,8 +53,6 @@ f.advanced(title:_("Auth ID")) { f.entry { div(align:"right") { - - input (type:"button", value:_("Add Server"), class:"repeatable-add show-if-last") input (type:"button", value:_("Delete Server"), class:"repeatable-delete") } } From d1ba3594512becfcbc6449e2818f0964225c3f14 Mon Sep 17 00:00:00 2001 From: David Tanner Date: Thu, 25 Jun 2015 22:06:04 -0600 Subject: [PATCH 2/2] Wrong StringUtils --- src/main/java/org/jenkinsci/plugins/ghprb/Ghprb.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/jenkinsci/plugins/ghprb/Ghprb.java b/src/main/java/org/jenkinsci/plugins/ghprb/Ghprb.java index 4503971ae..4de9b4b8f 100644 --- a/src/main/java/org/jenkinsci/plugins/ghprb/Ghprb.java +++ b/src/main/java/org/jenkinsci/plugins/ghprb/Ghprb.java @@ -31,7 +31,7 @@ import org.apache.commons.collections.Predicate; import org.apache.commons.collections.PredicateUtils; import org.apache.commons.collections.functors.InstanceofPredicate; -import org.codehaus.plexus.util.StringUtils; +import org.apache.commons.lang.StringUtils; import org.jenkinsci.plugins.ghprb.extensions.GhprbExtension; import org.jenkinsci.plugins.ghprb.extensions.GhprbExtensionDescriptor; import org.jenkinsci.plugins.ghprb.extensions.GhprbProjectExtension;