1- import json
2- import os
31import 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
5056if __name__ == "__main__" :
0 commit comments