I’m doing some statistics on gitlab.com projects (where I’m maintainer), via gitlab REST APIs.
I read with attention available documentation REST API | GitLab and I had success retrieving many data regarding merge requests and issues.
But I have problems retrieving COMMENTS on commits (associated or not associated with merge requests). By example, to retrieve comments associated to a given commit ID, following docs: Commits API | GitLab, the way to go seems to be:
GET /projects/:id/repository/commits/:sha/comments. So I retrieve the commit associated to a merge request with:
$ curl "https://gitlab.com/api/v4/projects/99999999/merge_requests/486/commits?private_token=XXXXXXXXXX" | python -m json.tool
"title": "First draft of document update for identiy chapter",
So I retrive my commit ID (associated with the specified merge request of the project).
Nevertheless when I ask comments of the commit:
$ curl "https://gitlab.com/api/v4/projects/99999999/repository/commits/5715a628a6a408177d6288a6cd00b79a5cef6ec8/comments?private_token=XXXXXXXXXXXXXX" | python -m json.tool
I get a void list. That’s seems FALSE because I see on the corresponding merge request gitlab page that some commit COMMENTS do exist. Where I’m wrong?
HOW I retrieve all MERGE REQUESTS and or COMMITS comments, using Gitlab REST API?
Sure, I’d be happy to help you retrieve merge request commit comments using the GitLab API.
First, let’s clarify what we’re trying to achieve. We want to retrieve comments made on commits that are part of a merge request. These comments are also known as “notes” in GitLab terminology.
To do this, we’ll need to use a combination of GitLab’s Merge Requests API and Notes API. Here’s a step-by-step guide:
Get the Merge Request Commits
First, we need to get the list of commits for a specific merge request. We can do this using the following endpoint:
:id with your project ID and
:merge_request_iid with the IID of the merge request you’re interested in.
This will return a list of commits for the merge request. Each commit in the list has a
id field which is the commit SHA.
Get the Notes for Each Commit
Now that we have the list of commit SHAs, we can get the notes for each commit. We can do this using the following endpoint:
:id with your project ID and
:merge_request_iid with the IID of the merge request.
This will return a list of notes for the merge request. Each note in the list has a
noteable_id field which is the commit SHA the note is attached to.
Filter the Notes
Finally, we can filter the notes by comparing the
noteable_id field of each note with the commit SHAs we got in step 1. The notes with a
noteable_id that matches one of our commit SHAs are the comments made on the commits of the merge request.
I hope this helps! Let us know if you have any questions.
For more information, you can refer to the GitLab API documentation:
@gitlab-greg : Thanks lot for the valuable inputs. Even i was strucked with the same scenario instead i need merge requests comments for all without specifying the merge request/commit id. Is it possible? could you please provide your inputs. Thanks
i need merge requests comments for all without specifying the merge request/commit id. Is it possible? could you please provide your inputs. Thanks
The Notes API used to retrieve comments on MRs) requires
merge_request_iid, so no, it’s not possible to do this without specifying the MR id. However, it is possible to accomplish what you’re looking for with multiple API requests, which it can be automated programmatically.
List project merge requests to gather merge request
iid values for use in step 2
List all merge request notes using
To fetch >20 results via these REST API endpoints, you’ll need to use pagination REST API | GitLab
So, for example, if I have a project called
1234 and I want to write a very basic bash script that uses the
glab CLI as an API client, I’d start with something like:
# get mr_iids for all MRs in a project
glab api --paginate "projects/$PROJECT_ID/merge_requests?per_page=100" | jq -r '. | .iid' > "$PROJECT_ID-mr-iids.txt"
# loop through all mr_iids in this project and save notes to $PROJECT_ID-notes.txt
while IFS= read -r mr_iid;
do glab api --paginate "projects/$PROJECT_ID/merge_requests/notes/$mr_iid" >> "$PROJECT_ID-notes.txt"
done < "$PROJECT_ID-mr-iids.txt"