Skip to content

Commit bed36cd

Browse files
authored
Merge pull request #2 from rsb-23/develop
PR comment token fix
2 parents bed4786 + 33f6b0e commit bed36cd

File tree

11 files changed

+86
-51
lines changed

11 files changed

+86
-51
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ jobs:
1212
fetch-depth: 0
1313

1414
- name: Run script
15-
id: spelling
1615
uses: ./
1716
with:
17+
token: ${{ secrets.PAT }}
1818
files: |
1919
tests/file.txt
2020
tests/file.json

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# actions/check-typo
2+
3+
This Github Action uses AI to find typos and grammatical errors in PR changes.
4+
5+
## Usage
6+
Refer [test.yml](./.github/workflows/test.yml)
7+
```yaml
8+
- name: Check typos in data files
9+
uses: actions/check-diff-typo@v1
10+
with:
11+
token: ${{ secrets.PAT }}
12+
files: |
13+
tests/file.txt
14+
tests/file.json
15+
GROQ_API_KEY: ${{ secrets.GROQ_API_KEY }}
16+
```

action.yml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
name: 'Check-Diff-Typo'
2-
description: 'Analyzes and comments on PR diff typos for specified files'
1+
name: 'Check-Typo'
2+
description: 'Checks diff of specified files for typos and suggests fixes on PR.'
33
author: 'Rishabh B'
44
branding:
55
icon: 'git-pull-request'
66
color: 'green'
77

88
inputs:
9+
token:
10+
description: 'PAT with `pull_request:write` permission'
11+
required: true
12+
default: ${{ github.token }}
913
files:
1014
description: 'Comma-separated list of files to analyze'
1115
required: true
@@ -14,10 +18,6 @@ inputs:
1418
description: 'Groq API Key'
1519
required: true
1620

17-
outputs:
18-
comment:
19-
description: 'Generated comment markdown'
20-
value: ${{ steps.spelling.outputs.comment }}
2121

2222
runs:
2323
using: "composite"
@@ -28,6 +28,8 @@ runs:
2828
cache: 'pip'
2929
- name: Setup python env
3030
run: |
31+
python -m venv .venv
32+
source .venv/bin/activate
3133
python -m pip install --upgrade pip
3234
pip install -r requirements.txt
3335
shell: bash
@@ -40,13 +42,11 @@ runs:
4042
id: spelling
4143
shell: bash
4244
env:
43-
PR_BASE: ${{ github.event.pull_request.base.ref }}
45+
PR_BASE: ${{ github.base_ref }}
4446
INPUT_FILES: ${{ inputs.files }}
4547
GROQ_API_KEY: ${{ inputs.GROQ_API_KEY }}
46-
run: python src/spell_check.py
47-
48-
- name: Comment PR
49-
uses: peter-evans/create-or-update-comment@v4
50-
with:
51-
issue-number: ${{ github.event.pull_request.number }}
52-
body: ${{ steps.spelling.outputs.comment }}
48+
token: ${{ inputs.token }}
49+
PR_NO: ${{ github.event.pull_request.number }}
50+
run: |
51+
source .venv/bin/activate
52+
python main.py

local_test.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
1-
import os
2-
import tempfile
3-
4-
from src.spell_check import main
5-
6-
7-
def run():
8-
if "GITHUB_OUTPUT" not in os.environ:
9-
from dotenv import load_dotenv
10-
11-
load_dotenv()
12-
with tempfile.NamedTemporaryFile(mode="w+", delete_on_close=False) as f:
13-
os.environ["GITHUB_OUTPUT"] = f.name
14-
main()
1+
from dotenv import load_dotenv
152

3+
load_dotenv()
164

175
if __name__ == "__main__":
18-
run()
6+
from src.spell_check import main
7+
8+
main()

main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from src.spell_check import main
2+
3+
if __name__ == "__main__":
4+
main()

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
groq==0.13.0
2+
requests~=2.32.3

src/__init__.py

Whitespace-only changes.

