0

Using python-gitlab I succeed to retrieve notes from a merge request.

For example, I get:

"1444097483": {"id": 1444097483, "type": "DiffNote", "body": "rezrezrezr", "attachment": null, "author": {"id": 14931486, "username": "trap.francois", "name": "Fran\u00e7ois Trap", "state": "active"}, "created_at": "2023-06-23T19:42:00.846Z", "updated_at": "2023-06-23T19:42:00.846Z" ...

But I do not understand how to link the notes together to recreate the discussion threads. Any ideas?

The code I am currently running to test the API is:

import gitlab
import json

gl = gitlab.Gitlab("https://gitlab.com/", private_token='xxx')
#gl.enable_debug()
project = gl.projects.get(47098438)

mr = project.mergerequests.get(2)
notes = mr.notes.list()

d = dict()

for n in notes:
    n_content = mr.notes.get(n.id)
    d[n.id] = n_content.__dict__['_attrs']
    
with open("sample.json", "w") as outfile:
    json.dump(d, outfile)

Thank you.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
mopfbm
  • 3
  • 2

1 Answers1

0

You need to use a discussions to get grouped notes by MR threads. Example:

gl = gitlab.Gitlab(url, private_token=private_token)
project = gl.projects.get(project_id)
mr = project.mergerequests.get(mr_id)

result = []

discussions = mr.discussions.list(all=True)
for discussion in discussions:
    discussion_id = discussion.attributes.get('id')
    notes = discussion.attributes.get('notes')
    for note in notes:
        data = {
            'discussion_id': discussion_id,
            'note': note
        }
        result.append(data)
I_SER_I
  • 1
  • 3