Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
If setenv and envinject are both configured for a job the
EnvInjectMigrationBuildWrapper will overwrite existing envinject config.

This fix introduces the migration in SetEnvBuildWrapper to the original project
so it can check if there is any existing config and transfer that over before
returning the replacement. It currently only looks at the propertiesContent
and any other config will still be overwritten.

Change-Id: Ief68f06d714f0a31f8fc8461aaa4cd893c64599b
  • Loading branch information
rsandell committed Mar 25, 2014
1 parent 0287bec commit eac6c25
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@ work
*.ipr
*.iml
*.iws
.idea/*
Expand Up @@ -5,6 +5,7 @@
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.BuildableItemWithBuildWrappers;
import hudson.tasks.BuildWrapperDescriptor;
import org.jenkinsci.plugins.envinject.EnvInjectBuildWrapper;
import org.jenkinsci.plugins.envinject.EnvInjectJobPropertyInfo;
Expand All @@ -21,7 +22,7 @@ public class EnvFileBuildWrapper extends EnvInjectMigrationBuildWrapper {
private transient String filePath;

@Override
public EnvInjectBuildWrapper getEnvInjectBuildWrapper() {
public EnvInjectBuildWrapper getEnvInjectBuildWrapper(BuildableItemWithBuildWrappers originalItem) {
EnvInjectJobPropertyInfo jobPropertyInfo = new EnvInjectJobPropertyInfo(filePath, null, null, null, null, false);
EnvInjectBuildWrapper envInjectBuildWrapper = new EnvInjectBuildWrapper();
envInjectBuildWrapper.setInfo(jobPropertyInfo);
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/hudson/plugins/setenv/SetEnvBuildWrapper.java
Expand Up @@ -5,6 +5,7 @@
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.BuildableItemWithBuildWrappers;
import hudson.tasks.BuildWrapperDescriptor;
import org.jenkinsci.plugins.envinject.EnvInjectBuildWrapper;
import org.jenkinsci.plugins.envinject.EnvInjectJobPropertyInfo;
Expand All @@ -21,8 +22,16 @@ public class SetEnvBuildWrapper extends EnvInjectMigrationBuildWrapper {
private transient String localVarText;

@Override
public EnvInjectBuildWrapper getEnvInjectBuildWrapper() {
EnvInjectJobPropertyInfo jobPropertyInfo = new EnvInjectJobPropertyInfo(null, localVarText, null, null, null, false);
public EnvInjectBuildWrapper getEnvInjectBuildWrapper(BuildableItemWithBuildWrappers originalItem) {
String varText = localVarText;
EnvInjectBuildWrapper existing = originalItem.getBuildWrappersList().get(EnvInjectBuildWrapper.class);
if (existing != null && existing.getInfo() != null) {
String existingContent = existing.getInfo().getPropertiesContent();
if (existingContent != null && !existingContent.isEmpty()) {
varText = varText + "\n" + existingContent;
}
}
EnvInjectJobPropertyInfo jobPropertyInfo = new EnvInjectJobPropertyInfo(null, varText, null, null, null, false);
EnvInjectBuildWrapper envInjectBuildWrapper = new EnvInjectBuildWrapper();
envInjectBuildWrapper.setInfo(jobPropertyInfo);
return envInjectBuildWrapper;
Expand Down
@@ -1,5 +1,6 @@
package org.jenkinsci.plugins.envinject.migration;

import hudson.model.BuildableItemWithBuildWrappers;
import hudson.tasks.BuildWrapper;
import org.jenkinsci.plugins.envinject.EnvInjectBuildWrapper;

Expand All @@ -12,6 +13,7 @@ public abstract class EnvInjectMigrationBuildWrapper extends BuildWrapper {
* Gets the new object with the mapped fields
*
* @return an EnvInjectBuildWrapper object
* @param originalItem
*/
protected abstract EnvInjectBuildWrapper getEnvInjectBuildWrapper();
protected abstract EnvInjectBuildWrapper getEnvInjectBuildWrapper(BuildableItemWithBuildWrappers originalItem);
}
Expand Up @@ -79,7 +79,7 @@ public void onLoaded() {
buildWrapperIterator.remove();

//Add new wrapper
addOrModifyEnvInjectBuildWrapper(buildableItemWithBuildWrappers.getBuildWrappersList(), oldWrapper.getEnvInjectBuildWrapper());
addOrModifyEnvInjectBuildWrapper(buildableItemWithBuildWrappers.getBuildWrappersList(), oldWrapper.getEnvInjectBuildWrapper(buildableItemWithBuildWrappers));

//Save the job with the new elements (the config.xml is overridden)
buildableItemWithBuildWrappers.save();
Expand Down
@@ -0,0 +1,60 @@
/*
* The MIT License
*
* Copyright 2014 Sony Mobile Communications AB. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.envinject.migration;

import hudson.model.FreeStyleProject;
import org.jenkinsci.plugins.envinject.EnvInjectBuildWrapper;
import org.jenkinsci.plugins.envinject.EnvInjectJobProperty;
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.hudson.test.recipes.LocalData;

/**
* Tests the migrations.
*
* @author Robert Sandell
*/
public class EnvInjectMigrationBuildWrapperTest extends HudsonTestCase {

/**
* Tests that an old project containing both a set-env setting and a envInject wrapper
* doesn't get overwritten in the migration.
*/
@LocalData
@Bug(22169)
public void testSetEnvAndEnvInject() {
FreeStyleProject project = (FreeStyleProject) jenkins.getItem("Experimental_SetEnvMigration");
assertNotNull(project);
EnvInjectBuildWrapper wrapper = project.getBuildWrappersList().get(EnvInjectBuildWrapper.class);
String content = wrapper.getInfo().getPropertiesContent();

assertStringContains(content, "ONE=one");
assertStringContains(content, "HELLO=world");
assertStringContains(content, "ME=you");

EnvInjectJobProperty property = project.getProperty(EnvInjectJobProperty.class);
assertStringContains(property.getInfo().getPropertiesContent(), "ZERO=0");
}
}
Binary file not shown.

0 comments on commit eac6c25

Please sign in to comment.