-
-
Notifications
You must be signed in to change notification settings - Fork 118
Add draft ItemConfigurator support for Folder #749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package com.cloudbees.hudson.plugins.folder; | ||
|
|
||
| import hudson.Extension; | ||
| import io.jenkins.plugins.casc.ConfigurationContext; | ||
| import io.jenkins.plugins.casc.ConfiguratorException; | ||
| import io.jenkins.plugins.casc.ItemConfigurator; | ||
| import io.jenkins.plugins.casc.model.CNode; | ||
| import io.jenkins.plugins.casc.model.Mapping; | ||
| import jenkins.model.Jenkins; | ||
| import java.io.IOException; | ||
|
|
||
| @Extension | ||
| public class FolderItemConfigurator implements ItemConfigurator<Folder> { | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return "folder"; | ||
| } | ||
|
|
||
| @Override | ||
| public Class<Folder> getTarget() { | ||
| return Folder.class; | ||
| } | ||
|
|
||
| @Override | ||
| public Folder configure(String name, CNode config, ConfigurationContext context) throws ConfiguratorException { | ||
| try { | ||
| Jenkins jenkins = Jenkins.get(); | ||
| Folder folder = (Folder) jenkins.getItem(name); | ||
|
|
||
| if (folder == null) { | ||
| folder = jenkins.createProject(Folder.class, name); | ||
| } | ||
|
|
||
| Mapping mapping = config.asMapping(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume this doesn't work with credentials as yet? Credentials are the biggest pain point I'm aware of with folders and job dsl
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not yet. At the moment, this configurator only supports creating/updating the folder itself, along with description and displayName. I haven't added support for configuring folder credentials yet. |
||
|
|
||
| if (mapping.containsKey("description")) { | ||
| folder.setDescription(mapping.getScalarValue("description")); | ||
| } | ||
|
|
||
| if (mapping.containsKey("displayName")) { | ||
| folder.setDisplayName(mapping.getScalarValue("displayName")); | ||
| } | ||
|
|
||
| folder.save(); | ||
| return folder; | ||
|
|
||
| } catch (IOException e) { | ||
| throw new ConfiguratorException("Failed to configure folder: " + name, e); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package com.cloudbees.hudson.plugins.folder; | ||
|
|
||
| import io.jenkins.plugins.casc.ConfigurationAsCode; | ||
| import io.jenkins.plugins.casc.misc.ConfiguredWithCode; | ||
| import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule; | ||
| import jenkins.model.Jenkins; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNotNull; | ||
|
|
||
| public class FolderItemConfiguratorTest { | ||
|
|
||
| @Rule | ||
| public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule(); | ||
|
|
||
| @Test | ||
| @ConfiguredWithCode("create-folder.yaml") | ||
| public void shouldCreateNewFolder() { | ||
| Folder folder = (Folder) Jenkins.get().getItem("team-alpha"); | ||
|
|
||
| assertNotNull("Folder should have been created by JCasC", folder); | ||
| assertEquals("Team Alpha", folder.getDisplayName()); | ||
| assertEquals("Description for Team Alpha", folder.getDescription()); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldUpdateExistingFolder() throws Exception { | ||
| Folder existingFolder = j.jenkins.createProject(Folder.class, "team-beta"); | ||
| existingFolder.setDescription("Old Description"); | ||
| existingFolder.setDisplayName("Old Display Name"); | ||
|
|
||
| ConfigurationAsCode.get().configure( | ||
| Objects.requireNonNull(getClass().getResource("update-folder.yaml")).toExternalForm() | ||
| ); | ||
|
|
||
| Folder updatedFolder = (Folder) Jenkins.get().getItem("team-beta"); | ||
|
|
||
| assertNotNull(updatedFolder); | ||
| assertEquals("Team Beta Updated", updatedFolder.getDisplayName()); | ||
| assertEquals("New Description via JCasC", updatedFolder.getDescription()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| items: | ||
| - folder: | ||
| name: "team-alpha" | ||
| displayName: "Team Alpha" | ||
| description: "Description for Team Alpha" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| items: | ||
| - folder: | ||
| name: "team-beta" | ||
| displayName: "Team Beta Updated" | ||
| description: "New Description via JCasC" |
Uh oh!
There was an error while loading. Please reload this page.