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 #9 from tinyspeck/BUILD-2840-preliminary-testing
Browse files Browse the repository at this point in the history
[BUILD-2840] Support Promoted Builds Jobs
  • Loading branch information
pamgluss-slack authored Oct 26, 2022
2 parents 4ef2b93 + 44ade00 commit afcd611
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,40 @@ public static String getJobNameBasedOnPath(File file) {
return segments[segments.length - 2];
}

public static String getJobPromotedBuildsXMLs(File file)
throws IOException {

IOUtils ioUtils = new IOUtils();

File directoryPath = file.getAbsoluteFile().getParentFile();

String returnXML = "";

if(new File(directoryPath, "promotions").exists()) {
File promotionsPath = new File(directoryPath.getAbsolutePath() + "/promotions");
for (File promotionStepDir : promotionsPath.listFiles()) {
if (new File(promotionStepDir, "config.xml").exists()) {
String promotionConfigPath = promotionStepDir.getAbsolutePath() + "/config.xml";
// Get rid of the <?xml version='1.1' encoding='UTF-8'?> heading otherwise wont parse
String promotionStepXML = ioUtils.readFromFile(promotionConfigPath);
int endOfXMLHeadingIndex = promotionStepXML.indexOf("\n");

String versionHeadingRemovedXML = promotionStepXML.substring(endOfXMLHeadingIndex).trim();

// Insert name of job in between first line in promoted job and the rest of the file
int endOfNewSubstringXML = versionHeadingRemovedXML.indexOf("\n");
String firstHalfString = versionHeadingRemovedXML.substring(0, endOfNewSubstringXML).trim();
String secondHalfString = versionHeadingRemovedXML.substring(endOfNewSubstringXML).trim();
String buildStepName = String.format("<promotedBuildStepName>%s</promotedBuildStepName>\n", getJobNameBasedOnPath(new File(promotionConfigPath)));

returnXML += firstHalfString + buildStepName + secondHalfString;
}
}
}
String completeXML = "<root>\n"+ returnXML + "\n</root>";
return completeXML;
}

