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

feat: Add auto-PR description action #20

Merged
merged 1 commit into from
May 20, 2024
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
7 changes: 7 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Problem

# Solution

# Testing

# Reverting
43 changes: 43 additions & 0 deletions .github/workflows/generate_pull_request_description.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Generate PR Description

on:
pull_request:
types: [opened, synchronize]

jobs:
generate-pull-request-description:
name: Write PR Description
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.ref }}

- name: Setup Python
uses: actions/setup-python@v3
with:
python-version: "3.12"

- name: Install Dependencies
run: |
cd ./ai
python -m pip install --upgrade pip
pip install -r requirements.txt

- name: Get PR Info
id: pr_info
run: |
pr_num=${{ github.event.pull_request.number }}
echo "pull_request_number=$pr_num" >> $GITHUB_ENV

- name: Generate PR Description
env:
BRANCH_NAME: ${{ github.event.pull_request.head.ref }}
GITHUB_TOKEN: ${{ secrets.MY_GITHUB_TOKEN }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
PINECONE_API_KEY: ${{ secrets.PINECONE_API_KEY }}
PR_NUMBER: ${{ env.pull_request_number }}
REPO_PATH: ${{ github.repository }}
run: python ./ai/main.py
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Nathan Thomas

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
162 changes: 162 additions & 0 deletions ai/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
60 changes: 60 additions & 0 deletions ai/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from github import Github
from src.constants import *
from src.utility import *
import random
import json
import sys

def main():
# Initialize PyGithub
g = Github(GITHUB_API_KEY)

# Fetch repository
repo = g.get_repo(REPO_PATH)
pull_request = repo.get_pull(PULL_REQUEST_NUMBER)

# Get pull request template content
root_dir = os.getcwd()
file_path = os.path.join(root_dir, PULL_REQUEST_TEMPLATE_PATH)
with open(file_path, "r") as file:
pull_request_description_template = file.read()

# Get all current pull request diffs
pull_request_diffs = [
{
"filename": file.filename,
"patch": file.patch
}
for file in pull_request.get_files()
]

# Get all current pull request commit messages and convert to JSON
commit_messages = [commit.commit.message for commit in pull_request.get_commits()]
json_pull_request_diffs = json.dumps(pull_request_diffs)

# Do embeddings on codebase
current_index_name = "autopr"
embed_documents(repo, current_index_name)

# Search embeddings
codebase_context = get_embeddings_for_diffs(current_index_name, pull_request_diffs)

# Delete index and codebase embeddings
delete_embeddings_for_codebase(current_index_name)

# Build prompt
prompt = format_data_for_prompt(
pull_request_diffs,
commit_messages,
codebase_context,
pull_request_description_template,
)

# Call model for PR description
pr_description = generate_pr_description(prompt)

# Update PR description
update_pr_description(repo, PULL_REQUEST_NUMBER, pr_description)

if __name__ == '__main__':
main()
77 changes: 77 additions & 0 deletions ai/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
aiohttp==3.9.5
aiosignal==1.3.1
annotated-types==0.6.0
anyio==4.3.0
attrs==23.2.0
backoff==2.2.1
beautifulsoup4==4.12.3
certifi==2024.2.2
cffi==1.16.0
chardet==5.2.0
charset-normalizer==3.3.2
click==8.1.7
cryptography==42.0.7
dataclasses-json==0.6.6
deepdiff==7.0.1
Deprecated==1.2.14
distro==1.9.0
emoji==2.11.1
filetype==1.2.0
frozenlist==1.4.1
h11==0.14.0
httpcore==1.0.5
httpx==0.27.0
idna==3.7
joblib==1.4.2
jsonpatch==1.33
jsonpath-python==1.0.6
jsonpointer==2.4
langchain==0.2.0
langchain-community==0.2.0
langchain-core==0.2.0
langchain-openai==0.1.7
langchain-pinecone==0.1.1
langchain-text-splitters==0.2.0
langdetect==1.0.9
langsmith==0.1.59
lxml==5.2.2
Markdown==3.6
marshmallow==3.21.2
multidict==6.0.5
mypy-extensions==1.0.0
nltk==3.8.1
numpy==1.26.4
openai==1.30.1
ordered-set==4.1.0
orjson==3.10.3
packaging==23.2
pinecone-client==3.2.2
pycparser==2.22
pydantic==2.7.1
pydantic_core==2.18.2
PyGithub==2.3.0
PyJWT==2.8.0
PyNaCl==1.5.0
pypdf==4.2.0
python-dateutil==2.9.0.post0
python-iso639==2024.4.27
python-magic==0.4.27
PyYAML==6.0.1
rapidfuzz==3.9.1
regex==2024.5.15
requests==2.31.0
six==1.16.0
sniffio==1.3.1
soupsieve==2.5
SQLAlchemy==2.0.30
tabulate==0.9.0
tenacity==8.3.0
tiktoken==0.7.0
tqdm==4.66.4
typing-inspect==0.9.0
typing_extensions==4.11.0
unstructured==0.11.8
unstructured-client==0.22.0
urllib3==2.2.1
wrapt==1.16.0
yarl==1.9.4
11 changes: 11 additions & 0 deletions ai/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from setuptools import setup, find_packages

with open("requirements.txt") as f:
requirements = f.read().splitlines()

setup(
name="ai",
version="0.0.0-alpha",
packages=find_packages(),
install_requires=requirements,
)
10 changes: 10 additions & 0 deletions ai/src/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import os

BRANCH_NAME = os.getenv("BRANCH_NAME", "")
EMBEDDING_MODEL="text-embedding-3-large"
GITHUB_API_KEY = os.getenv("GITHUB_TOKEN", "")
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "")
PINECONE_API_KEY = os.getenv("PINECONE_API_KEY", "")
PULL_REQUEST_NUMBER = int(os.getenv('PR_NUMBER', "0"))
PULL_REQUEST_TEMPLATE_PATH = ".github/PULL_REQUEST_TEMPLATE.md"
REPO_PATH = os.getenv('REPO_PATH', "")
Loading
Loading