Skip to content
This repository has been archived by the owner on Jun 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #78 from tinyspeck/BUILD_3185_add_cleanup_pattern
Browse files Browse the repository at this point in the history
BUILD-3185-preBuildCleanup include/exclude pattern
  • Loading branch information
pamgluss-slack authored Jan 28, 2023
2 parents 45b1469 + 1694737 commit 00af21b
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public AbstractDSLStrategy(int tabs, IDescriptor descriptor, boolean shouldInitC
propertiesToBeSkipped.add("caseSensitive");
propertiesToBeSkipped.add("followSymlinks");
propertiesToBeSkipped.add("completeBuild");
propertiesToBeSkipped.add("externalDelete");
propertiesToBeSkipped.add("skipWhenFailed");
propertiesToBeSkipped.add("notFailBuild");
propertiesToBeSkipped.add("unstableReturn");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package com.adq.jenkins.xmljobtodsl.dsl.strategies.custom;

import com.adq.jenkins.xmljobtodsl.dsl.strategies.DSLObjectStrategy;
import com.adq.jenkins.xmljobtodsl.parsers.PropertyDescriptor;
import java.util.ArrayList;
import java.util.List;

/*
Strategy for preBuildCleanup attribute, to correctly render includePattern and excludePattern, which is in the XML as:
<hudson.plugins.ws__cleanup.PreBuildCleanup plugin="[email protected]">
<patterns>
<hudson.plugins.ws__cleanup.Pattern>
<pattern>results/*</pattern>
<type>INCLUDE</type>
</hudson.plugins.ws__cleanup.Pattern>
</patterns>
<deleteDirs>false</deleteDirs>
<cleanupParameter>test</cleanupParameter>
</hudson.plugins.ws__cleanup.PreBuildCleanup>
The value within type determines the method name: either "includePattern" or "excludePattern"
"pattern" is then inserted as the value for that method
Returns the dsl as:
preBuildCleanup {
includePattern("test")
deleteDirectories(false)
cleanupParameter("test")
}
*/

public class DSLCleanupPatternStrategy extends DSLObjectStrategy {

PropertyDescriptor typeChild = null;
PropertyDescriptor patternChild = null;


public DSLCleanupPatternStrategy(int tabs, PropertyDescriptor propertyDescriptor, String name) {
this(tabs, propertyDescriptor, name, true);

List<PropertyDescriptor> children = propertyDescriptor.getProperties();
List<PropertyDescriptor> leftoverProps = new ArrayList<>();
List<PropertyDescriptor> patternProps = new ArrayList<>();

separateProperties(children, leftoverProps, patternProps);
extractProperties(propertyDescriptor.getProperties());
processPatternProps(patternProps, leftoverProps, propertyDescriptor);
}

public DSLCleanupPatternStrategy(int tabs, PropertyDescriptor propertyDescriptor, String name, boolean shouldInitChildren) {
super(tabs, propertyDescriptor, name, shouldInitChildren);

List<PropertyDescriptor> children = propertyDescriptor.getProperties();
List<PropertyDescriptor> leftoverProps = new ArrayList<>();
List<PropertyDescriptor> patternProps = new ArrayList<>();

separateProperties(children, leftoverProps, patternProps);
extractProperties(patternProps);
processPatternProps(patternProps, leftoverProps, propertyDescriptor);
}

private void separateProperties(List<PropertyDescriptor> children, List<PropertyDescriptor> leftoverProps, List<PropertyDescriptor> patternProps) {
for (PropertyDescriptor child : children) {
if (!child.getName().equals("patterns")) {
leftoverProps.add(child);
}
if (child.getName().equals("patterns")) {
patternProps.add(child);
}
}
}

private void extractProperties(List<PropertyDescriptor> patternProps) {
for (PropertyDescriptor prop : patternProps) {
if (prop.getName().equals("patterns")) {
for (PropertyDescriptor innerProp : prop.getProperties()) {
for (PropertyDescriptor nestedProp : innerProp.getProperties()) {
if (nestedProp.getName().equals("pattern")) {
patternChild = nestedProp;
System.out.println(patternChild);
}
if (nestedProp.getName().equals("type")) {
typeChild = nestedProp;
System.out.println(typeChild);
}
}
}
}
}
}

private void processPatternProps(List<PropertyDescriptor> patternProps, List<PropertyDescriptor> leftoverProps, PropertyDescriptor propertyDescriptor) {
if(!patternProps.isEmpty()) {
if (typeChild.getValue() != null && patternChild.getValue() != null) {
if (typeChild.getValue().equals("INCLUDE")) {
PropertyDescriptor newChild = new PropertyDescriptor("includePattern",
propertyDescriptor, patternChild.getValue());

leftoverProps.add(0, newChild);
} else if (typeChild.getValue().equals("EXCLUDE")) {
PropertyDescriptor newChild = new PropertyDescriptor("excludePattern",
propertyDescriptor,
patternChild.getValue());

leftoverProps.add(0, newChild);
}
}
}
propertyDescriptor.replaceProperties(leftoverProps);
initChildren(propertyDescriptor);
}

}

25 changes: 21 additions & 4 deletions src/main/resources/translator.properties
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,9 @@ hudson.plugins.ws__cleanup.WsCleanup.type = OBJECT

publishers.hudson.plugins.ws__cleanup.WsCleanup.deleteDirs = deleteDirs

hudson.plugins.ws__cleanup.WsCleanup.externalDelete = externalDelete
hudson.plugins.ws__cleanup.WsCleanup.externalDelete.type = com.adq.jenkins.xmljobtodsl.dsl.strategies.custom.DSLDoNotDisplayEmptyMethodStrategy

cleanWhenAborted = cleanWhenAborted
cleanWhenFailure = cleanWhenFailure
cleanWhenNotBuilt = cleanWhenNotBuilt
Expand All @@ -816,13 +819,14 @@ hudson.plugins.ws__cleanup.WsCleanup.patterns.type = OBJECT
hudson.plugins.ws__cleanup.WsCleanup.patterns.hudson.plugins.ws__cleanup.Pattern = pattern
hudson.plugins.ws__cleanup.WsCleanup.patterns.hudson.plugins.ws__cleanup.Pattern.type = OBJECT

hudson.plugins.ws__cleanup.WsCleanup.patterns.hudson.plugins.ws__cleanup.Pattern.pattern = pattern

hudson.plugins.ws__cleanup.WsCleanup.patterns.hudson.plugins.ws__cleanup.Pattern.type_child = type

# If you have a child attribute called "type" refer to it as "type_child" in this file instead
# See AbstractDSLStrategy.java for where that is handled.
type = type
type_child = type
hudson.plugins.ws__cleanup.Pattern.pattern = pattern
hudson.plugins.ws__cleanup.Pattern.type_child = type


hudson.plugins.heavy__job.HeavyJobProperty = heavyJobProperty
hudson.plugins.heavy__job.HeavyJobProperty.type = OBJECT
Expand Down Expand Up @@ -851,8 +855,21 @@ timeoutMinutes = absolute
hudson.plugins.timestamper.TimestamperBuildWrapper = timestamps

hudson.plugins.ws__cleanup.PreBuildCleanup = preBuildCleanup
hudson.plugins.ws__cleanup.PreBuildCleanup.type = OBJECT
hudson.plugins.ws__cleanup.PreBuildCleanup.type = com.adq.jenkins.xmljobtodsl.dsl.strategies.custom.DSLCleanupPatternStrategy


hudson.plugins.ws__cleanup.PreBuildCleanup.patterns = INNER
hudson.plugins.ws__cleanup.PreBuildCleanup.patterns.type = INNER
hudson.plugins.ws__cleanup.PreBuildCleanup.patterns.hudson.plugins.ws__cleanup.Pattern = pattern
hudson.plugins.ws__cleanup.PreBuildCleanup.patterns.hudson.plugins.ws__cleanup.Pattern.type = INNER
hudson.plugins.ws__cleanup.PreBuildCleanup.patterns.hudson.plugins.ws__cleanup.Pattern.pattern = pattern


hudson.plugins.ws__cleanup.PreBuildCleanup.externalDelete = deleteCommand
hudson.plugins.ws__cleanup.PreBuildCleanup.externalDelete.type = com.adq.jenkins.xmljobtodsl.dsl.strategies.custom.DSLDoNotDisplayEmptyMethodStrategy

cleanupParameter = cleanupParameter
cleanupParameter.type = com.adq.jenkins.xmljobtodsl.dsl.strategies.custom.DSLDoNotDisplayEmptyMethodStrategy

com.cloudbees.jenkins.plugins.sshagent.SSHAgentBuildWrapper = sshAgent

Expand Down
Binary file modified xml-job-to-job-dsl.hpi
Binary file not shown.

0 comments on commit 00af21b

Please sign in to comment.