forked from morficus/Parameterized-Remote-Trigger-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from scottanderson/master
Multiple bug fixes
- Loading branch information
Showing
4 changed files
with
291 additions
and
3 deletions.
There are no files selected for viewing
151 changes: 151 additions & 0 deletions
151
src/main/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildInfoExporterAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package org.jenkinsci.plugins.ParameterizedRemoteTrigger; | ||
|
||
import hudson.EnvVars; | ||
import hudson.model.EnvironmentContributingAction; | ||
import hudson.model.Result; | ||
import hudson.model.AbstractBuild; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
class BuildInfoExporterAction implements EnvironmentContributingAction { | ||
|
||
public static final String JOB_NAME_VARIABLE = "LAST_TRIGGERED_JOB_NAME"; | ||
public static final String ALL_JOBS_NAME_VARIABLE = "TRIGGERED_JOB_NAMES"; | ||
public static final String BUILD_NUMBER_VARIABLE_PREFIX = "TRIGGERED_BUILD_NUMBER_"; | ||
public static final String ALL_BUILD_NUMBER_VARIABLE_PREFIX = "TRIGGERED_BUILD_NUMBERS_"; | ||
public static final String BUILD_RESULT_VARIABLE_PREFIX = "TRIGGERED_BUILD_RESULT_"; | ||
public static final String BUILD_RUN_COUNT_PREFIX = "TRIGGERED_BUILD_RUN_COUNT_"; | ||
public static final String RUN = "_RUN_"; | ||
|
||
private List<BuildReference> builds; | ||
|
||
public BuildInfoExporterAction(AbstractBuild<?, ?> parentBuild, BuildReference buildRef) { | ||
super(); | ||
|
||
this.builds = new ArrayList<BuildReference>(); | ||
this.builds.add(buildRef); | ||
} | ||
|
||
static BuildInfoExporterAction addBuildInfoExporterAction(AbstractBuild<?, ?> parentBuild, String triggeredProject, int buildNumber, Result buildResult) { | ||
BuildReference reference = new BuildReference(triggeredProject, buildNumber, buildResult); | ||
|
||
BuildInfoExporterAction action = parentBuild.getAction(BuildInfoExporterAction.class); | ||
if (action == null) { | ||
action = new BuildInfoExporterAction(parentBuild, reference); | ||
parentBuild.getActions().add(action); | ||
} else { | ||
action.addBuildReference(reference); | ||
} | ||
return action; | ||
} | ||
|
||
public void addBuildReference(BuildReference buildRef) { | ||
this.builds.add(buildRef); | ||
} | ||
|
||
public static class BuildReference { | ||
public final String projectName; | ||
public final int buildNumber; | ||
public final Result buildResult; | ||
|
||
public BuildReference(String projectName, int buildNumber, Result buildResult) { | ||
this.projectName = projectName; | ||
this.buildNumber = buildNumber; | ||
this.buildResult = buildResult; | ||
} | ||
} | ||
|
||
public String getIconFileName() { | ||
// TODO Auto-generated method stub | ||
return null; | ||
} | ||
|
||
public String getDisplayName() { | ||
// TODO Auto-generated method stub | ||
return null; | ||
} | ||
|
||
public String getUrlName() { | ||
// TODO Auto-generated method stub | ||
return null; | ||
} | ||
|
||
public void buildEnvVars(AbstractBuild<?, ?> build, EnvVars env) { | ||
for (String project : getProjectsWithBuilds()) { | ||
String sanatizedBuildName = project.replaceAll("[^a-zA-Z0-9]+", "_"); | ||
List<BuildReference> refs = getBuildRefs(project); | ||
|
||
env.put(ALL_BUILD_NUMBER_VARIABLE_PREFIX + sanatizedBuildName, getBuildNumbersString(refs, ",")); | ||
env.put(BUILD_RUN_COUNT_PREFIX + sanatizedBuildName, Integer.toString(refs.size())); | ||
for (BuildReference br : refs) { | ||
if (br.buildNumber != 0) { | ||
String tiggeredBuildRunResultKey = BUILD_RESULT_VARIABLE_PREFIX + sanatizedBuildName + RUN + Integer.toString(br.buildNumber); | ||
env.put(tiggeredBuildRunResultKey, br.buildResult.toString()); | ||
} | ||
} | ||
BuildReference lastBuild = null; | ||
for (int i = (refs.size()); i > 0; i--) { | ||
if (refs.get(i - 1).buildNumber != 0) { | ||
lastBuild = refs.get(i - 1); | ||
break; | ||
} | ||
} | ||
if (lastBuild != null) { | ||
env.put(BUILD_NUMBER_VARIABLE_PREFIX + sanatizedBuildName, Integer.toString(lastBuild.buildNumber)); | ||
env.put(BUILD_RESULT_VARIABLE_PREFIX + sanatizedBuildName, lastBuild.buildResult.toString()); | ||
} | ||
} | ||
} | ||
|
||
private List<BuildReference> getBuildRefs(String project) { | ||
List<BuildReference> refs = new ArrayList<BuildReference>(); | ||
for (BuildReference br : builds) { | ||
if (br.projectName.equals(project)) refs.add(br); | ||
} | ||
return refs; | ||
} | ||
|
||
/** | ||
* Gets a string for all of the build numbers | ||
* | ||
* @param refs List of build references to process. | ||
* @param separator | ||
* @return String containing all the build numbers from refs, never null but | ||
* can be empty | ||
*/ | ||
private String getBuildNumbersString(List<BuildReference> refs, String separator) { | ||
StringBuilder buf = new StringBuilder(); | ||
boolean first = true; | ||
|
||
for (BuildReference s : refs) { | ||
if (s.buildNumber != 0) { | ||
if (first) { | ||
first = false; | ||
} else { | ||
buf.append(separator); | ||
} | ||
buf.append(s.buildNumber); | ||
} | ||
} | ||
return buf.toString(); | ||
} | ||
|
||
/** | ||
* Gets the unique set of project names that have a linked build. | ||
* | ||
* @return Set of project names that have at least one build linked. | ||
*/ | ||
private Set<String> getProjectsWithBuilds() { | ||
Set<String> projects = new HashSet<String>(); | ||
|
||
for (BuildReference br : this.builds) { | ||
if (br.buildNumber != 0) { | ||
projects.add(br.projectName); | ||
} | ||
} | ||
return projects; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/SearchPattern.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.jenkinsci.plugins.ParameterizedRemoteTrigger; | ||
|
||
import java.util.Iterator; | ||
import java.util.NoSuchElementException; | ||
|
||
/** | ||
* Search around a specified {@link startingValue} by a magnitude of {@link maxDrift}. | ||
*/ | ||
public class SearchPattern implements Iterable<Integer> { | ||
private final int startingValue; | ||
private final int maxDrift; | ||
|
||
public SearchPattern(int startingValue, int maxDrift) { | ||
this.startingValue = startingValue; | ||
this.maxDrift = maxDrift; | ||
} | ||
|
||
public Iterator<Integer> iterator() { | ||
return new Iterator<Integer>() { | ||
private int drift = 0; | ||
|
||
public boolean hasNext() { | ||
return (drift != -maxDrift - 1); | ||
} | ||
|
||
public Integer next() { | ||
if (! hasNext()) throw new NoSuchElementException(); | ||
int ret = startingValue + drift; | ||
if (drift < 0) { | ||
drift = -drift; | ||
} else { | ||
drift = -drift - 1; | ||
} | ||
return ret; | ||
} | ||
|
||
public void remove() { | ||
next(); | ||
} | ||
}; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/test/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/SearchPatternTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.jenkinsci.plugins.ParameterizedRemoteTrigger; | ||
|
||
import junit.framework.TestCase; | ||
|
||
import java.util.Iterator; | ||
|
||
public class SearchPatternTest extends TestCase { | ||
|
||
public void testSearchPattern() { | ||
SearchPattern sp = new SearchPattern(5, 2); | ||
// Test iterator() twice | ||
for (int x = 0; x < 2; x++) { | ||
Iterator<Integer> it = sp.iterator(); | ||
assertEquals(5, it.next().intValue()); | ||
assertEquals(4, it.next().intValue()); | ||
assertEquals(6, it.next().intValue()); | ||
assertEquals(3, it.next().intValue()); | ||
assertEquals(7, it.next().intValue()); | ||
assertEquals(false, it.hasNext()); | ||
} | ||
} | ||
|
||
} |