|
45 | 45 | VERSION_TYPES,
|
46 | 46 | )
|
47 | 47 | from readthedocs.builds.managers import (
|
| 48 | + AutomationRuleMatchManager, |
48 | 49 | BuildManager,
|
49 | 50 | ExternalBuildManager,
|
50 | 51 | ExternalVersionManager,
|
@@ -1045,18 +1046,24 @@ def get_match_arg(self):
|
1045 | 1046 | )
|
1046 | 1047 | return match_arg or self.match_arg
|
1047 | 1048 |
|
1048 |
| - def run(self, version, *args, **kwargs): |
| 1049 | + def run(self, version, **kwargs): |
1049 | 1050 | """
|
1050 | 1051 | Run an action if `version` matches the rule.
|
1051 | 1052 |
|
1052 | 1053 | :type version: readthedocs.builds.models.Version
|
1053 | 1054 | :returns: True if the action was performed
|
1054 | 1055 | """
|
1055 |
| - if version.type == self.version_type: |
1056 |
| - match, result = self.match(version, self.get_match_arg()) |
1057 |
| - if match: |
1058 |
| - self.apply_action(version, result) |
1059 |
| - return True |
| 1056 | + if version.type != self.version_type: |
| 1057 | + return False |
| 1058 | + |
| 1059 | + match, result = self.match(version, self.get_match_arg()) |
| 1060 | + if match: |
| 1061 | + self.apply_action(version, result) |
| 1062 | + AutomationRuleMatch.objects.register_match( |
| 1063 | + rule=self, |
| 1064 | + version=version, |
| 1065 | + ) |
| 1066 | + return True |
1060 | 1067 | return False
|
1061 | 1068 |
|
1062 | 1069 | def match(self, version, match_arg):
|
@@ -1239,3 +1246,29 @@ def get_edit_url(self):
|
1239 | 1246 | 'projects_automation_rule_regex_edit',
|
1240 | 1247 | args=[self.project.slug, self.pk],
|
1241 | 1248 | )
|
| 1249 | + |
| 1250 | + |
| 1251 | +class AutomationRuleMatch(TimeStampedModel): |
| 1252 | + rule = models.ForeignKey( |
| 1253 | + VersionAutomationRule, |
| 1254 | + verbose_name=_('Matched rule'), |
| 1255 | + related_name='matches', |
| 1256 | + on_delete=models.CASCADE, |
| 1257 | + ) |
| 1258 | + |
| 1259 | + # Metadata from when the match happened. |
| 1260 | + version_name = models.CharField(max_length=255) |
| 1261 | + match_arg = models.CharField(max_length=255) |
| 1262 | + action = models.CharField( |
| 1263 | + max_length=255, |
| 1264 | + choices=VersionAutomationRule.ACTIONS, |
| 1265 | + ) |
| 1266 | + version_type = models.CharField( |
| 1267 | + max_length=32, |
| 1268 | + choices=VERSION_TYPES, |
| 1269 | + ) |
| 1270 | + |
| 1271 | + objects = AutomationRuleMatchManager() |
| 1272 | + |
| 1273 | + class Meta: |
| 1274 | + ordering = ('-modified', '-created') |
0 commit comments