private JobDescriptor[] getJobDescriptors(File[] files)
throws IOException, ParserConfigurationException, SAXException {
IOUtils ioUtils = new IOUtils();
Expand All @@ -140,8 +174,17 @@ private JobDescriptor[] getJobDescriptors(File[] files)

for (File file : files) {
String jobName = getJobNameBasedOnPath(file);

String xml = ioUtils.readFromFile(file);
JobDescriptor descriptor = new XmlParser(jobName, xml).parse();

String jobPromotedBuildsXML = getJobPromotedBuildsXMLs(file);

JobDescriptor descriptor;
if(jobPromotedBuildsXML.isEmpty()) {
descriptor = new XmlParser(jobName, xml).parse();
} else {
descriptor = new XmlParser(jobName, xml, jobPromotedBuildsXML).parse();
}
descriptors.add(descriptor);
}
return descriptors.toArray(new JobDescriptor[descriptors.size()]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public AbstractDSLStrategy(int tabs, IDescriptor descriptor, boolean shouldInitC
propertiesToBeSkipped.add("hudson.plugins.git.extensions.impl.PathRestriction");
propertiesToBeSkipped.add("unstableReturn");
propertiesToBeSkipped.add("ignoreMissing");
propertiesToBeSkipped.add("activeProcessNames");

try {
initProperties();
Expand Down
68 changes: 67 additions & 1 deletion src/main/java/com/adq/jenkins/xmljobtodsl/parsers/XmlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,34 @@ public class XmlParser {

private String xml;
private String jobName;
private String promotedBuildsXml;

public XmlParser(String jobName, String xml) {
this.jobName = jobName;
this.xml = xml;
prepareXml();
}

public XmlParser(String jobName, String xml, String promotedBuildsXml) {
this.jobName = jobName;
this.xml = xml;
this.promotedBuildsXml = promotedBuildsXml;
prepareXml();
preparePromotedBuilds();
}

private String prepareXml() {
return this.xml = this.xml.replaceAll(">%n", "")
.replaceAll("\\s*<", "<")
.replaceAll("&#xd;", String.format("%n"));
}

private String preparePromotedBuilds(){
return this.promotedBuildsXml = this.promotedBuildsXml.replaceAll(">%n", "")
.replaceAll("\\s*<", "<")
.replaceAll("&#xd;", String.format("%n"));
}

public JobDescriptor parse() throws IOException, SAXException, ParserConfigurationException {
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
Expand All @@ -39,7 +54,14 @@ public JobDescriptor parse() throws IOException, SAXException, ParserConfigurati

List<PropertyDescriptor> properties = new ArrayList<>();

properties.addAll(getChildNodes(null, doc.getChildNodes()));
if(promotedBuildsXml != null) {
InputSource promotedBuildsInputSource = new InputSource(new StringReader(promotedBuildsXml));
Document promotedBuildsDoc = docBuilder.parse(promotedBuildsInputSource);
promotedBuildsDoc.getDocumentElement().normalize();
properties.addAll(getChildNodes(null, doc.getChildNodes(), promotedBuildsDoc.getChildNodes()));
} else {
properties.addAll(getChildNodes(null, doc.getChildNodes()));
}

return new JobDescriptor(jobName, properties);
}
Expand Down Expand Up @@ -84,6 +106,50 @@ public List<PropertyDescriptor> getChildNodes(PropertyDescriptor parent, NodeLis
return properties;
}

public List<PropertyDescriptor> getChildNodes(PropertyDescriptor parent, NodeList childNodes, NodeList promotedBuildsChildNodes) {
List<PropertyDescriptor> properties = new ArrayList<>();
for (int i = 0; i < childNodes.getLength(); i++) {
Node node = childNodes.item(i);
if (node.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
String name = node.getNodeName();

Map<String, String> attributes = null;
if (node.hasAttributes()) {
attributes = getAttributes(node.getAttributes());
}

if (!node.hasChildNodes()) {
PropertyDescriptor descriptor = new PropertyDescriptor(name, parent, attributes);
properties.add(descriptor);
continue;
}

Node firstChild = node.getFirstChild();

if (firstChild.getNodeType() == Node.TEXT_NODE && !((Text) firstChild).isElementContentWhitespace()) {
String value = node.getFirstChild().getNodeValue();
value = value.replaceAll("\n", String.format("%n"));
PropertyDescriptor descriptor = new PropertyDescriptor(name, parent, value, attributes);
properties.add(descriptor);
continue;
}

List<PropertyDescriptor> childProperties = new ArrayList<>();
PropertyDescriptor descriptor = new PropertyDescriptor(name, parent, childProperties, attributes);
if(name.equals("hudson.plugins.promoted__builds.JobPropertyImpl")){
childProperties.addAll(getChildNodes(descriptor, promotedBuildsChildNodes));
}

if (firstChild.getNodeType() == Node.ELEMENT_NODE) {
childProperties.addAll(getChildNodes(descriptor, node.getChildNodes(), promotedBuildsChildNodes));
}
properties.add(descriptor);
}
return properties;
}

public Map<String, String> getAttributes(NamedNodeMap attributes) {
Map<String, String> attributesMap = new HashMap<>();
for (int i = 0; i < attributes.getLength(); i++) {
Expand Down
25 changes: 19 additions & 6 deletions src/main/resources/translator.properties
Original file line number Diff line number Diff line change
Expand Up @@ -546,13 +546,13 @@ hudson.plugins.ws__cleanup.WsCleanup.patterns.pattern.pattern = pattern
hudson.plugins.ws__cleanup.WsCleanup.patterns.pattern.pattern.type = METHOD
type = type

hudson.tasks.junit.JUnitResultArchiver = jUnitResultArchiver
hudson.tasks.junit.JUnitResultArchiver.type = OBJECT

hudson.plugins.heavy__job.HeavyJobProperty = heavyJobProperty
hudson.plugins.heavy__job.HeavyJobProperty.type = OBJECT
weight = weight

hudson.tasks.junit.JUnitResultArchiver = archiveJunit
hudson.tasks.junit.JUnitResultArchiver.type = CLOSURE

testResults = testResults

healthScaleFactor = healthScaleFactor
Expand Down Expand Up @@ -687,9 +687,22 @@ info = INNER
propertiesContent = env
propertiesContent.type = com.adq.jenkins.xmljobtodsl.dsl.strategies.custom.DSLPropertyAsMethodParametersStrategy

#hudson.plugins.promoted__builds.JobPropertyImpl = it / 'properties' / 'hudson.plugins.promoted.builds.JobPropertyImpl'
#hudson.plugins.promoted__builds.JobPropertyImpl.type = CONFIGURE
#activeProcessNames = add
# Promoted Builds requires injected XML to combine disparate XML files
root = root
root.type = INNER

hudson.plugins.promoted__builds.JobPropertyImpl = promotions
hudson.plugins.promoted__builds.JobPropertyImpl.type = OBJECT

promotedBuildStepName = name

hudson.plugins.promoted__builds.PromotionProcess = promotion
hudson.plugins.promoted__builds.PromotionProcess.type = OBJECT

icon = icon

conditions = conditions
conditions.type = OBJECT

com.sonyericsson.rebuild.RebuildSettings = it / 'properties' / 'com.sonyericsson.rebuild.RebuildSettings'
com.sonyericsson.rebuild.RebuildSettings.type = CONFIGURE
Expand Down
Binary file modified xml-job-to-job-dsl.hpi
Binary file not shown.

0 comments on commit afcd611

Please sign in to comment.