Skip to content

Commit

Permalink
Merge pull request jenkinsci#111 from jenkinsci/fix-auth
Browse files Browse the repository at this point in the history
Add missing method and fix bug.
  • Loading branch information
DavidTanner committed Jun 16, 2015
2 parents 406fce6 + 4fec48c commit 6f323cc
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 13 deletions.
41 changes: 37 additions & 4 deletions src/main/java/org/jenkinsci/plugins/ghprb/GhprbGitHubAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<GhprbExtension, GhprbExtensionDescriptor>(Saveable.NOOP);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package hudson.plugins.git.GhprbGitHubAuth;

f = namespace(lib.FormTagLib)
c = namespace(lib.CredentialsTagLib)

Expand All @@ -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()
}
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
xml = namespace("http://www.w3.org/XML/1998/namespace")
j = namespace("jelly:core")
f = namespace("/lib/form")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
xml = namespace("http://www.w3.org/XML/1998/namespace")
j = namespace("jelly:core")
f = namespace("/lib/form")

Expand Down

0 comments on commit 6f323cc

Please sign in to comment.