diff --git a/Jenkinsfile b/Jenkinsfile index 85e1f3a..18218b7 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,2 +1,5 @@ -// Build the plugin using https://github.com/jenkins-infra/pipeline-library -buildPlugin(jenkinsVersions: [null, '2.107.2']) +/* + See the documentation for more options: + https://github.com/jenkins-infra/pipeline-library/ +*/ +buildPlugin(useContainerAgent: true) diff --git a/README.md b/README.md index 32da999..4b1fde1 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,15 @@ you're using have been translated into the specified language). To additionally force this language on all users, overriding their browser language, you can check the "Ignore browser preference and force this language to all users" option. +Since version 1.5 JCasC is supported + +``` +unclassified: + locale: + systemLocale: en + ignoreAcceptLanguage: true +``` + ### Changelog * See [GitHub releases](https://github.com/jenkinsci/locale-plugin/releases) for new releases diff --git a/pom.xml b/pom.xml index 96c0e17..d4c8002 100644 --- a/pom.xml +++ b/pom.xml @@ -7,12 +7,14 @@ org.jenkins-ci.plugins plugin - 3.15 + 4.34 + - 1.625.3 - 7 + 2.289.3 + 8 + 1346.ve8cfa_3473c94 io.jenkins.plugins @@ -58,7 +60,33 @@ https://github.com/jenkinsci/locale-plugin HEAD + + + + + io.jenkins.tools.bom + bom-2.289.x + 1148.v7261f385f859 + import + pom + + + + + + + io.jenkins + configuration-as-code + ${configuration-as-code.version} + test + + + io.jenkins.configuration-as-code + test-harness + ${configuration-as-code.version} + test + + - - + \ No newline at end of file diff --git a/src/main/java/hudson/plugins/locale/LocaleFilter.java b/src/main/java/hudson/plugins/locale/LocaleFilter.java index b3e1bee..78dcdc2 100644 --- a/src/main/java/hudson/plugins/locale/LocaleFilter.java +++ b/src/main/java/hudson/plugins/locale/LocaleFilter.java @@ -28,7 +28,7 @@ public void init(FilterConfig filterConfig) public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (request instanceof HttpServletRequest) { - PluginImpl plugin = (PluginImpl) Jenkins.getActiveInstance().getPlugin("locale"); + PluginImpl plugin = PluginImpl.get(); if (plugin != null && plugin.isIgnoreAcceptLanguage()) { request = new HttpServletRequestWrapper((HttpServletRequest) request) { @Override diff --git a/src/main/java/hudson/plugins/locale/PluginImpl.java b/src/main/java/hudson/plugins/locale/PluginImpl.java index e6dc8e0..f05997e 100644 --- a/src/main/java/hudson/plugins/locale/PluginImpl.java +++ b/src/main/java/hudson/plugins/locale/PluginImpl.java @@ -1,13 +1,17 @@ package hudson.plugins.locale; import com.thoughtworks.xstream.XStream; -import hudson.Plugin; +import hudson.Extension; import hudson.Util; import hudson.XmlFile; -import hudson.model.Descriptor.FormException; +import hudson.init.InitMilestone; +import hudson.init.Initializer; import hudson.util.PluginServletFilter; import hudson.util.XStream2; +import jenkins.model.GlobalConfiguration; +import jenkins.model.Jenkins; import net.sf.json.JSONObject; +import org.jenkinsci.Symbol; import org.jvnet.localizer.LocaleProvider; import org.kohsuke.stapler.StaplerRequest; @@ -20,7 +24,9 @@ /** * @author Kohsuke Kawaguchi */ -public class PluginImpl extends Plugin { +@Extension +@Symbol("locale") +public class PluginImpl extends GlobalConfiguration { private String systemLocale; private boolean ignoreAcceptLanguage; @@ -30,9 +36,25 @@ public class PluginImpl extends Plugin { */ private transient final Locale originalLocale = Locale.getDefault(); + public static PluginImpl get() { + return Jenkins.get().getExtensionList(PluginImpl.class).get(0); + } + + public PluginImpl() { + load(); + } + @Override - public void start() - throws Exception { + protected XmlFile getConfigFile() { + return new XmlFile(XSTREAM, new File(Jenkins.get().getRootDir(), "locale.xml")); // for backward compatibility + } + + @Initializer(after = InitMilestone.EXTENSIONS_AUGMENTED) + public static void init() throws Exception { + PluginImpl.get().start(); + } + + private void start() throws ServletException { load(); LocaleProvider.setProvider(new LocaleProvider() { LocaleProvider original = LocaleProvider.getProvider(); @@ -50,23 +72,17 @@ public Locale get() { } @Override - protected void load() - throws IOException { + public void load() { super.load(); setSystemLocale(systemLocale); // make the loaded value take effect } - @Override - protected XmlFile getConfigXml() { - return new XmlFile(XSTREAM, new File(Jenkins.getActiveInstance().getRootDir(), "locale.xml")); - } @Override - public void configure(StaplerRequest req, JSONObject jsonObject) - throws IOException, ServletException, FormException { - setSystemLocale(jsonObject.getString("systemLocale")); - ignoreAcceptLanguage = jsonObject.getBoolean("ignoreAcceptLanguage"); + public boolean configure(StaplerRequest req, JSONObject jsonObject) throws FormException { + req.bindJSON(this, jsonObject); save(); + return false; } public boolean isIgnoreAcceptLanguage() { @@ -77,8 +93,7 @@ public String getSystemLocale() { return systemLocale; } - public void setSystemLocale(String systemLocale) - throws IOException { + public void setSystemLocale(String systemLocale) { systemLocale = Util.fixEmptyAndTrim(systemLocale); Locale.setDefault(systemLocale == null ? originalLocale : parse(systemLocale)); this.systemLocale = systemLocale; diff --git a/src/main/resources/hudson/plugins/locale/PluginImpl/config.jelly b/src/main/resources/hudson/plugins/locale/PluginImpl/config.jelly index 736a086..cd290e1 100644 --- a/src/main/resources/hudson/plugins/locale/PluginImpl/config.jelly +++ b/src/main/resources/hudson/plugins/locale/PluginImpl/config.jelly @@ -1,11 +1,11 @@ - - + + - + diff --git a/src/test/java/hudson/plugins/locale/ConfigurationAsCodeTest.java b/src/test/java/hudson/plugins/locale/ConfigurationAsCodeTest.java new file mode 100644 index 0000000..66c9d4a --- /dev/null +++ b/src/test/java/hudson/plugins/locale/ConfigurationAsCodeTest.java @@ -0,0 +1,22 @@ +package hudson.plugins.locale; + +import jenkins.model.Jenkins; +import io.jenkins.plugins.casc.misc.ConfiguredWithCode; +import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule; +import org.junit.Rule; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class ConfigurationAsCodeTest { + + @Rule public JenkinsConfiguredWithCodeRule r = new JenkinsConfiguredWithCodeRule(); + + @Test + @ConfiguredWithCode("configuration-as-code.yml") + public void should_support_configuration_as_code() throws Exception { + PluginImpl plugin = Jenkins.get().getExtensionList(PluginImpl.class).get(0); + assertEquals("fr", plugin.getSystemLocale()); + assertEquals(true, plugin.isIgnoreAcceptLanguage()); + } +} diff --git a/src/test/java/hudson/plugins/locale/MigrationTest.java b/src/test/java/hudson/plugins/locale/MigrationTest.java index da355ff..cfa9392 100644 --- a/src/test/java/hudson/plugins/locale/MigrationTest.java +++ b/src/test/java/hudson/plugins/locale/MigrationTest.java @@ -20,7 +20,7 @@ public class MigrationTest { @LocalData @Test public void dataMigration_13() { - PluginImpl plugin = (PluginImpl) Jenkins.getActiveInstance().getPlugin("locale"); + PluginImpl plugin = Jenkins.get().getExtensionList(PluginImpl.class).get(0); assertEquals("en-US", plugin.getSystemLocale()); assertEquals(true, plugin.isIgnoreAcceptLanguage()); } diff --git a/src/test/resources/hudson/plugins/locale/configuration-as-code.yml b/src/test/resources/hudson/plugins/locale/configuration-as-code.yml new file mode 100644 index 0000000..e4fef68 --- /dev/null +++ b/src/test/resources/hudson/plugins/locale/configuration-as-code.yml @@ -0,0 +1,4 @@ +unclassified: + locale: + systemLocale: fr + ignoreAcceptLanguage: true