Beispiel #1
0
def branches():
    """Returns a list of branches in a given repository by querying Gerrit.

    GET /gerrit/branches?param=<value>

    :arg str project: Project to query branches from. (required)

    JSON::

        {
          "branches": [
            {
              "ref": "refs/heads/stable/beryllium",
              "revision": "8f72284f3808328604bdff7f91a6999094f7c6d7"
            },
            ...
            ]
        }
    """
    mapping = {
        'project': request.args.get('project', None),
    }
    result = check_parameters(mapping)
    if not result:
        gerrit = GerritHandler(app.config['GERRIT_URL'])
        branches = gerrit.project_branches_list(mapping['project'])
        if not branches:
            result = {'error': 'No branches found for {0}.'.format(mapping['project'])}
        else:
            result = {'branches': branches}
    return jsonify(result)
Beispiel #2
0
def branches():
    """Returns a list of branches in a given repository by querying Gerrit.

    GET /gerrit/branches?param=<value>

    :arg str project: Project to query branches from. (required)

    JSON::

        {
          "branches": [
            {
              "ref": "refs/heads/stable/beryllium",
              "revision": "8f72284f3808328604bdff7f91a6999094f7c6d7"
            },
            ...
            ]
        }
    """
    mapping = {
        'project': request.args.get('project', None),
    }
    result = check_parameters(mapping)
    if not result:
        gerrit = GerritHandler(app.config['GERRIT_URL'])
        branches = gerrit.project_branches_list(mapping['project'])
        if not branches:
            result = {
                'error':
                'No branches found for {0}.'.format(mapping['project'])
            }
        else:
            result = {'branches': branches}
    return jsonify(result)
Beispiel #3
0
def merged_changes():
    """Returns a list of merged changes in a given repository by querying Gerrit.

    GET /gerrit/changes?param=<value>

    :arg str project: Project to query changes from. (required)
    :arg str branch: Branch to pull changes from. (default: master)

    JSON::

        {
          "changes": [
            {
              "_number": 37706,
              "branch": "master",
              "change_id": "I4168e023b77bfddbb6f72057e849925ba2dffa17",
              "created": "2016-04-18 02:42:33.000000000",
              "deletions": 0,
              "hashtags": [],
              "id": "spectrometer~master~I4168e023b77bfddbb6f72057e849925ba2dffa17",
              "insertions": 119,
              "owner": {
                "_account_id": 2759
              },
              "project": "spectrometer",
              "status": "MERGED",
              "subject": "Add API to return commits since ref",
              "submittable": false,
              "topic": "git-api",
              "updated": "2016-04-19 09:03:03.000000000"
            },
            ...
            ]
        }
    """
    mapping = {
        'project': request.args.get('project', None),
        'branch': request.args.get('branch', 'master')
    }
    result = check_parameters(mapping)
    if not result:
        gerrit = GerritHandler(app.config['GERRIT_URL'])
        changes = gerrit.project_merged_changes_list(mapping['project'],
                                                     mapping['branch'])
        if not changes:
            result = {
                'error': 'No changes found for {0}.'.format(mapping['project'])
            }
        else:
            result = {'changes': changes}
    return jsonify(result)
Beispiel #4
0
def merged_changes():
    """Returns a list of merged changes in a given repository by querying Gerrit.

    GET /gerrit/changes?param=<value>

    :arg str project: Project to query changes from. (required)
    :arg str branch: Branch to pull changes from. (default: master)

    JSON::

        {
          "changes": [
            {
              "_number": 37706,
              "branch": "master",
              "change_id": "I4168e023b77bfddbb6f72057e849925ba2dffa17",
              "created": "2016-04-18 02:42:33.000000000",
              "deletions": 0,
              "hashtags": [],
              "id": "spectrometer~master~I4168e023b77bfddbb6f72057e849925ba2dffa17",
              "insertions": 119,
              "owner": {
                "_account_id": 2759
              },
              "project": "spectrometer",
              "status": "MERGED",
              "subject": "Add API to return commits since ref",
              "submittable": false,
              "topic": "git-api",
              "updated": "2016-04-19 09:03:03.000000000"
            },
            ...
            ]
        }
    """
    mapping = {
        'project': request.args.get('project', None),
        'branch': request.args.get('branch', 'master')
    }
    result = check_parameters(mapping)
    if not result:
        gerrit = GerritHandler(app.config['GERRIT_URL'])
        changes = gerrit.project_merged_changes_list(mapping['project'], mapping['branch'])
        if not changes:
            result = {'error': 'No changes found for {0}.'.format(mapping['project'])}
        else:
            result = {'changes': changes}
    return jsonify(result)
Beispiel #5
0
def create_app(config):
    app = Flask(__name__)
    config = os.path.abspath(config)
    app.config.from_pyfile(config)
    app.debug = app.config.get('DEBUG', False)

    # Setup semi-permanent cache stored in os temp directory
    try:
        app.cache_file = os.path.join(tempfile.gettempdir(),
                                      'spectrometer-cache.p')
        app.cache = pickle.load(open(app.cache_file, "rb"))
    except IOError:
        app.cache = {}

    # Flask profiler is only active when in debug mode
    profiler = Profiler()
    profiler.init_app(app)

    if not app.debug:
        # Setup Logger
        logdir = app.config.get('LOG_DIR', '/var/log/spectrometer')
        logfile = os.path.join(logdir, 'spectrometer.log')

        logging.getLogger().setLevel(logging.NOTSET)
        logging.getLogger('git.cmd').setLevel(logging.INFO)
        formatter = logging.Formatter(
            '%(asctime)s (%(levelname)8s) %(name)-40s: %(message)s')
        console_handler = logging.StreamHandler()
        console_handler.setFormatter(formatter)
        logging.getLogger().addHandler(console_handler)

        try:
            file_handler = RotatingFileHandler(logfile,
                                               maxBytes=20000000,
                                               backupCount=20)
            file_handler.setFormatter(formatter)
            logging.getLogger().addHandler(file_handler)
            log.info('File logger activated.')
        except IOError:
            log.warn(
                'Unable to activate File logger. Please ensure that the '
                'log directory ({0}) is writable by the spectrometer user.'.
                format(logdir))

    # Prep resource handlers
    app.gerrithandler = GerritHandler(app.config['GERRIT_URL'])
    app.githandlers = {}

    # Stop Flask debug mode from running the scheduler twice
    if not app.debug or os.environ.get('WERKZEUG_RUN_MAIN') == 'true':
        run_scheduler(app)

    app.route('/')(views.status)

    app.register_blueprint(gitapi, url_prefix='/git')
    app.register_blueprint(gerritapi, url_prefix='/gerrit')

    return app
