Skip to content

Commit

Permalink
fix(jenkins): don't npe on dynamic choice parameters (#502)
Browse files Browse the repository at this point in the history
  • Loading branch information
emjburns committed Sep 4, 2019
1 parent d087e74 commit 3aecf2f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,10 @@ class BuildController {
static void validateJobParameters(JobConfig jobConfig, Map<String, String> requestParams) {
jobConfig.parameterDefinitionList.each { parameterDefinition ->
String matchingParam = requestParams[parameterDefinition.name]
if (matchingParam != null && parameterDefinition.type == 'ChoiceParameterDefinition' && !parameterDefinition.choices.contains(matchingParam)) {
if (matchingParam != null &&
parameterDefinition.type == 'ChoiceParameterDefinition' &&
parameterDefinition.choices != null &&
!parameterDefinition.choices.contains(matchingParam)) {
throw new InvalidJobParameterException("`${matchingParam}` is not a valid choice " +
"for `${parameterDefinition.name}`. Valid choices are: ${parameterDefinition.choices.join(', ')}")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import retrofit.client.Response
import spock.lang.Shared
import spock.lang.Specification

import static com.netflix.spinnaker.igor.build.BuildController.*
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
Expand Down Expand Up @@ -310,4 +311,39 @@ class BuildControllerSpec extends Specification {
response.status == HttpStatus.BAD_REQUEST.value()
response.errorMessage == "Job '${JOB_NAME}' is not buildable. It may be disabled."
}

void 'validation successful for null list of choices'() {
given:
Map<String, String> requestParams = ["hey" : "you"]
ParameterDefinition parameterDefinition = new ParameterDefinition()
parameterDefinition.choices = null
parameterDefinition.type = "ChoiceParameterDefinition"
parameterDefinition.name = "hey"
JobConfig jobConfig = new JobConfig()
jobConfig.parameterDefinitionList = [parameterDefinition]

when:
validateJobParameters(jobConfig, requestParams)

then:
noExceptionThrown()
}

void 'validation failed for option not in list of choices'() {
given:
Map<String, String> requestParams = ["hey" : "you"]
ParameterDefinition parameterDefinition = new ParameterDefinition()
parameterDefinition.choices = ["why", "not"]
parameterDefinition.type = "ChoiceParameterDefinition"
parameterDefinition.name = "hey"
JobConfig jobConfig = new JobConfig()
jobConfig.parameterDefinitionList = [parameterDefinition]

when:
validateJobParameters(jobConfig, requestParams)

then:
thrown(InvalidJobParameterException)
}

}

0 comments on commit 3aecf2f

Please sign in to comment.