audit-labs/audit-tools
A collection of scripts, queries, and other goodies you can use in an audit.
clone: git clone https://gitbay.org/audit-labs/audit-tools.git
v1.0.0: applications/gitlab/collectors/api.py · raw
1"""Shared GitLab API helpers."""
2
3from urllib.parse import quote
4
5import requests
6
7DEFAULT_BASE_URL = "https://gitlab.com/api/v4"
8
9
10def enc(value):
11 """URL-encode a group or project identifier.
12
13 GitLab accepts either a numeric ID or a URL-encoded path (e.g.
14 ``my-group/sub-group``). Numeric IDs pass through unchanged.
15 """
16 return quote(str(value), safe="")
17
18
19def paginate(url, cfg, params=None):
20 """Fetch all pages from a GitLab endpoint using the X-Next-Page header."""
21 results = []
22 p = dict(params or {})
23 p["per_page"] = 100
24 page = 1
25
26 while True:
27 p["page"] = page
28 resp = requests.get(
29 url, headers=cfg["headers"], params=p, timeout=cfg["timeout"]
30 )
31 resp.raise_for_status()
32 data = resp.json()
33 if not data:
34 break
35 results.extend(data)
36 next_page = resp.headers.get("X-Next-Page")
37 if not next_page:
38 break
39 page = int(next_page)
40
41 return results