Beispiel #6
0
def tags():
    """Returns a list of tags in a given repository by querying Gerrit.

    GET /gerrit/tags?param=<value>

    :arg str project: Project to query tags from. (required)

    JSON::

        {
          "tags": [
            {
              "message": "OpenDaylight Beryllium-SR1 release",
              "object": "f76cc0a12dc8f06dae3cedc31d06add72df8de5d",
              "ref": "refs/tags/release/beryllium-sr1",
              "revision": "8b92d614ee48b4fc5ba11c3f38c92dfa14d43655",
              "tagger": {
                "date": "2016-03-23 13:34:09.000000000",
                "email": "*****@*****.**",
                "name": "Thanh Ha",
                "tz": -240
              }
            },
            ...
            ]
        }
    """
    mapping = {
        'project': request.args.get('project', None),
    }
    result = check_parameters(mapping)
    if not result:
        gerrit = GerritHandler(app.config['GERRIT_URL'])
        tags = gerrit.project_tags_list(mapping['project'])
        if not branches:
            result = {
                'error': 'No tags found for {0}.'.format(mapping['project'])
            }
        else:
            result = {'tags': tags}
    return jsonify(result)
Beispiel #7
0
def mirror_repos(mirror_dir, gerrit_url):
    """Updates repository mirrors

    This function creates a multiprocessing pool that will parallelize the
    repo mirroring process. It will create as many worker threads as there are
    cpus on the system.

    Arguments:

        :arg str mirror_dir: Path to directory containing repos.
        :arg str gerrit_url: URL to the Gerrit server. Used for cloning repos.
    """
    log.info('Updating git mirrors.')
    gerrit = GerritHandler(gerrit_url)
    projects = gerrit.projects_list()

    pool = Pool()
    jobs = [(project, mirror_dir, gerrit_url) for project in projects]
    pool.map(update_repo_parallel, jobs)
    pool.close()
    pool.join()
Beispiel #8
0
def projects():
    """Returns a list of projects by querying Gerrit.

    GET /gerrit/projects

    JSON::

        {
          "projects": [
            "groupbasedpolicy",
            "spectrometer",
            "releng/autorelease",
            "snmp4sdn",
            "ovsdb",
            "nemo",
            ...
            ]
        }
    """
    gerrit = GerritHandler(app.config['GERRIT_URL'])
    return jsonify({'projects': gerrit.projects_list()})
Beispiel #9
0
def mirror_repos(mirror_dir, gerrit_url):
    """Updates repository mirrors

    This function creates a multiprocessing pool that will parallelize the
    repo mirroring process. It will create as many worker threads as there are
    cpus on the system.

    Arguments:

        :arg str mirror_dir: Path to directory containing repos.
        :arg str gerrit_url: URL to the Gerrit server. Used for cloning repos.
    """
    log.info('Updating git mirrors.')
    gerrit = GerritHandler(gerrit_url)
    projects = gerrit.projects_list()

    pool = Pool()
    jobs = [(project, mirror_dir, gerrit_url) for project in projects]
    pool.map(update_repo_parallel, jobs)
    pool.close()
    pool.join()
Beispiel #10
0
def projects():
    """Returns a list of projects by querying Gerrit.

    GET /gerrit/projects

    JSON::

        {
          "projects": [
            "groupbasedpolicy",
            "spectrometer",
            "releng/autorelease",
            "snmp4sdn",
            "ovsdb",
            "nemo",
            ...
            ]
        }
    """
    gerrit = GerritHandler(app.config['GERRIT_URL'])
    return jsonify({'projects': gerrit.projects_list()})
Beispiel #11
0
def tags():
    """Returns a list of tags in a given repository by querying Gerrit.

    GET /gerrit/tags?param=<value>

    :arg str project: Project to query tags from. (required)

    JSON::

        {
          "tags": [
            {
              "message": "OpenDaylight Beryllium-SR1 release",
              "object": "f76cc0a12dc8f06dae3cedc31d06add72df8de5d",
              "ref": "refs/tags/release/beryllium-sr1",
              "revision": "8b92d614ee48b4fc5ba11c3f38c92dfa14d43655",
              "tagger": {
                "date": "2016-03-23 13:34:09.000000000",
                "email": "*****@*****.**",
                "name": "Thanh Ha",
                "tz": -240
              }
            },
            ...
            ]
        }
    """
    mapping = {
        'project': request.args.get('project', None),
    }
    result = check_parameters(mapping)
    if not result:
        gerrit = GerritHandler(app.config['GERRIT_URL'])
        tags = gerrit.project_tags_list(mapping['project'])
        if not branches:
            result = {'error': 'No tags found for {0}.'.format(mapping['project'])}
        else:
            result = {'tags': tags}
    return jsonify(result)