Posting webhook status now works

This commit is contained in:
Jeff Bradberry
2019-09-18 15:30:54 -04:00
parent 40b03eb6ef
commit 286da3a7eb
3 changed files with 11 additions and 12 deletions

View File

@@ -570,19 +570,19 @@ class WebhookMixin(models.Model):
return return
service_header = { service_header = {
'github': 'Authorization', 'github': ('Authorization', 'token {}'),
'gitlab': 'PRIVATE-TOKEN', 'gitlab': ('PRIVATE-TOKEN', '{}'),
} }
service_statuses = { service_statuses = {
'github': { 'github': {
'new': 'pending', 'pending': 'pending',
'successful': 'success', 'successful': 'success',
'failed': 'failure', 'failed': 'failure',
'canceled': 'failure', # Github doesn't have a 'canceled' status :( 'canceled': 'failure', # Github doesn't have a 'canceled' status :(
'error': 'error', 'error': 'error',
}, },
'gitlab': { 'gitlab': {
'new': 'pending', 'pending': 'pending',
'running': 'running', 'running': 'running',
'successful': 'success', 'successful': 'success',
'failed': 'failed', 'failed': 'failed',
@@ -601,8 +601,9 @@ class WebhookMixin(models.Model):
'state': statuses[status], 'state': statuses[status],
'context': 'ansible/awx' if license_type == 'open' else 'ansible/tower', 'context': 'ansible/awx' if license_type == 'open' else 'ansible/tower',
} }
headers = {service_header[self.webhook_service]: self.webhook_credential.get_input('token')} k, v = service_header[self.webhook_service]
response = requests.post(status_api, data=data, headers=headers) headers = {k: v.format(self.webhook_credential.get_input('token'))}
response = requests.post(status_api, data=json.dumps(data), headers=headers)
except Exception: except Exception:
logger.exception("Posting webhook status caused an error.") logger.exception("Posting webhook status caused an error.")
return return
@@ -610,4 +611,5 @@ class WebhookMixin(models.Model):
if response.status_code < 400: if response.status_code < 400:
logger.debug("Webhook status update sent.") logger.debug("Webhook status update sent.")
else: else:
logger.debug("Posting webhook status failed, code: {}".format(response.status_code)) logger.error("Posting webhook status failed, code: {}".format(response.status_code))
logger.error(response.text)

View File

@@ -462,8 +462,6 @@ class JobNotificationMixin(object):
return (notification_subject, notification_body) return (notification_subject, notification_body)
def send_notification_templates(self, status): def send_notification_templates(self, status):
self.update_webhook_status(status)
from awx.main.tasks import send_notifications # avoid circular import from awx.main.tasks import send_notifications # avoid circular import
if status not in ['running', 'succeeded', 'failed']: if status not in ['running', 'succeeded', 'failed']:
raise ValueError(_("status must be either running, succeeded or failed")) raise ValueError(_("status must be either running, succeeded or failed"))

View File

@@ -1206,6 +1206,8 @@ class UnifiedJob(PolymorphicModel, PasswordFieldsModel, CommonModelNameNotUnique
def websocket_emit_status(self, status): def websocket_emit_status(self, status):
connection.on_commit(lambda: self._websocket_emit_status(status)) connection.on_commit(lambda: self._websocket_emit_status(status))
if hasattr(self, 'update_webhook_status'):
connection.on_commit(lambda: self.update_webhook_status(status))
def notification_data(self): def notification_data(self):
return dict(id=self.id, return dict(id=self.id,
@@ -1422,6 +1424,3 @@ class UnifiedJob(PolymorphicModel, PasswordFieldsModel, CommonModelNameNotUnique
def is_isolated(self): def is_isolated(self):
return bool(self.controller_node) return bool(self.controller_node)
def update_webhook_status(self, status):
return