-
-
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
Add models for automation rules #5323
Conversation
readthedocs/builds/models.py
Outdated
max_length=32, | ||
choices=RULE_TYPES, | ||
) | ||
rule_arg = models.CharField( |
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.
Should this be rule_args
? I can only think of one arg for the rule.
max_length=32, | ||
choices=ACTIONS, | ||
) | ||
action_arg = models.CharField( |
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.
Same here, but I can think of some cases where we could need more than one. But, this also could be solved by creating several rules with different action_arg.
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.
How is this action_arg
used from a user perspective and what is it useful for?
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.
Any extra option needed to execute the action. Like, I want to activate the version and set the privacy to public/private. But I can think this can be solved by creating two rules. First activate version v
and a second one set version v
to public/private.
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.
I'd say that we should keep this simple for now.
If 2 actions are needed for a specific version (match rule), two entries should be created. I'd remove the action_arg
for now, and add it later if we find that we need it.
readthedocs/builds/models.py
Outdated
return self.apply_action(version, match_result, self.action_arg) | ||
return False | ||
|
||
def match(self, version, arg): |
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.
I'm thinking is passing *args
and **kwargs
to match and apply_action, still not sure. And maybe select a better name for arg
. Or just use self.rule_arg
, but this makes it easier to test :)
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.
I think it could be better to use self.rule_arg
inside the method because it will more clear when reading the code and it's a common pattern.
|
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 good. I like the pattern designed here.
readthedocs/builds/models.py
Outdated
proxy = True | ||
|
||
def match(self, version, arg): | ||
regex = re.compile(arg) |
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.
There is no need to compile the regex if it will be used just once. You can just call re.match
directly.
readthedocs/builds/models.py
Outdated
|
||
def match(self, version, arg): | ||
""" | ||
Returns an object different from None if the version matches the rule. |
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.
I think it's better to return True
if it matches and False
if there is no matching.
What is the match_result
useful for?
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.
Actually, in case that match_result
would be useful for something I'd suggest to have a specific method for that and leave match
returning a boolean.
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.
We need to pass the match result, in case of regex, that would be used to capture the groups in a later phase of the feature.
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.
How exactly that "later phase of the feature" will use it?
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.
I mean, I don't see how the match result will be useful from a user's perspective. Or, how the user will have access to it.
In case we need it internally, how are we going to us it?
max_length=32, | ||
choices=ACTIONS, | ||
) | ||
action_arg = models.CharField( |
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.
How is this action_arg
used from a user perspective and what is it useful for?
readthedocs/builds/models.py
Outdated
return self.apply_action(version, match_result, self.action_arg) | ||
return False | ||
|
||
def match(self, version, arg): |
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.
I think it could be better to use self.rule_arg
inside the method because it will more clear when reading the code and it's a common pattern.
@@ -0,0 +1,2 @@ | |||
def activate_version_from_regex_action(version, match_result, arg): |
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.
Is it needed to be specific here on from_regex_action
?
I suppose that this function should be just like version.active = True
and version.save()
. No need to use match_result
nor arg
here.
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.
All function actions will receive all the parameters. Match result and arg was explained above ^
For the moment I'm going to assume that having separate rules is the way to go instead of having extra parameters (only for the names, models will still have that till final decision). /cc @agjohnson |
@@ -48,7 +56,6 @@ def version_name(self, obj): | |||
|
|||
|
|||
class VersionAdmin(GuardedModelAdmin): | |||
search_fields = ('slug', 'project__name') |
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 was repeated below
This is ready for review, consider #5323 (comment) This is how the admin looks like, we can expand it with search and other stuff as we need them. |
I agree with this. Also, it may worth to find other services that allow these kind of rules and see how they handle this case. Docker Hub for example, does something simple where you write a regex rule to be tagged as a Docker tag, like |
It wasn't so much code after all, need to write tests though |
project.slug, added_versions | ||
) | ||
|
||
# TODO: move this to an automation rule |
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.
I wanted to move this to the run_automation_rules
function, but it depends on the previous state of one version. It can be refactored for later
Test are written 📖 and as usual they helped me to fix bugs on time :) |
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.
💯
I suppose that at this point, I will have to accept the usage of re.search
;)
👍 |
@stsewd I think this PR is ready to merge. Can you please resolve the conflicts so we can merge it? |
Done 👍 |
Still wip, I would like some input. I want to use the same pattern that we use for subclassing Integrations. Also, not sure if the model is fine in the
builds
app or if I should create a new app. I'm subclassing regex as a simple example for now.Ref #4001