Skip to content

Commit

Permalink
fix: fail open when not running on a PR
Browse files Browse the repository at this point in the history
  • Loading branch information
JonZeolla committed Jun 3, 2024
1 parent 25d71c4 commit 109703a
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions code_review.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@
salacious-code-reviews script entrypoint
"""

import ast
import json
import os
import re
import json
import ast

import openai
from github import Github, PullRequest, GithubException
from github import Github, GithubException, PullRequest
from openai import OpenAI

from code_reviews import config, constants


def get_github_session() -> Github:
log.info("Creating GitHub Session...")

if not os.getenv("GITHUB_TOKEN"):
log.error("Please provide a valid GITHUB_TOKEN environment variable!")
raise SystemExit(1)
log.error("No GITHUB_TOKEN environment variable was detected")
raise SystemExit(0)

return Github(os.getenv("GITHUB_TOKEN"))

Expand All @@ -27,8 +29,8 @@ def get_openai_session() -> OpenAI:
log.info("Setting up OpenAI Session...")

if not os.getenv("OPENAI_API_KEY"):
log.error("Please provide a valid OPENAI_API_KEY environment variable!")
raise SystemExit(1)
log.error("No OPENAI_API_KEY environment variable was detected")
raise SystemExit(0)

return OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

Expand All @@ -42,7 +44,7 @@ def get_repo_and_pr() -> dict:
log.info("Getting repo and pull request from runner environment...")

if not github_ref:
log.error("Cannot find pull request, exiting!")
log.error("Failed to find the GITHUB_REF environment variable")
raise SystemExit(1)

# If the split from GITHUB_REF is not an int, exit
Expand All @@ -56,8 +58,8 @@ def get_repo_and_pr() -> dict:
log.error(f"GITHUB_REF does not contain a valid PR number: {github_ref}")
raise SystemExit(1)
else:
log.error("GITHUB_REF does not reference a pull request")
raise SystemExit(1)
log.warn("Not running on a pull request; skipping the Salacious code review...")
raise SystemExit(0)

if not github_repo:
log.error("Cannot get repository name, exiting!")
Expand Down Expand Up @@ -209,10 +211,10 @@ def sanitize_json_string(json_string):
"ChatCompletion object does not contain expected 'choices' or 'message' structure"
)

except openai.APIError as err:
log.error(f"Salacious failed due to API error: {err}")
except openai.RateLimitError as err:
log.error(f"Salacious failed due to an exceeded rate limit: {err}")
except openai.APIError as err:
log.error(f"Salacious failed due to API error: {err}")
except Exception as e:
log.error(f"Salacious failed due to unexpected error during API call: {str(e)}")

Expand Down

0 comments on commit 109703a

Please sign in to comment.