Work with repo without releases
This commit is contained in:
@@ -19,16 +19,30 @@ github_latest_release_metric = Gauge(
|
||||
# GitHub API token (replace with your actual token)
|
||||
github_api_token = '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'
|
||||
|
||||
# Function to fetch the latest release version from GitHub
|
||||
def get_latest_release(repo_url):
|
||||
# Function to fetch the latest release version or the latest tag from GitHub
|
||||
def get_latest_version(repo_url):
|
||||
api_url = f"https://api.github.com/repos/{repo_url}/releases/latest"
|
||||
headers = {
|
||||
'Authorization': f'Bearer {github_api_token}',
|
||||
'Accept': 'application/vnd.github.v3+json'
|
||||
}
|
||||
response = requests.get(api_url, headers=headers)
|
||||
data = response.json()
|
||||
return data["tag_name"]
|
||||
|
||||
# If the repository has releases, get the latest release version
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
return data["tag_name"]
|
||||
|
||||
# If the repository doesn't have releases, get the latest tag
|
||||
if response.status_code == 404:
|
||||
api_url = f"https://api.github.com/repos/{repo_url}/tags"
|
||||
response = requests.get(api_url, headers=headers)
|
||||
data = response.json()
|
||||
if data:
|
||||
return data[0]["name"]
|
||||
|
||||
# If both release and tag information is not available, return None
|
||||
return None
|
||||
|
||||
@app.route('/metrics')
|
||||
def metrics():
|
||||
@@ -38,14 +52,16 @@ def metrics():
|
||||
return "Error: Please provide a 'repo' parameter.", 400
|
||||
|
||||
try:
|
||||
latest_release = get_latest_release(repo_url)
|
||||
print(f"Latest release: {latest_release}")
|
||||
|
||||
# Reset the metric to its initial state
|
||||
github_latest_release_metric.clear()
|
||||
latest_version = get_latest_version(repo_url)
|
||||
|
||||
# Set the metric value to 1 with appropriate labels
|
||||
github_latest_release_metric.labels(repo=repo_url, version=latest_release).set(1)
|
||||
if latest_version:
|
||||
print(f"Latest version: {latest_version}")
|
||||
|
||||
# Reset the metric to its initial state
|
||||
github_latest_release_metric.clear()
|
||||
|
||||
# Set the metric value to 1 with appropriate labels
|
||||
github_latest_release_metric.labels(repo=repo_url, version=latest_version).set(1)
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user