Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitHub pull request tools #1190

Merged
merged 3 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from superagi.helper.tool_helper import register_toolkits, register_marketplace_toolkits
from superagi.lib.logger import logger
from superagi.llms.google_palm import GooglePalm
from superagi.llms.llm_model_factory import build_model_with_api_key
from superagi.llms.openai import OpenAi
from superagi.llms.replicate import Replicate
from superagi.llms.hugging_face import HuggingFace
Expand Down Expand Up @@ -345,15 +346,8 @@ async def validate_llm_api_key(request: ValidateAPIKeyRequest, Authorize: AuthJW
"""API to validate LLM API Key"""
source = request.model_source
api_key = request.model_api_key
valid_api_key = False
if source == "OpenAi":
valid_api_key = OpenAi(api_key=api_key).verify_access_key()
elif source == "Google Palm":
valid_api_key = GooglePalm(api_key=api_key).verify_access_key()
elif source == "Replicate":
valid_api_key = Replicate(api_key=api_key).verify_access_key()
elif source == "Hugging Face":
valid_api_key = HuggingFace(api_key=api_key).verify_access_key()
model = build_model_with_api_key(source, api_key)
valid_api_key = model.verify_access_key() if model is not None else False
if valid_api_key:
return {"message": "Valid API Key", "status": "success"}
else:
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,5 @@ html2text==2020.1.16
duckduckgo-search==3.8.3
google-generativeai==0.1.0
unstructured==0.8.1
ai21==1.2.6
typing-extensions==4.5.0
8 changes: 3 additions & 5 deletions superagi/controllers/organisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from superagi.helper.encyption_helper import decrypt_data
from superagi.helper.tool_helper import register_toolkits
from superagi.llms.google_palm import GooglePalm
from superagi.llms.llm_model_factory import build_model_with_api_key
from superagi.llms.openai import OpenAi
from superagi.models.configuration import Configuration
from superagi.models.organisation import Organisation
Expand Down Expand Up @@ -170,11 +171,8 @@ def get_llm_models(organisation=Depends(get_user_organisation)):
detail="Organisation not found")

decrypted_api_key = decrypt_data(model_api_key.value)
models = []
if model_source.value == "OpenAi":
models = OpenAi(api_key=decrypted_api_key).get_models()
elif model_source.value == "Google Palm":
models = GooglePalm(api_key=decrypted_api_key).get_models()
model = build_model_with_api_key(model_source.value, decrypted_api_key)
models = model.get_models() if model is not None else []

return models

Expand Down
136 changes: 135 additions & 1 deletion superagi/helper/github_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from superagi.types.storage_types import StorageType
from superagi.config.config import get_config
from superagi.helper.s3_helper import S3Helper

from datetime import timedelta, datetime
import json

