Ejemplo n.º 1
0
def flag_project(session, project, reason, user_email, user_id):
    """ Flag a project in the database.

    """

    flag = anitya.lib.model.ProjectFlag(
        user=user_email,
        project=project,
        reason=reason)

    session.add(flag)

    try:
        session.flush()
    except SQLAlchemyError as err:
        log.exception(err)
        session.rollback()
        raise anitya.lib.exceptions.AnityaException(
            'Could not flag this project.')

    anitya.log(
        session,
        project=project,
        topic='project.flag',
        message=dict(
            agent=user_id,
            project=project.name,
            reason=reason,
            packages=[pkg.__json__() for pkg in project.packages],
        )
    )
    session.commit()
    return flag
Ejemplo n.º 2
0
def set_flag_state(session, flag, state, user_id):
    """ Change the state of a ProjectFlag in the database.

    """

    # Don't toggle the state or send a new fedmsg if the flag's
    # state wouldn't actually be changed.
    if flag.state == state:
        raise anitya.lib.exceptions.AnityaException('Flag state unchanged.')

    flag.state = state
    session.add(flag)

    try:
        session.flush()
    except SQLAlchemyError as err:
        _log.exception(err)
        session.rollback()
        raise anitya.lib.exceptions.AnityaException(
            'Could not set the state of this flag.')

    anitya.log(session,
               topic='project.flag.set',
               message=dict(
                   agent=user_id,
                   flag=flag.id,
                   state=state,
               ))
    session.commit()
    return flag
Ejemplo n.º 3
0
def set_flag_state(session, flag, state, user_id):
    """ Change the state of a ProjectFlag in the database.

    """

    # Don't toggle the state or send a new fedmsg if the flag's
    # state wouldn't actually be changed.
    if flag.state == state:
        raise anitya.lib.exceptions.AnityaException(
            'Flag state unchanged.')

    flag.state = state
    session.add(flag)

    try:
        session.flush()
    except SQLAlchemyError as err:
        log.exception(err)
        session.rollback()
        raise anitya.lib.exceptions.AnityaException(
            'Could not set the state of this flag.')

    anitya.log(
        session,
        topic='project.flag.set',
        message=dict(
            agent=user_id,
            flag=flag.id,
            state=state,
        )
    )
    session.commit()
    return flag
Ejemplo n.º 4
0
def add_distro():

    if not is_admin():
        flask.abort(401)

    form = anitya.forms.DistroForm()

    if form.validate_on_submit():
        name = form.name.data

        distro = anitya.lib.model.Distro(name)

        anitya.log(SESSION,
                   distro=distro,
                   topic='distro.add',
                   message=dict(
                       agent=flask.g.auth.openid,
                       distro=distro.name,
                   ))

        try:
            SESSION.add(distro)
            SESSION.commit()
            flask.flash('Distribution added')
        except SQLAlchemyError as err:
            flask.flash('Could not add this distro, already exists?', 'error')
        return flask.redirect(flask.url_for('distros'))

    return flask.render_template('distro_add.html',
                                 current='distros',
                                 form=form)
Ejemplo n.º 5
0
def delete_distro(distro_name):
    """ Delete a distro """

    distro = anitya.lib.model.Distro.by_name(SESSION, distro_name)
    if not distro:
        flask.abort(404)

    if not is_admin():
        flask.abort(401)

    form = anitya.forms.ConfirmationForm()

    if form.validate_on_submit():
        anitya.log(
            SESSION,
            distro=distro,
            topic='distro.remove',
            message=dict(
                agent=flask.g.auth.openid,
                distro=distro.name,
            )
        )

        SESSION.delete(distro)
        SESSION.commit()
        flask.flash('Distro %s has been removed' % distro_name)
        return flask.redirect(flask.url_for('distros'))

    return flask.render_template(
        'distro_delete.html',
        current='distros',
        distro=distro,
        form=form)
