From 75a120952ca5143a9d5bbfdad3679451b092d5ba Mon Sep 17 00:00:00 2001 From: MarkRx Date: Mon, 9 Dec 2024 09:27:27 -0700 Subject: [PATCH 1/2] Add version metadata and --version command --- .../algo/ai_handlers/litellm_ai_handler.py | 5 ++-- pr_agent/cli.py | 2 ++ pr_agent/version.py | 23 +++++++++++++++++++ pyproject.toml | 2 +- 4 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 pr_agent/version.py diff --git a/pr_agent/algo/ai_handlers/litellm_ai_handler.py b/pr_agent/algo/ai_handlers/litellm_ai_handler.py index ad1fc5643..8ebc5704a 100644 --- a/pr_agent/algo/ai_handlers/litellm_ai_handler.py +++ b/pr_agent/algo/ai_handlers/litellm_ai_handler.py @@ -9,6 +9,7 @@ from pr_agent.algo.ai_handlers.base_ai_handler import BaseAiHandler from pr_agent.config_loader import get_settings from pr_agent.log import get_logger +from pr_agent.version import get_version OPENAI_RETRIES = 5 @@ -132,7 +133,7 @@ def capture_logs(message): if "langfuse" in callbacks: metadata.update({ "trace_name": command, - "tags": [git_provider, command], + "tags": [git_provider, command, f'version:{get_version()}'], "trace_metadata": { "command": command, "pr_url": pr_url, @@ -141,7 +142,7 @@ def capture_logs(message): if "langsmith" in callbacks: metadata.update({ "run_name": command, - "tags": [git_provider, command], + "tags": [git_provider, command, f'version:{get_version()}'], "extra": { "metadata": { "command": command, diff --git a/pr_agent/cli.py b/pr_agent/cli.py index 5f8afc885..3c0661cad 100644 --- a/pr_agent/cli.py +++ b/pr_agent/cli.py @@ -5,6 +5,7 @@ from pr_agent.agent.pr_agent import PRAgent, commands from pr_agent.config_loader import get_settings from pr_agent.log import get_logger, setup_logger +from pr_agent.version import get_version log_level = os.environ.get("LOG_LEVEL", "INFO") setup_logger(log_level) @@ -45,6 +46,7 @@ def set_parser(): To edit any configuration parameter from 'configuration.toml', just add -config_path=. For example: 'python cli.py --pr_url=... review --pr_reviewer.extra_instructions="focus on the file: ..."' """) + parser.add_argument('--version', action='version', version=f'pr-agent {get_version()}') parser.add_argument('--pr_url', type=str, help='The URL of the PR to review', default=None) parser.add_argument('--issue_url', type=str, help='The URL of the Issue to review', default=None) parser.add_argument('command', type=str, help='The', choices=commands, default='review') diff --git a/pr_agent/version.py b/pr_agent/version.py new file mode 100644 index 000000000..1df1621a2 --- /dev/null +++ b/pr_agent/version.py @@ -0,0 +1,23 @@ +import os +import sys +from importlib.metadata import version, PackageNotFoundError + +from pr_agent.log import get_logger + +def get_version() -> str: + # First check pyproject.toml if running directly out of repository + if os.path.exists("pyproject.toml"): + if sys.version_info >= (3, 11): + import tomllib + with open("pyproject.toml", "rb") as f: + data = tomllib.load(f) + return data["project"]["version"] + else: + get_logger().warn("Unable to determine local version from pyproject.toml") + + # Otherwise get the installed pip package version + try: + return version('pr-agent') + except PackageNotFoundError: + get_logger().error("Unable to find package named 'pr-agent'") + return "unknown" diff --git a/pyproject.toml b/pyproject.toml index cc4264288..9da073583 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "pr-agent" -version = "0.2.4" +version = "0.2.6" authors = [{ name = "CodiumAI", email = "tal.r@codium.ai" }] From 452abe2e189c98f03182ad1b3d0cbef8a919b826 Mon Sep 17 00:00:00 2001 From: MarkRx Date: Tue, 17 Dec 2024 08:44:53 -0700 Subject: [PATCH 2/2] Move get_version to algo/util.py; fix version to 0.25 --- .../algo/ai_handlers/litellm_ai_handler.py | 2 +- pr_agent/algo/utils.py | 23 +++++++++++++++++++ pr_agent/cli.py | 2 +- pr_agent/version.py | 23 ------------------- pyproject.toml | 2 +- 5 files changed, 26 insertions(+), 26 deletions(-) delete mode 100644 pr_agent/version.py diff --git a/pr_agent/algo/ai_handlers/litellm_ai_handler.py b/pr_agent/algo/ai_handlers/litellm_ai_handler.py index 8ebc5704a..c53c1ec3d 100644 --- a/pr_agent/algo/ai_handlers/litellm_ai_handler.py +++ b/pr_agent/algo/ai_handlers/litellm_ai_handler.py @@ -7,9 +7,9 @@ from tenacity import retry, retry_if_exception_type, stop_after_attempt from pr_agent.algo.ai_handlers.base_ai_handler import BaseAiHandler +from pr_agent.algo.utils import get_version from pr_agent.config_loader import get_settings from pr_agent.log import get_logger -from pr_agent.version import get_version OPENAI_RETRIES = 5 diff --git a/pr_agent/algo/utils.py b/pr_agent/algo/utils.py index c8a793350..e6e7a9621 100644 --- a/pr_agent/algo/utils.py +++ b/pr_agent/algo/utils.py @@ -7,11 +7,13 @@ import json import os import re +import sys import textwrap import time import traceback from datetime import datetime from enum import Enum +from importlib.metadata import PackageNotFoundError, version from typing import Any, List, Tuple import html2text @@ -1106,3 +1108,24 @@ def process_description(description_full: str) -> Tuple[str, List]: get_logger().exception(f"Failed to process description: {e}") return base_description_str, files + +def get_version() -> str: + # First check pyproject.toml if running directly out of repository + if os.path.exists("pyproject.toml"): + if sys.version_info >= (3, 11): + import tomllib + with open("pyproject.toml", "rb") as f: + data = tomllib.load(f) + if "project" in data and "version" in data["project"]: + return data["project"]["version"] + else: + get_logger().warning("Version not found in pyproject.toml") + else: + get_logger().warning("Unable to determine local version from pyproject.toml") + + # Otherwise get the installed pip package version + try: + return version('pr-agent') + except PackageNotFoundError: + get_logger().warning("Unable to find package named 'pr-agent'") + return "unknown" diff --git a/pr_agent/cli.py b/pr_agent/cli.py index 3c0661cad..32be260ef 100644 --- a/pr_agent/cli.py +++ b/pr_agent/cli.py @@ -3,9 +3,9 @@ import os from pr_agent.agent.pr_agent import PRAgent, commands +from pr_agent.algo.utils import get_version from pr_agent.config_loader import get_settings from pr_agent.log import get_logger, setup_logger -from pr_agent.version import get_version log_level = os.environ.get("LOG_LEVEL", "INFO") setup_logger(log_level) diff --git a/pr_agent/version.py b/pr_agent/version.py deleted file mode 100644 index 1df1621a2..000000000 --- a/pr_agent/version.py +++ /dev/null @@ -1,23 +0,0 @@ -import os -import sys -from importlib.metadata import version, PackageNotFoundError - -from pr_agent.log import get_logger - -def get_version() -> str: - # First check pyproject.toml if running directly out of repository - if os.path.exists("pyproject.toml"): - if sys.version_info >= (3, 11): - import tomllib - with open("pyproject.toml", "rb") as f: - data = tomllib.load(f) - return data["project"]["version"] - else: - get_logger().warn("Unable to determine local version from pyproject.toml") - - # Otherwise get the installed pip package version - try: - return version('pr-agent') - except PackageNotFoundError: - get_logger().error("Unable to find package named 'pr-agent'") - return "unknown" diff --git a/pyproject.toml b/pyproject.toml index 9da073583..c841d27f2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "pr-agent" -version = "0.2.6" +version = "0.2.5" authors = [{ name = "CodiumAI", email = "tal.r@codium.ai" }]