feat: Add URL attachment to issue and handle Sentry event webhook
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Ching 2024-04-04 14:05:38 +08:00
parent 3a2c0a510b
commit 979b5e8f1a

56
app.py
View File

@ -73,6 +73,27 @@ def linear_issue():
return jsonify({'message': 'Ok'}), 200 return jsonify({'message': 'Ok'}), 200
def _attach_url_to_issue(issue_id, url, title=None):
data = {'query': 'mutation { attachmentLinkURL (title: "%s" url: "%s" issueId: "%s") { success } }' % (title, url, issue_id)}
headers = {'Authorization': LINEAR_API_KEY}
resp = requests.post(LINEAR_API_URL, json=data, headers=headers)
if resp.status_code != 200:
logger.error('Failed to attach url to issue: %s' % resp.text)
return False
if not resp.json().get('data', {}).get('attachmentLinkURL', {}).get('success'):
logger.error('Failed to attach url to issue: %s' % resp.text)
return False
return True
def _get_labels():
headers = {'Authorization': LINEAR_API_KEY}
data = {'query': '{issueLabels { edges { node { id name } }}}'}
resp = requests.post(LINEAR_API_URL, json=data, headers=headers)
labels = []
for label in resp.json().get('data').get('issueLabels').get('edges'):
labels.append(label['node'])
return labels
@app.route('/gitea/push', methods=['POST']) @app.route('/gitea/push', methods=['POST'])
def gitea_push(): def gitea_push():
""" https://docs.gitea.io/en-us/webhooks/ """ https://docs.gitea.io/en-us/webhooks/
@ -100,17 +121,6 @@ def gitea_push():
logger.error('Failed to get completed state') logger.error('Failed to get completed state')
return jsonify({'message': 'Failed to get completed state'}), 500 return jsonify({'message': 'Failed to get completed state'}), 500
def _attach_url_to_issue(issue_id, url, title=None):
data = {'query': 'mutation { attachmentLinkURL (title: "%s" url: "%s" issueId: "%s") { success } }' % (title, url, issue_id)}
resp = requests.post(LINEAR_API_URL, json=data, headers=headers)
if resp.status_code != 200:
logger.error('Failed to attach url to issue: %s' % resp.text)
return False
if not resp.json().get('data', {}).get('attachmentLinkURL', {}).get('success'):
logger.error('Failed to attach url to issue: %s' % resp.text)
return False
return True
# check if the commit message contains a linear issue id like 'TUN-21' # check if the commit message contains a linear issue id like 'TUN-21'
# if yes, update the issue state to completed # if yes, update the issue state to completed
for commit in data['commits']: for commit in data['commits']:
@ -142,5 +152,29 @@ def gitea_push():
return jsonify({'message': 'Ok'}), 200 return jsonify({'message': 'Ok'}), 200
@app.route('/sentry/event', methods=['POST'])
def sentry_event():
"""
https://docs.sentry.io/product/integrations/integration-platform/webhooks/
"""
data = request.json
logger.info('Received sentry event webhook: %s' % data)
event = request.headers.get('Sentry-Hook-Resource')
if event == 'issue':
if data.get('action') == 'created':
title = f"{data['issue']['shortId']} {data['issue']['title']}"
body = data['issue']['culprit']
# create a new issue in linear
create_data = {'query': 'mutation { issueCreate(input: { title: "%s" description: "%s" teamId: "1f28d52c-c91a-4c48-8ca8-96425dfd6516" assigneeId: "38c20f6d-8088-461c-9ea3-9f36e185cb62" labelIds: ["b40ea30a-e48b-4511-bde7-0a3c732cf752"]}) { issue { id } } }' % (title, body)}
resp = requests.post(LINEAR_API_URL, json=create_data, headers={'Authorization': LINEAR_API_KEY})
if resp.status_code != 200:
logger.error('Failed to create issue: %s' % resp.text)
return jsonify({'message': 'Failed to create issue'}), 500
issue_id = resp.json().get('data').get('issueCreate').get('issue').get('id')
_attach_url_to_issue(issue_id, data['issue']['web_url'], title)
return jsonify({'message': 'Ok'}), 200
if __name__ == "__main__": if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000) app.run(host="0.0.0.0", port=5000)