Skip to content

Commit 3655b4c

Browse files
committed
feat: allowing for custom branch name and PR title
1 parent e387672 commit 3655b4c

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

src/ansys/tools/repo_sync/__main__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@
9999
default=False,
100100
help="Generates a random branch name instead of the typical ``sync/file-sync``. Used for testing purposes mainly.",
101101
)
102+
@click.option(
103+
"--target-branch-name",
104+
type=str,
105+
default="sync/file-sync",
106+
help="Name of the branch to create for the synchronization, by default it is 'sync/file-sync'.",
107+
)
108+
@click.option(
109+
"--pull-request-title",
110+
type=str,
111+
default="sync: file sync performed by ansys-tools-repo-sync",
112+
help="Title of the pull request created after synchronization.",
113+
)
102114
def synchronize(
103115
owner,
104116
repository,
@@ -112,6 +124,8 @@ def synchronize(
112124
dry_run,
113125
skip_ci,
114126
random_branch_name,
127+
target_branch_name,
128+
pull_request_title,
115129
):
116130
"""CLI command to execute the repository synchronization."""
117131
_synchronize(
@@ -127,6 +141,8 @@ def synchronize(
127141
dry_run=dry_run,
128142
skip_ci=skip_ci,
129143
random_branch_name=random_branch_name,
144+
target_branch_name=target_branch_name,
145+
pull_request_title=pull_request_title,
130146
)
131147

132148

src/ansys/tools/repo_sync/repo_sync.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ def synchronize(
148148
dry_run: bool = False,
149149
skip_ci: bool = False,
150150
random_branch_name: bool = False,
151+
target_branch_name: str = "sync/file-sync",
152+
pull_request_title: str = "sync: file sync performed by ansys-tools-repo-sync",
151153
) -> Union[str, None]:
152154
"""Synchronize a folder to a remote repository.
153155
@@ -178,23 +180,24 @@ def synchronize(
178180
skip_ci : bool, optional
179181
Whether to add a ``[skip ci]`` prefix to the commit message or not. By default ``False``.
180182
random_branch_name : bool, optional
181-
For testing purposes - generates a random suffix for the branch name ``sync/file-sync``.
183+
For testing purposes - generates a random suffix for the branch name.
184+
target_branch_name : str, optional
185+
Name of the branch to create for the synchronization, by default it is 'sync/file-sync'.
186+
pull_request_title : str, optional
187+
Title of the pull request created after synchronization, by default it is
188+
'sync: file sync performed by ansys-tools-repo-sync'.
182189
183190
Returns
184191
-------
185192
Union[str, None]
186193
Pull request URL. In case of dry-run or no files modified, ``None`` is returned.
187194
188195
"""
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-
193196
# If requested, add random suffix
194197
if random_branch_name:
195198
from secrets import token_urlsafe
196199

197-
new_branch_name = f"{new_branch_name}-{token_urlsafe(16)}"
200+
target_branch_name = f"{target_branch_name}-{token_urlsafe(16)}"
198201

199202
# Authenticate with GitHub
200203
g = Github(auth=Auth.Token(token))
@@ -240,18 +243,18 @@ def synchronize(
240243
dirs_exist_ok=True,
241244
)
242245

243-
print(f">>> Checking out new branch '{new_branch_name}' from '{branch_checked_out}'...")
246+
print(f">>> Checking out new branch '{target_branch_name}' from '{branch_checked_out}'...")
244247
repo = Repo(repo_path)
245248
try:
246249
# Commit changes to a new branch
247250
repo.git.checkout(branch_checked_out)
248-
repo.git.checkout("-b", new_branch_name)
249-
print(f">>> Committing changes to branch '{new_branch_name}'...")
251+
repo.git.checkout("-b", target_branch_name)
252+
print(f">>> Committing changes to branch '{target_branch_name}'...")
250253
repo.git.add("--all")
251254
repo.index.commit(f"{'[skip ci] ' if skip_ci else ''}sync: add changes from local folder")
252255

253256
# Get a list of the files modified
254-
output = repo.git.diff("--compact-summary", f"{branch_checked_out}", f"{new_branch_name}")
257+
output = repo.git.diff("--compact-summary", f"{branch_checked_out}", f"{target_branch_name}")
255258

256259
# If output is empty, avoid creating PR
257260
if not output:
@@ -264,17 +267,17 @@ def synchronize(
264267
pull_request = None
265268
if not dry_run:
266269
# Push changes to remote repositories
267-
print(f">>> Force-pushing branch '{new_branch_name}' remotely...")
268-
repo.git.push("--force", "origin", new_branch_name)
270+
print(f">>> Force-pushing branch '{target_branch_name}' remotely...")
271+
repo.git.push("--force", "origin", target_branch_name)
269272

270273
# Create a pull request
271274
try:
272-
print(f">>> Creating pull request from '{new_branch_name}'...")
275+
print(f">>> Creating pull request from '{target_branch_name}'...")
273276
pull_request = pygithub_repo.create_pull(
274-
title=pr_title,
277+
title=pull_request_title,
275278
body="Please review and merge these changes.",
276279
base=branch_checked_out,
277-
head=new_branch_name,
280+
head=target_branch_name,
278281
)
279282
except GithubException as err:
280283
if err.args[0] == 422 or err.data["message"] == "Validation Failed":
@@ -286,7 +289,7 @@ def synchronize(
286289
# Find the associated PR (must be opened...)
287290
associated_pull_request = None
288291
for pr in prs:
289-
if pr.head.ref == new_branch_name:
292+
if pr.head.ref == target_branch_name:
290293
associated_pull_request = pr
291294
break
292295

0 commit comments

Comments
 (0)