Skip to content

Commit 9d883a5

Browse files
Copy groups or restrictions between folders (#29)
1 parent d97019e commit 9d883a5

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

copyGrpsBetweenFolders.groovy

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import com.cloudbees.hudson.plugins.folder.*
2+
import com.cloudbees.jenkins.plugins.foldersplus.*
3+
import com.cloudbees.hudson.plugins.folder.properties.FolderProxyGroupContainer
4+
5+
String name1 = 'test'
6+
String name2 = 'test2'
7+
8+
9+
AbstractFolder < ? > folderAbs1 = AbstractFolder.class.cast(Jenkins.instance.getAllItems(Folder.class).find{it.name.equals(name1)})
10+
AbstractFolder < ? > folderAbs2 = AbstractFolder.class.cast(Jenkins.instance.getAllItems(Folder.class).find{it.name.equals(name2)})
11+
12+
if(folderAbs1 == null || folderAbs2 == null){
13+
println 'folders not found'
14+
} else {
15+
/*
16+
// CloudBess Folder > 5.2.2
17+
SecurityGrantsFolderProperty property1 = SecurityGrantsFolderProperty.of(folderAbs1)
18+
SecurityGrantsFolderProperty property2 = SecurityGrantsFolderProperty.of(folderAbs2,true)
19+
20+
println "property2 : " + property2
21+
println "property1 : " + property1
22+
if (property1 != null) {
23+
property1.getSecurityGrants().each {
24+
println "SecurityGrant : " + it
25+
property2.addSecurityGrant(it)
26+
}
27+
}
28+
29+
*/
30+
// CloudBees Folder <=5.2.2
31+
FolderProxyGroupContainer propertyFPG1 = folderAbs1.getProperties().get(FolderProxyGroupContainer.class);
32+
FolderProxyGroupContainer propertyFPG2 = folderAbs2.getProperties().get(FolderProxyGroupContainer.class);
33+
34+
if (propertyFPG2 == null) {
35+
FolderProxyGroupContainer c = new FolderProxyGroupContainer()
36+
folderAbs2.getProperties().replace(c)
37+
c.owner=folderAbs2
38+
propertyFPG2 = folderAbs2.getProperties().get(FolderProxyGroupContainer.class)
39+
}
40+
41+
println "propertyFPG2 : " + propertyFPG2
42+
println "propertyFPG1 : " + propertyFPG1
43+
if (propertyFPG1 != null) {
44+
propertyFPG1.getGroups().findAll{it != null}.each {
45+
println "Group : " + it
46+
propertyFPG2.addGroup(it)
47+
}
48+
propertyFPG1.getRoleFilters().findAll{it != null}.each {
49+
println "RoleFilter : " + it
50+
propertyFPG2.addRoleFilter(it)
51+
}
52+
propertyFPG2.save()
53+
}
54+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import com.cloudbees.hudson.plugins.folder.*
2+
import com.cloudbees.jenkins.plugins.foldersplus.*
3+
import com.cloudbees.hudson.plugins.folder.properties.FolderProxyGroupContainer
4+
import com.cloudbees.hudson.plugins.folder.properties.SubItemFilterProperty
5+
6+
String source = 'sub folder'
7+
String destination = 'z'
8+
9+
10+
AbstractFolder < ? > folderAbs1 = AbstractFolder.class.cast(Jenkins.instance.getAllItems(Folder.class).find{it.name.equals(source)})
11+
AbstractFolder < ? > folderAbs2 = AbstractFolder.class.cast(Jenkins.instance.getAllItems(Folder.class).find{it.name.equals(destination)})
12+
13+
if(folderAbs1 == null || folderAbs2 == null){
14+
println 'folders not found'
15+
} else {
16+
17+
println folderAbs2
18+
SubItemFilterProperty propertyFPG1 = folderAbs1.getProperties().get(SubItemFilterProperty.class);
19+
folderAbs2.getItems().findAll{it instanceof Folder}.each { folderChild ->
20+
SubItemFilterProperty propertyFPG2 = folderChild.getProperties().get(SubItemFilterProperty.class);
21+
22+
if (propertyFPG2 == null || propertyFPG2.allowedTypes == null) {
23+
SubItemFilterProperty c = new SubItemFilterProperty(new ArrayList<String>())
24+
folderChild.getProperties().replace(c)
25+
c.owner=folderChild
26+
propertyFPG2 = folderChild.getProperties().get(SubItemFilterProperty.class)
27+
}
28+
29+
println "propertyFPG2 : " + propertyFPG2
30+
println "propertyFPG1 : " + propertyFPG1
31+
if (propertyFPG1 != null && propertyFPG1.allowedTypes != null) {
32+
propertyFPG2.allowedTypes.addAll(propertyFPG1.allowedTypes)
33+
}
34+
}
35+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import com.cloudbees.hudson.plugins.folder.*
2+
import com.cloudbees.jenkins.plugins.foldersplus.*
3+
import com.cloudbees.hudson.plugins.folder.properties.FolderProxyGroupContainer
4+
import com.cloudbees.hudson.plugins.folder.properties.SubItemFilterProperty
5+
6+
String source = 'folderOrg'
7+
String destination = 'folderDest'
8+
9+
10+
AbstractFolder < ? > folderAbs1 = AbstractFolder.class.cast(Jenkins.instance.getAllItems(Folder.class).find{it.name.equals(source)})
11+
AbstractFolder < ? > folderAbs2 = AbstractFolder.class.cast(Jenkins.instance.getAllItems(Folder.class).find{it.name.equals(destination)})
12+
13+
if(folderAbs1 == null || folderAbs2 == null){
14+
println 'folders not found'
15+
} else {
16+
17+
println folderAbs2
18+
SubItemFilterProperty propertyFPG1 = folderAbs1.getProperties().get(SubItemFilterProperty.class);
19+
SubItemFilterProperty propertyFPG2 = folderAbs2.getProperties().get(SubItemFilterProperty.class);
20+
21+
if (propertyFPG2 == null || propertyFPG2.allowedTypes == null) {
22+
SubItemFilterProperty c = new SubItemFilterProperty(new ArrayList<String>())
23+
folderAbs2.getProperties().replace(c)
24+
c.owner=folderAbs2
25+
propertyFPG2 = folderAbs2.getProperties().get(SubItemFilterProperty.class)
26+
}
27+
28+
println "propertyFPG2 : " + propertyFPG2
29+
println "propertyFPG1 : " + propertyFPG1
30+
if (propertyFPG1 != null && propertyFPG1.allowedTypes != null) {
31+
propertyFPG2.allowedTypes.addAll(propertyFPG1.allowedTypes)
32+
}
33+
}

0 commit comments

Comments
 (0)