Commit ba13bae8 authored by Eloy Lafuente's avatar Eloy Lafuente
Browse files

Remove CloneMasterViewTask from maintenance scripts

This has been superseed by CloneJobsFromViewTask that allows
cloning from master (and from any other view) and also supports
more jobs, like the workplace ones.
parent 86d71253
package org.moodle.ci.maintenance;
import org.jenkinsci.plugins.workflow.job.WorkflowJob
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition
import hudson.model.ParametersDefinitionProperty
/**
* Task to easily clone all the jobs in the master (B - master)
*
* Clone all the jobs from the master view/branch to another view/branch,
* performing all the required modifications.
* Requires:
* - Both source and target views to exist and be configured below.
* - New runner, versions... of scripts to be already available.
*/
class CloneMasterViewTask {
/**
* Source view name, locked to master one, given the nature of
* the replacements.
*/
static final String sourceViewName = 'B - master'
/**
* Configurable name of the target view. Must exist.
*/
protected String targetViewName = 'B - 35'
/**
* Prefix to use in the name of the new jobs (should match view name number).
*/
protected String targetPrefix = '35'
/**
* Filter (substring matching) to restrict the clone operation.
*/
protected filter = 'S.99.99'
/**
* Run the configured clone operation.
*/
int run(Script out) {
// Verify views exist.
def sourceView = Jenkins.instance.getView(sourceViewName)
if (sourceView == null) {
out.println 'Configuration error: sourceViewName view does not exist'
return 1
}
def targetView = Jenkins.instance.getView(targetViewName)
if (targetView == null) {
out.println 'Configuration error: targetViewName view does not exist'
return 1
}
// Iterate over all the projects in the source view.
for (item in sourceView.getItems()) {
def name = item.getName()
out.println 'Processing "' + name + '"'
// Apply filter if existing.
if (filter != '' && !name.contains(filter)) {
out.println ' Skipped, does not match "' + filter + "'"
continue
}
// Calculate new name and verify the job doesn't exist.
def match = (name =~ /^([A-Z]+)(\..*)$/)
def namePrefix = ''
def nameSuffix = ''
if (match.find()) {
namePrefix = match.group(1)
nameSuffix = match.group(2)
} else {
out.println ' Skipped, unable to split job name into prefix and suffix'
continue
}
def newName = namePrefix + targetPrefix + nameSuffix
if (Jenkins.instance.getItem(newName)) {
out.println ' Skipped, already exits "' + newName + '"'
continue
}
out.println ' Creating new job "' + newName + "'"
// Copy, disable and save.
def job = Jenkins.instance.copy(item, newName)
job.disabled = true
out.println ' Job created'
// Apply pipeline replacements
if (job.class.simpleName == 'WorkflowJob') {
def script = job.getDefinition().getScript()
// Master() by prefix()
def search = 'Master()'
def replacement = 'M' + targetPrefix + '()'
script = script.replace(search, replacement)
job.setDefinition(new CpsFlowDefinition(script, true))
out.println ' Pipeline script adapted: "' + search + '" => "' + replacement + '"'
}
// Apply paramenter replacements
job.getProperties().values().each { property ->
if (property instanceof ParametersDefinitionProperty) {
property.getParameterDefinitionNames().each { paramName ->
// Any parameter which name contains "branch" and with default value = "master"
// is replaced by new MOODLE_XX_STABLE default value
if (paramName.contains("branch")) {
def paramDefaultValue = property.getParameterDefinition(paramName).getDefaultParameterValue().getValue()
if (paramDefaultValue == 'master') {
def newDefaultValue = 'MOODLE_' + targetPrefix + '_STABLE'
property.getParameterDefinition(paramName).setDefaultValue(newDefaultValue)
out.println ' Parameter "' + paramName + '" default changed: "master" => "' + newDefaultValue + '"'
}
}
}
}
}
// Save the job
job.save()
}
return 0
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment