Skip to content

Commit

Permalink
[Fix JENKINS-30178] Add default parameters defined in the job
Browse files Browse the repository at this point in the history
  • Loading branch information
fbelzunc authored and MarkEWaite committed Dec 8, 2015
1 parent 7bb68ef commit 0a661ce
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions src/main/java/hudson/plugins/git/GitStatus.java
Expand Up @@ -206,7 +206,7 @@ public List<ResponseContributor> onNotifyCommit(URIish uri, String sha1, List<Pa
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine("Received notification for uri = " + uri + " ; sha1 = " + sha1 + " ; branches = " + Arrays.toString(branches));
}

List<ParameterValue> allBuildParameters = new ArrayList<ParameterValue>(buildParameters);
List<ResponseContributor> result = new ArrayList<ResponseContributor>();
// run in high privilege to see all the projects anonymous users don't see.
// this is safe because when we actually schedule a build, it's a build that can
Expand Down Expand Up @@ -278,13 +278,26 @@ public List<ResponseContributor> onNotifyCommit(URIish uri, String sha1, List<Pa
}
if (!branchFound) continue;
urlFound = true;

if (!(project instanceof AbstractProject && ((AbstractProject) project).isDisabled())) {
//JENKINS-30178 Add default parameters defined in the job
if (project instanceof Job) {
Set<String> buildParametersNames = new HashSet<String>();
for (ParameterValue parameterValue: allBuildParameters) {
buildParametersNames.add(parameterValue.getName());
}

List<ParameterValue> jobParametersValues = getDefaultParametersValues((Job) project);
for (ParameterValue defaultParameterValue : jobParametersValues) {
if (!buildParametersNames.contains(defaultParameterValue.getName())) {
allBuildParameters.add(defaultParameterValue);
}
}
}
if (!parametrizedBranchSpec && isNotEmpty(sha1)) {
LOGGER.info("Scheduling " + project.getFullDisplayName() + " to build commit " + sha1);
scmTriggerItem.scheduleBuild2(scmTriggerItem.getQuietPeriod(),
new CauseAction(new CommitHookCause(sha1)),
new RevisionParameterAction(sha1, matchedURL), new ParametersAction(buildParameters));
new RevisionParameterAction(sha1, matchedURL), new ParametersAction(allBuildParameters));
result.add(new ScheduledResponseContributor(project));
} else {
LOGGER.info("Triggering the polling of " + project.getFullDisplayName());
Expand Down Expand Up @@ -312,6 +325,35 @@ public List<ResponseContributor> onNotifyCommit(URIish uri, String sha1, List<Pa
}
}

/**
* Get the default parameters values from a job
*
*/
private ArrayList<ParameterValue> getDefaultParametersValues(Job<?,?> job) {
ArrayList<ParameterValue> defValues;
ParametersDefinitionProperty paramDefProp = job.getProperty(ParametersDefinitionProperty.class);

if (paramDefProp != null) {
List <ParameterDefinition> parameterDefinition = paramDefProp.getParameterDefinitions();
defValues = new ArrayList<ParameterValue>(parameterDefinition.size());

} else {
defValues = new ArrayList<ParameterValue>();
return defValues;
}

/* Scan for all parameter with an associated default values */
for (ParameterDefinition paramDefinition : paramDefProp.getParameterDefinitions()) {
ParameterValue defaultValue = paramDefinition.getDefaultParameterValue();

if (defaultValue != null) {
defValues.add(defaultValue);
}
}

return defValues;
}

/**
* A response contributor for triggering polling of a project.
*
Expand Down

0 comments on commit 0a661ce

Please sign in to comment.