Snippets size returns 0 through statistics in the GitLab API

Hello,

I retrieve snippets size through GitLab statistics API always returns 0 (even if there is a 8k snippet in my test project) :

Get the size (in bytes) of the snippets storage

def get_snippets_size(project_id):
api_path = f"/projects/{project_id}?statistics=true"
res = gitlab_api(api_path).json()
return res[‘statistics’][‘snippets_size’]

How can I retrieve the snippets size?

Regards,


Below is a more extended excerpt of the script in which Repository, LFS, wiki and storage sizes are properly retrieved.

#!/usr/bin/python3

This pre-receive hook:

- computes the total size of repository + LFS objects

Requirements:

- install python3-requests package

- use token with Gitlab API access

import sys
import fileinput
import os
import requests

TOKEN = “r1WebFQPh2DSSz26zQYz” #TODO: change token

Function for accessing Gitlab api

def gitlab_api(path):
path = f"http://gitlab-example.com/api/v4{path}" #TODO : change gitlab url
res = requests.get(path, headers={‘Authorization’: f’Bearer {TOKEN}'})
return res

Get the size (in bytes) of the repository storage

def get_repo_size(project_id):
api_path = f"/projects/{project_id}?statistics=true"
res = gitlab_api(api_path).json()
return res[‘statistics’][‘repository_size’]

Get the size (in bytes) of the lfs objects storage

def get_lfs_size(project_id):
api_path = f"/projects/{project_id}?statistics=true"
res = gitlab_api(api_path).json()
return res[‘statistics’][‘lfs_objects_size’]

Get the size (in bytes) of the wiki storage

def get_wiki_size(project_id):
api_path = f"/projects/{project_id}?statistics=true"
res = gitlab_api(api_path).json()
return res[‘statistics’][‘wiki_size’]

Get the size (in bytes) of the job artifacts storage

def get_job_artifacts_size(project_id):
api_path = f"/projects/{project_id}?statistics=true"
res = gitlab_api(api_path).json()
return res[‘statistics’][‘job_artifacts_size’]

Get the size (in bytes) of the pipeline artifacts storage

def get_pipeline_artifacts_size(project_id):
api_path = f"/projects/{project_id}?statistics=true"
res = gitlab_api(api_path).json()
return res[‘statistics’][‘pipeline_artifacts_size’]

Get the size (in bytes) of the packages storage

def get_packages_size(project_id):
api_path = f"/projects/{project_id}?statistics=true"
res = gitlab_api(api_path).json()
return res[‘statistics’][‘packages_size’]

Get the size (in bytes) of the snippets storage

def get_snippets_size(project_id):
api_path = f"/projects/{project_id}?statistics=true"
res = gitlab_api(api_path).json()
return res[‘statistics’][‘snippets_size’]

Get the size (in bytes) of the uploads storage

def get_uploads_size(project_id):
api_path = f"/projects/{project_id}?statistics=true"
res = gitlab_api(api_path).json()
return res[‘statistics’][‘uploads_size’]

Get the storage size (in bytes) of the repository

def get_storage_size(project_id):
api_path = f"/projects/{project_id}?statistics=true"
res = gitlab_api(api_path).json()
return res[‘statistics’][‘storage_size’]

Hook execution script

project_path = os.environ.get(“GL_PROJECT_PATH”)
project_id = os.environ.get(“GL_REPOSITORY”).replace(“project-”, “”)
username = os.environ.get(“GL_USERNAME”)

repository_size = get_repo_size(project_id)
repo_size_kb = repository_size/1024

lfs_size = get_lfs_size(project_id)
lfs_size_kb = lfs_size/1024

wiki_size = get_wiki_size(project_id)
wiki_size_kb = wiki_size/1024

job_artifacts_size = get_job_artifacts_size(project_id)
job_artifacts_size_kb = job_artifacts_size/1024

pipeline_artifacts_size = get_pipeline_artifacts_size(project_id)
pipeline_artifacts_size_kb = pipeline_artifacts_size/1024

packages_size = get_packages_size(project_id)
packages_size_kb = packages_size/1024

snippets_size = get_snippets_size(project_id)
snippets_size_kb = snippets_size/1024

uploads_size = get_uploads_size(project_id)
uploads_size_kb = uploads_size/1024

total_size_kb = lfs_size_kb + repo_size_kb + wiki_size_kb + job_artifacts_size_kb + pipeline_artifacts_size_kb + packages_size_kb + snippets_size_kb + uploads_size_kb

storage_size = get_storage_size(project_id)
storage_size_kb = storage_size/1024

print(f" Repository size : {repo_size_kb:.2f} KB")
print(f" LFS objects size : {lfs_size_kb:.2f} KB")
print(f" Wiki size : {wiki_size_kb:.2f} KB")
print(f" Snippets size : {snippets_size_kb:.2f} KB")
print(f" Total size: {total_size_kb:.2f} KB")
print(f" Storage size : {storage_size_kb:.2f} KB")