From 4fec48cc14c62d2beed86c5a053d1086afa63553 Mon Sep 17 00:00:00 2001 From: David Tanner Date: Tue, 16 Jun 2015 09:16:48 -0600 Subject: [PATCH] Add missing method and fix bug. The button to test connection to the API wasn't tied to a method. Also fixed bug when using multiple GitHub api endpoints. --- .../plugins/ghprb/GhprbGitHubAuth.java | 41 +++++++++++++++++-- .../jenkinsci/plugins/ghprb/GhprbTrigger.java | 2 +- .../ghprb/GhprbGitHubAuth/config.groovy | 13 +++--- .../plugins/ghprb/GhprbTrigger/config.groovy | 1 - .../plugins/ghprb/GhprbTrigger/global.groovy | 1 - 5 files changed, 45 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/ghprb/GhprbGitHubAuth.java b/src/main/java/org/jenkinsci/plugins/ghprb/GhprbGitHubAuth.java index ec16b8025..61466c59a 100644 --- a/src/main/java/org/jenkinsci/plugins/ghprb/GhprbGitHubAuth.java +++ b/src/main/java/org/jenkinsci/plugins/ghprb/GhprbGitHubAuth.java @@ -14,6 +14,7 @@ import java.util.logging.Logger; import org.kohsuke.github.GHAuthorization; +import org.kohsuke.github.GHMyself; import org.kohsuke.github.GitHub; import org.kohsuke.github.GitHubBuilder; import org.kohsuke.stapler.AncestorInPath; @@ -168,22 +169,27 @@ public FormValidation doCreateApiToken( @QueryParameter("username") final String username, @QueryParameter("password") final String password) { try { - GitHub gh; + + GitHubBuilder builder = new GitHubBuilder() + .withEndpoint(serverAPIUrl) + .withConnector(new HttpConnectorWithJenkinsProxy()); if (StringUtils.isEmpty(credentialsId)) { if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) { return FormValidation.error("Username and Password required"); } - gh = GitHub.connectToEnterprise(serverAPIUrl, username, password); + + builder.withPassword(username, password); } else { StandardCredentials credentials = Ghprb.lookupCredentials(null, credentialsId, serverAPIUrl); if (credentials instanceof StandardUsernamePasswordCredentials) { StandardUsernamePasswordCredentials upCredentials = (StandardUsernamePasswordCredentials) credentials; - gh = GitHub.connectToEnterprise(serverAPIUrl, upCredentials.getUsername(), upCredentials.getPassword().getPlainText()); + builder.withPassword(upCredentials.getUsername(), upCredentials.getPassword().getPlainText()); } else { - return FormValidation.error("No credentials provided"); + return FormValidation.error("No username/password credentials provided"); } } + GitHub gh = builder.build(); GHAuthorization token = gh.createToken(Arrays.asList(GHAuthorization.REPO_STATUS, GHAuthorization.REPO), "Jenkins GitHub Pull Request Builder", null); String tokenId; @@ -227,6 +233,33 @@ public FormValidation doCheckServerAPIUrl(@QueryParameter String value) { return FormValidation.warning("GitHub API URI is \"https://api.github.com\". GitHub Enterprise API URL ends with \"/api/v3\""); } + 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"); + } + GitHub gh = builder.build(); + GHMyself me = gh.getMyself(); + return FormValidation.ok("Connected to " + serverAPIUrl + " as " + me.getName()); + } catch (Exception ex) { + return FormValidation.error("Unable to connect to GitHub API: " + ex); + } + } } } diff --git a/src/main/java/org/jenkinsci/plugins/ghprb/GhprbTrigger.java b/src/main/java/org/jenkinsci/plugins/ghprb/GhprbTrigger.java index 7dca9e5d1..065ba5e32 100644 --- a/src/main/java/org/jenkinsci/plugins/ghprb/GhprbTrigger.java +++ b/src/main/java/org/jenkinsci/plugins/ghprb/GhprbTrigger.java @@ -552,7 +552,7 @@ public boolean configure(StaplerRequest req, JSONObject formData) throws FormExc autoCloseFailedPullRequests = formData.getBoolean("autoCloseFailedPullRequests"); displayBuildErrorsOnDownstreamBuilds = formData.getBoolean("displayBuildErrorsOnDownstreamBuilds"); - githubAuth = req.bindJSONToList(GhprbGitHubAuth.class, formData.getJSONObject("githubAuth")); + githubAuth = req.bindJSONToList(GhprbGitHubAuth.class, formData.get("githubAuth")); extensions = new DescribableList(Saveable.NOOP); 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 9a5c57c7e..348356c62 100644 --- a/src/main/resources/org/jenkinsci/plugins/ghprb/GhprbGitHubAuth/config.groovy +++ b/src/main/resources/org/jenkinsci/plugins/ghprb/GhprbGitHubAuth/config.groovy @@ -1,5 +1,3 @@ -package hudson.plugins.git.GhprbGitHubAuth; - f = namespace(lib.FormTagLib) c = namespace(lib.CredentialsTagLib) @@ -17,9 +15,12 @@ f.entry(title:_("Credentials"), field:"credentialsId") { }""" /* workaround for JENKINS-19124 */) } -f.advanced(title:_("Test Credentials")) { - f.entry(title:_("Test Credentials")) { - f.validateButton(title:_("Connect to API"), progress:_("Connecting..."), with:"credentialsId", method:"connectToAPI") +f.entry(title:_("Test Credentials")) { + f.validateButton(title:_("Connect to API"), progress:_("Connecting..."), with:"serverAPIUrl,credentialsId", method:"testGithubAccess") +} + +f.advanced(title:_("Create API Token")) { + f.entry(title:_("Create API Token")) { f.entry(title:_("Username temp"), field:"username") { f.textbox() } @@ -34,7 +35,7 @@ f.entry(field: "description", title: _("Description")) { f.textbox() } -f.advanced() { +f.advanced(title:_("Auth ID")) { f.entry(field: instance != null ? null : 'id', title: _("ID")) { f.textbox(name: "_.id", value: instance != null ? instance.id : null, readonly: instance != null ? 'readonly' : null) } diff --git a/src/main/resources/org/jenkinsci/plugins/ghprb/GhprbTrigger/config.groovy b/src/main/resources/org/jenkinsci/plugins/ghprb/GhprbTrigger/config.groovy index 622a1926c..f42b58c5e 100644 --- a/src/main/resources/org/jenkinsci/plugins/ghprb/GhprbTrigger/config.groovy +++ b/src/main/resources/org/jenkinsci/plugins/ghprb/GhprbTrigger/config.groovy @@ -1,4 +1,3 @@ -xml = namespace("http://www.w3.org/XML/1998/namespace") j = namespace("jelly:core") f = namespace("/lib/form") diff --git a/src/main/resources/org/jenkinsci/plugins/ghprb/GhprbTrigger/global.groovy b/src/main/resources/org/jenkinsci/plugins/ghprb/GhprbTrigger/global.groovy index 64786f59a..b8765cc46 100644 --- a/src/main/resources/org/jenkinsci/plugins/ghprb/GhprbTrigger/global.groovy +++ b/src/main/resources/org/jenkinsci/plugins/ghprb/GhprbTrigger/global.groovy @@ -1,4 +1,3 @@ -xml = namespace("http://www.w3.org/XML/1998/namespace") j = namespace("jelly:core") f = namespace("/lib/form")