Skip to content
Permalink
Browse files

[FIXED JENKINS-40773] always use root build to discover correct item …

…or item group
  • Loading branch information
imod committed Jan 5, 2017
1 parent 79c988a commit b8cba8571543df1a1da1dd4690cdd09a7f6bb7c1
@@ -42,6 +42,8 @@

private String settingsConfigId;

private transient boolean failIfSettingsNotFound = false;

/**
* Default constructor used to load class via reflection by the maven-plugin for backward compatibility
*/
@@ -54,6 +56,13 @@ public MvnGlobalSettingsProvider(String settingsConfigId) {
this.settingsConfigId = settingsConfigId;
}

/*
* currently for testing only
*/
public void setFailIfSettingsNotFound(boolean failIfSettingsNotFound) {
this.failIfSettingsNotFound = failIfSettingsNotFound;
}

public String getSettingsConfigId() {
return settingsConfigId;
}
@@ -63,12 +72,16 @@ public void setSettingsConfigId(String settingsConfigId) {
}

@Override
public FilePath supplySettings(AbstractBuild<?, ?> build, TaskListener listener) {
public FilePath supplySettings(AbstractBuild<?, ?> originBuild, TaskListener listener) {
if (StringUtils.isNotBlank(settingsConfigId)) {

AbstractBuild<?, ?> build = originBuild.getRootBuild();

Config c = null;
if (build instanceof Item) {
c = ConfigFiles.getByIdOrNull((Item) build, settingsConfigId);
} else if (build.getParent() instanceof Item) {
c = ConfigFiles.getByIdOrNull((Item) build.getParent(), settingsConfigId);
} else if (build instanceof ItemGroup) {
c = ConfigFiles.getByIdOrNull((ItemGroup) build, settingsConfigId);
} else if (build.getParent() instanceof ItemGroup) {
@@ -77,7 +90,11 @@ public FilePath supplySettings(AbstractBuild<?, ?> build, TaskListener listener)


if (c == null) {
listener.getLogger().println("ERROR: your Apache Maven build is setup to use a config with id " + settingsConfigId + " but can not find the config");
String msg = "your Apache Maven build is setup to use a global config with id " + settingsConfigId + " but can not find the config";
listener.getLogger().println("ERROR: " + msg);
if (failIfSettingsNotFound) {
throw new IllegalStateException(msg);
}
} else {

GlobalMavenSettingsConfig config;
@@ -43,6 +43,8 @@

private String settingsConfigId;

private transient boolean failIfSettingsNotFound = false;

/**
* Default constructor used to load class via reflection by the maven-plugin for backward compatibility
*/
@@ -55,6 +57,13 @@ public MvnSettingsProvider(String settingsConfigId) {
this.settingsConfigId = settingsConfigId;
}

/*
* currently for testing only
*/
public void setFailIfSettingsNotFound(boolean failIfSettingsNotFound) {
this.failIfSettingsNotFound = failIfSettingsNotFound;
}

public String getSettingsConfigId() {
return settingsConfigId;
}
@@ -64,20 +73,29 @@ public void setSettingsConfigId(String settingsConfigId) {
}

@Override
public FilePath supplySettings(AbstractBuild<?, ?> build, TaskListener listener) {
public FilePath supplySettings(AbstractBuild<?, ?> originBuild, TaskListener listener) {

AbstractBuild<?, ?> build = originBuild.getRootBuild();

if (StringUtils.isNotBlank(settingsConfigId)) {

Config c = null;
if (build instanceof Item) {
c = ConfigFiles.getByIdOrNull((Item) build, settingsConfigId);
} else if (build.getParent() instanceof Item) {
c = ConfigFiles.getByIdOrNull((Item) build.getParent(), settingsConfigId);
} else if (build instanceof ItemGroup) {
c = ConfigFiles.getByIdOrNull((ItemGroup) build, settingsConfigId);
} else if (build.getParent() instanceof ItemGroup) {
c = ConfigFiles.getByIdOrNull((ItemGroup) build.getParent(), settingsConfigId);
}

if (c == null) {
listener.getLogger().println("ERROR: your Apache Maven build is setup to use a config with id " + settingsConfigId + " but can not find the config");
String msg = "your Apache Maven build is setup to use a config with id " + settingsConfigId + " but can not find the config";
listener.getLogger().println("ERROR: " + msg);
if (failIfSettingsNotFound) {
throw new IllegalStateException(msg);
}
} else {

MavenSettingsConfig config;
@@ -3,8 +3,10 @@
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import hudson.maven.MavenModuleSet;
import hudson.model.Cause;
import hudson.model.FreeStyleProject;
import hudson.model.Item;
import hudson.model.Result;
import hudson.tasks.Maven;
import org.jenkinsci.lib.configprovider.ConfigProvider;
import org.jenkinsci.lib.configprovider.model.Config;
@@ -16,15 +18,18 @@
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.ExtractResourceSCM;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.JenkinsRule.WebClient;
import org.jvnet.hudson.test.ToolInstallations;

import javax.inject.Inject;

import static org.junit.Assert.assertNotSame;

/**
* @author Kohsuke Kawaguchi
* @author Dominik Bartholdi (imod)
*/
public class MvnSettingsProviderTest {
@Rule
@@ -117,4 +122,27 @@ public void configMustBeVisbleFromConfigPage() throws Exception {
Assert.assertEquals("comment NOK", c2.comment, comment.getValueAttribute());
}
}

@Test
@Bug(40737)
public void mavenSettingsMustBeFoundInFreestyleProject() throws Exception {
jenkins.jenkins.getInjector().injectMembers(this);

final FreeStyleProject p = jenkins.createFreeStyleProject();

String mvnName = ToolInstallations.configureMaven3().getName();
Config c1 = createSetting(mavenSettingProvider);
Config c2 = createSetting(globalMavenSettingsConfigProvider);

MvnSettingsProvider s1 = new MvnSettingsProvider(c1.id);
s1.setFailIfSettingsNotFound(true);
MvnGlobalSettingsProvider s2 = new MvnGlobalSettingsProvider(c2.id);
s2.setFailIfSettingsNotFound(true);

Maven m = new Maven("clean", mvnName, null, null, null, false, s1, s2);
p.getBuildersList().add(m);
p.setScm(new ExtractResourceSCM(getClass().getResource("/maven3-project.zip")));

jenkins.assertBuildStatus(Result.SUCCESS, p.scheduleBuild2(0, new Cause.UserCause()).get());
}
}

0 comments on commit b8cba85

Please sign in to comment.