-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Add models for automation rules #5323
Changes from 18 commits
2ae80d1
d0e04e0
d53bad2
d287286
c5e6010
cb5e1b9
4fd56f6
e570a58
353bc8a
7a60668
409e960
8f58e77
8dfe290
0d4ab95
74f1b9a
7936f55
4c77043
11add5d
5e90f62
0903763
26fc59b
7660b86
a2c50ac
0f06958
bf2f954
73030c0
6237ca4
25faa28
eda1cd2
39c8cc5
a6d959d
2ae831d
78e6e62
5d0c706
b2a1339
b07b660
a86d427
cbce26a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -711,8 +711,10 @@ class VersionAutomationRule(PolymorphicModel, TimeStampedModel): | |
"""Versions automation rules for projects.""" | ||
|
||
ACTIVATE_VERSION_ACTION = 'activate-version' | ||
SET_DEFAULT_VERSION_ACTION = 'set-default-version' | ||
ACTIONS = ( | ||
(ACTIVATE_VERSION_ACTION, _('Activate version on match')), | ||
stsewd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(SET_DEFAULT_VERSION_ACTION, _('Set as default version on match')), | ||
) | ||
|
||
project = models.ForeignKey( | ||
|
@@ -759,37 +761,36 @@ def run(self, version, *args, **kwargs): | |
:returns: True if the action was performed | ||
""" | ||
if version.type == self.version_type: | ||
match_result = self.match(version, self.match_arg) | ||
if match_result is not None: | ||
self.apply_action(version, match_result, self.action_arg) | ||
match, result = self.match(version, self.match_arg) | ||
if match: | ||
self.apply_action(version, result) | ||
return True | ||
return False | ||
|
||
def match(self, version, match_arg): | ||
""" | ||
Returns an object different from None if the version matches the rule. | ||
Returns True and the match result if the version matches the rule. | ||
|
||
:type version: readthedocs.builds.models.Version | ||
:param str match_arg: Additional argument to perform the match | ||
:returns: An object different from `None` if the match is successful. | ||
:returns: A tuple of (boolean, match_resul). | ||
The result will be passed to `apply_action`. | ||
""" | ||
return None | ||
return False, None | ||
|
||
def apply_action(self, version, match_result, action_arg): | ||
def apply_action(self, version, match_result): | ||
""" | ||
Apply the action from allowed_actions. | ||
|
||
:type version: readthedocs.builds.models.Version | ||
:param any match_result: Additional context from the match operation | ||
:param str action_arg: Additional argument to perform the action | ||
:raises: NotImplementedError if the action | ||
isn't implemented or supported for this rule. | ||
""" | ||
action = self.allowed_actions.get(self.action) | ||
if action is None: | ||
raise NotImplementedError | ||
action(version, match_result, action_arg) | ||
action(version, match_result, self.action_arg) | ||
|
||
def __str__(self): | ||
class_name = self.__class__.__name__ | ||
|
@@ -804,6 +805,7 @@ class RegexAutomationRule(VersionAutomationRule): | |
|
||
allowed_actions = { | ||
VersionAutomationRule.ACTIVATE_VERSION_ACTION: actions.activate_version, | ||
VersionAutomationRule.SET_DEFAULT_VERSION_ACTION: actions.set_default_version, | ||
} | ||
|
||
class Meta: | ||
|
@@ -814,7 +816,7 @@ def match(self, version, match_arg): | |
match = re.search( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd say that what we want here is
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For me the result of fullmatch is unexpected. In regex I always need to start with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
You are an advanced user :) I'm sure that many people will end up matching versions that they don't want to match. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The thing is that this should not be exposed to general users, so we need all the power of regex internally more than exposing this to users. |
||
match_arg, version.verbose_name | ||
) | ||
return match | ||
return bool(match), match | ||
except Exception as e: | ||
log.info('Error parsing regex: %s', e) | ||
return None | ||
return False, None |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
from django.shortcuts import get_object_or_404 | ||
from django.template.loader import render_to_string | ||
from rest_framework import decorators, permissions, status, viewsets | ||
from rest_framework.parsers import MultiPartParser, JSONParser, FormParser | ||
from rest_framework.parsers import JSONParser, MultiPartParser | ||
from rest_framework.renderers import BaseRenderer, JSONRenderer | ||
from rest_framework.response import Response | ||
|
||
|
@@ -215,6 +215,17 @@ def sync_versions(self, request, **kwargs): # noqa: D205 | |
status=status.HTTP_400_BAD_REQUEST, | ||
) | ||
|
||
try: | ||
api_utils.run_automation_rules(project, added_versions) | ||
stsewd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
except Exception: | ||
# Don't interrupt the request if something goes wrong | ||
# in the automation rules. | ||
log.exception( | ||
'Failed to execute automation rules for [%s]: %s', | ||
project.slug, added_versions | ||
) | ||
|
||
# TODO: move this to an automation rule | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted to move this to the |
||
promoted_version = project.update_stable_version() | ||
new_stable = project.get_stable_version() | ||
if promoted_version and new_stable and new_stable.active: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I said in another comment, I'd remove everything that it's not needed at this point:
match_result
andaction_arg
here.If we want to include them in this PR, I think it would be good to have the use case in this PR also otherwise we are adding code that it's not useful and only complicate things.