class GithubHelper:
def __init__(self, github_access_token, github_username):
Expand Down Expand Up @@ -238,6 +239,7 @@ def add_file(self, repository_owner, repository_name, file_name, folder_path, he
logger.info('Failed to upload file content:', file_response.json()['message'])
return file_response.status_code


def create_pull_request(self, repository_owner, repository_name, head_branch, base_branch, headers):
"""
Creates a pull request in the given repository.
Expand Down Expand Up @@ -335,3 +337,135 @@ def _get_file_contents(self, file_name, agent_id, agent_execution_id, session):
with open(final_path, "r") as file:
attachment_data = file.read().decode('utf-8')
return attachment_data


def get_pull_request_content(self, repository_owner, repository_name, pull_request_number):
"""
Gets the content of a specific pull request from a GitHub repository.

Args:
repository_owner (str): Owner of the repository.
repository_name (str): Name of the repository.
pull_request_number (int): pull request id.
headers (dict): Dictionary containing the headers, usually including the Authorization token.

Returns:
dict: Dictionary containing the pull request content or None if not found.
"""
pull_request_url = f'https://api.github.com/repos/{repository_owner}/{repository_name}/pulls/{pull_request_number}'
headers = {
"Authorization": f"token {self.github_access_token}" if self.github_access_token else None,
"Content-Type": "application/vnd.github+json",
"Accept": "application/vnd.github.v3.diff",
}

response = requests.get(pull_request_url, headers=headers)

if response.status_code == 200:
logger.info('Successfully fetched pull request content.')
return response.text
elif response.status_code == 404:
logger.warning('Pull request not found.')
else:
logger.warning('Failed to fetch pull request content: ', response.text)

return None

def get_latest_commit_id_of_pull_request(self, repository_owner, repository_name, pull_request_number):
"""
Gets the latest commit id of a specific pull request from a GitHub repository.
:param repository_owner: owner
:param repository_name: repository name
:param pull_request_number: pull request id

:return:
latest commit id of the pull request
"""
url = f'https://api.github.com/repos/{repository_owner}/{repository_name}/pulls/{pull_request_number}/commits'
headers = {
"Authorization": f"token {self.github_access_token}" if self.github_access_token else None,
"Content-Type": "application/json",
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
commits = response.json()
latest_commit = commits[-1] # Assuming the last commit is the latest
return latest_commit.get('sha')
else:
logger.warning(f'Failed to fetch commits for pull request: {response.json()["message"]}')
return None


def add_line_comment_to_pull_request(self, repository_owner, repository_name, pull_request_number,
commit_id, file_path, position, comment_body):
"""
Adds a line comment to a specific pull request from a GitHub repository.

:param repository_owner: owner
:param repository_name: repository name
:param pull_request_number: pull request id
:param commit_id: commit id
:param file_path: file path
:param position: position
:param comment_body: comment body

:return:
dict: Dictionary containing the comment content or None if not found.
"""
comments_url = f'https://api.github.com/repos/{repository_owner}/{repository_name}/pulls/{pull_request_number}/comments'
headers = {
"Authorization": f"token {self.github_access_token}",
"Content-Type": "application/json",
"Accept": "application/vnd.github.v3+json"
}
data = {
"commit_id": commit_id,
"path": file_path,
"position": position,
"body": comment_body
}
response = requests.post(comments_url, headers=headers, json=data)
if response.status_code == 201:
logger.info('Successfully added line comment to pull request.')
return response.json()
else:
logger.warning(f'Failed to add line comment: {response.json()["message"]}')
return None

def get_pull_requests_created_in_last_x_seconds(self, repository_owner, repository_name, x_seconds):
"""
Gets the pull requests created in the last x seconds.

Args:
repository_owner (str): Owner of the repository
repository_name (str): Repository name
x_seconds (int): The number of seconds in the past to look for PRs

Returns:
list: List of pull request objects that were created in the last x seconds
"""
# Calculate the time x seconds ago
time_x_seconds_ago = datetime.utcnow() - timedelta(seconds=x_seconds)

# Convert to the ISO8601 format GitHub expects, remove milliseconds
time_x_seconds_ago_str = time_x_seconds_ago.strftime('%Y-%m-%dT%H:%M:%SZ')

# Search query
query = f'repo:{repository_owner}/{repository_name} type:pr created:>{time_x_seconds_ago_str}'

url = f'https://api.github.com/search/issues?q={query}'
headers = {
"Authorization": f"token {self.github_access_token}",
"Content-Type": "application/json",
}

response = requests.get(url, headers=headers)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here we can avoid using url and headers and make method small by passing them as param and avoid url,headers as temp var?


if response.status_code == 200:
pull_request_urls = []
for pull_request in response.json()['items']:
pull_request_urls.append(pull_request['html_url'])
return pull_request_urls
else:
logger.warning(f'Failed to fetch PRs: {response.json()["message"]}')
return []
12 changes: 12 additions & 0 deletions superagi/llms/llm_model_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,17 @@ def get_model(organisation_id, api_key, model="gpt-3.5-turbo", **kwargs):
elif provider_name == 'Hugging Face':
print("Provider is Hugging Face")
return HuggingFace(model=model_instance.model_name, end_point=model_instance.end_point, api_key=api_key, **kwargs)
else:
print('Unknown provider.')

def build_model_with_api_key(provider_name, api_key):
if provider_name.lower() == 'openai':
return OpenAi(api_key=api_key)
elif provider_name.lower() == 'replicate':
return Replicate(api_key=api_key)
elif provider_name.lower() == 'google palm':
return GooglePalm(api_key=api_key)
elif provider_name.lower() == 'hugging face':
return HuggingFace(api_key=api_key)
else:
print('Unknown provider.')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Print statement can be changed to logger

3 changes: 0 additions & 3 deletions superagi/resource_manager/llama_document_summary.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import os

from langchain.chat_models import ChatGooglePalm
from llama_index.indices.response import ResponseMode
from llama_index.schema import Document

from superagi.config.config import get_config
from superagi.lib.logger import logger
from superagi.types.model_source_types import ModelSourceType


class LlamaDocumentSummary:
Expand Down
66 changes: 66 additions & 0 deletions superagi/tools/github/fetch_pull_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from typing import Type, Optional

from pydantic import BaseModel, Field

from superagi.helper.github_helper import GithubHelper
from superagi.llms.base_llm import BaseLlm
from superagi.tools.base_tool import BaseTool


class GithubFetchPullRequestSchema(BaseModel):
repository_name: str = Field(
...,
description="Repository name in which file hase to be added",
)
repository_owner: str = Field(
...,
description="Owner of the github repository",
)
time_in_seconds: int = Field(
...,
description="Gets pull requests from last `time_in_seconds` seconds",
)


class GithubFetchPullRequest(BaseTool):
"""
Add File tool

Attributes:
name : The name.
description : The description.
args_schema : The args schema.
agent_id: The agent id.
agent_execution_id: The agent execution id.
"""
llm: Optional[BaseLlm] = None
name: str = "Github Fetch Pull Requests"
args_schema: Type[BaseModel] = GithubFetchPullRequestSchema
description: str = "Fetch pull requests from github"
agent_id: int = None
agent_execution_id: int = None

def _execute(self, repository_name: str, repository_owner: str, time_in_seconds: int = 86400) -> str:
"""
Execute the add file tool.

Args:
repository_name: The name of the repository to add file to.
repository_owner: Owner of the GitHub repository.
time_in_seconds: Gets pull requests from last `time_in_seconds` seconds

Returns:
List of all pull request ids
"""
try:
github_access_token = self.get_tool_config("GITHUB_ACCESS_TOKEN")
github_username = self.get_tool_config("GITHUB_USERNAME")
github_helper = GithubHelper(github_access_token, github_username)

pull_request_urls = github_helper.get_pull_requests_created_in_last_x_seconds(repository_owner,
repository_name,
time_in_seconds)

return "Pull requests: " + str(pull_request_urls)
except Exception as err:
return f"Error: Unable to fetch pull requests {err}"
5 changes: 4 additions & 1 deletion superagi/tools/github/github_toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from superagi.tools.base_tool import BaseTool, BaseToolkit, ToolConfiguration
from superagi.tools.github.add_file import GithubAddFileTool
from superagi.tools.github.delete_file import GithubDeleteFileTool
from superagi.tools.github.fetch_pull_request import GithubFetchPullRequest
from superagi.tools.github.search_repo import GithubRepoSearchTool
from superagi.tools.github.review_pull_request import GithubReviewPullRequest
from superagi.types.key_type import ToolConfigKeyType


Expand All @@ -12,7 +14,8 @@ class GitHubToolkit(BaseToolkit, ABC):
description: str = "GitHub Tool Kit contains all github related to tool"

def get_tools(self) -> List[BaseTool]:
return [GithubAddFileTool(), GithubDeleteFileTool(), GithubRepoSearchTool()]
return [GithubAddFileTool(), GithubDeleteFileTool(), GithubRepoSearchTool(), GithubReviewPullRequest(),
GithubFetchPullRequest()]

def get_env_keys(self) -> List[ToolConfiguration]:
return [
Expand Down
50 changes: 50 additions & 0 deletions superagi/tools/github/prompts/code_review.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Your purpose is to act as a highly experienced software engineer and provide a thorough review of the code chunks and suggest code snippets to improve key areas such as:
- Logic
- Modularity
- Maintainability
- Complexity

Do not comment on minor code style issues, missing comments/documentation. Identify and resolve significant concerns to improve overall code quality while deliberately disregarding minor issues

Following is the github pull request diff content:
```
{{DIFF_CONTENT}}
```

Instructions:
1. Do not comment on existing lines and deleted lines.
2. Ignore the lines start with '-'.
3. Only consider lines starting with '+' for review.
4. Do not comment on frontend and graphql code.

Respond with only valid JSON conforming to the following schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"comments": {
"type": "array",
"items": {
"type": "object",
"properties": {
"file_path": {
"type": "string",
"description": "The path to the file where the comment should be added."
},
"line": {
"type": "integer",
"description": "The line number where the comment should be added. "
},
"comment": {
"type": "string",
"description": "The content of the comment."
}
},
"required": ["file_name", "line", "comment"]
}
}
},
"required": ["comments"]
}

Ensure response is valid JSON conforming to the following schema.
Loading