Ejemplo n.º 6
0
def delete_distro(distro_name):
    """ Delete a distro """

    distro = anitya.lib.model.Distro.by_name(SESSION, distro_name)
    if not distro:
        flask.abort(404)

    if not is_admin():
        flask.abort(401)

    form = anitya.forms.ConfirmationForm()

    if form.validate_on_submit():
        anitya.log(SESSION,
                   distro=distro,
                   topic='distro.remove',
                   message=dict(
                       agent=flask.g.auth.openid,
                       distro=distro.name,
                   ))

        SESSION.delete(distro)
        SESSION.commit()
        flask.flash('Distro %s has been removed' % distro_name)
        return flask.redirect(flask.url_for('distros'))

    return flask.render_template('distro_delete.html',
                                 current='distros',
                                 distro=distro,
                                 form=form)
Ejemplo n.º 7
0
def flag_project(session, project, reason, user_email, user_id):
    """ Flag a project in the database.

    """

    flag = anitya.lib.model.ProjectFlag(user=user_email,
                                        project=project,
                                        reason=reason)

    session.add(flag)

    try:
        session.flush()
    except SQLAlchemyError as err:
        _log.exception(err)
        session.rollback()
        raise anitya.lib.exceptions.AnityaException(
            'Could not flag this project.')

    anitya.log(session,
               project=project,
               topic='project.flag',
               message=dict(
                   agent=user_id,
                   project=project.name,
                   reason=reason,
                   packages=[pkg.__json__() for pkg in project.packages],
               ))
    session.commit()
    return flag
Ejemplo n.º 8
0
def delete_project_version(project_id, version):

    project = anitya.lib.model.Project.get(SESSION, project_id)
    if not project:
        flask.abort(404)

    version_obj = None
    for vers in project.versions_obj:
        if version == vers.version:
            version_obj = vers
            break

    if version_obj is None:
        flask.abort(
            404,
            'Version %s not found for project %s' % (version, project.name)
        )

    if not is_admin():
        flask.abort(401)

    form = anitya.forms.ConfirmationForm()
    confirm = flask.request.form.get('confirm', False)

    if form.validate_on_submit():
        if confirm:
            anitya.log(
                SESSION,
                project=project,
                topic='project.version.remove',
                message=dict(
                    agent=flask.g.auth.openid,
                    project=project.name,
                    version=version,
                )
            )

            # Delete the record of the version for this project
            SESSION.delete(version_obj)
            # Adjust the latest_version if needed
            if project.latest_version == version:
                project.latest_version = None
                SESSION.add(project)
            SESSION.commit()

            flask.flash('Version for %s has been removed' % version)
        return flask.redirect(
            flask.url_for('project', project_id=project.id))

    return flask.render_template(
        'version_delete.html',
        current='projects',
        project=project,
        version=version,
        form=form)
Ejemplo n.º 9
0
def delete_project_version(project_id, version):

    project = anitya.lib.model.Project.get(SESSION, project_id)
    if not project:
        flask.abort(404)

    version_obj = None
    for vers in project.versions_obj:
        if version == vers.version:
            version_obj = vers
            break

    if version_obj is None:
        flask.abort(
            404,
            'Version %s not found for project %s' % (version, project.name)
        )

    if not is_admin():
        flask.abort(401)

    form = anitya.forms.ConfirmationForm()
    confirm = flask.request.form.get('confirm', False)

    if form.validate_on_submit():
        if confirm:
            anitya.log(
                SESSION,
                project=project,
                topic='project.version.remove',
                message=dict(
                    agent=flask.g.auth.openid,
                    project=project.name,
                    version=version,
                )
            )

            # Delete the record of the version for this project
            SESSION.delete(version_obj)
            # Adjust the latest_version if needed
            if project.latest_version == version:
                project.latest_version = None
                SESSION.add(project)
            SESSION.commit()

            flask.flash('Version for %s has been removed' % version)
        return flask.redirect(
            flask.url_for('project', project_id=project.id))

    return flask.render_template(
        'version_delete.html',
        current='projects',
        project=project,
        version=version,
        form=form)
