Created
May 23, 2023 08:49
-
-
Save y-young/000b3acad291f1e0fc4160087808fc98 to your computer and use it in GitHub Desktop.
Export project data from sentry.io
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
"""Export Sentry project data from sentry.io.""" | |
import os | |
import json | |
import requests | |
SENTRY_AUTH_TOKEN = os.environ.get("SENTRY_AUTH_TOKEN") | |
SENTRY_ORGANIZATION_SLUG = os.environ.get("SENTRY_ORGANIZATION_SLUG") | |
SENTRY_PROJECT_SLUG = os.environ.get("SENTRY_PROJECT_SLUG") | |
def sentry_api_request(method, endpoint, data=None): | |
"""Make a request to the Sentry API.""" | |
url = f"https://sentry.io/api/0/{endpoint}" | |
headers = { | |
"Authorization": f"Bearer {SENTRY_AUTH_TOKEN}", | |
"Content-Type": "application/json", | |
} | |
response = requests.request(method, url, headers=headers, json=data, timeout=5) | |
response.raise_for_status() | |
return response.json() | |
def json_dump(data): | |
"""Dump data to JSON file with indentation.""" | |
return json.dumps(data, indent=2) | |
def export_project_data(): | |
"""Export Sentry project data from sentry.io.""" | |
print("Getting project data...") | |
# Get project details | |
project = sentry_api_request( | |
"GET", f"projects/{SENTRY_ORGANIZATION_SLUG}/{SENTRY_PROJECT_SLUG}/" | |
) | |
with open("project.json", "w") as f: | |
f.write(json_dump(project)) | |
print("Getting issues...") | |
# Get all issues for the project | |
issues = sentry_api_request( | |
"GET", | |
f"projects/{SENTRY_ORGANIZATION_SLUG}/{SENTRY_PROJECT_SLUG}/issues/", | |
) | |
print("Found", len(issues), "issues") | |
# Export issues to files | |
os.makedirs("issues", exist_ok=True) | |
for issue in issues: | |
issue_id = issue["id"] | |
print("Processing issue", issue_id) | |
# Get issue details | |
issue_details = sentry_api_request("GET", f"issues/{issue_id}/") | |
# Export issue to file | |
os.makedirs(f"issues/{issue_id}", exist_ok=True) | |
with open(f"issues/{issue_id}/issue.json", "w") as f: | |
f.write(json_dump(issue_details)) | |
# Get all events for the issue | |
events = sentry_api_request("GET", f"issues/{issue_id}/events/?full=true") | |
print("\tFound", len(events), "events") | |
# Export events to files | |
os.makedirs(f"issues/{issue_id}/events", exist_ok=True) | |
for event in events: | |
event_id = event["eventID"] | |
print("\tProcessing event", event_id) | |
# Export event to file | |
with open(f"issues/{issue_id}/events/{event_id}.json", "w") as f: | |
f.write(json_dump(event)) | |
print("Getting releases...") | |
# Get all releases for the organization | |
releases = sentry_api_request( | |
"GET", f"organizations/{SENTRY_ORGANIZATION_SLUG}/releases/" | |
) | |
print("Found", len(releases), "releases") | |
with open("releases.json", "w") as f: | |
f.write(json_dump(releases)) | |
if __name__ == "__main__": | |
export_project_data() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment