Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#314 null pointer exception bit bucket ppr action.get type() is null bug #319

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import javaposse.jobdsl.plugin.ContextExtensionPoint;
import javaposse.jobdsl.plugin.DslExtensionMethod;

@Extension(optional = true)
@Extension
public class BitBucketPPRHookJobDslExtension extends ContextExtensionPoint {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

package io.jenkins.plugins.bitbucketpushandpullrequest.filter.repository;

import static java.util.Objects.isNull;

import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;
Expand All @@ -37,6 +39,7 @@


public class BitBucketPPRRepositoryPushActionFilter extends BitBucketPPRRepositoryActionFilter {

private static final Logger logger =
Logger.getLogger(BitBucketPPRRepositoryPushActionFilter.class.getName());

Expand Down Expand Up @@ -69,6 +72,12 @@
logger.info(
() -> "Should trigger build for the bitbucket action: " + bitbucketAction.toString() + "?");

if (isNull(bitbucketAction) || isNull(bitbucketAction.getType())) {

Check warning on line 75 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/filter/repository/BitBucketPPRRepositoryPushActionFilter.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 75 is only partially covered, 2 branches are missing
logger.info(
"The bitbucketAction or the bitbucketAction type are not set.");
return false;
}

if (!bitbucketAction.getType().equalsIgnoreCase("BRANCH")
&& !bitbucketAction.getType().equalsIgnoreCase("named_branch")
&& !bitbucketAction.getType().equalsIgnoreCase("UPDATE")
Expand All @@ -82,14 +91,15 @@
}

if (this.triggerOnlyIfTagPush && !bitbucketAction.getType().equalsIgnoreCase("TAG")) {
return false;
return false;
}

return matches(allowedBranches, bitbucketAction.getTargetBranch(), null);
}

@Override
public BitBucketPPRTriggerCause getCause(File pollingLog, BitBucketPPRAction bitbucketAction, BitBucketPPRHookEvent bitBucketEvent)
public BitBucketPPRTriggerCause getCause(File pollingLog, BitBucketPPRAction bitbucketAction,
BitBucketPPRHookEvent bitBucketEvent)
throws IOException {
return new BitBucketPPRRepositoryCause(pollingLog, bitbucketAction, bitBucketEvent);
}
Expand All @@ -107,6 +117,11 @@
return triggerAlsoIfNothingChanged;
}

@Override
public boolean shouldSendApprove() {
return isToApprove;
}

@Extension
public static class ActionFilterDescriptorImpl extends BitBucketPPRRepositoryActionDescriptor {

Expand All @@ -115,9 +130,4 @@
return "Bitbucket Cloud Push";
}
}

@Override
public boolean shouldSendApprove() {
return isToApprove;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,24 @@

package io.jenkins.plugins.bitbucketpushandpullrequest.filter.repository;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import static java.util.Objects.isNull;

import hudson.Extension;
import io.jenkins.plugins.bitbucketpushandpullrequest.action.BitBucketPPRAction;
import io.jenkins.plugins.bitbucketpushandpullrequest.cause.BitBucketPPRTriggerCause;
import io.jenkins.plugins.bitbucketpushandpullrequest.cause.repository.BitBucketPPRServerRepositoryCause;
import io.jenkins.plugins.bitbucketpushandpullrequest.model.BitBucketPPRHookEvent;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;


public class BitBucketPPRServerRepositoryPushActionFilter
extends BitBucketPPRRepositoryActionFilter {

private static final Logger logger =
Logger.getLogger(BitBucketPPRServerRepositoryPushActionFilter.class.getName());

Expand Down Expand Up @@ -71,50 +72,63 @@
logger
.info(() -> "Should trigger build for bitbucket action" + bitbucketAction.toString() + "?");

if (isNull(bitbucketAction) || isNull(bitbucketAction.getType())) {

Check warning on line 75 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/filter/repository/BitBucketPPRServerRepositoryPushActionFilter.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 75 is only partially covered, 2 branches are missing
logger.info(
"The bitbucketAction or the bitbucketAction type are not set.");
return false;
}

if (!bitbucketAction.getType().equalsIgnoreCase("BRANCH")
&& !bitbucketAction.getType().equalsIgnoreCase("named_branch")
&& !bitbucketAction.getType().equalsIgnoreCase("UPDATE")
&& !bitbucketAction.getType().equalsIgnoreCase("TAG")
&& !this.triggerAlsoIfTagPush) {
logger.info(
() -> "Neither bitbucketActionType is BRANCH, nor UPDATE, nor trigger on tag push is set for bitbucket type: "
+ bitbucketAction.getType() + ".");
logger.info(
() ->
"Neither bitbucketActionType is BRANCH, nor UPDATE, nor trigger on tag push is set for bitbucket type: "
+ bitbucketAction.getType() + ".");

return false;
}

if (this.triggerOnlyIfTagPush && !bitbucketAction.getType().equalsIgnoreCase("TAG")) {
return false;
return false;
}

logger.log(Level.FINEST, "the target branch is: {0}.", bitbucketAction.getTargetBranch());
logger.log(Level.FINEST, "the target branch ref id is: {0}.", bitbucketAction.getTargetBranchRefId());
logger.log(Level.FINEST, "the target branch ref id is: {0}.",
bitbucketAction.getTargetBranchRefId());

logger.log(Level.FINEST, "The allowed branches are: {0}.", allowedBranches);
return matches(allowedBranches, bitbucketAction.getTargetBranch(), null)
|| matches(allowedBranches, bitbucketAction.getTargetBranchRefId(), null);
}

@Override
public BitBucketPPRTriggerCause getCause(File pollingLog, BitBucketPPRAction bitbucketAction,
public BitBucketPPRTriggerCause getCause(File pollingLog, BitBucketPPRAction bitbucketAction,
BitBucketPPRHookEvent bitBucketEvent)
throws IOException {
return new BitBucketPPRServerRepositoryCause(pollingLog, bitbucketAction, bitBucketEvent);
}

public String getAllowedBranches() {
return allowedBranches;
}

public void setAllowedBranches(String allowedBranches) {
this.allowedBranches = allowedBranches;
}

@Override
public boolean shouldTriggerAlsoIfNothingChanged() {
return triggerAlsoIfNothingChanged;
}

@Override
public boolean shouldSendApprove() {
return isToApprove;

Check warning on line 129 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/filter/repository/BitBucketPPRServerRepositoryPushActionFilter.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 88-129 are not covered by tests
}

@Extension
public static class ActionFilterDescriptorImpl extends BitBucketPPRRepositoryActionDescriptor {

Expand All @@ -123,9 +137,4 @@
return "Bitbucket Server Push";
}
}

@Override
public boolean shouldSendApprove() {
return isToApprove;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import io.jenkins.plugins.bitbucketpushandpullrequest.action.BitBucketPPRAction;
import java.util.HashMap;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import hudson.EnvVars;

Expand All @@ -15,7 +18,7 @@ public class BitBucketPPRRepositoryPushActionFilterTest {
@Test
public void testMatches() {
String allowedBranches = "master";

BitBucketPPRRepositoryPushActionFilter c =
new BitBucketPPRRepositoryPushActionFilter(false, false, allowedBranches);

Expand Down Expand Up @@ -131,7 +134,6 @@ public void testUsesRefsHeads() {
BitBucketPPRRepositoryPushActionFilter c =
new BitBucketPPRRepositoryPushActionFilter(false, false, allowedBranches);


assertTrue(c.matches(allowedBranches, "refs/heads/jenkins", null));
assertTrue(c.matches(allowedBranches, "refs/heads/jane", null));
assertTrue(c.matches(allowedBranches, "refs/heads/jones", null));
Expand Down Expand Up @@ -219,4 +221,14 @@ public void testUsesJavaPatternToExcludeMultipleBranches() {
assertFalse(m.matches(allowedBranches, "origin/master", null));
assertFalse(m.matches(allowedBranches, "origin/develop", null));
}


@Test
public void shouldTriggerBuildReturnsFalseIsTypeNotSet() {
BitBucketPPRAction bitbucketAction = Mockito.mock(BitBucketPPRAction.class);
Mockito.when(bitbucketAction.getType()).thenReturn(null);
BitBucketPPRRepositoryPushActionFilter c = new BitBucketPPRRepositoryPushActionFilter(false,
false, "master");
assertFalse(c.shouldTriggerBuild(bitbucketAction));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import io.jenkins.plugins.bitbucketpushandpullrequest.action.BitBucketPPRAction;
import java.util.HashMap;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import hudson.EnvVars;

Expand Down Expand Up @@ -201,4 +204,12 @@ public void testUsesJavaPatternToExcludeMultipleBranches() {
assertFalse(m.matches(allowedBranches, "origin/master", null));
assertFalse(m.matches(allowedBranches, "origin/develop", null));
}

@Test
public void shouldTriggerBuildReturnsFalseIsTypeNotSet() {
BitBucketPPRAction bitbucketAction = Mockito.mock(BitBucketPPRAction.class);
Mockito.when(bitbucketAction.getType()).thenReturn(null);
BitBucketPPRServerRepositoryPushActionFilter c = new BitBucketPPRServerRepositoryPushActionFilter(false, false, "master");
assertFalse(c.shouldTriggerBuild(bitbucketAction));
}
}
Loading