Skip to content

Commit d06d6ea

Browse files
authored
feat: allowing for custom branch name and PR title (#7931)
1 parent e387672 commit d06d6ea

File tree

8 files changed

+91
-39
lines changed

8 files changed

+91
-39
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,5 @@ dmypy.json
8181
# VSCode settings
8282
.vscode
8383

84-
doc/_build
84+
doc/_build
85+
doc/source/api

doc/source/_autoapi_templates/index.rst

Lines changed: 0 additions & 14 deletions
This file was deleted.

doc/source/conf.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,20 @@
3333
"version_match": get_version_match(__version__),
3434
},
3535
"check_switcher": False,
36+
"ansys_sphinx_theme_autoapi": {
37+
"project": project,
38+
},
3639
}
3740

3841
# Sphinx extensions
3942
extensions = [
4043
"sphinx.ext.autodoc",
4144
"sphinx.ext.autosummary",
42-
"autoapi.extension",
4345
"sphinx_autodoc_typehints",
4446
"numpydoc",
4547
"sphinx.ext.intersphinx",
4648
"sphinx_copybutton",
49+
"ansys_sphinx_theme.extension.autoapi",
4750
]
4851

4952
# Intersphinx mapping

doc/source/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
:hidden:
1010
:maxdepth: 3
1111

12-
autoapi/index
12+
api/index

src/ansys/tools/repo_sync/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,11 @@
3131
# Ease import statements
3232
# ------------------------------------------------------------------------------
3333

34+
from .constants import DEFAULT_BRANCH_NAME, DEFAULT_PULL_REQUEST_TITLE
3435
from .repo_sync import synchronize
36+
37+
__all__ = [
38+
"synchronize",
39+
"DEFAULT_BRANCH_NAME",
40+
"DEFAULT_PULL_REQUEST_TITLE",
41+
]

src/ansys/tools/repo_sync/__main__.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,17 @@
2727
.. code::
2828
2929
repo-sync \
30-
--token <token> \
31-
--owner <organization-name> \
32-
--repository <repository-name> \
33-
--from-dir <path-to-dir-containing-files-to-sync> \
34-
--to-dir <target-dir-for-sync> \
35-
--include-manifest <path-to-manifest>
30+
--token <token> \
31+
--owner <organization-name> \
32+
--repository <repository-name> \
33+
--from-dir <path-to-dir-containing-files-to-sync> \
34+
--to-dir <target-dir-for-sync> \
35+
--include-manifest <path-to-manifest>
3636
3737
"""
3838
import click
3939

40+
from .constants import DEFAULT_BRANCH_NAME, DEFAULT_PULL_REQUEST_TITLE
4041
from .repo_sync import synchronize as _synchronize
4142

4243

@@ -99,6 +100,18 @@
99100
default=False,
100101
help="Generates a random branch name instead of the typical ``sync/file-sync``. Used for testing purposes mainly.",
101102
)
103+
@click.option(
104+
"--target-branch-name",
105+
type=str,
106+
default=DEFAULT_BRANCH_NAME,
107+
help=f"Name of the branch to create for the synchronization, by default it is '{DEFAULT_BRANCH_NAME}'.",
108+
)
109+
@click.option(
110+
"--pull-request-title",
111+
type=str,
112+
default=DEFAULT_PULL_REQUEST_TITLE,
113+
help=f"Title of the pull request created after synchronization, by default it is {DEFAULT_PULL_REQUEST_TITLE}",
114+
)
102115
def synchronize(
103116
owner,
104117
repository,
@@ -112,6 +125,8 @@ def synchronize(
112125
dry_run,
113126
skip_ci,
114127
random_branch_name,
128+
target_branch_name,
129+
pull_request_title,
115130
):
116131
"""CLI command to execute the repository synchronization."""
117132
_synchronize(
@@ -127,6 +142,8 @@ def synchronize(
127142
dry_run=dry_run,
128143
skip_ci=skip_ci,
129144
random_branch_name=random_branch_name,
145+
target_branch_name=target_branch_name,
146+
pull_request_title=pull_request_title,
130147
)
131148

132149

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright (C) 2023 - 2025 ANSYS, Inc. and/or its affiliates.
2+
# SPDX-License-Identifier: MIT
3+
#
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
"""Module containing constants for the repo sync tool."""
24+
25+
DEFAULT_BRANCH_NAME = "sync/file-sync"
26+
"""Default branch name for the synchronization process."""
27+
28+
DEFAULT_PULL_REQUEST_TITLE = "sync: file sync performed by ansys-tools-repo-sync"
29+
"""Default title for the pull request created during synchronization."""

src/ansys/tools/repo_sync/repo_sync.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
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+
2325
from fnmatch import filter
2426
import os
2527
import re
@@ -30,6 +32,8 @@
3032
from git import Repo
3133
from github import Auth, Github, GithubException
3234

35+
from .constants import DEFAULT_BRANCH_NAME, DEFAULT_PULL_REQUEST_TITLE
36+
3337

3438
def 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

Comments
 (0)