@@ -0,0 +1,2675 @@
+#!/usr/bin/env python%0A%0A%22%22%22%0ACreate Jenkins job corresponding to each Github repository.%0A%22%22%22%0A%0Aimport os%0Aimport getpass%0Aimport github3%0Aimport jenkinsapi%0A%0A# Github user(s) which repositories are to be created in Jenkins%0AGITHUB_USERS = %5B%22taverna%22%5D%0A%0A# Branches which existance means a corresponding Jenkins job is%0A# created. The job will be called $repository-$branch, except for %0A# the master branch, which is simply $repository%0ABRANCHES = %5B%22master%22, %22maintenance%22%5D%0A%0A# Jenkins instance where jobs will be created, e.g.%0A# http://localhost:8080/jenkins/%0AJENKINS = %22http://build.mygrid.org.uk/ci/%22%0A%0A# Pre-existing Jenkins job which config is%0A# to be used as a template for any new jobs%0A# %0A# Note: The template must be both a valid %0A# Jenkins name and a Github repository%0A# as naive search-replace is used on the%0A# Jenkins Job Config XML%0A# The string %22master%22 will be search-replaced for%0A# other branches%0AJENKINS_JOB_TEMPLATE = %22taverna-wsdl-activity%22%0A# The pre-configured user/repo substring of the github URLs in the%0A# Jenkins job-template - this will be search-replaced to%0A# $user/$repo%0AJENKINS_JOB_TEMPLATE_REPO = %22taverna/taverna-wsdl-activity%22%0A%0A# Jenkins user with write-access in Jenkins%0A# The library will prompt on the console at runtime for%0A# the jenkins password.%0A#%0A# Set the user to None for readonly mode, in  which case%0A# new Jenkins jobs will not be created, but their name%0A# printed on the console.%0A# %0AJENKINS_USER = os.environ.get(%22JENKINS_USER%22) or getpass.getuser()%0A%0A%0Aclass Github2JenkinsException(Exception):%0A    pass %0A%0A%0Agh = github3.GitHub()%0A%0A%0A_jenkins = None%0A%0Adef jenkins():%0A    global _jenkins%0A    if _jenkins is not None:%0A        return _jenkins%0A%0A    password = os.environ.get(%22JENKINS_PASSWORD%22)%0A    if JENKINS_USER and not password:%0A        # Need to ask for password    %0A        print %22Jenkins:%22, JENKINS%0A        password = getpass.getpass(%22Password for user %22 + JENKINS_USER +%0A                                   %22 %5Bempty for read-only%5D: %22)%0A%0A    if not password: %0A        _jenkins = jenkinsapi.jenkins.Jenkins(JENKINS)%0A    else:%0A        _jenkins = jenkinsapi.jenkins.Jenkins(JENKINS, JENKINS_USER, password)%0A    return _jenkins    %0A%0Adef repos(username, must_have_branch):%0A   for repo in gh.iter_user_repos(username):%0A       if repo.branch(must_have_branch):%0A            yield repo%0A%0A%0A_jenkins_template = None%0Adef create_template(job_name, repository):%0A    global _jenkins_template%0A    if _jenkins_template is not None:%0A        return _jenkins_template%0A    _jenkins_template = jenkins()%5BJENKINS_JOB_TEMPLATE%5D    %0A    if not _jenkins_template:%0A        raise Github2JenkinsException(%22Can't find template %22 + JENKINS_JOB_TEMPLATE)%0A    return _jenkins_template%0A%0A%0A

Create Jenkins jobs per Github repository