-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Automation Rules: support external versions #7664
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
Open
stsewd
wants to merge
1
commit into
main
Choose a base branch
from
automation-rules-pr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -1,5 +1,6 @@ | ||||
"""Django forms for the builds app.""" | ||||
|
||||
import itertools | ||||
import re | ||||
import textwrap | ||||
|
||||
|
@@ -14,6 +15,8 @@ | |||
ALL_VERSIONS, | ||||
BRANCH, | ||||
BRANCH_TEXT, | ||||
EXTERNAL, | ||||
EXTERNAL_TEXT, | ||||
TAG, | ||||
TAG_TEXT, | ||||
) | ||||
|
@@ -99,8 +102,9 @@ class RegexAutomationRuleForm(forms.ModelForm): | |||
""" | ||||
A regular expression to match the version. | ||||
<a href="https://docs.readthedocs.io/page/automation-rules.html#user-defined-matches"> | ||||
Check the documentation for valid patterns. | ||||
Check the documentation | ||||
</a> | ||||
for valid patterns. | ||||
""" | ||||
)), | ||||
required=False, | ||||
|
@@ -113,6 +117,7 @@ class Meta: | |||
'predefined_match_arg', | ||||
'match_arg', | ||||
'version_type', | ||||
'action_arg', | ||||
'action', | ||||
] | ||||
# Don't pollute the UI with help texts | ||||
|
@@ -133,6 +138,7 @@ def __init__(self, *args, **kwargs): | |||
(None, '-' * 9), | ||||
(BRANCH, BRANCH_TEXT), | ||||
(TAG, TAG_TEXT), | ||||
(EXTERNAL, EXTERNAL_TEXT), | ||||
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. Pretty sure we aren't meant to show this to users. I think we want
|
||||
] | ||||
|
||||
# Remove privacy actions not available in community | ||||
|
@@ -155,6 +161,48 @@ def __init__(self, *args, **kwargs): | |||
if self.instance.pk and self.instance.predefined_match_arg: | ||||
self.initial['match_arg'] = self.instance.get_match_arg() | ||||
|
||||
def clean_action(self): | ||||
"""Check the action is allowed for the type of version.""" | ||||
action = self.cleaned_data['action'] | ||||
version_type = self.cleaned_data['version_type'] | ||||
internal_allowed_actions = set( | ||||
itertools.chain( | ||||
VersionAutomationRule.allowed_actions_on_create.keys(), | ||||
VersionAutomationRule.allowed_actions_on_delete.keys(), | ||||
) | ||||
) | ||||
allowed_actions = { | ||||
BRANCH: internal_allowed_actions, | ||||
TAG: internal_allowed_actions, | ||||
EXTERNAL: set(VersionAutomationRule.allowed_actions_on_external_versions.keys()), | ||||
} | ||||
if action not in allowed_actions.get(version_type, []): | ||||
raise forms.ValidationError( | ||||
_('Invalid action for this version type.'), | ||||
) | ||||
return action | ||||
|
||||
def clean_action_arg(self): | ||||
""" | ||||
Validate the action argument. | ||||
|
||||
Currently only external versions accept this argument, | ||||
and it's the pattern to match the base_branch. | ||||
""" | ||||
action_arg = self.cleaned_data['action_arg'] | ||||
version_type = self.cleaned_data['version_type'] | ||||
if version_type == EXTERNAL: | ||||
if not action_arg: | ||||
action_arg = '.*' | ||||
try: | ||||
re.compile(action_arg) | ||||
return action_arg | ||||
except Exception: | ||||
raise forms.ValidationError( | ||||
_('Invalid Python regular expression.'), | ||||
) | ||||
return '' | ||||
|
||||
def clean_match_arg(self): | ||||
"""Check that a custom match was given if a predefined match wasn't used.""" | ||||
match_arg = self.cleaned_data['match_arg'] | ||||
|
@@ -185,6 +233,7 @@ def save(self, commit=True): | |||
predefined_match_arg=self.cleaned_data['predefined_match_arg'], | ||||
version_type=self.cleaned_data['version_type'], | ||||
action=self.cleaned_data['action'], | ||||
action_arg=self.cleaned_data['action_arg'], | ||||
) | ||||
if not rule.description: | ||||
rule.description = rule.get_description() | ||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
readthedocs/builds/migrations/0029_add_build_external_version_action.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 2.2.16 on 2020-11-16 17:37 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('builds', '0028_add_delete_version_action'), | ||
] | ||
|
||
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 branch/tag deletion)'), ('build-external-version', 'Build version')], help_text='Action to apply to matching versions', max_length=32, verbose_name='Action'), | ||
), | ||
] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is super useful, and we should probably be promoting this more.