-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathentrypoint.py
More file actions
executable file
·91 lines (78 loc) · 3.03 KB
/
entrypoint.py
File metadata and controls
executable file
·91 lines (78 loc) · 3.03 KB
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
91
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
import re
import json
import requests
import unidiff
from github import Github
from codespell_lib._codespell import (build_dict, default_dictionary,
word_regex_def, fix_case)
def setup_dict():
misspellings = {}
build_dict(default_dictionary, misspellings, set())
return misspellings
def get_patched_files(diff_text):
patchset = unidiff.PatchSet(diff_text)
patched_files = {}
for patch in patchset:
diff_file = patch.path
for hunk in patch:
for line in hunk.target_lines():
if line.is_added:
patched_files.setdefault(diff_file, []).append((line.target_line_no,
line.value))
return patched_files
def check_spelling(pfiles, misspellings, word_regex):
fixes = {}
for fname in pfiles:
for line_no, line_val in pfiles[fname]:
for word in word_regex.findall(line_val):
lword = word.lower()
if lword in misspellings:
fixword = fix_case(word, misspellings[lword].data)
fixes.setdefault(fname, []).append((line_no, word, fixword))
return fixes
def comment_pr(fixes, conf, token):
if fixes:
gh = Github(login_or_token=token)
pr = gh.get_repo(conf['repository']['full_name']).get_pull(conf['number'])
body = 'Possible misspellings:\n'
for fname, fix in fixes.items():
body += '- `{0!s}`\n'.format(fname)
for line, wrong, right in fix:
body += ' - line {0:d}: `{1!s}` => `{2!s}`\n'.format(line,
wrong,
right)
pr.create_issue_comment(body=body)
def main():
if len(sys.argv) < 2:
print('No github token found, aborting!')
return 1
gh_token = sys.argv[1]
if os.environ['GITHUB_EVENT_NAME'] != 'pull_request':
print('We only work on pull requests. Doing nothing.')
return 0
with open(os.environ['GITHUB_EVENT_PATH'], 'r') as f:
conf = json.loads(f.read())
if not conf:
print('No GITHUB_EVENT_PATH in environment. Check Your workflow.')
return 1
if conf['action'] not in ['opened', 'edited', 'synchronize']:
print('Action mismatch: {0!s}. Doing nothing.'.format(conf['action']))
return 0
diff_url = conf['pull_request']['diff_url']
diff_request = requests.request(method='GET', url=diff_url)
if diff_request.status_code != 200:
print('Could not get diff of pull request. Aborting.')
return 1
pfiles = get_patched_files(diff_request.text)
word_regex = re.compile(word_regex_def)
misspellings = setup_dict()
fixes = check_spelling(pfiles, misspellings, word_regex)
print(fixes)
comment_pr(fixes, conf, gh_token)
return 0
if __name__ == '__main__':
main()