src/config.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import os
2+
3+
4+
def get_env(x):
5+
return os.environ[x]
6+
7+
8+
GROQ_API_KEY = get_env("GROQ_API_KEY")
9+
PAT_TOKEN = get_env("token")
10+
11+
REPO = get_env("GITHUB_REPOSITORY")
12+
PR_BASE = get_env("PR_BASE")
13+
PR_NO = get_env("PR_NO")
14+
INPUT_FILES = [*map(str.strip, get_env("INPUT_FILES").splitlines())]

src/groq_ai.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import json
2-
import os
32
from functools import cache
43

54
from groq import NOT_GIVEN, BadRequestError, Groq
65

6+
from src.config import GROQ_API_KEY
7+
78
model = ["llama-3.1-8b-instant", "llama3-70b-8192"][1]
89

910

1011
@cache
1112
def get_groq_client():
12-
return Groq(api_key=os.environ.get("GROQ_API_KEY"))
13+
return Groq(api_key=GROQ_API_KEY)
1314

1415

1516
def ask_groq(query_text: str, system_content: str, json_schema: dict = None) -> dict:
@@ -25,16 +26,19 @@ def ask_groq(query_text: str, system_content: str, json_schema: dict = None) ->
2526
response_format=response_format,
2627
)
2728
json_str = chat_completion.choices[0].message.content
29+
json_str = json_str.strip()
2830
except BadRequestError as e:
2931
failed_json = e.body["error"]["failed_generation"] # noqa
3032
json_str = failed_json.replace('"""', '"').replace("\n", "\\n")
3133
if not json_schema:
3234
return json_str
3335
try:
34-
return json.loads(json_str.replace("\n", ""))
36+
# json_str = re.sub("\n +", "", json_str)
37+
return json.loads(json_str)
3538
except json.JSONDecodeError as e:
36-
print(e.doc, e.pos)
37-
raise
39+
print(e.doc.encode(), e.pos)
40+
print(f"ERROR : {e.args}")
41+
return {"suggestions": ["..."]}
3842

3943

4044
def find_typos(query_text):

src/spell_check.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,43 @@
1-
import json
2-
import os
31
import subprocess
42

5-
from groq_ai import find_typos
3+
import requests
64

5+
from src.config import INPUT_FILES, PAT_TOKEN, PR_BASE, PR_NO, REPO
6+
from src.groq_ai import find_typos
77

8-
def process_diff(file_path, base_branch):
8+
9+
def process_diff(file_path, base_branch=PR_BASE):
910
try:
1011
# Get diff from PR
1112
diff_command = f"git diff -U0 origin/{base_branch}... -- {file_path}"
1213
diff_output = subprocess.check_output(diff_command.split()).decode("utf-8")
1314
except subprocess.CalledProcessError:
1415
print("Error in process diff")
15-
return 0
16+
return -1
1617

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

2122

22-
def main():
23-
# Get required inputs
24-
pr_base = os.environ["PR_BASE"]
25-
files = [*map(str.strip, os.environ["INPUT_FILES"].splitlines())]
26-
print(pr_base, *files, sep="\n")
23+
def post_comment(comment):
24+
url = f"https://api.github.com/repos/{REPO}/issues/{PR_NO}/comments"
25+
headers = {
26+
"Accept": "application/vnd.github+json",
27+
"Authorization": f"Bearer {PAT_TOKEN}",
28+
"X-GitHub-Api-Version": "2022-11-28",
29+
}
30+
resp = requests.post(url, headers=headers, json={"body": comment})
31+
resp.raise_for_status()
2732

28-
results = {file_path: process_diff(file_path, pr_base) for file_path in files if file_path}
33+
34+
def main():
35+
# Process diff(s) for each file
36+
results = {file_path: process_diff(file_path) for file_path in INPUT_FILES if file_path}
2937

3038
# Create markdown comment for fixes
3139
flag = False
32-
comment = "## Suggested Typo Fixes\n\n"
40+
comment = "### Suggested Typo Fixes\n"
3341
for file_path, suggestion in results.items():
3442
if suggestion == -1:
3543
continue
@@ -41,10 +49,8 @@ def main():
4149
else:
4250
comment = "### No typos found"
4351

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

4955

5056
if __name__ == "__main__":

0 commit comments

Comments
 (0)