This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.py
90 lines (74 loc) · 2.36 KB
/
entrypoint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from github import Github
import argparse
import os
import requests
import json
import re
from difflib import get_close_matches
from urllib.parse import urlparse
def setup_args():
parser = argparse.ArgumentParser()
parser.add_argument("--token", help="A GitHub token for the repo")
return parser.parse_args()
def fuzzy_review_comment_check(comment_body):
first_word = comment_body.split()[0]
if first_word != "⚠️":
first_word = re.sub('[^A-Za-z0-9]+', '', first_word) #removing special character
match = get_close_matches(first_word, pref_list, cutoff=0.8)
if not match:
return False
else:
return True
def parse_review_comment(data, github):
for comment in data:
if "in_reply_to_id" not in comment:
if not fuzzy_review_comment_check(comment["body"]):
review_comment_edit(comment["id"], github, comment["body"])
def review_comment_edit(id, github, body):
url = "https://api.github.com/repos/{}/pulls/comments/{}".format(
repository_name, id
)
headers = {
"Accept": "application/vnd.github.v3+json",
"Authorization": "Bearer " + str(github),
}
print(headers)
payload = {
"body": "⚠️ " + str(body)
+ " \n\n⚠️ [PR Comment Etiquette](https://github.com/HomeXLabs/reviewington/blob/main/docs/pr_etiquette.md) not followed on above comment ⚠️"
}
resp = requests.patch(url=url, headers=headers, data=json.dumps(payload))
def main():
global repository_name
global pref_list
global pr
job_name = os.environ["GITHUB_WORKFLOW"]
repository_name = os.environ["GITHUB_REPOSITORY"]
git_ref = os.environ["GITHUB_REF"]
# setup arguments
args = setup_args()
github = args.token
pref_list = [
"Change",
"Question",
"Concern",
"Discussion",
"Praise",
"Suggestion",
"Nitpick",
"Guide",
"⚠️ ",
]
pr = git_ref.split("/")[2]
url = "https://api.github.com/repos/{}/pulls/{}/comments".format(
repository_name, pr
)
headers = {
"Accept": "application/vnd.github.v3+json",
"Authorization": "Bearer " + str(github),
}
resp = requests.get(url=url, headers=headers)
data = resp.json()
parse_review_comment(data, github)
if __name__ == "__main__":
main()