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.

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.