Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
<dependency>
<groupId>io.jenkins</groupId>
<artifactId>configuration-as-code</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jenkins.configuration-as-code</groupId>
Expand Down
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);
Comment thread
somiljain2006 marked this conversation as resolved.
}

Mapping mapping = config.asMapping();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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"