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:
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?
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:
GET /projects/:id/merge_requests/:merge_request_iid/commits
Replace :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:
GET /projects/:id/merge_requests/:merge_request_iid/notes
Again, replace :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 project_id and 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.
GET /projects/$PROJECT_ID/merge_requests/$MR_IID/notes"
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 testproject with project_id of 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:
#!/bin/bash
PROJECT_ID=1234
# 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"