Ejemplo n.º 10
0
def create_project(session,
                   name,
                   homepage,
                   user_id,
                   backend='custom',
                   version_url=None,
                   version_prefix=None,
                   regex=None,
                   check_release=False,
                   insecure=False):
    """ Create the project in the database.

    """
    # Set the ecosystem if there's one associated with the given backend
    ecosystems = [
        e for e in anitya.lib.plugins.ECOSYSTEM_PLUGINS.get_plugins()
        if e.default_backend == backend
    ]
    ecosystem_name = ecosystems[0].name if len(ecosystems) == 1 else None

    project = anitya.lib.model.Project(name=name,
                                       homepage=homepage,
                                       backend=backend,
                                       ecosystem_name=ecosystem_name,
                                       version_url=version_url,
                                       regex=regex,
                                       version_prefix=version_prefix,
                                       insecure=insecure)

    session.add(project)

    try:
        session.flush()
    except IntegrityError:
        session.rollback()
        raise anitya.lib.exceptions.ProjectExists(project)
    except SQLAlchemyError as err:
        _log.exception(err)
        session.rollback()
        raise anitya.lib.exceptions.AnityaException(
            'Could not add this project, already exists?')

    anitya.log(session,
               project=project,
               topic='project.add',
               message=dict(
                   agent=user_id,
                   project=project.name,
               ))
    session.commit()
    if check_release is True:
        anitya.check_release(project, session)
    return project
Ejemplo n.º 11
0
def create_project(
        session, name, homepage, user_id, backend='custom',
        version_url=None, version_prefix=None, regex=None,
        check_release=False, insecure=False):
    """ Create the project in the database.

    """
    # Set the ecosystem if there's one associated with the given backend
    ecosystems = [e for e in anitya.lib.plugins.ECOSYSTEM_PLUGINS.get_plugins()
                  if e.default_backend == backend]
    ecosystem_name = ecosystems[0].name if len(ecosystems) == 1 else None

    project = anitya.lib.model.Project(
        name=name,
        homepage=homepage,
        backend=backend,
        ecosystem_name=ecosystem_name,
        version_url=version_url,
        regex=regex,
        version_prefix=version_prefix,
        insecure=insecure
    )

    session.add(project)

    try:
        session.flush()
    except IntegrityError:
        session.rollback()
        raise anitya.lib.exceptions.ProjectExists(project)
    except SQLAlchemyError as err:
        _log.exception(err)
        session.rollback()
        raise anitya.lib.exceptions.AnityaException(
            'Could not add this project, already exists?')

    anitya.log(
        session,
        project=project,
        topic='project.add',
        message=dict(
            agent=user_id,
            project=project.name,
        )
    )
    session.commit()
    if check_release is True:
        anitya.check_release(project, session)
    return project
Ejemplo n.º 12
0
def delete_project_mapping(project_id, distro_name, pkg_name):

    project = anitya.lib.model.Project.get(SESSION, project_id)
    if not project:
        flask.abort(404)

    distro = anitya.lib.model.Distro.get(SESSION, distro_name)
    if not distro:
        flask.abort(404)

    package = anitya.lib.model.Packages.get(
        SESSION, project.id, distro.name, pkg_name)
    if not package:
        flask.abort(404)

    if not is_admin():
        flask.abort(401)

    form = anitya.forms.ConfirmationForm()
    confirm = flask.request.form.get('confirm', False)

    if form.validate_on_submit():
        if confirm:
            anitya.log(
                SESSION,
                project=project,
                topic='project.map.remove',
                message=dict(
                    agent=flask.g.auth.openid,
                    project=project.name,
                    distro=distro.name,
                )
            )

            SESSION.delete(package)
            SESSION.commit()

            flask.flash('Mapping for %s has been removed' % project.name)
        return flask.redirect(
            flask.url_for('project', project_id=project.id))

    return flask.render_template(
        'regex_delete.html',
        current='projects',
        project=project,
        package=package,
        form=form)
Ejemplo n.º 13
0
def delete_project_mapping(project_id, distro_name, pkg_name):

    project = anitya.lib.model.Project.get(SESSION, project_id)
    if not project:
        flask.abort(404)

    distro = anitya.lib.model.Distro.get(SESSION, distro_name)
    if not distro:
        flask.abort(404)

    package = anitya.lib.model.Packages.get(
        SESSION, project.id, distro.name, pkg_name)
    if not package:
        flask.abort(404)

    if not is_admin():
        flask.abort(401)

    form = anitya.forms.ConfirmationForm()
    confirm = flask.request.form.get('confirm', False)

    if form.validate_on_submit():
        if confirm:
            anitya.log(
                SESSION,
                project=project,
                topic='project.map.remove',
                message=dict(
                    agent=flask.g.auth.openid,
                    project=project.name,
                    distro=distro.name,
                )
            )

            SESSION.delete(package)
            SESSION.commit()

            flask.flash('Mapping for %s has been removed' % project.name)
        return flask.redirect(
            flask.url_for('project', project_id=project.id))

    return flask.render_template(
        'regex_delete.html',
        current='projects',
        project=project,
        package=package,
        form=form)
