Skip to content

Commit

Permalink
Merge pull request jenkinsci#285 from jenkinsci/master
Browse files Browse the repository at this point in the history
Merge in latest from jenkinsci
  • Loading branch information
DavidTanner committed Jun 13, 2015
2 parents 27e9361 + 2ffd677 commit 85eec74
Show file tree
Hide file tree
Showing 28 changed files with 886 additions and 271 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ If you want to manually build the job, in the job setting check ``This build is

### Updates

#### -> 1.22.x
* Fix issue where if a project was disabled the Jenkins Trigger process would crash
* Fix commit status context
* Add one line test results for downstream builds
* Miscellaneous bug fixes

#### -> 1.22
* Move commit status over to extension form. It is now configurable, satisfying #81, #73, and #19 at least.

Expand Down
13 changes: 12 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<artifactId>ghprb</artifactId>
<name>GitHub Pull Request Builder</name>
<version>1.23-SNAPSHOT</version>
<version>1.24-SNAPSHOT</version>
<packaging>hpi</packaging>

<url>https://wiki.jenkins-ci.org/display/JENKINS/GitHub+pull+request+builder+plugin</url>
Expand Down Expand Up @@ -41,6 +41,7 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<dependencies>
Expand Down Expand Up @@ -94,6 +95,16 @@
<version>0.12</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>credentials</artifactId>
<version>1.21</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plain-credentials</artifactId>
<version>1.1</version>
</dependency>
</dependencies>

<build>
Expand Down
70 changes: 69 additions & 1 deletion src/main/java/org/jenkinsci/plugins/ghprb/Ghprb.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,44 @@
package org.jenkinsci.plugins.ghprb;

import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.CredentialsStore;
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
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.HostnamePortSpecification;
import com.cloudbees.plugins.credentials.domains.HostnameSpecification;
import com.cloudbees.plugins.credentials.domains.PathSpecification;
import com.cloudbees.plugins.credentials.domains.SchemeSpecification;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
import com.coravy.hudson.plugins.github.GithubProjectProperty;

import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Cause;
import hudson.model.Item;
import hudson.model.Result;
import hudson.model.Saveable;
import hudson.security.ACL;
import hudson.util.DescribableList;
import hudson.util.LogTaskListener;
import hudson.util.Secret;

import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.PredicateUtils;
import org.apache.commons.collections.functors.InstanceofPredicate;
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;

import java.net.URI;
import java.util.*;
import java.util.concurrent.ConcurrentMap;
import java.util.logging.Level;
Expand Down Expand Up @@ -108,7 +127,7 @@ public GhprbRepository getRepository() {
}

public GhprbGitHub getGitHub() {
return trigger.getDescriptor().getGitHub();
return new GhprbGitHub(trigger);
}

void run() {
Expand Down Expand Up @@ -330,4 +349,53 @@ public static void addIfMissing(DescribableList<GhprbExtension, GhprbExtensionDe
extensions.add(ext);
}

public static StandardCredentials lookupCredentials(Item project, String credentialId, String uri) {
return (credentialId == null) ? null : CredentialsMatchers.firstOrNull(
CredentialsProvider.lookupCredentials(StandardCredentials.class, project, ACL.SYSTEM,
GitURIRequirementsBuilder.fromUri(uri).build()),
CredentialsMatchers.withId(credentialId));
}

public static String createCredentials(String serverAPIUrl, String token) throws Exception {
String description = serverAPIUrl + " GitHub auto generated token credentials";
StringCredentialsImpl credentials = new StringCredentialsImpl(
CredentialsScope.GLOBAL,
UUID.randomUUID().toString(),
description,
Secret.fromString(token));
return createCredentials(serverAPIUrl, credentials);
}

public static String createCredentials(String serverAPIUrl, String username, String password) throws Exception {
String description = serverAPIUrl + " GitHub auto generated Username password credentials";
UsernamePasswordCredentialsImpl credentials = new UsernamePasswordCredentialsImpl(
CredentialsScope.GLOBAL,
UUID.randomUUID().toString(),
description,
username,
password);
return createCredentials(serverAPIUrl, credentials);
}

private static String createCredentials(String serverAPIUrl, StandardCredentials credentials) throws Exception {
List<DomainSpecification> specifications = new ArrayList<DomainSpecification>(2);

URI serverUri = new URI(serverAPIUrl);

if (serverUri.getPort() > 0) {
specifications.add(new HostnamePortSpecification(serverUri.getHost() + ":" + serverUri.getPort(), null));
} else {
specifications.add(new HostnameSpecification(serverUri.getHost(), null));
}

specifications.add(new SchemeSpecification(serverUri.getScheme()));
specifications.add(new PathSpecification(serverUri.getPath(), null, false));



Domain domain = new Domain(serverAPIUrl, "Auto generated credentials domain", specifications);
CredentialsStore provider = new SystemCredentialsProvider.StoreImpl();
provider.addDomain(domain, credentials);
return credentials.getId();
}
}
42 changes: 5 additions & 37 deletions src/main/java/org/jenkinsci/plugins/ghprb/GhprbGitHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,22 @@
import org.kohsuke.github.GHOrganization;
import org.kohsuke.github.GHUser;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.GitHubBuilder;

import com.google.common.annotations.VisibleForTesting;

/**
* @author janinko
*/
public class GhprbGitHub {
private static final Logger logger = Logger.getLogger(GhprbGitHub.class.getName());
private GitHub gh;

private void connect() throws IOException {
String accessToken = GhprbTrigger.getDscp().getAccessToken();
String serverAPIUrl = GhprbTrigger.getDscp().getServerAPIUrl();
if (accessToken != null && !accessToken.isEmpty()) {
try {
gh = new GitHubBuilder()
.withEndpoint(serverAPIUrl)
.withOAuthToken(accessToken)
.withConnector(new HttpConnectorWithJenkinsProxy())
.build();
} catch (IOException e) {
logger.log(Level.SEVERE, "Can''t connect to {0} using oauth", serverAPIUrl);
throw e;
}
} else {
if (serverAPIUrl.contains("api/v3")) {
gh = GitHub.connectToEnterprise(serverAPIUrl,
GhprbTrigger.getDscp().getUsername(), GhprbTrigger.getDscp().getPassword());
} else {
gh = new GitHubBuilder().withPassword(GhprbTrigger.getDscp().getUsername(),
GhprbTrigger.getDscp().getPassword()).withConnector(new HttpConnectorWithJenkinsProxy()).build();
}
}
private final GhprbTrigger trigger;

public GhprbGitHub(GhprbTrigger trigger) {
this.trigger = trigger;
}

public GitHub get() throws IOException {
if (gh == null) {
connect();
}
return gh;
return trigger.getGitHub();
}

@VisibleForTesting
void setGitHub(GitHub gh) {
this.gh = gh;
}

public boolean isUserMemberOfOrganization(String organisation, GHUser member) {
boolean orgHasMember = false;
try {
Expand Down
Loading

0 comments on commit 85eec74

Please sign in to comment.