Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #3 from lanwen/JENKINS-30223_fix
[FIXES JENKINS-30223] Migrate to new version (1.13.1) of GH Plugin
  • Loading branch information
riccardoelasticbox committed Sep 1, 2015
2 parents bac7fef + ab9c85d commit 86df8fb
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 65 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -64,7 +64,7 @@
<dependency>
<groupId>com.coravy.hudson.plugins.github</groupId>
<artifactId>github</artifactId>
<version>1.10</version>
<version>1.13.1</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
Expand Down
Expand Up @@ -160,8 +160,8 @@ private void configureGit(GitHubRepositoryName gitHubRepoName, PullRequestBuildT
StringUtils.isBlank(git.getUserRemoteConfigs().get(0).getUrl()))) {
LOGGER.info(MessageFormat.format("Git is selected as SCM of project {0} but not yet configured, configuring it",
project.getFullName()));
String url = MessageFormat.format("https://{0}/{1}/{2}.git", gitHubRepoName.host,
gitHubRepoName.userName, gitHubRepoName.repositoryName);
String url = MessageFormat.format("https://{0}/{1}/{2}.git", gitHubRepoName.getHost(),
gitHubRepoName.getUserName(), gitHubRepoName.getRepositoryName());
userRemoteConfigs.add(new UserRemoteConfig(url, "origin", "+refs/pull/*:refs/remotes/origin/pr/*", null));
List<BranchSpec> branches = new ArrayList<BranchSpec>();
branches.add(new BranchSpec("${PR_COMMIT}"));
Expand Down
Expand Up @@ -79,7 +79,7 @@ protected void execute(TaskListener listener) throws IOException {
GitHub gitHub = PullRequestManager.getInstance().createGitHub(repoName);
if (gitHub != null) {
try {
GHRepository repo = gitHub.getRepository(MessageFormat.format("{0}/{1}", repoName.userName, repoName.repositoryName));
GHRepository repo = gitHub.getRepository(MessageFormat.format("{0}/{1}", repoName.getUserName(), repoName.getRepositoryName()));
Set<String> openPullRequestURLs = new HashSet<String>();
for (GHPullRequest ghPullRequest : repo.getPullRequests(GHIssueState.OPEN)) {
openPullRequestURLs.add(ghPullRequest.getHtmlUrl().toString());
Expand Down
Expand Up @@ -13,8 +13,6 @@
package com.elasticbox.jenkins.triggers.github;

import com.elasticbox.jenkins.triggers.PullRequestBuildTrigger;
import com.cloudbees.jenkins.Credential;
import com.cloudbees.jenkins.GitHubPushTrigger;
import com.cloudbees.jenkins.GitHubRepositoryName;
import com.elasticbox.jenkins.ElasticBoxCloud;
import com.elasticbox.jenkins.builders.BuilderListener;
Expand All @@ -31,27 +29,25 @@
import hudson.security.ACL;
import java.io.IOException;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContextHolder;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.github.GitHubPlugin;
import org.kohsuke.github.GHEventPayload;
import org.kohsuke.github.GHPullRequest;
import org.kohsuke.github.GitHub;

import static java.text.MessageFormat.format;
import static org.jenkinsci.plugins.github.config.GitHubServerConfig.withHost;

/**
*
* @author Phong Nguyen Le
Expand Down Expand Up @@ -125,58 +121,16 @@ public GitHub createGitHub(GitHubRepositoryName gitHubRepoName) {
return gitHub;
}

private String getHost(Credential credential) {
if (StringUtils.isNotBlank(credential.apiUrl)) {
try {
String host = new URL(credential.apiUrl).getHost();
return "api.github.com".equals(host) ? "github.com" : host;
} catch (MalformedURLException ex) {
LOGGER.log(Level.SEVERE, MessageFormat.format("Invalid GitHub API URL: {0}", credential.apiUrl), ex);
}
}

return "github.com";
}

private GitHub connect(Credential credential) {
try {
return credential.login();
} catch (IOException ex) {
LOGGER.log(Level.WARNING, MessageFormat.format("Error logging in GitHub at ''{0}'' with credential of user ''{1}''", getHost(credential), credential.username), ex);
return null;
}
}

private GitHub connect(GitHubRepositoryName gitHubRepoName) {
List<Credential> credentials = ((GitHubPushTrigger.DescriptorImpl) Jenkins.getInstance().getDescriptorOrDie(GitHubPushTrigger.class)).getCredentials();
List<Credential> hostMatchedCredentials = new ArrayList<Credential>();
// try with the credential of the repository owner first
for (Credential credential : credentials) {
if (gitHubRepoName.host.equals(getHost(credential))) {
if (gitHubRepoName.userName.equals(credential.username)) {
GitHub gitHub = connect(credential);
if (gitHub != null) {
return gitHub;
}
} else {
hostMatchedCredentials.add(credential);
}
}
}

if (hostMatchedCredentials.isEmpty()) {
LOGGER.warning(MessageFormat.format("Cannot find any credential for GitHub at {0}", gitHubRepoName.host));
Iterator<GitHub> withAuth = GitHubPlugin.configuration()
.findGithubConfig(withHost(gitHubRepoName.getHost())).iterator();

if (withAuth.hasNext()) {
return withAuth.next();
} else {
// try other credentials for the same host
for (Credential credential : hostMatchedCredentials) {
GitHub gitHub = connect(credential);
if (gitHub != null) {
return gitHub;
}
}
LOGGER.warning(format("Cannot find any credential for GitHub at {0}", gitHubRepoName.getHost()));
return null;
}

return null;
}

private GitHub createGitHub(JSONObject payload) {
Expand Down
Expand Up @@ -12,9 +12,8 @@

package com.elasticbox.jenkins.tests;

import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.elasticbox.jenkins.util.Condition;
import com.cloudbees.jenkins.Credential;
import com.cloudbees.jenkins.GitHubPushTrigger;
import com.elasticbox.Client;
import com.elasticbox.ClientException;
import com.elasticbox.jenkins.triggers.PullRequestBuildTrigger;
Expand Down Expand Up @@ -60,6 +59,9 @@
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.jenkinsci.plugins.github.GitHubPlugin;
import org.jenkinsci.plugins.github.config.GitHubServerConfig;
import org.jenkinsci.plugins.github.config.GitHubTokenCredentialsCreator;
import org.junit.Assert;
import org.junit.Before;
import org.kohsuke.github.*;
Expand Down Expand Up @@ -125,8 +127,14 @@ public void setup() throws Exception {
ghPullRequest.close();
pullRequest = new MockPullRequest(ghPullRequest);
testTag = UUID.randomUUID().toString().substring(0, 30);
GitHubPushTrigger.DescriptorImpl descriptor = (GitHubPushTrigger.DescriptorImpl) jenkins.getInstance().getDescriptor(GitHubPushTrigger.class);
descriptor.getCredentials().add(new Credential(TestUtils.GITHUB_USER, apiGithubAddress, TestUtils.GITHUB_ACCESS_TOKEN));

StandardCredentials creds = jenkins.getInstance()
.getDescriptorByType(GitHubTokenCredentialsCreator.class)
.createCredentials(apiGithubAddress, TestUtils.GITHUB_ACCESS_TOKEN, TestUtils.GITHUB_USER);
GitHubServerConfig config = new GitHubServerConfig(creds.getId());
config.setApiUrl(apiGithubAddress);
GitHubPlugin.configuration().getConfigs().add(config);

TestUtils.TemplateResolver templateResolver = new TemplateResolveImpl() {

@Override
Expand Down

0 comments on commit 86df8fb

Please sign in to comment.