Ejemplo n.º 14
0
def create_project(session,
                   name,
                   homepage,
                   user_id,
                   backend='custom',
                   version_url=None,
                   version_prefix=None,
                   regex=None,
                   check_release=False):
    """ Create the project in the database.

    """
    # Set the ecosystem if there's one associated with the given backend
    backend_ref = anitya.lib.model.Backend.by_name(session, name=backend)
    ecosystem_ref = backend_ref.default_ecosystem

    project = anitya.lib.model.Project(
        name=name,
        homepage=homepage,
        backend=backend,
        ecosystem=ecosystem_ref,
        version_url=version_url,
        regex=regex,
        version_prefix=version_prefix,
    )

    session.add(project)

    try:
        session.flush()
    except SQLAlchemyError as err:
        log.exception(err)
        session.rollback()
        raise anitya.lib.exceptions.AnityaException(
            'Could not add this project, already exists?')

    anitya.log(session,
               project=project,
               topic='project.add',
               message=dict(
                   agent=user_id,
                   project=project.name,
               ))
    session.commit()
    if check_release is True:
        anitya.check_release(project, session)
    return project
Ejemplo n.º 15
0
def create_project(
        session, name, homepage, user_id, backend='custom',
        version_url=None, version_prefix=None, regex=None,
        check_release=False):
    """ Create the project in the database.

    """
    # Set the ecosystem if there's one associated with the given backend
    backend_ref = anitya.lib.model.Backend.by_name(session, name=backend)
    ecosystem_ref = backend_ref.default_ecosystem

    project = anitya.lib.model.Project(
        name=name,
        homepage=homepage,
        backend=backend,
        ecosystem=ecosystem_ref,
        version_url=version_url,
        regex=regex,
        version_prefix=version_prefix,
    )

    session.add(project)

    try:
        session.flush()
    except SQLAlchemyError as err:
        log.exception(err)
        session.rollback()
        raise anitya.lib.exceptions.AnityaException(
            'Could not add this project, already exists?')

    anitya.log(
        session,
        project=project,
        topic='project.add',
        message=dict(
            agent=user_id,
            project=project.name,
        )
    )
    session.commit()
    if check_release is True:
        anitya.check_release(project, session)
    return project
Ejemplo n.º 16
0
def delete_project(project_id):

    project = anitya.lib.model.Project.get(SESSION, project_id)
    if not project:
        flask.abort(404)

    if not is_admin():
        flask.abort(401)

    project_name = project.name

    form = anitya.forms.ConfirmationForm()
    confirm = flask.request.form.get('confirm', False)

    if form.validate_on_submit():
        if confirm:
            anitya.log(
                SESSION,
                project=project,
                topic='project.remove',
                message=dict(
                    agent=flask.g.auth.openid,
                    project=project.name,
                )
            )

            for version in project.versions_obj:
                SESSION.delete(version)

            SESSION.delete(project)
            SESSION.commit()
            flask.flash('Project %s has been removed' % project_name)
            return flask.redirect(flask.url_for('projects'))
        else:
            return flask.redirect(
                flask.url_for('project', project_id=project.id))

    return flask.render_template(
        'project_delete.html',
        current='projects',
        project=project,
        form=form)
