Skip to content

Automation rules: add delete version action #7644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/automation-rules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ Currently, the following actions are available:
i.e. the version of your project that `/` redirects to.
See more in :ref:`automatic-redirects:Root URL`.
It also activates and builds the version.
- **Delete version**: When a branch or tag is deleted from your repository,
Read the Docs will delete it *only if isn't active*.
This action allows you to delete *active* versions when a branch or tag is deleted from your repository.

.. note::

Expand All @@ -69,6 +72,12 @@ Currently, the following actions are available:
The stable version is also automatically updated at the same time.
See more in :doc:`versions`.

.. note::

Default versions aren't deleted even if they match a rule.
You can use the ``Set version as default`` action to change the default version
before deleting the current one.

Order
-----

Expand Down Expand Up @@ -99,6 +108,13 @@ Activate only new branches that belong to the ``1.x`` release
- Version type: ``Branch``
- Action: ``Activate version``

Automatically delete an active version of a deleted branch
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- Match: ``Any version``
- Version type: ``Branch``
- Action: ``Delete version``

Set as default new tags that have the ``-stable`` or ``-release`` suffix
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
39 changes: 27 additions & 12 deletions readthedocs/api/v2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
STABLE_VERBOSE_NAME,
TAG,
)
from readthedocs.builds.models import Version
from readthedocs.builds.models import RegexAutomationRule, Version

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -140,7 +140,12 @@ def _set_or_create_version(project, slug, version_id, verbose_name, type_):


def delete_versions_from_db(project, version_data):
"""Delete all versions not in the current repo."""
"""
Delete all versions not in the current repo.

:returns: The slug of the deleted versions from the database,
and the slug of active versions that where deleted from the repository.
"""
# We use verbose_name for tags
# because several tags can point to the same identifier.
versions_tags = [
Expand All @@ -153,7 +158,6 @@ def delete_versions_from_db(project, version_data):
to_delete_qs = (
project.versions
.exclude(uploaded=True)
.exclude(active=True)
.exclude(slug__in=NON_REPOSITORY_VERSIONS)
)

Expand All @@ -166,18 +170,23 @@ def delete_versions_from_db(project, version_data):
identifier__in=versions_branches,
)

ret_val = set(to_delete_qs.values_list('slug', flat=True))
if ret_val:
deleted_active_versions = set(
to_delete_qs.filter(active=True).values_list('slug', flat=True)
)

to_delete_qs = to_delete_qs.exclude(active=True)
deleted_versions = set(to_delete_qs.values_list('slug', flat=True))
if deleted_versions:
log.info(
'(Sync Versions) Deleted Versions: project=%s, versions=[%s]',
project.slug, ' '.join(ret_val),
project.slug, ' '.join(deleted_versions),
)
to_delete_qs.delete()

return ret_val
return deleted_versions, deleted_active_versions


def run_automation_rules(project, versions_slug):
def run_automation_rules(project, added_versions, deleted_active_versions):
"""
Runs the automation rules on each version.

Expand All @@ -188,10 +197,16 @@ def run_automation_rules(project, versions_slug):
Currently the versions aren't sorted in any way,
the same order is keeped.
"""
versions = project.versions.filter(slug__in=versions_slug)
rules = project.automation_rules.all()
for version, rule in itertools.product(versions, rules):
rule.run(version)
class_ = RegexAutomationRule
actions = [
(added_versions, class_.allowed_actions_on_create),
(deleted_active_versions, class_.allowed_actions_on_delete),
]
for versions_slug, allowed_actions in actions:
versions = project.versions.filter(slug__in=versions_slug)
rules = project.automation_rules.filter(action__in=allowed_actions)
for version, rule in itertools.product(versions, rules):
rule.run(version)


