2020# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121# SOFTWARE.
2222
23+ """Module containing the sync tool implementation."""
24+
2325from fnmatch import filter
2426import os
2527import re
3032from git import Repo
3133from github import Auth , Github , GithubException
3234
35+ from .constants import DEFAULT_BRANCH_NAME , DEFAULT_PULL_REQUEST_TITLE
36+
3337
3438def include_patterns (* patterns ):
3539 """Include listed patterns in ``copytree()``.
@@ -148,6 +152,8 @@ def synchronize(
148152 dry_run : bool = False ,
149153 skip_ci : bool = False ,
150154 random_branch_name : bool = False ,
155+ target_branch_name : str = DEFAULT_BRANCH_NAME ,
156+ pull_request_title : str = DEFAULT_PULL_REQUEST_TITLE ,
151157) -> Union [str , None ]:
152158 """Synchronize a folder to a remote repository.
153159
@@ -178,23 +184,24 @@ def synchronize(
178184 skip_ci : bool, optional
179185 Whether to add a ``[skip ci]`` prefix to the commit message or not. By default ``False``.
180186 random_branch_name : bool, optional
181- For testing purposes - generates a random suffix for the branch name ``sync/file-sync``.
187+ For testing purposes - generates a random suffix for the branch name.
188+ target_branch_name : str, optional
189+ Name of the branch to create for the synchronization, by default it is 'sync/file-sync'.
190+ pull_request_title : str, optional
191+ Title of the pull request created after synchronization, by default it is
192+ 'sync: file sync performed by ansys-tools-repo-sync'.
182193
183194 Returns
184195 -------
185196 Union[str, None]
186197 Pull request URL. In case of dry-run or no files modified, ``None`` is returned.
187198
188199 """
189- # New branch name and PR title
190- new_branch_name = "sync/file-sync"
191- pr_title = "sync: file sync performed by ansys-tools-repo-sync"
192-
193200 # If requested, add random suffix
194201 if random_branch_name :
195202 from secrets import token_urlsafe
196203
197- new_branch_name = f"{ new_branch_name } -{ token_urlsafe (16 )} "
204+ target_branch_name = f"{ target_branch_name } -{ token_urlsafe (16 )} "
198205
199206 # Authenticate with GitHub
200207 g = Github (auth = Auth .Token (token ))
@@ -240,18 +247,20 @@ def synchronize(
240247 dirs_exist_ok = True ,
241248 )
242249
243- print (f">>> Checking out new branch '{ new_branch_name } ' from '{ branch_checked_out } '..." )
250+ print (f">>> Checking out new branch '{ target_branch_name } ' from '{ branch_checked_out } '..." )
244251 repo = Repo (repo_path )
245252 try :
246253 # Commit changes to a new branch
247254 repo .git .checkout (branch_checked_out )
248- repo .git .checkout ("-b" , new_branch_name )
249- print (f">>> Committing changes to branch '{ new_branch_name } '..." )
255+ repo .git .checkout ("-b" , target_branch_name )
256+ print (f">>> Committing changes to branch '{ target_branch_name } '..." )
250257 repo .git .add ("--all" )
251258 repo .index .commit (f"{ '[skip ci] ' if skip_ci else '' } sync: add changes from local folder" )
252259
253260 # Get a list of the files modified
254- output = repo .git .diff ("--compact-summary" , f"{ branch_checked_out } " , f"{ new_branch_name } " )
261+ output = repo .git .diff (
262+ "--compact-summary" , f"{ branch_checked_out } " , f"{ target_branch_name } "
263+ )
255264
256265 # If output is empty, avoid creating PR
257266 if not output :
@@ -264,17 +273,17 @@ def synchronize(
264273 pull_request = None
265274 if not dry_run :
266275 # Push changes to remote repositories
267- print (f">>> Force-pushing branch '{ new_branch_name } ' remotely..." )
268- repo .git .push ("--force" , "origin" , new_branch_name )
276+ print (f">>> Force-pushing branch '{ target_branch_name } ' remotely..." )
277+ repo .git .push ("--force" , "origin" , target_branch_name )
269278
270279 # Create a pull request
271280 try :
272- print (f">>> Creating pull request from '{ new_branch_name } '..." )
281+ print (f">>> Creating pull request from '{ target_branch_name } '..." )
273282 pull_request = pygithub_repo .create_pull (
274- title = pr_title ,
283+ title = pull_request_title ,
275284 body = "Please review and merge these changes." ,
276285 base = branch_checked_out ,
277- head = new_branch_name ,
286+ head = target_branch_name ,
278287 )
279288 except GithubException as err :
280289 if err .args [0 ] == 422 or err .data ["message" ] == "Validation Failed" :
@@ -286,7 +295,7 @@ def synchronize(
286295 # Find the associated PR (must be opened...)
287296 associated_pull_request = None
288297 for pr in prs :
289- if pr .head .ref == new_branch_name :
298+ if pr .head .ref == target_branch_name :
290299 associated_pull_request = pr
291300 break
292301
0 commit comments