# Imports
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.template import loader
from .base import ErrorHandler
from .compat import BitbucketClient
# Exports
__all__ = (
"Bitbucket",
)
# Classes
[docs]class Bitbucket(ErrorHandler):
required_settings = [
"SUPERDJANGO_ERROR_CAPTURE_BITBUCKET_EMAIL",
"SUPERDJANGO_ERROR_CAPTURE_BITBUCKET_OWNER",
"SUPERDJANGO_ERROR_CAPTURE_BITBUCKET_PASSWORD",
"SUPERDJANGO_ERROR_CAPTURE_BITBUCKET_REPO",
]
[docs] def __init__(self):
"""Check for presence of bitbucket-python."""
if BitbucketClient is None:
raise ImproperlyConfigured("The Bitbucket package is required to use the Bitbucket backend: Use "
"pip install bitbucket-python")
super().__init__()
[docs] def handle(self, report):
"""Post an error to Bitbucket issues."""
body = self._parse_template("errors/bitbucket_body.txt")
title = self._parse_template("errors/bitbucket_title.txt")
client = BitbucketClient(
settings.SUPERDJANGO_ERROR_CAPTURE_BITBUCKET_EMAIL,
settings.SUPERDJANGO_ERROR_CAPTURE_BITBUCKET_PASSWORD,
settings.SUPERDJANGO_ERROR_CAPTURE_BITBUCKET_OWNER
)
# https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/
# %7Bworkspace%7D/%7Brepo_slug%7D/issues/%7Bissue_id%7D
# TODO: No idea if this is the data expected by bitbucket API to create an issue.
data = {
'description': body,
'title': title,
}
response = client.create_issue(settings.SUPERDJANGO_ERROR_CAPTURE_BITBUCKET_REPO, data)
# TODO: No idea if this is how to acquire the ID or the URL is correct. What about jira issues?
issue_id = response['id']
issue_url = "https://bitbucket.org/%s/%s/issues/%s/" % (
settings.SUPERDJANGO_ERROR_CAPTURE_BITBUCKET_OWNER,
settings.SUPERDJANGO_ERROR_CAPTURE_BITBUCKET_REPO,
issue_id
)
return {
'issue_url': issue_url
}