class RemoteOrganizationPagination(PageNumberPagination):
Expand Down
4 changes: 2 additions & 2 deletions readthedocs/api/v2/views/model_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def sync_versions(self, request, **kwargs): # noqa: D205
type=BRANCH,
)
added_versions.update(ret_set)
deleted_versions = delete_versions_from_db(project, data)
deleted_versions, deleted_active_versions = delete_versions_from_db(project, data)
except Exception as e:
log.exception('Sync Versions Error')
return Response(
Expand All @@ -233,7 +233,7 @@ def sync_versions(self, request, **kwargs): # noqa: D205
# The order of added_versions isn't deterministic.
# We don't track the commit time or any other metadata.
# We usually have one version added per webhook.
run_automation_rules(project, added_versions)
run_automation_rules(project, added_versions, deleted_active_versions)
except Exception:
# Don't interrupt the request if something goes wrong
# in the automation rules.
Expand Down
7 changes: 7 additions & 0 deletions readthedocs/builds/automation_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,10 @@ def set_private_privacy_level(version, match_result, action_arg, *args, **kwargs
"""Sets the privacy_level of the version to private."""
version.privacy_level = PRIVATE
version.save()


def delete_version(version, match_result, action_arg, *args, **kwargs):
"""Delete a version if isn't marked as the default version."""
if version.project.default_version == version.slug:
return
version.delete()
18 changes: 18 additions & 0 deletions readthedocs/builds/migrations/0028_add_delete_version_action.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.16 on 2020-11-05 19:26

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('builds', '0027_add_privacy_level_automation_rules'),
]

operations = [
migrations.AlterField(
model_name='versionautomationrule',
name='action',
field=models.CharField(choices=[('activate-version', 'Activate version'), ('hide-version', 'Hide version'), ('make-version-public', 'Make version public'), ('make-version-private', 'Make version private'), ('set-default-version', 'Set version as default'), ('delete-version', 'Delete version (on deletion)')], help_text='Action to apply to matching versions', max_length=32, verbose_name='Action'),
),
]
18 changes: 15 additions & 3 deletions readthedocs/builds/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,7 @@ class VersionAutomationRule(PolymorphicModel, TimeStampedModel):
"""Versions automation rules for projects."""

ACTIVATE_VERSION_ACTION = 'activate-version'
DELETE_VERSION_ACTION = 'delete-version'
HIDE_VERSION_ACTION = 'hide-version'
MAKE_VERSION_PUBLIC_ACTION = 'make-version-public'
MAKE_VERSION_PRIVATE_ACTION = 'make-version-private'
Expand All @@ -958,8 +959,12 @@ class VersionAutomationRule(PolymorphicModel, TimeStampedModel):
(MAKE_VERSION_PUBLIC_ACTION, _('Make version public')),
(MAKE_VERSION_PRIVATE_ACTION, _('Make version private')),
(SET_DEFAULT_VERSION_ACTION, _('Set version as default')),
(DELETE_VERSION_ACTION, _('Delete version (on deletion)')),
)

allowed_actions_on_create = {}
allowed_actions_on_delete = {}

