def create_job(ref, template, config, ref_config): '''Create a jenkins job. :param ref: hg branch name :param template: the config of the template job to use :param config: global config (parsed yaml) :param ref_config: the effective config for this branch ''' print('\nprocessing branch: %s' % ref) sanitized_ref = sanitize(ref, ref_config['sanitize']) sanitized_ref = sanitized_ref.replace('/', ref_config['namesep']) match = ref_config['re'].match(ref) groups, groupdict = match.groups(), match.groupdict() # Placeholders available to the 'substitute' and 'namefmt' options. fmtdict = { 'repo': sanitize(config['repo'], ref_config['sanitize']), 'branch': sanitized_ref, 'repo-orig': config['repo'], 'branch-orig': ref, } job_name = ref_config['namefmt'].format(*groups, **merge(groupdict, fmtdict)) job = Job(job_name, ref, template, _main.jenkins) fmtdict['job_name'] = job_name print('. job name: %s' % job.name) print('. job exists: %s' % job.exists) try: scm_el = job.xml.xpath('scm[@class="hudson.plugins.mercurial.MercurialSCM"]')[0] except IndexError: msg = 'Template job %s is not configured to use Mercurial as an SCM' raise RuntimeError(msg % template) # :bug: # Set branch. el = scm_el.xpath('//branch') # Newer version of the jenkins hg plugin store the branch in the # 'revision' element. if not el: el = scm_el.xpath('//revision') el[0].text = ref # Set the state of the newly created job. job.set_state(ref_config['enable']) # Since some plugins (such as sidebar links) can't interpolate the # job name, we do it for them. job.substitute(list(ref_config['substitute'].items()), fmtdict, groups, groupdict) job.create(ref_config['overwrite'], config['dryrun']) if config['debug']: debug_refconfig(ref_config) return job_name
def create_job(branch, template, config, branch_config): """Create a jenkins job. :param branch: svn branch name (ex: branches/feature-one) :param template: the config of the template job to use :param config: global config (parsed yaml) :param branch_config: the effective config for this branch """ print("\nprocessing branch: %s" % branch) # Job names with '/' in them are problematic. sanitized_branch = branch.replace("/", branch_config["namesep"]) match = branch_config["re"].match(branch) groups, groupdict = match.groups(), match.groupdict() # Placeholders available to the 'substitute' and 'namefmt' options. fmtdict = { "branch": branch.split("/")[-1], "path": branch.replace("/", branch_config["namesep"]), "repo": sanitize(config["repo"], branch_config["sanitize"]), "path-orig": branch, "repo-orig": config["repo"], } job_name = branch_config["namefmt"].format(*groups, **merge(groupdict, fmtdict)) job = Job(job_name, branch, template, _main.jenkins) fmtdict["job_name"] = job_name print(". job name: %s" % job.name) print(". job exists: %s" % job.exists) try: scm_el = job.xml.xpath('scm[@class="hudson.scm.SubversionSCM"]')[0] except IndexError: msg = "Template job %s is not configured to use SVN as an SCM" raise RuntimeError(msg % template) # :bug: # set branch el = scm_el.xpath("//remote")[0] el.text = path.join(config["repo"], branch) # Set the branch that git plugin will locally checkout to. el = scm_el.xpath("//local")[0] el.text = "." # Set the state of the newly created job. job.set_state(branch_config["enable"]) # Since some plugins (such as sidebar links) can't interpolate the # job name, we do it for them. job.substitute(list(branch_config["substitute"].items()), fmtdict, groups, groupdict) job.create(branch_config["overwrite"], config["dryrun"], tag=branch_config["tag"]) if config["debug"]: debug_refconfig(branch_config) return job_name
def substitute(self, items, fmtdict, groups, groupdict): for el in self.xml.xpath("//text()"): for k, v in items: if k in el: p = el.getparent() ctx = merge(groupdict, fmtdict) nv = p.text.replace(k, v.format(*groups, **ctx)) p.text = nv
def create_job(branch, template, config, branch_config): '''Create a jenkins job. :param branch: svn branch name (ex: branches/feature-one) :param template: the config of the template job to use :param config: global config (parsed yaml) :param branch_config: the effective config for this branch ''' print('\nprocessing branch: %s' % branch) # Job names with '/' in them are problematic. sanitized_branch = branch.replace('/', branch_config['namesep']) match = branch_config['re'].match(branch) groups, groupdict = match.groups(), match.groupdict() # Placeholders available to the 'substitute' and 'namefmt' options. fmtdict = { 'branch': branch.split('/')[-1], 'path': branch.replace('/', branch_config['namesep']), 'path-orig': branch, } job_name = branch_config['namefmt'].format(*groups, **merge(groupdict, fmtdict)) job = Job(job_name, branch, template, _main.jenkins) fmtdict['job_name'] = job_name print('. job name: %s' % job.name) print('. job exists: %s' % job.exists) try: scm_el = job.xml.xpath('scm[@class="hudson.scm.SubversionSCM"]')[0] except IndexError: msg = 'Template job %s is not configured to use SVN as an SCM' raise RuntimeError(msg % template) # :bug: # set branch el = scm_el.xpath('//remote')[0] el.text = path.join(config['repo'], branch) # Set the branch that git plugin will locally checkout to. el = scm_el.xpath('//local')[0] el.text = '.' # Set the state of the newly created job. job.set_state(branch_config['enable']) # Since some plugins (such as sidebar links) can't interpolate the # job name, we do it for them. job.substitute(list(branch_config['substitute'].items()), fmtdict, groups, groupdict) job.create(branch_config['overwrite'], config['dryrun'], tag=branch_config['tag']) if config['debug']: debug_refconfig(branch_config) return job_name
def create_job(ref, template, config, ref_config): '''Create a jenkins job. :param ref: hg branch name :param template: the config of the template job to use :param config: global config (parsed yaml) :param ref_config: the effective config for this branch ''' print('\nprocessing branch: %s' % ref) sanitized_ref = sanitize(ref, ref_config['sanitize']) sanitized_ref = sanitized_ref.replace('/', ref_config['namesep']) match = ref_config['re'].match(ref) groups, groupdict = match.groups(), match.groupdict() # placeholders available to the 'substitute' and 'namefmt' options fmtdict = { 'branch': sanitized_ref, 'branch-orig': ref, } job_name = ref_config['namefmt'].format(*groups, **merge(groupdict, fmtdict)) job = Job(job_name, ref, template, _main.jenkins) fmtdict['job_name'] = job_name print('. job name: %s' % job.name) print('. job exists: %s' % job.exists) try: scm_el = job.xml.xpath( 'scm[@class="hudson.plugins.mercurial.MercurialSCM"]')[0] except IndexError: msg = 'Template job %s is not configured to use Mercurial as an SCM' raise RuntimeError(msg % template) # :bug: # set branch el = scm_el.xpath('//branch')[0] el.text = ref # set the state of the newly created job job.set_state(ref_config['enable']) # since some plugins (such as sidebar links) can't interpolate the job # name, we do it for them job.substitute(list(ref_config['substitute'].items()), fmtdict, groups, groupdict) job.create(ref_config['overwrite'], config['dryrun']) if config['debug']: debug_refconfig(ref_config) return job_name
def create_job(ref, template, config, ref_config): """Create a jenkins job. :param ref: hg branch name :param template: the config of the template job to use :param config: global config (parsed yaml) :param ref_config: the effective config for this branch """ print("\nprocessing branch: %s" % ref) sanitized_ref = sanitize(ref, ref_config["sanitize"]) sanitized_ref = sanitized_ref.replace("/", ref_config["namesep"]) match = ref_config["re"].match(ref) groups, groupdict = match.groups(), match.groupdict() # placeholders available to the 'substitute' and 'namefmt' options fmtdict = {"branch": sanitized_ref, "branch-orig": ref} job_name = ref_config["namefmt"].format(*groups, **merge(groupdict, fmtdict)) job = Job(job_name, ref, template, _main.jenkins) fmtdict["job_name"] = job_name print(". job name: %s" % job.name) print(". job exists: %s" % job.exists) try: scm_el = job.xml.xpath('scm[@class="hudson.plugins.mercurial.MercurialSCM"]')[0] except IndexError: msg = "Template job %s is not configured to use Mercurial as an SCM" raise RuntimeError(msg % template) # :bug: # set branch el = scm_el.xpath("//branch") if not el: el = scm_el.xpath("//revision") el[0].text = ref # set the state of the newly created job job.set_state(ref_config["enable"]) # since some plugins (such as sidebar links) can't interpolate the job # name, we do it for them job.substitute(list(ref_config["substitute"].items()), fmtdict, groups, groupdict) job.create(ref_config["overwrite"], config["dryrun"]) if config["debug"]: debug_refconfig(ref_config) return job_name
def create_job(ref, template, config, ref_config): '''Create a jenkins job. :param ref: git ref name (ex: refs/heads/something) :param template: the config of the template job to use :param config: global config (parsed yaml) :param ref_config: the effective config for this ref :returns: the name of the newly created job ''' print('\nprocessing ref: %s' % ref) shortref = re.sub('^refs/(heads|tags|remotes)/', '', ref) sanitized_ref = sanitize(ref, ref_config['sanitize']) sanitized_shortref = sanitize(shortref, ref_config['sanitize']) # Job names with '/' in them are problematic (todo: consolidate with sanitize()). sanitized_ref = sanitized_ref.replace('/', ref_config['namesep']) sanitized_shortref = sanitized_shortref.replace('/', ref_config['namesep']) match = ref_config['re'].match(ref) groups, groupdict = match.groups(), match.groupdict() # Placeholders available to the 'substitute' and 'namefmt' options. fmtdict = { 'ref': sanitized_ref, 'shortref': sanitized_shortref, 'repo': sanitize(config['repo'], ref_config['sanitize']), 'ref-orig': ref, 'repo-orig': config['repo'], 'shortref-orig': shortref, } job_name = ref_config['namefmt'].format(*groups, **merge(groupdict, fmtdict)) job = Job(job_name, ref, template, _main.jenkins) fmtdict['job_name'] = job_name print('. job name: %s' % job.name) print('. job exists: %s' % job.exists) try: scm_el = job.xml.xpath('scm[@class="hudson.plugins.git.GitSCM"]')[0] except IndexError: msg = 'Template job %s is not configured to use Git as an SCM' raise RuntimeError(msg % template) # :bug: # Get remote name. remote = scm_el.xpath('//hudson.plugins.git.UserRemoteConfig/name') remote = remote[0].text if remote else 'origin' # Set branch. el = scm_el.xpath('//hudson.plugins.git.BranchSpec/name')[0] # :todo: jenkins is being very capricious about the branch-spec # el.text = '%s/%s' % (remote, shortref) # :todo: el.text = shortref # Set the branch that the git plugin will locally checkout to. el = scm_el.xpath('//localBranch') el = etree.SubElement(scm_el, 'localBranch') if not el else el[0] el.text = shortref # the original shortref (with '/') # Set the state of the newly created job. job.set_state(ref_config['enable']) # Since some plugins (such as sidebar links) can't interpolate the # job name, we do it for them. job.substitute(list(ref_config['substitute'].items()), fmtdict, groups, groupdict) job.create(ref_config['overwrite'], config['dryrun'], tag=ref_config['tag']) if config['debug']: debug_refconfig(ref_config) return job_name
def create_job(ref, template, config, ref_config): '''Create a jenkins job. :param ref: git ref name (ex: refs/heads/something) :param template: the config of the template job to use :param config: global config (parsed yaml) :param ref_config: the effective config for this ref :returns: the name of the newly created job ''' print('\nprocessing ref: %s' % ref) shortref = re.sub('^refs/(heads|tags|remotes)/', '', ref) sanitized_ref = sanitize(ref, ref_config['sanitize']) sanitized_shortref = sanitize(shortref, ref_config['sanitize']) # job names with '/' in them are problematic (todo: consolidate with sanitize()) sanitized_ref = sanitized_ref.replace('/', ref_config['namesep']) sanitized_shortref = sanitized_shortref.replace('/', ref_config['namesep']) match = ref_config['re'].match(ref) groups, groupdict = match.groups(), match.groupdict() # placeholders available to the 'substitute' and 'namefmt' options fmtdict = { 'ref': sanitized_ref, 'shortref': sanitized_shortref, 'ref-orig': ref, 'shortref-orig': shortref, } job_name = ref_config['namefmt'].format(*groups, **merge(groupdict, fmtdict)) job = Job(job_name, ref, template, _main.jenkins) fmtdict['job_name'] = job_name print('. job name: %s' % job.name) print('. job exists: %s' % job.exists) try: scm_el = job.xml.xpath('scm[@class="hudson.plugins.git.GitSCM"]')[0] # get remote name remote = scm_el.xpath('//hudson.plugins.git.UserRemoteConfig/name') remote = remote[0].text if remote else 'origin' # set branch el = scm_el.xpath('//hudson.plugins.git.BranchSpec/name')[0] # :todo: jenkins is being very caprecious about the branchspec # el.text = '%s/%s' % (remote, shortref) # :todo: el.text = shortref # set the branch that git plugin will locally checkout to el = scm_el.xpath('//localBranch') el = etree.SubElement(scm_el, 'localBranch') if not el else el[0] el.text = shortref # the original shortref (with '/') except IndexError: msg = 'Template job %s is not configured to use Git as an SCM' # raise RuntimeError(msg % template) # :bug: # set the state of the newly created job job.set_state(ref_config['enable']) # since some plugins (such as sidebar links) can't interpolate the job # name, we do it for them job.substitute(list(ref_config['substitute'].items()), fmtdict, groups, groupdict) job.create(ref_config['overwrite'], config['dryrun'], tag=ref_config['tag']) if config['debug']: debug_refconfig(ref_config) return job_name
def create_job(branch, template, config, branch_config): '''Create a jenkins job. :param branch: svn branch name (ex: branches/feature-one) :param template: the config of the template job to use :param config: global config (parsed yaml) :param branch_config: the effective config for this branch ''' print('\nprocessing branch: %s' % branch) # job names with '/' in them are problematic sanitized_branch = branch.replace('/', branch_config['namesep']) match = branch_config['re'].match(branch) groups, groupdict = match.groups(), match.groupdict() # placeholders available to the 'substitute' and 'namefmt' options fmtdict = { 'branch': branch.split('/')[-1], 'path': branch.replace('/', branch_config['namesep']), 'path-orig': branch, } job_name = branch_config['namefmt'].format(*groups, **merge(groupdict, fmtdict)) job = Job(job_name, branch, template, _main.jenkins) fmtdict['job_name'] = job_name print('. job name: %s' % job.name) print('. job exists: %s' % job.exists) try: scm_el = job.xml.xpath('scm[@class="hudson.scm.SubversionSCM"]')[0] except IndexError: msg = 'Template job %s is not configured to use SVN as an SCM' raise RuntimeError(msg % template) # :bug: # set branch el = scm_el.xpath('//remote')[0] el.text = path.join(config['repo'], branch) # set the branch that git plugin will locally checkout to el = scm_el.xpath('//local')[0] el.text = '.' # set the state of the newly created job job.set_state(branch_config['enable']) # since some plugins (such as sidebar links) can't interpolate the job # name, we do it for them job.substitute(list(branch_config['substitute'].items()), fmtdict, groups, groupdict) job.create(branch_config['overwrite'], config['dryrun'], tag=branch_config['tag']) if config['debug']: debug_refconfig(branch_config) return job_name