Skip to content
Permalink
Browse files

Merge pull request #83 from paul-schwendenman/null-build-cause

[FIXED JENKINS-32693] - Fix BuildCauseRetriever when AbstractBuild has Null CauseAction
  • Loading branch information...
oleg-nenashev committed Jan 30, 2016
2 parents 61417c7 + e6fd97c commit 784f1d92af0c4f3a6f5c4c355f0c2e648e59171a
@@ -33,13 +33,19 @@

public Map<String, String> getTriggeredCause(AbstractBuild<?, ?> build) {
CauseAction causeAction = build.getAction(CauseAction.class);
List<Cause> buildCauses = causeAction.getCauses();
Map<String, String> env = new HashMap<String, String>();
List<String> directCauseNames = new ArrayList<String>();
Set<String> rootCauseNames = new LinkedHashSet<String>();
for (Cause cause : buildCauses) {
directCauseNames.add(getTriggerName(cause));
insertRootCauseNames(rootCauseNames, cause, 0);

if (causeAction != null) {
List<Cause> buildCauses = causeAction.getCauses();
for (Cause cause : buildCauses) {
directCauseNames.add(getTriggerName(cause));
insertRootCauseNames(rootCauseNames, cause, 0);
}
} else {
directCauseNames.add("UNKNOWN");
rootCauseNames.add("UNKNOWN");
}
env.putAll(buildCauseEnvironmentVariables(ENV_CAUSE, directCauseNames));
env.putAll(buildCauseEnvironmentVariables(ENV_ROOT_CAUSE, rootCauseNames));
@@ -0,0 +1,36 @@
package org.jenkinsci.plugins.envinject.sevice;

import hudson.model.AbstractBuild;
import hudson.model.CauseAction;
import org.jenkinsci.plugins.envinject.EnvInjectPluginAction;
import org.jenkinsci.plugins.envinject.service.BuildCauseRetriever;
import org.junit.Before;
import org.junit.Test;

import java.util.Map;

import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;


public class BuildCauseRetrieverTest {

private AbstractBuild build;

private BuildCauseRetriever buildCause;

@Before
public void setUp() {
build = mock(AbstractBuild.class);
buildCause = new BuildCauseRetriever();
}

@Test
public void abstractBuildWithNullCauseActionReturnsUnknown() throws Exception {
when(build.getAction(CauseAction.class)).thenReturn(null);
Map<String, String> envVars = buildCause.getTriggeredCause(build);
assertTrue(envVars.get("BUILD_CAUSE").equals("UNKNOWN"));
}

}

0 comments on commit 784f1d9

Please sign in to comment.
You can’t perform that action at this time.