Skip to content

Commit 1d8a22b

Browse files
Add format yml command
1 parent 4ddf370 commit 1d8a22b

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

src/upgrade_dependencies/main.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
from rich.progress import Progress, SpinnerColumn, TextColumn
1313
from rich.text import Text
1414

15+
import upgrade_dependencies.utils as utils
1516
from upgrade_dependencies.dependency import GitHubDependency, PyPIDependency
1617
from upgrade_dependencies.project import Project
17-
from upgrade_dependencies.utils import get_git_status, run_shell_command
1818

1919
if TYPE_CHECKING:
2020
from upgrade_dependencies.dependency import Dependency
@@ -339,11 +339,11 @@ def update(
339339
else:
340340
branch_name = f"dependency/{dep.short_name}-{version}"
341341

342-
run_shell_command(["git", "checkout", "-b", branch_name])
342+
utils.run_shell_command(["git", "checkout", "-b", branch_name])
343343

344344
# get status of files before changes
345345
progress.update(task, description="Updating dependency...")
346-
files_before = get_git_status()
346+
files_before = utils.get_git_status()
347347

348348
# warning if there are changed files
349349
if len(files_before) > 0:
@@ -356,16 +356,16 @@ def update(
356356
project.update_dependency(dependency=dep, version=version)
357357

358358
# run uv.lock, don't worry if it doesn't work (i.e. uv not installed)
359-
run_shell_command(["uv", "lock"], suppress_errors=True)
359+
utils.run_shell_command(["uv", "lock"], suppress_errors=True)
360360

361361
# get status of files after changes
362-
files_after = get_git_status()
362+
files_after = utils.get_git_status()
363363

364364
# get only the files that were changed
365365
changed_files = [f for f in files_after if f not in files_before]
366366

367367
# git add the changed files
368-
run_shell_command(["git", "add", *changed_files])
368+
utils.run_shell_command(["git", "add", *changed_files])
369369

370370
# commit the changes
371371
progress.update(task, description="Committing changes...")
@@ -378,11 +378,11 @@ def update(
378378
else:
379379
commit_message = f"Bump {dep.package_name} from {old_ver} to {version}"
380380

381-
run_shell_command(["git", "commit", "-m", commit_message])
381+
utils.run_shell_command(["git", "commit", "-m", commit_message])
382382

383383
# push the branch to GitHub
384384
progress.update(task, description="Pushing changes to GitHub...")
385-
run_shell_command(["git", "push", "origin", branch_name])
385+
utils.run_shell_command(["git", "push", "origin", branch_name])
386386

387387
# create pr_body
388388
progress.update(task, description="Creating pull request...")
@@ -400,7 +400,7 @@ def update(
400400
pr_body = ""
401401

402402
# create pull request
403-
pr = run_shell_command(
403+
pr = utils.run_shell_command(
404404
[
405405
"gh",
406406
"pr",
@@ -419,13 +419,19 @@ def update(
419419
)
420420

421421
# re-checkout master branch
422-
run_shell_command(["git", "checkout", target_branch])
422+
utils.run_shell_command(["git", "checkout", target_branch])
423423

424424
msg = f"✅ [bold]{dep.package_name}[/bold] updated! View the pull request at"
425425
msg += f" {pr.stdout}"
426426
rprint(msg)
427427

428428

429+
@app.command()
430+
def format_yml():
431+
"""Formats the workflow and pre-commit config yaml files."""
432+
utils.format_all_yml_files()
433+
434+
429435
def main():
430436
"""_summary_."""
431437
app()

src/upgrade_dependencies/utils.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,29 @@ def get_git_status() -> list[str]:
298298
changed_files.append(file_path)
299299

300300
return changed_files
301+
302+
303+
def format_all_yml_files() -> None:
304+
"""Formats the workflow and pre-commit config yaml files."""
305+
# pre-commit
306+
pre_commit_path = Path(".pre-commit-config.yaml")
307+
308+
with pre_commit_path.open("r") as f:
309+
data: dict[str, Any] = yaml.load(f) # pyright: ignore
310+
311+
with pre_commit_path.open("w") as temp_f:
312+
yaml.dump(data, temp_f) # pyright: ignore
313+
314+
# workflows
315+
workflows_dir = Path("") / ".github" / "workflows"
316+
317+
yaml_files = glob.glob(os.path.join(workflows_dir, "*.yml")) + glob.glob(
318+
os.path.join(workflows_dir, "*.yaml"),
319+
)
320+
321+
for file_path in yaml_files:
322+
with Path(file_path).open("r") as f:
323+
data: dict[str, Any] = yaml.load(f) # pyright: ignore
324+
325+
with Path(file_path).open("w") as temp_f:
326+
yaml.dump(data, temp_f) # pyright: ignore

todo.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
- [x] Use get_dependency
1010
- [x] Implement CLI
1111
- [x] Create changes to files
12-
- [ ] Handle uv better (not group)
1312
- [x] Create pull request
1413
- [ ] Add format yml files
1514
- [ ] Documentation
15+
- [ ] Handle uv better (not group)
16+
- [ ] Handle pre-commit yml (not yaml)
1617
- [ ] Add tests

0 commit comments

Comments
 (0)