Skip to content

Commit

Permalink
Update now creates a PR
Browse files Browse the repository at this point in the history
  • Loading branch information
robbievanleeuwen committed Nov 26, 2024
1 parent 14fcde9 commit 9b70e40
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 25 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ Creates PRs for dependency updates in python projects.
- [x] Github auth & async
- [x] Create dependency abstract class
- [x] Use get_dependency
- [ ] Implement CLI
- [x] Implement CLI
- [x] Create changes to files
- [ ] Handle uv better (not group)
- [ ] Create pull request
- [x] Create pull request
- [ ] Add tests
- [ ] Documentation
86 changes: 63 additions & 23 deletions src/upgrade_dependencies/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
from typing import TYPE_CHECKING

import typer
from packaging.version import Version
from rich import print as rprint
from rich.console import Group
from rich.panel import Panel
from rich.text import Text

from upgrade_dependencies.dependency import GitHubDependency, PyPIDependency
from upgrade_dependencies.project import Project
from upgrade_dependencies.utils import run_shell_command

if TYPE_CHECKING:
from upgrade_dependencies.dependency import Dependency
Expand Down Expand Up @@ -320,11 +323,14 @@ def update(
):
"""_summary_.
Make sure branch locally and on github do not already exist!
Args:
dependency: _description_
version: _description_
project_path: _description_
"""
# TODO: print status
project = Project(
project_path=project_path,
gh_pat=GH_PAT,
Expand All @@ -343,35 +349,69 @@ def update(
if version is None:
version = str(dep.get_latest_version())

# # create new branch
# if isinstance(dep, GitHubDependency) and dep.action:
# v = Version(version)
# branch_name = f"dependency/{dep.short_name}-v{v.major}"
# else:
# branch_name = f"dependency/{dep.short_name}-{version}"
# create new branch
if isinstance(dep, GitHubDependency) and dep.action:
v = Version(version)
branch_name = f"dependency/{dep.short_name}-v{v.major}"
else:
branch_name = f"dependency/{dep.short_name}-{version}"

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

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

# # stage changes - TODO: get files that are changed
# subprocess.run(['git', 'add', '.github/'])

# # commit the changes
# if isinstance(dep, GitHubDependency) and dep.action:
# old_v = Version(old_ver)
# v = Version(version)
# commit_message = f"Bump {dep.package_name} from v{old_v.major} to v{v.major}"
# else:
# commit_message = f"Bump {dep.package_name} from {old_ver} to {version}"

# subprocess.run(['git', 'commit', '-m', commit_message])

# # push the branch
# subprocess.run(['git', 'push', 'origin', branch_name])
# stage changes - TODO: get files that are changed
run_shell_command(["git", "add", ".github/"])

# commit the changes
if isinstance(dep, GitHubDependency) and dep.action:
old_v = Version(old_ver)
v = Version(version)
commit_message = f"Bump {dep.package_name} from v{old_v.major} to v{v.major}"
else:
commit_message = f"Bump {dep.package_name} from {old_ver} to {version}"

run_shell_command(["git", "commit", "-m", commit_message])

# push the branch
run_shell_command(["git", "push", "origin", branch_name])

# create pr_body
if isinstance(dep, PyPIDependency):
url = f"https://pypi.org/project/{dep.package_name}"
pr_body = f"Bumps [{dep.package_name}]({url}) from {old_ver} to {version}."
elif isinstance(dep, GitHubDependency):
old_v = Version(old_ver)
v = Version(version)
url = f"https://github.com/{dep.owner}/{dep.repo}"
pr_body = (
f"Bumps [{dep.package_name}]({url}) from v{old_v.major} to v{v.major}."
)
else:
pr_body = ""

# create pull request
run_shell_command(
[
"gh",
"pr",
"create",
"-a",
"@me",
"--base",
"master",
"--body",
pr_body,
"--label",
"dependencies",
"--title",
commit_message,
],
)

# TODO: create pull request
# re-checkout master
run_shell_command(["git", "checkout", "master"])


def main():
Expand Down
15 changes: 15 additions & 0 deletions src/upgrade_dependencies/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import glob
import os
import subprocess
from pathlib import Path
from typing import Any

Expand Down Expand Up @@ -247,3 +248,17 @@ def update_pre_commit(
# temp_file = Path(file_path).with_suffix(".temp")
with Path(file_path).open("w") as temp_f:
yaml.dump(data, temp_f) # pyright: ignore


def run_shell_command(shell_args: list[str]) -> None:
"""_summary_.
Args:
shell_args: _description_
"""
try:
subprocess.run(shell_args, check=True, capture_output=True, text=True) # noqa: S603
except subprocess.CalledProcessError as e:
msg = f"Command failed with return code {e.returncode}.\n"
msg += f"Error output: {e.stderr}"
raise RuntimeError(msg) from e

0 comments on commit 9b70e40

Please sign in to comment.