Skip to content

Commit

Permalink
Merge pull request #83 from paul-schwendenman/null-build-cause
Browse files Browse the repository at this point in the history
[FIXED JENKINS-32693] - Fix BuildCauseRetriever when AbstractBuild has Null CauseAction
  • Loading branch information
oleg-nenashev committed Jan 30, 2016
2 parents 61417c7 + e6fd97c commit 784f1d9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
Expand Up @@ -33,13 +33,19 @@ public class BuildCauseRetriever {

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));
Expand Down
@@ -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.