Commit 0be3c3e0 authored by Iswarabhotla Sunder's avatar Iswarabhotla Sunder

This is a new file

parent 255b6940
import os
import requests
from pinecone import Pinecone, ServerlessSpec
import re
import pinecone # Ensure to import pinecone correctly
# Initialize Pinecone instance with your API key
pc = Pinecone(api_key="pcsk_5L6s57_QRcqx63DUvLEk7bzohW7aYCuLChYPVZH9PX55sw49f2Uyt2CHqSGE9ERPGPBwnE",
environment="us-east-1-gcp") # Specify the environment if needed (e.g., "us-west1-gcp")
# Function to fetch pull request details from GitHub
def get_pull_requests(repo_owner, repo_name):
pull_requests_url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/pulls"
print(pull_requests_url)
response = requests.get(pull_requests_url)
if response.status_code == 200:
pull_requests = response.json()
for pr in pull_requests:
pr_number = pr['number']
pr_title = pr['title']
pr_owner = pr['user']['login'] # PR owner
reviewer_comments = get_review_comments(repo_owner, repo_name, pr_number)
store_in_pinecone(pr_number, pr_title, pr_owner, reviewer_comments)
else:
print(f"Failed to fetch pull requests: {response.status_code}")
print(f"Response: {response.text}")
# Function to fetch review comments for a pull request
def get_review_comments(repo_owner, repo_name, pr_number):
comments_url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/merge-requests/{pr_number}/comments"
response = requests.get(comments_url)
comments = []
if response.status_code == 200:
review_comments = response.json()
for comment in review_comments:
comment_author = comment['user']['login']
comment_body = comment['body']
comments.append({'author': comment_author, 'body': comment_body})
else:
print(f"Failed to fetch comments for PR #{pr_number}: {response.status_code}")
return comments
# Function to sanitize and create index name
def create_pinecone_index(pr_number):
index_name = f"pr-{pr_number}".lower() # Ensure the index name is lowercase
index_name = re.sub(r'[^a-z0-9-]', '-', index_name) # Replace non-alphanumeric characters with hyphens
# Check if index already exists
if index_name not in pc.list_indexes().names():
pc.create_index(
name=index_name,
dimension=1536, # Modify this based on your data's dimensionality
metric='euclidean', # You can also use cosine or others
spec=ServerlessSpec(cloud='aws', region='us-east-1')
)
return index_name
# Function to store PR details in Pinecone
def store_in_pinecone(pr_number, pr_title, pr_owner, reviewer_comments):
# Constructing the data to be stored in Pinecone
index_name = create_pinecone_index(pr_number)
# Store each reviewer comment along with the PR info
pr_data = []
for comment in reviewer_comments:
reviewer_name = comment['author']
comment_body = comment['body']
pr_data.append({
"pr_number": pr_number,
"pr_title": pr_title,
"pr_owner": pr_owner,
"reviewer_name": reviewer_name, # Adding reviewer name
"comment_body": comment_body # Store individual comments
})
# Accessing the created index using Pinecone Index class
index = pinecone.Index(index_name, host="us-east-1-gcp.pinecone.io") # Specify the host URL for your environment
# Insert PR data into Pinecone (you'll typically convert it to vectors, here we store raw data)
for pr in pr_data:
pr_vector = [float(pr_number)] # Ensure the vector data is of type float
index.upsert(vectors=[(str(pr_number), pr_vector, pr)]) # Upsert the data
print(f"Stored PR #{pr_number} and comment from {pr_data[0]['reviewer_name']} in Pinecone.")
# Main function to start fetching PRs
def main():
# Replace with your GitHub repo owner and name
repo_owner = "isunder" # GitHub username or organization name
repo_name = "ai-github-demo" # Repository name
get_pull_requests(repo_owner, repo_name)
if __name__ == "__main__":
main()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment