-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsentry_subject_prefix_script.py
More file actions
86 lines (63 loc) · 2.91 KB
/
Copy pathsentry_subject_prefix_script.py
File metadata and controls
86 lines (63 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
import os
import sys
import requests
class Sentry():
def __init__(self, base_url, org, token):
self.base_url = base_url
self.org = org
self.token = token
def _get_api_pagination(self, endpoint):
"""HTTP GET the Sentry API, following pagination links"""
headers = {'Authorization': f'Bearer {self.token}'}
results = []
url = f'{self.base_url}{endpoint}'
next = True
while next:
response = requests.get(url, headers=headers)
results.extend(response.json())
url = response.links.get('next', {}).get('url')
next = response.links.get('next', {}).get('results') == 'true'
if url == None:
next = False
return results
def _get_api(self, endpoint):
"""HTTP GET the Sentry API"""
headers = {'Authorization': f'Bearer {self.token}'}
url = f'{self.base_url}{endpoint}'
response = requests.get(url, headers=headers)
return response.json()
def _put_api(self, endpoint, data=None):
"""HTTP PUT the Sentry API"""
headers = {'Authorization': f'Bearer {self.token}'}
url = f'{self.base_url}{endpoint}'
return requests.put(url, headers=headers, data=data)
def get_project_slugs(self):
"""Return a list of project slugs in this Sentry org"""
results = self._get_api_pagination(f'/api/0/organizations/{self.org}/projects/')
return [project.get('slug', '') for project in results]
def get_project_details(self, project_slug):
"""Get project details"""
results = self._get_api(f'/api/0/projects/{self.org}/{project_slug}/')
return results
def update_project_details(self, project_slug, prefixdata):
"""Update project details"""
return self._put_api(f'/api/0/projects/{self.org}/{project_slug}/', data={'subjectPrefix': prefixdata})
if __name__ == '__main__':
onpremise_token = os.environ['SENTRY_ONPREMISE_AUTH_TOKEN']
cloud_token = os.environ['SENTRY_CLOUD_AUTH_TOKEN']
# copy over onpremise url (e.g. http://sentry.yourcompany.com)
sentry_onpremise = Sentry('<ON_PREMISE_URL>',
'<ON_PREMISE_ORG_SLUG>',
onpremise_token)
sentry_cloud = Sentry('<ORG_SLUG>',
'<CLOUD_ORG_SLUG>',
cloud_token)
onpremise_projects = sentry_onpremise.get_project_slugs()
for project in onpremise_projects:
onpremise_project_details = sentry_onpremise.get_project_details(project)
#get onpremise subjectPrefix
subject_prefix = onpremise_project_details.get('subjectPrefix', '')
#update new project with subjectPrefix if it was set in on-premise-project
if subject_prefix and subject_prefix!='':
result = sentry_cloud.update_project_details(project, subject_prefix)