Skip to content

Commit

Permalink
Using PR comment api
Browse files Browse the repository at this point in the history
  • Loading branch information
rsb-23 committed Dec 1, 2024
1 parent 26b349d commit acfb805
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
10 changes: 3 additions & 7 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ runs:
PR_BASE: ${{ github.event.pull_request.base.ref }}
INPUT_FILES: ${{ inputs.files }}
GROQ_API_KEY: ${{ inputs.GROQ_API_KEY }}
run: python src/spell_check.py

- name: Comment PR
uses: peter-evans/create-or-update-comment@v4
with:
token: ${{ inputs.token }}
issue-number: ${{ github.event.pull_request.number }}
body: ${{ steps.spelling.outputs.comment }}
PR_NO: ${{ github.event.pull_request.number }}
repo: $GITHUB_REPOSITORY
run: python src/spell_check.py
12 changes: 3 additions & 9 deletions local_test.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import os
import tempfile

from src.spell_check import main


def run():
if "GITHUB_OUTPUT" not in os.environ:
from dotenv import load_dotenv
from dotenv import load_dotenv

load_dotenv()
with tempfile.NamedTemporaryFile(mode="w+", delete_on_close=False) as f:
os.environ["GITHUB_OUTPUT"] = f.name
main()
load_dotenv()
main()


if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
groq==0.13.0
requests~=2.32.3
3 changes: 2 additions & 1 deletion src/groq_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def ask_groq(query_text: str, system_content: str, json_schema: dict = None) ->
return json.loads(json_str.replace("\n", ""))
except json.JSONDecodeError as e:
print(e.doc, e.pos)
raise
print(f"ERROR : {e.args}")
return {"suggestions": "..."}


def find_typos(query_text):
Expand Down
23 changes: 18 additions & 5 deletions src/spell_check.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import subprocess

import requests

from groq_ai import find_typos


Expand All @@ -11,13 +13,26 @@ def process_diff(file_path, base_branch):
diff_output = subprocess.check_output(diff_command.split()).decode("utf-8")
except subprocess.CalledProcessError:
print("Error in process diff")
return 0
return -1

# only checks for typos in added text
new_text = "".join(x for x in diff_output.splitlines(keepends=True) if x.startswith("+"))
return find_typos(new_text) if new_text else -1


def post_comment(comment):
pr_no = os.environ["PR_NO"]
repo = os.environ["repo"]
url = f"https://api.github.com/repos/{repo}/issues/{pr_no}/comments"
headers = {
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {os.environ['token']}",
"X-GitHub-Api-Version": "2022-11-28",
}
resp = requests.post(url, headers=headers, json={"body": comment})
resp.raise_for_status()


def main():
# Get required inputs
pr_base = os.environ["PR_BASE"]
Expand All @@ -36,14 +51,12 @@ def main():
comment += f"- {file_path}:\n {suggestion}\n"
flag = True
if flag:
comment += "**Tip**: Create a commit in this PR itself."
comment += "\n**Tip**: Create a commit in this PR itself."
else:
comment = "### No typos found"

# Set output for GitHub Actions
with open(os.environ["GITHUB_OUTPUT"], "a") as f:
f.write(f"comment={comment.encode()}\n")
print(comment)
post_comment(comment)


if __name__ == "__main__":
Expand Down

0 comments on commit acfb805

Please sign in to comment.