66from collections .abc import Iterable
77from datetime import timedelta
88from pathlib import Path
9- from typing import Union , Type
9+ from typing import Optional , Union , Type
1010
1111import click
1212import git
@@ -36,11 +36,17 @@ def sync_dependencies(
3636 pr_batch_size : int = 10 ,
3737 pause : timedelta = timedelta (seconds = 120 ),
3838 depfilter : str = "" ,
39+ template_version : Optional [str ] = None ,
3940) -> None :
4041 if not config .github_token :
4142 raise click .ClickException ("Can't continue, missing GitHub API token." )
4243
43- deptype_str = deptype .__name__ .lower ()
44+ if template_version is not None and not dry_run :
45+ click .secho (
46+ " > Custom template version provided for sync, but dry run not active. Forcing dry run" ,
47+ fg = "yellow" ,
48+ )
49+ dry_run = True
4450
4551 deps = read_dependency_list (dependency_list , depfilter )
4652 dep_count = len (deps )
@@ -49,38 +55,17 @@ def sync_dependencies(
4955 # Keep track of how many PRs we've created to better avoid running into rate limits
5056 update_count = 0
5157 for i , dn in enumerate (deps , start = 1 ):
52- click .secho (f"Synchronizing { dn } " , bold = True )
53- _ , dreponame = dn .split ("/" )
54- dname = dreponame .replace (f"{ deptype_str } -" , "" , 1 )
55-
56- # Clone dependency
57- try :
58- gr = gh .get_repo (dn )
59- except github .UnknownObjectException :
60- click .secho (f" > Repository { dn } doesn't exist, skipping..." , fg = "yellow" )
61- continue
62-
63- if gr .archived :
64- click .secho (f" > Repository { dn } is archived, skipping..." , fg = "yellow" )
65- continue
66-
67- d = deptype .clone (config , gr .clone_url , dname , version = gr .default_branch )
68-
69- if not (d .target_dir / ".cruft.json" ).is_file ():
70- click .echo (f" > Skipping repo { dn } which doesn't have `.cruft.json`" )
71- continue
72-
73- # Update the dependency
74- t = templater .from_existing (config , d .target_dir )
75- changed = t .update (
76- print_completion_message = False ,
77- commit = not dry_run ,
78- ignore_template_commit = True ,
58+ changed = sync_dependency (
59+ config ,
60+ gh ,
61+ dn ,
62+ deptype ,
63+ templater ,
64+ template_version ,
65+ dry_run ,
66+ pr_branch ,
67+ pr_label ,
7968 )
80-
81- # Create or update PR if there were updates
82- comment = render_pr_comment (d )
83- create_or_update_pr (d , dn , gr , changed , pr_branch , pr_label , dry_run , comment )
8469 if changed :
8570 update_count += 1
8671 if not dry_run and i < dep_count :
@@ -90,6 +75,56 @@ def sync_dependencies(
9075 _maybe_pause (update_count , pr_batch_size , pause )
9176
9277
78+ def sync_dependency (
79+ config : Config ,
80+ gh : github .Github ,
81+ depname : str ,
82+ deptype : Type [Union [Component , Package ]],
83+ templater ,
84+ template_version : Optional [str ],
85+ dry_run : bool ,
86+ pr_branch : str ,
87+ pr_label : Iterable [str ],
88+ ):
89+ deptype_str = deptype .__name__ .lower ()
90+
91+ click .secho (f"Synchronizing { depname } " , bold = True )
92+ _ , dreponame = depname .split ("/" )
93+ dname = dreponame .replace (f"{ deptype_str } -" , "" , 1 )
94+
95+ # Clone dependency
96+ try :
97+ gr = gh .get_repo (depname )
98+ except github .UnknownObjectException :
99+ click .secho (f" > Repository { depname } doesn't exist, skipping..." , fg = "yellow" )
100+ return
101+
102+ if gr .archived :
103+ click .secho (f" > Repository { depname } is archived, skipping..." , fg = "yellow" )
104+ return
105+
106+ d = deptype .clone (config , gr .clone_url , dname , version = gr .default_branch )
107+
108+ if not (d .target_dir / ".cruft.json" ).is_file ():
109+ click .echo (f" > Skipping repo { depname } which doesn't have `.cruft.json`" )
110+ return
111+
112+ # Update the dependency
113+ t = templater .from_existing (config , d .target_dir )
114+ if template_version is not None :
115+ t .template_version = template_version
116+ changed = t .update (
117+ print_completion_message = False ,
118+ commit = not dry_run ,
119+ ignore_template_commit = True ,
120+ )
121+
122+ # Create or update PR if there were updates
123+ comment = render_pr_comment (d )
124+ create_or_update_pr (d , depname , gr , changed , pr_branch , pr_label , dry_run , comment )
125+ return changed
126+
127+
93128def read_dependency_list (dependency_list : Path , depfilter : str ) -> list [str ]:
94129 try :
95130 deps = yaml_load (dependency_list )
0 commit comments