Skip to content

Commit

Permalink
Merge pull request #66 from lanwen/misc
Browse files Browse the repository at this point in the history
fix "Unknown content type null" in logs and since for 1.12.0 api
  • Loading branch information
KostyaSha committed Jul 29, 2015
2 parents 6f1354b + 9723aab commit a8dc555
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/cloudbees/jenkins/GitHubRepositoryName.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;

import javax.annotation.CheckForNull;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
Expand Down Expand Up @@ -119,6 +120,7 @@ protected GHRepository adapt(GitHub item) {
* This is useful if the caller only relies on the read access to the repository and doesn't need to
* walk possible candidates.
*/
@CheckForNull
public GHRepository resolveOne() {
for (GHRepository r : resolve())
return r;
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/cloudbees/jenkins/GitHubWebHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import hudson.util.Iterators.FilterIterator;
import hudson.util.SequentialExecutionQueue;
import jenkins.model.Jenkins;
import org.apache.commons.lang3.Validate;
import org.jenkinsci.plugins.github.extension.GHEventsSubscriber;
import org.jenkinsci.plugins.github.internal.GHPluginConfigException;
import org.jenkinsci.plugins.github.webhook.GHEventHeader;
Expand Down Expand Up @@ -174,9 +175,7 @@ public static GitHubWebHook get() {
@Nonnull
public static Jenkins getJenkinsInstance() throws IllegalStateException {
Jenkins instance = Jenkins.getInstance();
if (instance == null) {
throw new IllegalStateException("Jenkins has not been started, or was already shut down");
}
Validate.validState(instance != null, "Jenkins has not been started, or was already shut down");
return instance;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* Each time this plugin wants to get events list from subscribers it asks for applicable status
*
* @author lanwen (Merkushev Kirill)
* @since TODO
* @since 1.12.0
*/
public abstract class GHEventsSubscriber implements ExtensionPoint {
private static final Logger LOGGER = LoggerFactory.getLogger(GHEventsSubscriber.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,20 @@ public final Optional<E> firstMatch(Predicate<? super E> predicate) {
return Iterables.tryFind(iterable, predicate);
}

/**
* Returns an {@link Optional} containing the first element in this fluent iterable.
* If the iterable is empty, {@code Optional.absent()} is returned.
*
* @throws NullPointerException if the first element is null; if this is a possibility, use
* {@code iterator().next()} or {@link Iterables#getFirst} instead.
*/
public final Optional<E> first() {
Iterator<E> iterator = iterable.iterator();
return iterator.hasNext()
? Optional.of(iterator.next())
: Optional.<E>absent();
}

/**
* Returns list from wrapped iterable
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Utility class which holds converters or predicates (matchers) to filter or convert job lists
*
* @author lanwen (Merkushev Kirill)
* @since TODO
* @since 1.12.0
*/
public final class JobInfoHelpers {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.jenkinsci.plugins.github.util.misc;

import com.google.common.base.Function;
import com.google.common.base.Preconditions;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
* This abstract class calls {@link #applyNullSafe(Object)} only after success validation of inner object for null
*
* @author lanwen (Merkushev Kirill)
*/
public abstract class NullSafeFunction<F, T> implements Function<F, T> {

@Override
public T apply(@Nullable F input) {
return applyNullSafe(Preconditions.checkNotNull(input, "This function not allows to use null as argument"));
}

/**
* This method will be called inside of {@link #apply(Object)}
*/
protected abstract T applyNullSafe(@Nonnull F input);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.jenkinsci.plugins.github.util.misc;

import com.google.common.base.Predicate;

import javax.annotation.Nonnull;

import static com.google.common.base.Preconditions.checkNotNull;

/**
* This abstract class calls {@link #applyNullSafe(Object)} only after success validation of inner object for null
*
* @author lanwen (Merkushev Kirill)
*/

public abstract class NullSafePredicate<T> implements Predicate<T> {

@Override
public boolean apply(T input) {
return applyNullSafe(checkNotNull(input, "Argument for this predicate can't be null"));
}

/**
* This method will be called inside of {@link #apply(Object)}
*/
protected abstract boolean applyNullSafe(@Nonnull T input);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jenkinsci.plugins.github.webhook;

import com.cloudbees.jenkins.GitHubWebHook;
import com.google.common.base.Charsets;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -53,6 +54,11 @@ class PayloadHandler extends AnnotationHandler<GHEventPayload> {
*/
@Override
public Object parse(StaplerRequest req, GHEventPayload a, Class type, String param) throws ServletException {
if (req.getHeader(GitHubWebHook.URL_VALIDATION_HEADER) != null) {
// if self test for custom hook url
return null;
}

String contentType = req.getContentType();

if (!PAYLOAD_PROCESS.containsKey(contentType)) {
Expand All @@ -68,6 +74,7 @@ public Object parse(StaplerRequest req, GHEventPayload a, Class type, String par

/**
* used for application/x-www-form-urlencoded content-type
*
* @return function to extract payload from form request parameters
*/
protected static Function<StaplerRequest, String> fromForm() {
Expand All @@ -80,7 +87,8 @@ public String apply(StaplerRequest request) {
}

/**
* used for application/json content-type
* used for application/json content-type
*
* @return function to extract payload from body
*/
protected static Function<StaplerRequest, String> fromApplicationJson() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* Each manager works with only one hook url (created with {@link #forHookUrl(URL)})
*
* @author lanwen (Merkushev Kirill)
* @since TODO
* @since 1.12.0
*/
public class WebhookManager {
private static final Logger LOGGER = LoggerFactory.getLogger(WebhookManager.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* By default this plugin interested in push events only when job uses {@link GitHubPushTrigger}
*
* @author lanwen (Merkushev Kirill)
* @since TODO
* @since 1.12.0
*/
@Extension
@SuppressWarnings("unused")
Expand Down

0 comments on commit a8dc555

Please sign in to comment.