Ejemplo n.º 17
0
def delete_project(project_id):

    project = anitya.lib.model.Project.get(SESSION, project_id)
    if not project:
        flask.abort(404)

    if not is_admin():
        flask.abort(401)

    project_name = project.name

    form = anitya.forms.ConfirmationForm()
    confirm = flask.request.form.get('confirm', False)

    if form.validate_on_submit():
        if confirm:
            anitya.log(
                SESSION,
                project=project,
                topic='project.remove',
                message=dict(
                    agent=flask.g.auth.openid,
                    project=project.name,
                )
            )

            for version in project.versions_obj:
                SESSION.delete(version)

            SESSION.delete(project)
            SESSION.commit()
            flask.flash('Project %s has been removed' % project_name)
            return flask.redirect(flask.url_for('projects'))
        else:
            return flask.redirect(
                flask.url_for('project', project_id=project.id))

    return flask.render_template(
        'project_delete.html',
        current='projects',
        project=project,
        form=form)
Ejemplo n.º 18
0
def edit_distro(distro_name):

    distro = anitya.lib.model.Distro.by_name(SESSION, distro_name)
    if not distro:
        flask.abort(404)

    if not is_admin():
        flask.abort(401)

    form = anitya.forms.DistroForm(obj=distro)

    if form.validate_on_submit():
        name = form.name.data

        if name != distro.name:
            anitya.log(
                SESSION,
                distro=distro,
                topic='distro.edit',
                message=dict(
                    agent=flask.g.auth.openid,
                    old=distro.name,
                    new=name,
                )
            )

            distro.name = name

            SESSION.add(distro)
            SESSION.commit()
            message = 'Distribution edited'
            flask.flash(message)
        return flask.redirect(
            flask.url_for('distros')
        )

    return flask.render_template(
        'distro_edit.html',
        current='distros',
        distro=distro,
        form=form)
Ejemplo n.º 19
0
def edit_distro(distro_name):

    distro = anitya.lib.model.Distro.by_name(SESSION, distro_name)
    if not distro:
        flask.abort(404)

    if not is_admin():
        flask.abort(401)

    form = anitya.forms.DistroForm(obj=distro)

    if form.validate_on_submit():
        name = form.name.data

        if name != distro.name:
            anitya.log(
                SESSION,
                distro=distro,
                topic='distro.edit',
                message=dict(
                    agent=flask.g.auth.openid,
                    old=distro.name,
                    new=name,
                )
            )

            distro.name = name

            SESSION.add(distro)
            SESSION.commit()
            message = 'Distribution edited'
            flask.flash(message)
        return flask.redirect(
            flask.url_for('distros')
        )

    return flask.render_template(
        'distro_edit.html',
        current='distros',
        distro=distro,
        form=form)
Ejemplo n.º 20
0
def add_distro():

    if not is_admin():
        flask.abort(401)

    form = anitya.forms.DistroForm()

    if form.validate_on_submit():
        name = form.name.data

        distro = anitya.lib.model.Distro(name)

        anitya.log(
            SESSION,
            distro=distro,
            topic='distro.add',
            message=dict(
                agent=flask.g.auth.openid,
                distro=distro.name,
            )
        )

        try:
            SESSION.add(distro)
            SESSION.commit()
            flask.flash('Distribution added')
        except SQLAlchemyError as err:
            flask.flash(
                'Could not add this distro, already exists?', 'error')
        return flask.redirect(
            flask.url_for('distros')
        )

    return flask.render_template(
        'distro_add.html',
        current='distros',
        form=form)
Ejemplo n.º 21
0
def edit_project(
        session, project, name, homepage, backend, version_url,
        version_prefix, regex, insecure, user_id, check_release=False):
    """ Edit a project in the database.

    """
    changes = {}
    if name != project.name:
        old = project.name
        project.name = name.strip() if name else None
        changes['name'] = {'old': old, 'new': project.name}
    if homepage != project.homepage:
        old = project.homepage
        project.homepage = homepage.strip() if homepage else None
        changes['homepage'] = {'old': old, 'new': project.homepage}
    if backend != project.backend:
        old = project.backend
        project.backend = backend
        changes['backend'] = {'old': old, 'new': project.backend}
    if  version_url != project.version_url:
        old = project.version_url
        project.version_url = version_url.strip() if version_url else None
        if old != project.version_url:
            changes['version_url'] = {'old': old, 'new': project.version_url}
    if  version_prefix != project.version_prefix:
        old = project.version_prefix
        project.version_prefix = version_prefix.strip() if version_prefix else None
        if old != project.version_prefix:
            changes['version_prefix'] = {
                'old': old, 'new': project.version_prefix}
    if regex != project.regex:
        old = project.regex
        project.regex = regex.strip() if regex else None
        if old != project.regex:
            changes['regex'] = {'old': old, 'new': project.regex}
    if insecure != project.insecure:
        old = project.insecure
        project.insecure = insecure
        changes['insecure'] = {'old': old, 'new': project.insecure}

    try:
        if changes:
            anitya.log(
                session,
                project=project,
                topic='project.edit',
                message=dict(
                    agent=user_id,
                    project=project.name,
                    fields=list(changes.keys()),  # be backward compat
                    changes=changes,
                )
            )
            session.add(project)
            session.commit()
        if check_release is True:
            anitya.check_release(project, session)
    except SQLAlchemyError as err:
        log.exception(err)
        session.rollback()
        raise anitya.lib.exceptions.AnityaException(
            'Could not edit this project. Is there already a project '
            'with these name and homepage?')
Ejemplo n.º 22
0
def map_project(
        session, project, package_name, distribution, user_id,
        old_package_name=None, old_distro_name=None):
    """ Map a project to a distribution.

    """

    distribution = distribution.strip()

    distro_obj = anitya.lib.model.Distro.get(session, distribution)

    if not distro_obj:
        distro_obj = anitya.lib.model.Distro(name=distribution)
        anitya.log(
            session,
            distro=distro_obj,
            topic='distro.add',
            message=dict(
                agent=user_id,
                distro=distro_obj.name,
            )
        )
        session.add(distro_obj)
        try:
            session.flush()
        except SQLAlchemyError as err:  # pragma: no cover
            # We cannot test this situation
            session.rollback()
            raise anitya.lib.exceptions.AnityaException(
                'Could not add the distribution %s to the database, '
                'please inform an admin.' % distribution, 'errors')

    pkgname = old_package_name or package_name
    distro = old_distro_name or distribution
    pkg = anitya.lib.model.Packages.get(
        session, project.id, distro, pkgname)

    # Check that we can update the mapping to the new info provided
    other_pkg = anitya.lib.model.Packages.by_package_name_distro(
        session, package_name, distro)
    if other_pkg and other_pkg.project:
        raise anitya.lib.exceptions.AnityaInvalidMappingException(
                pkgname, distro, package_name, distribution,
                other_pkg.project.id, other_pkg.project.name)

    edited = None
    if not pkg:
        topic = 'project.map.new'
        if not other_pkg:
            pkg = anitya.lib.model.Packages(
                distro=distro_obj.name,
                project_id=project.id,
                package_name=package_name
            )
        else:
            other_pkg.project = project
            pkg = other_pkg
    else:
        topic = 'project.map.update'
        edited = []
        if pkg.distro != distro_obj.name:
            pkg.distro = distro_obj.name
            edited.append('distribution')
        if pkg.package_name != package_name:
            pkg.package_name = package_name
            edited.append('package_name')

    session.add(pkg)
    try:
        session.flush()
    except SQLAlchemyError as err:  # pragma: no cover
        log.exception(err)
        # We cannot test this situation
        session.rollback()
        raise anitya.lib.exceptions.AnityaException(
            'Could not add the mapping of %s to %s, please inform an '
            'admin.' % (package_name, distribution))

    message = dict(
        agent=user_id,
        project=project.name,
        distro=distro_obj.name,
        new=package_name,
    )
    if edited:
        message['prev'] = old_package_name or package_name
        message['edited'] = edited

    anitya.log(
        session,
        project=project,
        distro=distro_obj,
        topic=topic,
        message=message,
    )

    return pkg
