Reputation: 2222
Is there a GitLab API to get the commit count of a specific branch?
I can get the commits of the branch using following curl command, but not the commit count.
curl -X GET -H "PRIVATE-TOKEN: <my_private_token>" "http://<my_locally_hosted_web_server>/api/v4/projects/2/repository/commits/?ref_name=master"
Upvotes: 1
Views: 3096
Reputation: 94
PHP-CURL example using iteration through contributors
function commitStatistics()
{
$projectId = 100;
$projectPrivateToken = "secretToken123";
$url = $this->projectUrl . "/api/v4/projects/$projectId/repository/contributors";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('PRIVATE-TOKEN:' . $projectPrivateToken));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$total_counter = 0;
foreach (json_decode($result) as $contributor) {
$total_counter += $contributor->commits;
}
return $total_counter;
}
Upvotes: 0
Reputation: 656
You can simply use the headers informations 'X-Total' :
commits_count=$(curl -Ss -k --head --header "PRIVATE-TOKEN: <my_private_token>" "http://<my_locally_hosted_web_server>/api/v4/projects/:id/merge_requests/:merge_request_iid/commits?per_page=5" | grep -m 1 X-Total | cut -d':' -f2 )
Upvotes: 2
Reputation: 6269
Get the number of pages (see also gitlab pagination) and iterate over pages counting json array elements using jq :
TOTAL_PAGES=$(curl -Ss -k --head --header "PRIVATE-TOKEN: <my_private_token>" "http://<my_locally_hosted_web_server>/api/v4/projects/2/repository/commits/?ref_name=master" | grep x-total-pages | cut -d':' -f2 )
for ((i=1;i<=TOTAL_PAGES;i++)); do
SUM=$(($SUM + $(curl -Ss -k --request GET --header "PRIVATE-TOKEN: <my_private_token>" "http://<my_locally_hosted_web_server>/api/v4/projects/2/repository/commits/?ref_name=master&per_page=100&page=$i" | jq -r '. | length')));
done;
echo $SUM
Upvotes: 3