Skip to content

Update generate_pr_content.py #8

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

Closed
wants to merge 1 commit into from
Closed
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
48 changes: 30 additions & 18 deletions .github/scripts/generate_pr_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,58 @@
import requests
import subprocess


def setup_git():
# Set up Git by fetching all branches and tags
subprocess.run(['git', 'fetch', '--unshallow', '--tags', '--force'], check=True)
subprocess.run(['git', 'fetch', 'origin', '+refs/heads/*:refs/remotes/origin/*'], check=True)

def get_pr_diff():
base_branch = os.getenv('GITHUB_BASE_REF', 'main')
current_branch = os.getenv('GITHUB_HEAD_REF', 'main')
try:
# Checkout the base branch
subprocess.run(['git', 'checkout', f'origin/{base_branch}'], check=True)
# Get diff between base and current PR branch
diff = subprocess.check_output(['git', 'diff', f'origin/{base_branch}', f'origin/{current_branch}'], text=True)
except subprocess.CalledProcessError:
diff = "Error fetching diff or no changes to compare."
return diff

def call_api(url, headers, payload):
response = requests.post(url, headers=headers, json=payload)
if response.status_code != 200:
raise Exception(f"API request failed with status {response.status_code}: {response.text}")
return response.json()

def generate_summary(api_key, engine_url, prompt):
def generate_summary(api_key, prompt):
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
payload = {
"model": "code-davinci-002",
"model": "gpt-3.5-turbo",
"prompt": prompt,
"max_tokens": 150
"max_tokens": 200,
"temperature": 0.5
}
engine_url = "https://api.openai.com/v1/completions"
return call_api(engine_url, headers, payload)

def get_git_diff():
try:
diff = subprocess.check_output(['git', 'diff', 'HEAD^', 'HEAD'], text=True)
except subprocess.CalledProcessError:
diff = "No previous commit to compare."
return diff

def main():
diff = get_git_diff()
prompt = f"Summarize the following code changes in a detailed and formatted manner:\n{diff}"
setup_git()
diff = get_pr_diff()
prompt = f"Please summarize and analyze the following code changes:\n{diff}"

openai_summary = generate_summary(
os.getenv('OPENAI_API_KEY'),
"https://api.openai.com/v1/completions",
prompt
)["choices"][0]["text"]

# Assuming anthropic_summary setup remains the same
# Include similar error handling and model updating for Anthropic API as needed

formatted_content = f"## OpenAI Summary\n{openai_summary}\n\n## Further details to be added as required."
print(f"::set-output name=pr_content::{json.dumps(formatted_content)}")
print("Summary Generated Successfully: ", openai_summary)
# Print to output for GitHub Actions to use
print(f"::set-output name=pr_content::{json.dumps(openai_summary)}")

if __name__ == "__main__":
main()

Loading