14
14
STABLE_VERBOSE_NAME ,
15
15
TAG ,
16
16
)
17
- from readthedocs .builds .models import Version
17
+ from readthedocs .builds .models import RegexAutomationRule , Version
18
18
19
19
log = logging .getLogger (__name__ )
20
20
@@ -140,7 +140,12 @@ def _set_or_create_version(project, slug, version_id, verbose_name, type_):
140
140
141
141
142
142
def delete_versions_from_db (project , version_data ):
143
- """Delete all versions not in the current repo."""
143
+ """
144
+ Delete all versions not in the current repo.
145
+
146
+ :returns: The slug of the deleted versions from the database,
147
+ and the slug of active versions that where deleted from the repository.
148
+ """
144
149
# We use verbose_name for tags
145
150
# because several tags can point to the same identifier.
146
151
versions_tags = [
@@ -153,7 +158,6 @@ def delete_versions_from_db(project, version_data):
153
158
to_delete_qs = (
154
159
project .versions
155
160
.exclude (uploaded = True )
156
- .exclude (active = True )
157
161
.exclude (slug__in = NON_REPOSITORY_VERSIONS )
158
162
)
159
163
@@ -166,18 +170,23 @@ def delete_versions_from_db(project, version_data):
166
170
identifier__in = versions_branches ,
167
171
)
168
172
169
- ret_val = set (to_delete_qs .values_list ('slug' , flat = True ))
170
- if ret_val :
173
+ deleted_active_versions = set (
174
+ to_delete_qs .filter (active = True ).values_list ('slug' , flat = True )
175
+ )
176
+
177
+ to_delete_qs = to_delete_qs .exclude (active = True )
178
+ deleted_versions = set (to_delete_qs .values_list ('slug' , flat = True ))
179
+ if deleted_versions :
171
180
log .info (
172
181
'(Sync Versions) Deleted Versions: project=%s, versions=[%s]' ,
173
- project .slug , ' ' .join (ret_val ),
182
+ project .slug , ' ' .join (deleted_versions ),
174
183
)
175
184
to_delete_qs .delete ()
176
185
177
- return ret_val
186
+ return deleted_versions , deleted_active_versions
178
187
179
188
180
- def run_automation_rules (project , versions_slug ):
189
+ def run_automation_rules (project , added_versions , deleted_active_versions ):
181
190
"""
182
191
Runs the automation rules on each version.
183
192
@@ -188,10 +197,16 @@ def run_automation_rules(project, versions_slug):
188
197
Currently the versions aren't sorted in any way,
189
198
the same order is keeped.
190
199
"""
191
- versions = project .versions .filter (slug__in = versions_slug )
192
- rules = project .automation_rules .all ()
193
- for version , rule in itertools .product (versions , rules ):
194
- rule .run (version )
200
+ class_ = RegexAutomationRule
201
+ actions = [
202
+ (added_versions , class_ .allowed_actions_on_create ),
203
+ (deleted_active_versions , class_ .allowed_actions_on_delete ),
204
+ ]
205
+ for versions_slug , allowed_actions in actions :
206
+ versions = project .versions .filter (slug__in = versions_slug )
207
+ rules = project .automation_rules .filter (action__in = allowed_actions )
208
+ for version , rule in itertools .product (versions , rules ):
209
+ rule .run (version )
195
210
196
211
197
212
class RemoteOrganizationPagination (PageNumberPagination ):
0 commit comments