Last active
April 14, 2023 07:06
-
-
Save elanzini/4128ff31b72782baffc65d44e213add4 to your computer and use it in GitHub Desktop.
Counting repositories with Renovate or Dependabot installed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import requests | |
import time | |
# GitHub API endpoint | |
url = 'https://api.github.com' | |
# Get the GitHub token from the environment variable | |
github_token = os.environ.get('GITHUB_TOKEN') | |
# Define the API headers to include the authentication token | |
headers = { | |
'Authorization': f'token {github_token}', | |
'Accept': 'application/vnd.github.v3+json' | |
} | |
def get_total_repos(query): | |
while True: | |
response = requests.get(f'{url}/search/code?q={query}', headers=headers) | |
if response.status_code == 200: | |
data = response.json() | |
return data['total_count'] | |
elif response.status_code == 429: | |
print('Rate limit exceeded. Waiting for 60 seconds before retrying.') | |
time.sleep(60) | |
else: | |
print(f'Error occurred: {response.status_code}. Retrying in 5 seconds.') | |
time.sleep(5) | |
# Renovate | |
query_renovate = "filename:renovate.json+NOT+path:node_modules" | |
total_repos_renovate = get_total_repos(query_renovate) | |
print(f'Total repositories with Renovate installed: {total_repos_renovate}') | |
# Wait for 5 seconds before making another request | |
time.sleep(5) | |
# Dependabot | |
# If this keeps failing because of rate limit - just comment above and run separately | |
query_dependabot = "path:.github+filename:dependabot.yml" | |
total_repos_dependabot = get_total_repos(query_dependabot) | |
print(f'Total repositories with Dependabot installed: {total_repos_dependabot}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment