예제 #1
0
def delete_distro(distro_name):
    """ Delete a distro """

    distro = models.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():
        utilities.log(
            Session,
            distro=distro.__json__(),
            topic="distro.remove",
            message=dict(agent=flask.g.user.username, distro=distro.name),
        )

        Session.delete(distro)
        Session.commit()
        flask.flash("Distro %s has been removed" % distro_name)
        return flask.redirect(flask.url_for("anitya_ui.distros"))

    return flask.render_template(
        "distro_delete.html", current="distros", distro=distro, form=form
    )
예제 #2
0
def delete_project_version(project_id, version):

    project = models.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:
            utilities.log(
                Session,
                project=project.__json__(),
                topic="project.version.remove",
                message=dict(
                    agent=flask.g.user.username, 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("anitya_ui.project", project_id=project.id))

    return flask.render_template(
        "version_delete.html",
        current="projects",
        project=project,
        version=version,
        form=form,
    )
예제 #3
0
    def test_log_project_version_update(self, mock_method):
        """ Assert that 'project.version.update' topic is handled correctly. """
        project = models.Project(name="test")
        distro = models.Distro(name="Fedora")
        exp = (
            "new version: 1.0.0 found for project test "
            "in ecosystem pypi (project id: 1)."
        )
        message = {
            "agent": "anitya",
            "upstream_version": "1.0.0",
            "ecosystem": "pypi",
            "project": {"name": "test", "id": "1"},
        }
        final_msg = utilities.log(
            self.session,
            project=project,
            distro=distro,
            topic="project.version.update",
            message=message,
        )

        mock_method.assert_called_with(
            topic="project.version.update",
            msg=dict(project=project, distro=distro, message=message),
        )
        self.assertEqual(final_msg, exp)
예제 #4
0
    def test_log_project_map_update(self, mock_method):
        """ Assert that 'project.map.update' topic is handled correctly. """
        project = models.Project(name="test")
        distro = models.Distro(name="Fedora")
        exp = "anitya updated the name of test in Fedora from: test_old" " to: test_new"
        message = {
            "agent": "anitya",
            "project": "test",
            "distro": "Fedora",
            "prev": "test_old",
            "new": "test_new",
        }
        final_msg = utilities.log(
            self.session,
            project=project,
            distro=distro,
            topic="project.map.update",
            message=message,
        )

        mock_method.assert_called_with(
            topic="project.map.update",
            msg=dict(project=project, distro=distro, message=message),
        )
        self.assertEqual(final_msg, exp)
예제 #5
0
def delete_project_mapping(project_id, distro_name, pkg_name):

    project = models.Project.get(Session, project_id)
    if not project:
        flask.abort(404)

    distro = models.Distro.get(Session, distro_name)
    if not distro:
        flask.abort(404)

    package = models.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:
            utilities.log(
                Session,
                project=project.__json__(),
                topic="project.map.remove",
                message=dict(
                    agent=flask.g.user.username,
                    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("anitya_ui.project", project_id=project.id))

    return flask.render_template(
        "regex_delete.html",
        current="projects",
        project=project,
        package=package,
        form=form,
    )
예제 #6
0
def delete_project(project_id):

    project = models.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:
            utilities.log(
                Session,
                project=project.__json__(),
                topic="project.remove",
                message=dict(agent=flask.g.user.username, 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("anitya_ui.projects"))
        else:
            return flask.redirect(
                flask.url_for("anitya_ui.project", project_id=project.id)
            )

    return flask.render_template(
        "project_delete.html", current="projects", project=project, form=form
    )
예제 #7
0
def edit_distro(distro_name):

    distro = models.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:
            utilities.log(
                Session,
                distro=distro.__json__(),
                topic="distro.edit",
                message=dict(agent=flask.g.user.username, 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("anitya_ui.distros"))

    return flask.render_template(
        "distro_add_edit.html",
        context="Edit",
        current="distros",
        distro=distro,
        form=form,
    )
예제 #8
0
    def test_log_project_flag_set(self, mock_method):
        """ Assert that 'project.flag.set' topic is handled correctly. """
        project = models.Project(name="test")
        distro = models.Distro(name="Fedora")
        exp = "anitya set flag test to open"
        message = {"agent": "anitya", "flag": "test", "state": "open"}
        final_msg = utilities.log(
            self.session,
            project=project,
            distro=distro,
            topic="project.flag.set",
            message=message,
        )

        mock_method.assert_called_with(
            topic="project.flag.set",
            msg=dict(project=project, distro=distro, message=message),
        )
        self.assertEqual(final_msg, exp)
예제 #9
0
    def test_log_project_flag(self, mock_method):
        """ Assert that 'project.flag' topic is handled correctly. """
        project = models.Project(name="test")
        distro = models.Distro(name="Fedora")
        exp = "anitya flagged the project: test with reason: reason"
        message = {"agent": "anitya", "project": "test", "reason": "reason"}
        final_msg = utilities.log(
            self.session,
            project=project,
            distro=distro,
            topic="project.flag",
            message=message,
        )

        mock_method.assert_called_with(
            topic="project.flag",
            msg=dict(project=project, distro=distro, message=message),
        )
        self.assertEqual(final_msg, exp)
예제 #10
0
    def test_log_project_flag_set(self, mock_method):
        """ Assert that 'project.flag.set' topic is handled correctly. """
        project = models.Project(name="test")
        distro = models.Distro(name="Fedora")
        exp = "anitya set flag test to open"
        message = {"agent": "anitya", "flag": "test", "state": "open"}
        final_msg = utilities.log(
            self.session,
            project=project,
            distro=distro,
            topic="project.flag.set",
            message=message,
        )

        mock_method.assert_called_with(
            topic="project.flag.set",
            msg=dict(project=project, distro=distro, message=message),
        )
        self.assertEqual(final_msg, exp)
예제 #11
0
    def test_log_project_flag(self, mock_method):
        """ Assert that 'project.flag' topic is handled correctly. """
        project = models.Project(name="test")
        distro = models.Distro(name="Fedora")
        exp = "anitya flagged the project: test with reason: reason"
        message = {"agent": "anitya", "project": "test", "reason": "reason"}
        final_msg = utilities.log(
            self.session,
            project=project,
            distro=distro,
            topic="project.flag",
            message=message,
        )

        mock_method.assert_called_with(
            topic="project.flag",
            msg=dict(project=project, distro=distro, message=message),
        )
        self.assertEqual(final_msg, exp)
예제 #12
0
    def test_log_project_add_tried(self, mock_method):
        """ Assert that 'project.add.tried' topic is handled correctly. """
        project = models.Project(name="test")
        distro = models.Distro(name="Fedora")
        exp = "anitya tried to add an already existing project: test"
        message = {"agent": "anitya", "project": "test"}
        final_msg = utilities.log(
            self.session,
            project=project,
            distro=distro,
            topic="project.add.tried",
            message=message,
        )

        mock_method.assert_called_with(
            topic="project.add.tried",
            msg=dict(project=project, distro=distro, message=message),
        )
        self.assertEqual(final_msg, exp)
예제 #13
0
    def test_log_distro_remove(self, mock_method):
        """ Assert that 'distro.remove' topic is handled correctly. """
        project = models.Project(name="test")
        distro = models.Distro(name="Fedora")
        exp = "anitya deleted the distro named: Fedora"
        message = {"agent": "anitya", "distro": "Fedora"}
        final_msg = utilities.log(
            self.session,
            project=project,
            distro=distro,
            topic="distro.remove",
            message=message,
        )

        mock_method.assert_called_with(
            topic="distro.remove",
            msg=dict(project=project, distro=distro, message=message),
        )
        self.assertEqual(final_msg, exp)
예제 #14
0
    def test_log_distro_edit(self, mock_method):
        """ Assert that 'distro.edit' topic is handled correctly. """
        project = models.Project(name="test")
        distro = models.Distro(name="Fedora")
        exp = "anitya edited distro name from: Dummy to: Fedora"
        message = {"agent": "anitya", "old": "Dummy", "new": "Fedora"}
        final_msg = utilities.log(
            self.session,
            project=project,
            distro=distro,
            topic="distro.edit",
            message=message,
        )

        mock_method.assert_called_with(
            topic="distro.edit",
            msg=dict(project=project, distro=distro, message=message),
        )
        self.assertEqual(final_msg, exp)
예제 #15
0
    def test_log_project_version_remove(self, mock_method):
        """ Assert that 'project.version.remove' topic is handled correctly. """
        project = models.Project(name="test")
        distro = models.Distro(name="Fedora")
        exp = "anitya removed the version 1.0.0 of test"
        message = {"agent": "anitya", "project": "test", "version": "1.0.0"}
        final_msg = utilities.log(
            self.session,
            project=project,
            distro=distro,
            topic="project.version.remove",
            message=message,
        )

        mock_method.assert_called_with(
            topic="project.version.remove",
            msg=dict(project=project, distro=distro, message=message),
        )
        self.assertEqual(final_msg, exp)
예제 #16
0
    def test_log_project_add_tried(self, mock_method):
        """ Assert that 'project.add.tried' topic is handled correctly. """
        project = models.Project(name="test")
        distro = models.Distro(name="Fedora")
        exp = "anitya tried to add an already existing project: test"
        message = {"agent": "anitya", "project": "test"}
        final_msg = utilities.log(
            self.session,
            project=project,
            distro=distro,
            topic="project.add.tried",
            message=message,
        )

        mock_method.assert_called_with(
            topic="project.add.tried",
            msg=dict(project=project, distro=distro, message=message),
        )
        self.assertEqual(final_msg, exp)
예제 #17
0
    def test_log_distro_edit(self, mock_method):
        """ Assert that 'distro.edit' topic is handled correctly. """
        project = models.Project(name="test")
        distro = models.Distro(name="Fedora")
        exp = "anitya edited distro name from: Dummy to: Fedora"
        message = {"agent": "anitya", "old": "Dummy", "new": "Fedora"}
        final_msg = utilities.log(
            self.session,
            project=project,
            distro=distro,
            topic="distro.edit",
            message=message,
        )

        mock_method.assert_called_with(
            topic="distro.edit",
            msg=dict(project=project, distro=distro, message=message),
        )
        self.assertEqual(final_msg, exp)
예제 #18
0
    def test_log_project_map_remove(self, mock_method):
        """ Assert that 'project.map.remove' topic is handled correctly. """
        project = models.Project(name="test")
        distro = models.Distro(name="Fedora")
        exp = "anitya removed the mapping of test in Fedora"
        message = {"agent": "anitya", "project": "test", "distro": "Fedora"}
        final_msg = utilities.log(
            self.session,
            project=project,
            distro=distro,
            topic="project.map.remove",
            message=message,
        )

        mock_method.assert_called_with(
            topic="project.map.remove",
            msg=dict(project=project, distro=distro, message=message),
        )
        self.assertEqual(final_msg, exp)
예제 #19
0
    def test_log_distro_add(self, mock_method):
        """ Assert that 'distro.add' topic is handled correctly. """
        project = models.Project(name="test")
        distro = models.Distro(name="Fedora")
        exp = "anitya added the distro named: Fedora"
        message = {"agent": "anitya", "distro": "Fedora"}
        final_msg = utilities.log(
            self.session,
            project=project,
            distro=distro,
            topic="distro.add",
            message=message,
        )

        mock_method.assert_called_with(
            topic="distro.add",
            msg=dict(project=project, distro=distro, message=message),
        )
        self.assertEqual(final_msg, exp)
예제 #20
0
    def test_log_project_map_update(self):
        """ Assert that 'project.map.update' topic is handled correctly. """
        project = models.Project(name='test')
        distro = models.Distro(name='Fedora')
        exp = "anitya updated the name of test in Fedora from: test_old" \
              " to: test_new"
        message = {
            'agent': 'anitya',
            'project': 'test',
            'distro': 'Fedora',
            'prev': 'test_old',
            'new': 'test_new'
        }
        final_msg = utilities.log(self.session,
                                  project=project,
                                  distro=distro,
                                  topic='project.map.update',
                                  message=message)

        self.assertEqual(final_msg, exp)
예제 #21
0
    def test_log_project_version_update(self):
        """ Assert that 'project.version.update' topic is handled correctly. """
        project = models.Project(name='test')
        distro = models.Distro(name='Fedora')
        exp = "new version: 1.0.0 found for project test " \
              "in ecosystem pypi (project id: 1)."
        message = {
            'agent': 'anitya',
            'upstream_version': '1.0.0',
            'ecosystem': 'pypi',
            'project': {
                'name': 'test',
                'id': '1'
            }
        }
        final_msg = utilities.log(self.session,
                                  project=project,
                                  distro=distro,
                                  topic='project.version.update',
                                  message=message)

        self.assertEqual(final_msg, exp)
예제 #22
0
    def test_log_project_edit(self, mock_method):
        """ Assert that 'project.edit' topic is handled correctly. """
        project = models.Project(name="test")
        distro = models.Distro(name="Fedora")
        message = {
            "agent": "anitya",
            "project": "test",
            "changes": {"name": {"old": "dummy", "new": "test"}},
        }
        exp = "anitya edited the project: test fields: " "{}".format(message["changes"])
        final_msg = utilities.log(
            self.session,
            project=project,
            distro=distro,
            topic="project.edit",
            message=message,
        )

        mock_method.assert_called_with(
            topic="project.edit",
            msg=dict(project=project, distro=distro, message=message),
        )
        self.assertEqual(final_msg, exp)
예제 #23
0
    def test_log_project_edit(self):
        """ Assert that 'project.edit' topic is handled correctly. """
        project = models.Project(name='test')
        distro = models.Distro(name='Fedora')
        message = {
            'agent': 'anitya',
            'project': 'test',
            'changes': {
                'name': {
                    'old': 'dummy',
                    'new': 'test'
                }
            }
        }
        exp = "anitya edited the project: test fields: " \
              "{}".format(message['changes'])
        final_msg = utilities.log(self.session,
                                  project=project,
                                  distro=distro,
                                  topic='project.edit',
                                  message=message)

        self.assertEqual(final_msg, exp)
예제 #24
0
    def test_log_project_edit(self, mock_method):
        """ Assert that 'project.edit' topic is handled correctly. """
        project = models.Project(name="test")
        distro = models.Distro(name="Fedora")
        message = {
            "agent": "anitya",
            "project": "test",
            "changes": {"name": {"old": "dummy", "new": "test"}},
        }
        exp = "anitya edited the project: test fields: " "{}".format(message["changes"])
        final_msg = utilities.log(
            self.session,
            project=project,
            distro=distro,
            topic="project.edit",
            message=message,
        )

        mock_method.assert_called_with(
            topic="project.edit",
            msg=dict(project=project, distro=distro, message=message),
        )
        self.assertEqual(final_msg, exp)
예제 #25
0
    def post(self):
        """
        Create a new package associated with an existing project and distribution.

        **Example request**:

        .. sourcecode:: http

            POST /api/v2/packages/ HTTP/1.1
            Accept: application/json
            Accept-Encoding: gzip, deflate
            Authorization: Token gAOFi2wQPzUJFIfDkscAKjbJfXELCz0r44m57Ur2
            Connection: keep-alive
            Content-Length: 120
            Content-Type: application/json
            Host: localhost:5000
            User-Agent: HTTPie/0.9.4

            {
                "distribution": "Fedora",
                "package_name": "python-requests",
                "project_ecosystem": "pypi",
                "project_name": "requests"
            }

        .. sourcecode:: http

            HTTP/1.0 201 CREATED
            Content-Length: 69
            Content-Type: application/json
            Date: Mon, 15 Jan 2018 21:49:01 GMT
            Server: Werkzeug/0.14.1 Python/2.7.14

            {
                "distribution": "Fedora",
                "name": "python-requests"
            }


        :reqheader Authorization: API token to use for authentication
        :reqjson string distribution: The name of the distribution that contains this
            package.
        :reqjson string package_name: The name of the package in the distribution repository.
        :reqjson string project_name: The project name in Anitya.
        :reqjson string project_ecosystem: The ecosystem the project is a part of.
            If it's not part of an ecosystem, use the homepage used in the Anitya project.

        :statuscode 201: When the package was successfully created.
        :statuscode 400: When required arguments are missing or malformed.
        :statuscode 401: When your access token is missing or invalid
        :statuscode 409: When the package already exists.
        """
        distribution_help = _(
            "The name of the distribution that contains this package."
        )
        package_name_help = _("The name of the package in the distribution repository.")
        project_name_help = _("The project name in Anitya.")
        project_ecosystem_help = _(
            "The ecosystem the project is a part of. If it's not part of an ecosystem,"
            " use the homepage used in the Anitya project."
        )

        parser = _BASE_ARG_PARSER.copy()
        parser.add_argument(
            "distribution", type=str, help=distribution_help, required=True
        )
        parser.add_argument(
            "package_name", type=str, help=package_name_help, required=True
        )
        parser.add_argument(
            "project_name", type=str, help=project_name_help, required=True
        )
        parser.add_argument(
            "project_ecosystem", type=str, help=project_ecosystem_help, required=True
        )
        args = parser.parse_args(strict=True)
        try:
            project = models.Project.query.filter_by(
                name=args.project_name, ecosystem_name=args.project_ecosystem
            ).one()
        except NoResultFound:
            return (
                {
                    "error": 'Project "{}" in ecosystem "{}" not found'.format(
                        args.project_name, args.project_ecosystem
                    )
                },
                400,
            )

        try:
            distro = models.Distro.query.filter_by(name=args.distribution).one()
        except NoResultFound:
            return (
                {"error": 'Distribution "{}" not found'.format(args.distribution)},
                400,
            )

        try:
            package = models.Packages(
                distro_name=distro.name, project=project, package_name=args.package_name
            )

            Session.add(package)
            Session.commit()

            message = dict(
                agent=flask_login.current_user.email,
                project=project.name,
                distro=distro.name,
                new=package.package_name,
            )
            utilities.log(
                Session,
                project=project.__json__(),
                distro=distro.__json__(),
                topic="project.map.new",
                message=message,
            )
            return {u"distribution": distro.name, u"name": package.package_name}, 201
        except IntegrityError:
            Session.rollback()
            return {"error": "package already exists in distribution"}, 409
예제 #26
0
    def post(self):
        """
        Create a new package associated with an existing project and distribution.

        **Example request**:

        .. sourcecode:: http

            POST /api/v2/packages/ HTTP/1.1
            Accept: application/json
            Accept-Encoding: gzip, deflate
            Authorization: Token gAOFi2wQPzUJFIfDkscAKjbJfXELCz0r44m57Ur2
            Connection: keep-alive
            Content-Length: 120
            Content-Type: application/json
            Host: localhost:5000
            User-Agent: HTTPie/0.9.4

            {
                "distribution": "Fedora",
                "package_name": "python-requests",
                "project_ecosystem": "pypi",
                "project_name": "requests"
            }

        .. sourcecode:: http

            HTTP/1.0 201 CREATED
            Content-Length: 69
            Content-Type: application/json
            Date: Mon, 15 Jan 2018 21:49:01 GMT
            Server: Werkzeug/0.14.1 Python/2.7.14

            {
                "distribution": "Fedora",
                "name": "python-requests"
            }


        :reqheader Authorization: API token to use for authentication
        :reqjson string distribution: The name of the distribution that contains this
            package.
        :reqjson string package_name: The name of the package in the distribution repository.
        :reqjson string project_name: The project name in Anitya.
        :reqjson string project_ecosystem: The ecosystem the project is a part of.
            If it's not part of an ecosystem, use the homepage used in the Anitya project.

        :statuscode 201: When the package was successfully created.
        :statuscode 400: When required arguments are missing or malformed.
        :statuscode 401: When your access token is missing or invalid
        :statuscode 409: When the package already exists.
        """
        distribution_help = _(
            "The name of the distribution that contains this package.")
        package_name_help = _(
            "The name of the package in the distribution repository.")
        project_name_help = _("The project name in Anitya.")
        project_ecosystem_help = _(
            "The ecosystem the project is a part of. If it's not part of an ecosystem,"
            " use the homepage used in the Anitya project.")

        parser = _BASE_ARG_PARSER.copy()
        parser.add_argument("distribution",
                            type=str,
                            help=distribution_help,
                            required=True)
        parser.add_argument("package_name",
                            type=str,
                            help=package_name_help,
                            required=True)
        parser.add_argument("project_name",
                            type=str,
                            help=project_name_help,
                            required=True)
        parser.add_argument("project_ecosystem",
                            type=str,
                            help=project_ecosystem_help,
                            required=True)
        args = parser.parse_args(strict=True)
        try:
            project = models.Project.query.filter(
                func.lower(models.Project.name) == func.lower(
                    args.project_name),
                func.lower(models.Project.ecosystem_name) == func.lower(
                    args.project_ecosystem),
            ).one()
        except NoResultFound:
            return (
                {
                    "error":
                    'Project "{}" in ecosystem "{}" not found'.format(
                        args.project_name, args.project_ecosystem)
                },
                400,
            )

        try:
            distro = models.Distro.query.filter(
                func.lower(models.Distro.name) == func.lower(
                    args.distribution)).one()
        except NoResultFound:
            return (
                {
                    "error":
                    'Distribution "{}" not found'.format(args.distribution)
                },
                400,
            )

        try:
            package = models.Packages(distro_name=distro.name,
                                      project=project,
                                      package_name=args.package_name)

            Session.add(package)
            Session.commit()

            message = dict(
                agent=flask_login.current_user.email,
                project=project.name,
                distro=distro.name,
                new=package.package_name,
            )
            utilities.log(
                Session,
                project=project.__json__(),
                distro=distro.__json__(),
                topic="project.map.new",
                message=message,
            )
            return {
                "distribution": distro.name,
                "name": package.package_name
            }, 201
        except IntegrityError:
            Session.rollback()
            return {"error": "package already exists in distribution"}, 409