Skip to content

Commit 9b70e40

Browse files
Update now creates a PR
1 parent 14fcde9 commit 9b70e40

File tree

3 files changed

+80
-25
lines changed

3 files changed

+80
-25
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ Creates PRs for dependency updates in python projects.
1717
- [x] Github auth & async
1818
- [x] Create dependency abstract class
1919
- [x] Use get_dependency
20-
- [ ] Implement CLI
20+
- [x] Implement CLI
2121
- [x] Create changes to files
2222
- [ ] Handle uv better (not group)
23-
- [ ] Create pull request
23+
- [x] Create pull request
2424
- [ ] Add tests
2525
- [ ] Documentation

src/upgrade_dependencies/main.py

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
from typing import TYPE_CHECKING
66

77
import typer
8+
from packaging.version import Version
89
from rich import print as rprint
910
from rich.console import Group
1011
from rich.panel import Panel
1112
from rich.text import Text
1213

14+
from upgrade_dependencies.dependency import GitHubDependency, PyPIDependency
1315
from upgrade_dependencies.project import Project
16+
from upgrade_dependencies.utils import run_shell_command
1417

1518
if TYPE_CHECKING:
1619
from upgrade_dependencies.dependency import Dependency
@@ -320,11 +323,14 @@ def update(
320323
):
321324
"""_summary_.
322325
326+
Make sure branch locally and on github do not already exist!
327+
323328
Args:
324329
dependency: _description_
325330
version: _description_
326331
project_path: _description_
327332
"""
333+
# TODO: print status
328334
project = Project(
329335
project_path=project_path,
330336
gh_pat=GH_PAT,
@@ -343,35 +349,69 @@ def update(
343349
if version is None:
344350
version = str(dep.get_latest_version())
345351

346-
# # create new branch
347-
# if isinstance(dep, GitHubDependency) and dep.action:
348-
# v = Version(version)
349-
# branch_name = f"dependency/{dep.short_name}-v{v.major}"
350-
# else:
351-
# branch_name = f"dependency/{dep.short_name}-{version}"
352+
# create new branch
353+
if isinstance(dep, GitHubDependency) and dep.action:
354+
v = Version(version)
355+
branch_name = f"dependency/{dep.short_name}-v{v.major}"
356+
else:
357+
branch_name = f"dependency/{dep.short_name}-{version}"
352358

353-
# subprocess.run(['git', 'checkout', '-b', branch_name])
359+
run_shell_command(["git", "checkout", "-b", branch_name])
354360

355361
# update dependency
356362
project.update_dependency(dependency=dep, version=version)
357363

358-
# # stage changes - TODO: get files that are changed
359-
# subprocess.run(['git', 'add', '.github/'])
360-
361-
# # commit the changes
362-
# if isinstance(dep, GitHubDependency) and dep.action:
363-
# old_v = Version(old_ver)
364-
# v = Version(version)
365-
# commit_message = f"Bump {dep.package_name} from v{old_v.major} to v{v.major}"
366-
# else:
367-
# commit_message = f"Bump {dep.package_name} from {old_ver} to {version}"
368-
369-
# subprocess.run(['git', 'commit', '-m', commit_message])
370-
371-
# # push the branch
372-
# subprocess.run(['git', 'push', 'origin', branch_name])
364+
# stage changes - TODO: get files that are changed
365+
run_shell_command(["git", "add", ".github/"])
366+
367+
# commit the changes
368+
if isinstance(dep, GitHubDependency) and dep.action:
369+
old_v = Version(old_ver)
370+
v = Version(version)
371+
commit_message = f"Bump {dep.package_name} from v{old_v.major} to v{v.major}"
372+
else:
373+
commit_message = f"Bump {dep.package_name} from {old_ver} to {version}"
374+
375+
run_shell_command(["git", "commit", "-m", commit_message])
376+
377+
# push the branch
378+
run_shell_command(["git", "push", "origin", branch_name])
379+
380+
# create pr_body
381+
if isinstance(dep, PyPIDependency):
382+
url = f"https://pypi.org/project/{dep.package_name}"
383+
pr_body = f"Bumps [{dep.package_name}]({url}) from {old_ver} to {version}."
384+
elif isinstance(dep, GitHubDependency):
385+
old_v = Version(old_ver)
386+
v = Version(version)
387+
url = f"https://github.com/{dep.owner}/{dep.repo}"
388+
pr_body = (
389+
f"Bumps [{dep.package_name}]({url}) from v{old_v.major} to v{v.major}."
390+
)
391+
else:
392+
pr_body = ""
393+
394+
# create pull request
395+
run_shell_command(
396+
[
397+
"gh",
398+
"pr",
399+
"create",
400+
"-a",
401+
"@me",
402+
"--base",
403+
"master",
404+
"--body",
405+
pr_body,
406+
"--label",
407+
"dependencies",
408+
"--title",
409+
commit_message,
410+
],
411+
)
373412

374-
# TODO: create pull request
413+
# re-checkout master
414+
run_shell_command(["git", "checkout", "master"])
375415

376416

377417
def main():

src/upgrade_dependencies/utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import glob
44
import os
5+
import subprocess
56
from pathlib import Path
67
from typing import Any
78

@@ -247,3 +248,17 @@ def update_pre_commit(
247248
# temp_file = Path(file_path).with_suffix(".temp")
248249
with Path(file_path).open("w") as temp_f:
249250
yaml.dump(data, temp_f) # pyright: ignore
251+
252+
253+
def run_shell_command(shell_args: list[str]) -> None:
254+
"""_summary_.
255+
256+
Args:
257+
shell_args: _description_
258+
"""
259+
try:
260+
subprocess.run(shell_args, check=True, capture_output=True, text=True) # noqa: S603
261+
except subprocess.CalledProcessError as e:
262+
msg = f"Command failed with return code {e.returncode}.\n"
263+
msg += f"Error output: {e.stderr}"
264+
raise RuntimeError(msg) from e

0 commit comments

Comments
 (0)