Skip to content

Commit

Permalink
Merge pull request #2 from rsb-23/develop
Browse files Browse the repository at this point in the history
PR comment token fix
  • Loading branch information
rsb-23 authored Dec 1, 2024
2 parents bed4786 + 33f6b0e commit bed36cd
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ jobs:
fetch-depth: 0

- name: Run script
id: spelling
uses: ./
with:
token: ${{ secrets.PAT }}
files: |
tests/file.txt
tests/file.json
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# actions/check-typo

This Github Action uses AI to find typos and grammatical errors in PR changes.

## Usage
Refer [test.yml](./.github/workflows/test.yml)
```yaml
- name: Check typos in data files
uses: actions/check-diff-typo@v1
with:
token: ${{ secrets.PAT }}
files: |
tests/file.txt
tests/file.json
GROQ_API_KEY: ${{ secrets.GROQ_API_KEY }}
```
28 changes: 14 additions & 14 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
name: 'Check-Diff-Typo'
description: 'Analyzes and comments on PR diff typos for specified files'
name: 'Check-Typo'
description: 'Checks diff of specified files for typos and suggests fixes on PR.'
author: 'Rishabh B'
branding:
icon: 'git-pull-request'
color: 'green'

inputs:
token:
description: 'PAT with `pull_request:write` permission'
required: true
default: ${{ github.token }}
files:
description: 'Comma-separated list of files to analyze'
required: true
Expand All @@ -14,10 +18,6 @@ inputs:
description: 'Groq API Key'
required: true

outputs:
comment:
description: 'Generated comment markdown'
value: ${{ steps.spelling.outputs.comment }}

runs:
using: "composite"
Expand All @@ -28,6 +28,8 @@ runs:
cache: 'pip'
- name: Setup python env
run: |
python -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
pip install -r requirements.txt
shell: bash
Expand All @@ -40,13 +42,11 @@ runs:
id: spelling
shell: bash
env:
PR_BASE: ${{ github.event.pull_request.base.ref }}
PR_BASE: ${{ github.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:
issue-number: ${{ github.event.pull_request.number }}
body: ${{ steps.spelling.outputs.comment }}
token: ${{ inputs.token }}
PR_NO: ${{ github.event.pull_request.number }}
run: |
source .venv/bin/activate
python main.py
20 changes: 5 additions & 15 deletions local_test.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
import os
import tempfile

from src.spell_check import main


def run():
if "GITHUB_OUTPUT" not in os.environ:
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()
from dotenv import load_dotenv

load_dotenv()

if __name__ == "__main__":
run()
from src.spell_check import main

main()
4 changes: 4 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from src.spell_check import main

if __name__ == "__main__":
main()
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
Empty file added src/__init__.py
Empty file.
14 changes: 14 additions & 0 deletions src/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import os


def get_env(x):
return os.environ[x]


GROQ_API_KEY = get_env("GROQ_API_KEY")
PAT_TOKEN = get_env("token")

REPO = get_env("GITHUB_REPOSITORY")
PR_BASE = get_env("PR_BASE")
PR_NO = get_env("PR_NO")
INPUT_FILES = [*map(str.strip, get_env("INPUT_FILES").splitlines())]
14 changes: 9 additions & 5 deletions src/groq_ai.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import json
import os
from functools import cache

from groq import NOT_GIVEN, BadRequestError, Groq

from src.config import GROQ_API_KEY

model = ["llama-3.1-8b-instant", "llama3-70b-8192"][1]


@cache
def get_groq_client():
return Groq(api_key=os.environ.get("GROQ_API_KEY"))
return Groq(api_key=GROQ_API_KEY)


def ask_groq(query_text: str, system_content: str, json_schema: dict = None) -> dict:
Expand All @@ -25,16 +26,19 @@ def ask_groq(query_text: str, system_content: str, json_schema: dict = None) ->
response_format=response_format,
)
json_str = chat_completion.choices[0].message.content
json_str = json_str.strip()
except BadRequestError as e:
failed_json = e.body["error"]["failed_generation"] # noqa
json_str = failed_json.replace('"""', '"').replace("\n", "\\n")
if not json_schema:
return json_str
try:
return json.loads(json_str.replace("\n", ""))
# json_str = re.sub("\n +", "", json_str)
return json.loads(json_str)
except json.JSONDecodeError as e:
print(e.doc, e.pos)
raise
print(e.doc.encode(), e.pos)
print(f"ERROR : {e.args}")
return {"suggestions": ["..."]}


def find_typos(query_text):
Expand Down
36 changes: 21 additions & 15 deletions src/spell_check.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
import json
import os
import subprocess

from groq_ai import find_typos
import requests

from src.config import INPUT_FILES, PAT_TOKEN, PR_BASE, PR_NO, REPO
from src.groq_ai import find_typos

def process_diff(file_path, base_branch):

def process_diff(file_path, base_branch=PR_BASE):
try:
# Get diff from PR
diff_command = f"git diff -U0 origin/{base_branch}... -- {file_path}"
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 main():
# Get required inputs
pr_base = os.environ["PR_BASE"]
files = [*map(str.strip, os.environ["INPUT_FILES"].splitlines())]
print(pr_base, *files, sep="\n")
def post_comment(comment):
url = f"https://api.github.com/repos/{REPO}/issues/{PR_NO}/comments"
headers = {
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {PAT_TOKEN}",
"X-GitHub-Api-Version": "2022-11-28",
}
resp = requests.post(url, headers=headers, json={"body": comment})
resp.raise_for_status()

results = {file_path: process_diff(file_path, pr_base) for file_path in files if file_path}

def main():
# Process diff(s) for each file
results = {file_path: process_diff(file_path) for file_path in INPUT_FILES if file_path}

# Create markdown comment for fixes
flag = False
comment = "## Suggested Typo Fixes\n\n"
comment = "### Suggested Typo Fixes\n"
for file_path, suggestion in results.items():
if suggestion == -1:
continue
Expand All @@ -41,10 +49,8 @@ def main():
else:
comment = "### No typos found"

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


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion tests/file.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"fruit1": "pineaple", "friut2": "gauva"}
{"fruit1": "pineaple", "friut2": "graep"}

0 comments on commit bed36cd

Please sign in to comment.