Ejemplo n.º 23
0
def edit_project(session,
                 project,
                 name,
                 homepage,
                 backend,
                 version_url,
                 version_prefix,
                 regex,
                 insecure,
                 user_id,
                 check_release=False):
    """ Edit a project in the database.

    """
    changes = {}
    if name != project.name:
        old = project.name
        project.name = name.strip() if name else None
        changes['name'] = {'old': old, 'new': project.name}
    if homepage != project.homepage:
        old = project.homepage
        project.homepage = homepage.strip() if homepage else None
        changes['homepage'] = {'old': old, 'new': project.homepage}
    if backend != project.backend:
        old = project.backend
        project.backend = backend
        changes['backend'] = {'old': old, 'new': project.backend}
    if version_url != project.version_url:
        old = project.version_url
        project.version_url = version_url.strip() if version_url else None
        if old != project.version_url:
            changes['version_url'] = {'old': old, 'new': project.version_url}
    if version_prefix != project.version_prefix:
        old = project.version_prefix
        project.version_prefix = version_prefix.strip() \
            if version_prefix else None
        if old != project.version_prefix:
            changes['version_prefix'] = {
                'old': old,
                'new': project.version_prefix
            }
    if regex != project.regex:
        old = project.regex
        project.regex = regex.strip() if regex else None
        if old != project.regex:
            changes['regex'] = {'old': old, 'new': project.regex}
    if insecure != project.insecure:
        old = project.insecure
        project.insecure = insecure
        changes['insecure'] = {'old': old, 'new': project.insecure}

    try:
        if changes:
            anitya.log(
                session,
                project=project,
                topic='project.edit',
                message=dict(
                    agent=user_id,
                    project=project.name,
                    fields=list(changes.keys()),  # be backward compat
                    changes=changes,
                ))
            session.add(project)
            session.commit()
        if check_release is True:
            anitya.check_release(project, session)
    except SQLAlchemyError as err:
        _log.exception(err)
        session.rollback()
        raise anitya.lib.exceptions.AnityaException(
            'Could not edit this project. Is there already a project '
            'with these name and homepage?')
Ejemplo n.º 24
0
def map_project(session,
                project,
                package_name,
                distribution,
                user_id,
                old_package_name=None,
                old_distro_name=None):
    """ Map a project to a distribution.

    """

    distribution = distribution.strip()

    distro_obj = anitya.lib.model.Distro.get(session, distribution)

    if not distro_obj:
        distro_obj = anitya.lib.model.Distro(name=distribution)
        anitya.log(session,
                   distro=distro_obj,
                   topic='distro.add',
                   message=dict(
                       agent=user_id,
                       distro=distro_obj.name,
                   ))
        session.add(distro_obj)
        try:
            session.flush()
        except SQLAlchemyError as err:  # pragma: no cover
            # We cannot test this situation
            session.rollback()
            raise anitya.lib.exceptions.AnityaException(
                'Could not add the distribution %s to the database, '
                'please inform an admin.' % distribution, 'errors')

    pkgname = old_package_name or package_name
    distro = old_distro_name or distribution
    pkg = anitya.lib.model.Packages.get(session, project.id, distro, pkgname)

    # Check that we can update the mapping to the new info provided
    other_pkg = anitya.lib.model.Packages.by_package_name_distro(
        session, package_name, distro)
    if other_pkg and other_pkg.project:
        raise anitya.lib.exceptions.AnityaInvalidMappingException(
            pkgname, distro, package_name, distribution, other_pkg.project.id,
            other_pkg.project.name)

    edited = None
    if not pkg:
        topic = 'project.map.new'
        if not other_pkg:
            pkg = anitya.lib.model.Packages(distro=distro_obj.name,
                                            project_id=project.id,
                                            package_name=package_name)
        else:
            other_pkg.project = project
            pkg = other_pkg
    else:
        topic = 'project.map.update'
        edited = []
        if pkg.distro != distro_obj.name:
            pkg.distro = distro_obj.name
            edited.append('distribution')
        if pkg.package_name != package_name:
            pkg.package_name = package_name
            edited.append('package_name')

    session.add(pkg)
    try:
        session.flush()
    except SQLAlchemyError as err:  # pragma: no cover
        _log.exception(err)
        # We cannot test this situation
        session.rollback()
        raise anitya.lib.exceptions.AnityaException(
            'Could not add the mapping of %s to %s, please inform an '
            'admin.' % (package_name, distribution))

    message = dict(
        agent=user_id,
        project=project.name,
        distro=distro_obj.name,
        new=package_name,
    )
    if edited:
        message['prev'] = old_package_name or package_name
        message['edited'] = edited

    anitya.log(
        session,
        project=project,
        distro=distro_obj,
        topic=topic,
        message=message,
    )

    return pkg