Skip to content

Instantly share code, notes, and snippets.

@hpacleb
Created January 26, 2024 05:08
Show Gist options
  • Save hpacleb/5d2749e8b051bf526c8d74af26d703e8 to your computer and use it in GitHub Desktop.
Save hpacleb/5d2749e8b051bf526c8d74af26d703e8 to your computer and use it in GitHub Desktop.
A short python script that searches a keyword in a file of all private repositories using gitlab's api. Access token with read_api is needed.
import requests
def gitlab_code_search(api_url, private_token, project_id, search_query, project_name):
headers = {
'PRIVATE-TOKEN': private_token,
}
params = {
'scope': 'blobs',
'search': search_query,
}
url = f'{api_url}/projects/{project_id}/search'
try:
response = requests.get(url, headers=headers, params=params)
response.raise_for_status() # Raise an HTTPError for bad responses
search_results = response.json()
print('Searching ', project_name)
if search_results:
for result in search_results:
print(project_name, result['path'], result['data'])
return search_results
except requests.exceptions.RequestException as e:
print(f'Error: {e}')
return None
def get_all_projects(gitlab_url, access_token):
projects = []
page = 1
per_page = 100 # Adjust as needed
while True:
headers = {"PRIVATE-TOKEN": access_token}
params = {"page": page, "per_page": per_page, "visibility": "private"}
response = requests.get(f"{gitlab_url}/projects", headers=headers, params=params)
if response.status_code == 200:
current_projects = response.json()
if not current_projects:
break # No more projects to fetch
projects.extend(current_projects)
page += 1
else:
print(f"Failed to retrieve projects. Status code: {response.status_code}")
print(response.text)
break
return projects
if __name__ == "__main__":
gitlab_url = "https://gitlab.com/api/v4"
access_token = ""
search_query = ""
all_projects = get_all_projects(gitlab_url, access_token)
for project in all_projects:
gitlab_code_search(gitlab_url, access_token, project['id'], search_query, project['name_with_namespace'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment