Skip to content

Commit ae8629b

Browse files
committed
try to parse rages
1 parent 7a4e2f4 commit ae8629b

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

meeseeksdev/meeseeksbox/commands.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,26 @@ def replyadmin(*, session, payload, arguments, local_config=None):
111111
)
112112

113113

114+
def parsepatch(patch):
115+
"""
116+
Attempt to parse a github patch to tell us on which lines we are able to comment.
117+
"""
118+
ranges = []
119+
for l in patch.splitlines():
120+
if not l.startswith('@@'):
121+
continue
122+
s = l.split('@@')[1].strip()
123+
ranges.append([int(x) for x in s.split(' ')[1].split(',')])
124+
return [(s, s+x-1) for s, x in ranges]
125+
126+
def in_ranges(needle, haystack):
127+
n1, n2 = needle
128+
for h1, h2 in haystack:
129+
if (h1 < n1) and (n2 < h2):
130+
return True
131+
return False
132+
133+
114134
def _compute_pwd_changes(whitelist):
115135
import black
116136
from difflib import SequenceMatcher
@@ -129,6 +149,7 @@ def _compute_pwd_changes(whitelist):
129149
# we don't touch files not in this PR.
130150
continue
131151
patch = whitelist[p]
152+
ranges = parsepatch(patch)
132153
print('patch is:\n', patch)
133154
p = Path(p)
134155
old = p.read_text()
@@ -138,6 +159,9 @@ def _compute_pwd_changes(whitelist):
138159
ol = old.splitlines()
139160
s = SequenceMatcher(None, ol, nl)
140161
for t, a1, a2, b1, b2 in s.get_opcodes():
162+
if not in_ranges((a1, a2), ranges):
163+
print(f"-- we won't be able to suggest a change on {p}:{a1}-{a2}")
164+
continue
141165
if t == "replace":
142166

143167
c = "```suggestion\n"
@@ -156,7 +180,6 @@ def black_suggest(*, session, payload, arguments, local_config=None):
156180
print("===== reformatting suggestions. =====")
157181

158182
prnumber = payload["issue"]["number"]
159-
prtitle = payload["issue"]["title"]
160183
org_name = payload["repository"]["owner"]["login"]
161184
repo_name = payload["repository"]["name"]
162185

@@ -178,7 +201,7 @@ def black_suggest(*, session, payload, arguments, local_config=None):
178201

179202
commits_url = pr_data["commits_url"]
180203

181-
commits_data = session.ghrequest("GET", commits_url).json()
204+
# commits_data = session.ghrequest("GET", commits_url).json()
182205

183206
# that will likely fail, as if PR, we need to bypass the fact that the
184207
# requester has technically no access to committer repo.

0 commit comments

Comments
 (0)