Skip to content

Commit de140fe

Browse files
authored
fix: fail open when not running on a PR (#196)
1 parent 25d71c4 commit de140fe

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

code_review.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,24 @@
33
salacious-code-reviews script entrypoint
44
"""
55

6+
import ast
7+
import json
68
import os
79
import re
8-
import json
9-
import ast
10+
1011
import openai
11-
from github import Github, PullRequest, GithubException
12+
from github import Github, GithubException, PullRequest
1213
from openai import OpenAI
14+
1315
from code_reviews import config, constants
1416

1517

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

1921
if not os.getenv("GITHUB_TOKEN"):
20-
log.error("Please provide a valid GITHUB_TOKEN environment variable!")
21-
raise SystemExit(1)
22+
log.error("No GITHUB_TOKEN environment variable was detected")
23+
raise SystemExit(0)
2224

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

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

2931
if not os.getenv("OPENAI_API_KEY"):
30-
log.error("Please provide a valid OPENAI_API_KEY environment variable!")
31-
raise SystemExit(1)
32+
log.error("No OPENAI_API_KEY environment variable was detected")
33+
raise SystemExit(0)
3234

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

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

4446
if not github_ref:
45-
log.error("Cannot find pull request, exiting!")
47+
log.error("Failed to find the GITHUB_REF environment variable")
4648
raise SystemExit(1)
4749

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

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

212-
except openai.APIError as err:
213-
log.error(f"Salacious failed due to API error: {err}")
214214
except openai.RateLimitError as err:
215215
log.error(f"Salacious failed due to an exceeded rate limit: {err}")
216+
except openai.APIError as err:
217+
log.error(f"Salacious failed due to API error: {err}")
216218
except Exception as e:
217219
log.error(f"Salacious failed due to unexpected error during API call: {str(e)}")
218220

0 commit comments

Comments
 (0)