Module: tools Branch: master Commit: bbe90c2b8f64e3851db37b472986ad5fd4f90085 URL: https://source.winehq.org/git/tools.git/?a=commit;h=bbe90c2b8f64e3851db37b47...
Author: Jeremy White jwhite@codeweavers.com Date: Thu Apr 28 19:18:52 2022 -0500
Use position to try to match DiffNotes.
This seems to produce a better result.
Signed-off-by: Alexandre Julliard julliard@winehq.org
---
gitlab/gitlab-to-mail/gitlabtomail.py | 39 ++++++++++++----------------------- 1 file changed, 13 insertions(+), 26 deletions(-)
diff --git a/gitlab/gitlab-to-mail/gitlabtomail.py b/gitlab/gitlab-to-mail/gitlabtomail.py index a2f7079..6beedf3 100755 --- a/gitlab/gitlab-to-mail/gitlabtomail.py +++ b/gitlab/gitlab-to-mail/gitlabtomail.py @@ -152,33 +152,26 @@ def quote_original_comment(note): return prefix + '> ' + '> '.join(ret)
-def find_line_code_in_notes(notes, line_code): - for note in notes: - if 'diff_file' in note and 'truncated_diff_lines' in note: - for diff in note['truncated_diff_lines']: - if diff['line_code'] == line_code: - return "{}@{}/{}\n".format(note['diff_file']['new_path'], diff['new_line'], - str(diff['old_line']) + "> " + diff['text'] + "\n") - return None - - -def fetch_diffnotes(mr_iid, author, line_codes): +def fetch_diffnotes(mr_iid, author, note_id, position): # This is a KLUDGE. The discussions.json entry point is not documented. # It provides back an array of all discussions on a given Merge request. - # We can use it to pick out a particular DiffNote, using the ending line_code. + # We can use it to pick out a particular DiffNote, using the position. # Human readable text seems to come up in 'truncated_diff_lines'. # I've made an ask for a better way to do this, but it seems to work for now. url = urljoin(settings.GITLAB_URL, f"/{settings.GITLAB_PROJECT_NAME}/-/merge_requests/{mr_iid}/discussions.json") r = requests.get(url, headers={"PRIVATE-TOKEN": settings.GITLAB_TOKEN}) r.raise_for_status() - notes = r.json() + discussions = r.json() diffnotes = "" - for note in notes: - if 'line_code' in note and note['line_code'] in line_codes: - diffnotes += "{} commented about {}:\n".format(author, note['diff_file']['new_path']) - if 'truncated_diff_lines' in note: - for diff in note['truncated_diff_lines']: - diffnotes += "> " + diff['text'] + "\n" + for d in discussions: + if d['notes'][0]['id'] == str(note_id): + if 'truncated_diff_lines' in d and 'position' in d: + if d['position'] == position: + diffnotes += "{} commented about {}:\n".format(author, d['diff_file']['new_path']) + if 'truncated_diff_lines' in d: + for diff in d['truncated_diff_lines']: + diffnotes += "> " + diff['text'] + "\n" + break return diffnotes
@@ -340,13 +333,7 @@ def process_commented_on(event): # TODO - this is fairly fragile. hoping to get guidance here: # https://forum.gitlab.com/t/end-point-to-retrieve-a-diffnote/66926 if event['note']['type'] == 'DiffNote': - line_codes = [] - if 'position' in event['note'] and 'line_range' in event['note']['position']: - if 'start' in event['note']['position']['line_range']: - line_codes.append(event['note']['position']['line_range']['start']['line_code']) - if 'end' in event['note']['position']['line_range']: - line_codes.append(event['note']['position']['line_range']['end']['line_code']) - diffnote = fetch_diffnotes(event['note']['noteable_iid'], author, line_codes) + diffnote = fetch_diffnotes(event['note']['noteable_iid'], author, event['note']['id'], event['note']['position']) body += diffnote
if event['note']['type'] == 'DiscussionNote':