project = models.ForeignKey(
Project,
related_name='automation_rules',
Expand Down Expand Up @@ -1052,14 +1057,17 @@ def match(self, version, match_arg):

def apply_action(self, version, match_result):
"""
Apply the action from allowed_actions.
Apply the action from allowed_actions_on_*.

:type version: readthedocs.builds.models.Version
:param any match_result: Additional context from the match operation
:raises: NotImplementedError if the action
isn't implemented or supported for this rule.
"""
action = self.allowed_actions.get(self.action)
action = (
self.allowed_actions_on_create.get(self.action)
or self.allowed_actions_on_delete.get(self.action)
)
if action is None:
raise NotImplementedError
action(version, match_result, self.action_arg)
Expand Down Expand Up @@ -1166,14 +1174,18 @@ class RegexAutomationRule(VersionAutomationRule):

TIMEOUT = 1 # timeout in seconds

allowed_actions = {
allowed_actions_on_create = {
VersionAutomationRule.ACTIVATE_VERSION_ACTION: actions.activate_version,
VersionAutomationRule.HIDE_VERSION_ACTION: actions.hide_version,
VersionAutomationRule.MAKE_VERSION_PUBLIC_ACTION: actions.set_public_privacy_level,
VersionAutomationRule.MAKE_VERSION_PRIVATE_ACTION: actions.set_private_privacy_level,
VersionAutomationRule.SET_DEFAULT_VERSION_ACTION: actions.set_default_version,
}

allowed_actions_on_delete = {
VersionAutomationRule.DELETE_VERSION_ACTION: actions.delete_version,
}

class Meta:
proxy = True

Expand Down
50 changes: 49 additions & 1 deletion readthedocs/rtd_tests/tests/test_automation_rules.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from unittest import mock

import pytest
from django_dynamic_fixture import get

Expand All @@ -9,12 +10,12 @@
SEMVER_VERSIONS,
TAG,
)
from readthedocs.projects.constants import PUBLIC, PRIVATE
from readthedocs.builds.models import (
RegexAutomationRule,
Version,
VersionAutomationRule,
)
from readthedocs.projects.constants import PRIVATE, PUBLIC
from readthedocs.projects.models import Project


Expand Down Expand Up @@ -183,6 +184,53 @@ def test_action_activation(self, trigger_build):
assert version.active is True
trigger_build.assert_called_once()

@pytest.mark.parametrize('version_type', [BRANCH, TAG])
def test_action_delete_version(self, version_type):
slug = 'delete-me'
version = get(
Version,
slug=slug,
verbose_name=slug,
project=self.project,
active=True,
type=version_type,
)
rule = get(
RegexAutomationRule,
project=self.project,
priority=0,
match_arg='.*',
action=VersionAutomationRule.DELETE_VERSION_ACTION,
version_type=version_type,
)
assert rule.run(version) is True
assert not self.project.versions.filter(slug=slug).exists()

@pytest.mark.parametrize('version_type', [BRANCH, TAG])
def test_action_delete_version_on_default_version(self, version_type):
slug = 'delete-me'
version = get(
Version,
slug=slug,
verbose_name=slug,
project=self.project,
active=True,
type=version_type,
)
self.project.default_version = slug
self.project.save()

rule = get(
RegexAutomationRule,
project=self.project,
priority=0,
match_arg='.*',
action=VersionAutomationRule.DELETE_VERSION_ACTION,
version_type=version_type,
)
assert rule.run(version) is True
assert self.project.versions.filter(slug=slug).exists()

def test_action_set_default_version(self):
version = get(
Version,
Expand Down
72 changes: 68 additions & 4 deletions readthedocs/rtd_tests/tests/test_sync_versions.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# -*- coding: utf-8 -*-

import json
from unittest import mock

from django.test import TestCase
from django.urls import reverse

from readthedocs.builds.constants import BRANCH, STABLE, TAG, LATEST
from readthedocs.builds.constants import BRANCH, LATEST, STABLE, TAG
from readthedocs.builds.models import (
RegexAutomationRule,
Version,
Expand Down Expand Up @@ -774,7 +772,9 @@ def test_automation_rules_are_triggered_for_new_versions(self, run_automation_ru
content_type='application/json',
)
run_automation_rules.assert_called_with(
self.pip, {'new_branch', 'new_tag'}
self.pip,
{'new_branch', 'new_tag'},
{'0.8', '0.8.1'},
)

def test_automation_rule_activate_version(self):
Expand Down Expand Up @@ -837,6 +837,70 @@ def test_automation_rule_set_default_version(self):
self.pip.refresh_from_db()
self.assertEqual(self.pip.get_default_version(), 'new_tag')

def test_automation_rule_delete_version(self):
version_post_data = {
'tags': [
{
'identifier': 'new_tag',
'verbose_name': 'new_tag',
},
{
'identifier': '0.8.3',
'verbose_name': '0.8.3',
},
],
}
version_slug = '0.8'
RegexAutomationRule.objects.create(
project=self.pip,
priority=0,
match_arg=r'^0\.8$',
action=VersionAutomationRule.DELETE_VERSION_ACTION,
version_type=TAG,
)
version = self.pip.versions.get(slug=version_slug)
self.assertTrue(version.active)

self.client.post(
reverse('project-sync-versions', args=[self.pip.pk]),
data=json.dumps(version_post_data),
content_type='application/json',
)
self.assertFalse(self.pip.versions.filter(slug=version_slug).exists())

def test_automation_rule_dont_delete_default_version(self):
version_post_data = {
'tags': [
{
'identifier': 'new_tag',
'verbose_name': 'new_tag',
},
{
'identifier': '0.8.3',
'verbose_name': '0.8.3',
},
],
}
version_slug = '0.8'
RegexAutomationRule.objects.create(
project=self.pip,
priority=0,
match_arg=r'^0\.8$',
action=VersionAutomationRule.DELETE_VERSION_ACTION,
version_type=TAG,
)
version = self.pip.versions.get(slug=version_slug)
self.assertTrue(version.active)

self.pip.default_version = version_slug
self.pip.save()

self.client.post(
reverse('project-sync-versions', args=[self.pip.pk]),
data=json.dumps(version_post_data),
content_type='application/json',
)
self.assertTrue(self.pip.versions.filter(slug=version_slug).exists())

class TestStableVersion(TestCase):
fixtures = ['eric', 'test_data']
Expand Down