@@ -812,45 +812,88 @@ def raw_permalink(raw_link):
812
812
813
813
@dev_group .command ('trim-version-lock' )
814
814
@click .argument ('stack_version' )
815
+ @click .option ('--skip-rule-updates' , is_flag = True , help = 'Skip updating the rules' )
815
816
@click .option ('--dry-run' , is_flag = True , help = 'Print the changes rather than saving the file' )
816
- def trim_version_lock (stack_version : str , dry_run : bool ):
817
+ def trim_version_lock (stack_version : str , skip_rule_updates : bool , dry_run : bool ):
817
818
"""Trim all previous entries within the version lock file which are lower than the min_version."""
818
819
stack_versions = get_stack_versions ()
819
820
assert stack_version in stack_versions , \
820
821
f'Unknown min_version ({ stack_version } ), expected: { ", " .join (stack_versions )} '
821
822
822
823
min_version = Version .parse (stack_version )
823
824
version_lock_dict = default_version_lock .version_lock .to_dict ()
824
- removed = {}
825
+ removed = defaultdict (list )
826
+ rule_msv_drops = []
827
+
828
+ today = time .strftime ('%Y/%m/%d' )
829
+ rc : RuleCollection | None = None
830
+ if dry_run :
831
+ rc = RuleCollection ()
832
+ else :
833
+ if not skip_rule_updates :
834
+ click .echo ('Loading rules ...' )
835
+ rc = RuleCollection .default ()
825
836
826
837
for rule_id , lock in version_lock_dict .items ():
838
+ file_min_stack : Version | None = None
839
+ if 'min_stack_version' in lock :
840
+ file_min_stack = Version .parse ((lock ['min_stack_version' ]), optional_minor_and_patch = True )
841
+ if file_min_stack <= min_version :
842
+ removed [rule_id ].append (
843
+ f'locked min_stack_version <= { min_version } - { "will remove" if dry_run else "removing" } !'
844
+ )
845
+ rule_msv_drops .append (rule_id )
846
+ file_min_stack = None
847
+
848
+ if not dry_run :
849
+ lock .pop ('min_stack_version' )
850
+ if not skip_rule_updates :
851
+ # remove the min_stack_version and min_stack_comments from rules as well (and update date)
852
+ rule = rc .id_map .get (rule_id )
853
+ if rule :
854
+ new_meta = dataclasses .replace (
855
+ rule .contents .metadata ,
856
+ updated_date = today ,
857
+ min_stack_version = None ,
858
+ min_stack_comments = None
859
+ )
860
+ contents = dataclasses .replace (rule .contents , metadata = new_meta )
861
+ new_rule = TOMLRule (contents = contents , path = rule .path )
862
+ new_rule .save_toml ()
863
+ removed [rule_id ].append ('rule min_stack_version dropped' )
864
+ else :
865
+ removed [rule_id ].append ('rule not found to update!' )
866
+
827
867
if 'previous' in lock :
828
868
prev_vers = [Version .parse (v , optional_minor_and_patch = True ) for v in list (lock ['previous' ])]
829
- outdated_vers = [f" { v . major } . { v . minor } " for v in prev_vers if v < min_version ]
869
+ outdated_vers = [v for v in prev_vers if v < min_version ]
830
870
831
871
if not outdated_vers :
832
872
continue
833
873
834
874
# we want to remove all "old" versions, but save the latest that is >= the min version supplied as the new
835
875
# stack_version.
876
+ latest_version = max (outdated_vers )
836
877
837
- if dry_run :
838
- outdated_minus_current = [str (v ) for v in outdated_vers if v < stack_version ]
839
- if outdated_minus_current :
840
- removed [rule_id ] = outdated_minus_current
841
878
for outdated in outdated_vers :
842
- popped = lock ['previous' ].pop (str (outdated ))
843
- if outdated >= stack_version :
844
- lock ['previous' ][str (Version (stack_version [:2 ]))] = popped
879
+ short_outdated = f"{ outdated .major } .{ outdated .minor } "
880
+ popped = lock ['previous' ].pop (str (short_outdated ))
881
+ # the core of the update - we only need to keep previous entries that are newer than the min supported
882
+ # version (from stack-schema-map and stack-version parameter) and older than the locked
883
+ # min_stack_version for a given rule, if one exists
884
+ if file_min_stack and outdated == latest_version and outdated < file_min_stack :
885
+ lock ['previous' ][f'{ min_version .major } .{ min_version .minor } ' ] = popped
886
+ removed [rule_id ].append (f'{ short_outdated } updated to: { min_version .major } .{ min_version .minor } ' )
887
+ else :
888
+ removed [rule_id ].append (f'{ outdated } dropped' )
845
889
846
890
# remove the whole previous entry if it is now blank
847
891
if not lock ['previous' ]:
848
892
lock .pop ('previous' )
849
893
850
- if dry_run :
851
- click .echo (f'The following versions would be collapsed to { stack_version } :' if removed else 'No changes' )
852
- click .echo ('\n ' .join (f'{ k } : { ", " .join (v )} ' for k , v in removed .items ()))
853
- else :
894
+ click .echo (f'Changes { "that will be " if dry_run else "" } applied:' if removed else 'No changes' )
895
+ click .echo ('\n ' .join (f'{ k } : { ", " .join (v )} ' for k , v in removed .items ()))
896
+ if not dry_run :
854
897
new_lock = VersionLockFile .from_dict (dict (data = version_lock_dict ))
855
898
new_lock .save_to_file ()
856
899
0 commit comments