bitbucket.py
129 lines
| 4.8 KiB
| text/x-python
|
PythonLexer
r0 | # -*- coding: utf-8 -*- | |||
r112 | # Copyright 2010 - 2017 RhodeCode GmbH and the AppEnlight project authors | |||
r0 | # | |||
r112 | # Licensed under the Apache License, Version 2.0 (the "License"); | |||
# you may not use this file except in compliance with the License. | ||||
# You may obtain a copy of the License at | ||||
r0 | # | |||
r112 | # http://www.apache.org/licenses/LICENSE-2.0 | |||
r0 | # | |||
r112 | # Unless required by applicable law or agreed to in writing, software | |||
# distributed under the License is distributed on an "AS IS" BASIS, | ||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
# See the License for the specific language governing permissions and | ||||
# limitations under the License. | ||||
r0 | ||||
r153 | from appenlight.models.integrations.bitbucket import ( | |||
BitbucketIntegration, | ||||
IntegrationException, | ||||
) | ||||
r0 | from appenlight.models.report_comment import ReportComment | |||
from appenlight.models.services.report_group import ReportGroupService | ||||
from pyramid.view import view_config | ||||
from appenlight import forms | ||||
import logging | ||||
from datetime import datetime | ||||
from pyramid.httpexceptions import HTTPUnprocessableEntity | ||||
from webob.multidict import MultiDict | ||||
log = logging.getLogger(__name__) | ||||
from . import IntegrationView | ||||
class BitbucketView(IntegrationView): | ||||
r153 | @view_config( | |||
route_name="integrations_id", | ||||
match_param=["action=info", "integration=bitbucket"], | ||||
renderer="json", | ||||
) | ||||
r0 | def get_bitbucket_info(self): | |||
""" | ||||
Grab information about possible priority levels and assignable users | ||||
""" | ||||
try: | ||||
client = BitbucketIntegration.create_client( | ||||
self.request, | ||||
r153 | self.integration.config["user_name"], | |||
self.integration.config["repo_name"], | ||||
) | ||||
r0 | except IntegrationException as e: | |||
self.request.response.status_code = 503 | ||||
r153 | return {"error_messages": [str(e)]} | |||
r0 | assignees = client.get_assignees() | |||
priorities = client.get_priorities() | ||||
r153 | return {"assignees": assignees, "priorities": priorities} | |||
r0 | ||||
r153 | @view_config( | |||
route_name="integrations_id", | ||||
match_param=["action=create-issue", "integration=bitbucket"], | ||||
renderer="json", | ||||
) | ||||
r0 | def create_issue(self): | |||
""" | ||||
Creates a new issue in bitbucket issue tracker from report group | ||||
""" | ||||
r153 | report = ReportGroupService.by_id(self.request.unsafe_json_body["group_id"]) | |||
r0 | form_data = { | |||
r153 | "title": self.request.unsafe_json_body.get("title", "Unknown Title"), | |||
"content": self.request.unsafe_json_body.get("content", ""), | ||||
"kind": "bug", | ||||
"priority": self.request.unsafe_json_body["priority"], | ||||
"responsible": self.request.unsafe_json_body["responsible"]["user"], | ||||
r0 | } | |||
try: | ||||
client = BitbucketIntegration.create_client( | ||||
self.request, | ||||
r153 | self.integration.config["user_name"], | |||
self.integration.config["repo_name"], | ||||
) | ||||
r0 | issue = client.create_issue(form_data) | |||
except IntegrationException as e: | ||||
self.request.response.status_code = 503 | ||||
r153 | return {"error_messages": [str(e)]} | |||
r0 | ||||
r153 | comment_body = "Bitbucket issue created: %s " % issue["web_url"] | |||
comment = ReportComment( | ||||
owner_id=self.request.user.id, | ||||
report_time=report.first_timestamp, | ||||
body=comment_body, | ||||
) | ||||
r0 | report.comments.append(comment) | |||
return True | ||||
r153 | @view_config( | |||
route_name="integrations_id", | ||||
match_param=["action=setup", "integration=bitbucket"], | ||||
renderer="json", | ||||
permission="edit", | ||||
) | ||||
r0 | def setup(self): | |||
""" | ||||
Validates and creates integration between application and bitbucket | ||||
""" | ||||
resource = self.request.context.resource | ||||
form = forms.IntegrationBitbucketForm( | ||||
MultiDict(self.request.safe_json_body or {}), | ||||
r153 | csrf_context=self.request, | |||
**self.integration_config | ||||
) | ||||
if self.request.method == "POST" and form.validate(): | ||||
r0 | integration_config = { | |||
r153 | "repo_name": form.repo_name.data, | |||
"user_name": form.user_name.data, | ||||
"host_name": "https://bitbucket.org", | ||||
r0 | } | |||
if not self.integration: | ||||
# add new integration | ||||
r153 | self.integration = BitbucketIntegration(modified_date=datetime.utcnow()) | |||
self.request.session.flash("Integration added") | ||||
r0 | resource.integrations.append(self.integration) | |||
else: | ||||
r153 | self.request.session.flash("Integration updated") | |||
r0 | self.integration.config = integration_config | |||
return integration_config | ||||
r153 | elif self.request.method == "POST": | |||
r0 | return HTTPUnprocessableEntity(body=form.errors_json) | |||
return self.integration_config | ||||