Show More
The requested changes are too big and content was truncated. Show full diff
@@ -1,133 +1,141 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2016 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # AppEnlight Enterprise Edition, including its added features, Support |
|
19 | 19 | # services, and proprietary license terms, please see |
|
20 | 20 | # https://rhodecode.com/licenses/ |
|
21 | 21 | |
|
22 | 22 | import jira |
|
23 | 23 | from appenlight.models.integrations import (IntegrationBase, |
|
24 | 24 | IntegrationException) |
|
25 | 25 | |
|
26 | 26 | _ = str |
|
27 | 27 | |
|
28 | 28 | |
|
29 | 29 | class NotFoundException(Exception): |
|
30 | 30 | pass |
|
31 | 31 | |
|
32 | 32 | |
|
33 | 33 | class JiraIntegration(IntegrationBase): |
|
34 | 34 | __mapper_args__ = { |
|
35 | 35 | 'polymorphic_identity': 'jira' |
|
36 | 36 | } |
|
37 | 37 | front_visible = True |
|
38 | 38 | as_alert_channel = False |
|
39 | 39 | supports_report_alerting = False |
|
40 | 40 | action_notification = True |
|
41 | 41 | integration_action = 'Add issue to Jira' |
|
42 | 42 | |
|
43 | 43 | |
|
44 | 44 | class JiraClient(object): |
|
45 | 45 | def __init__(self, user_name, password, host_name, project, request=None): |
|
46 | 46 | self.user_name = user_name |
|
47 | 47 | self.password = password |
|
48 | 48 | self.host_name = host_name |
|
49 | 49 | self.project = project |
|
50 | 50 | self.request = request |
|
51 | 51 | try: |
|
52 | 52 | self.client = jira.client.JIRA(options={'server': host_name}, |
|
53 | 53 | basic_auth=(user_name, password)) |
|
54 | 54 | except jira.JIRAError as e: |
|
55 | 55 | raise IntegrationException( |
|
56 | 56 | 'Communication problem: HTTP_STATUS:%s, URL:%s ' % ( |
|
57 | 57 | e.status_code, e.url)) |
|
58 | 58 | |
|
59 | 59 | def get_projects(self): |
|
60 | 60 | projects = self.client.projects() |
|
61 | 61 | return projects |
|
62 | 62 | |
|
63 | def get_assignees(self): | |
|
63 | def get_assignees(self, request): | |
|
64 | 64 | """Gets list of possible assignees""" |
|
65 | cache_region = request.registry.cache_regions.redis_sec_30 | |
|
66 | @cache_region.cache_on_arguments('JiraClient.get_assignees') | |
|
67 | def cached(project_name): | |
|
65 | 68 | users = self.client.search_assignable_users_for_issues( |
|
66 |
None, project= |
|
|
69 | None, project=project_name) | |
|
67 | 70 | results = [] |
|
68 | 71 | for user in users: |
|
69 | 72 | results.append({"id": user.name, "name": user.displayName}) |
|
70 | 73 | return results |
|
74 | return cached(self.project) | |
|
71 | 75 | |
|
72 |
def get_ |
|
|
73 | def cached(project_name): | |
|
74 | metadata = self.client.createmeta( | |
|
75 | projectKeys=project_name, expand='projects.issuetypes.fields') | |
|
76 | assignees = self.get_assignees() | |
|
76 | def get_issue_types(self, request): | |
|
77 | metadata = self.get_metadata(request) | |
|
78 | assignees = self.get_assignees(request) | |
|
77 | 79 |
|
|
78 | 80 |
|
|
79 | 81 |
|
|
80 | 82 |
|
|
81 | 83 |
|
|
82 | 84 |
|
|
83 | 85 |
|
|
84 | 86 |
|
|
85 | 87 |
|
|
86 | 88 |
|
|
87 | 89 |
|
|
88 | 90 |
|
|
89 | 91 |
|
|
90 | 92 |
|
|
91 | 93 |
|
|
92 | 94 |
|
|
93 | 95 |
|
|
94 | 96 |
|
|
95 | 97 |
|
|
96 | 98 |
|
|
97 | 99 |
|
|
98 | 100 |
|
|
99 | ||
|
100 | 101 |
|
|
101 | 102 |
|
|
102 | 103 |
|
|
103 | 104 | |
|
105 | def get_metadata(self, request): | |
|
106 | # cache_region = request.registry.cache_regions.redis_sec_30 | |
|
107 | # @cache_region.cache_on_arguments('JiraClient.get_metadata') | |
|
108 | def cached(project_name): | |
|
109 | return self.client.createmeta( | |
|
110 | projectKeys=project_name, expand='projects.issuetypes.fields') | |
|
104 | 111 | return cached(self.project) |
|
105 | 112 | |
|
106 | def create_issue(self, form_data): | |
|
107 | metadata = self.get_metadata() | |
|
113 | def create_issue(self, form_data, request): | |
|
114 | issue_types = self.get_issue_types(request) | |
|
108 | 115 | payload = { |
|
109 | 116 | 'project': {'key': form_data['project']}, |
|
110 | 117 | 'summary': form_data['title'], |
|
111 | 118 | 'description': form_data['content'], |
|
112 |
'issuetype': {'id': ' |
|
|
119 | 'issuetype': {'id': form_data['issue_type']}, | |
|
113 | 120 | "priority": {'id': form_data['priority']}, |
|
114 | 121 | "assignee": {'name': form_data['responsible']}, |
|
115 | 122 | } |
|
116 |
for issue_type in |
|
|
117 |
if issue_type['id'] == ' |
|
|
123 | for issue_type in issue_types: | |
|
124 | if issue_type['id'] == form_data['issue_type']: | |
|
118 | 125 | for field in issue_type['fields']: |
|
126 | # set some defaults for other required fields | |
|
119 | 127 | if field == 'reporter': |
|
120 |
payload["reporter"] = {'id': self.user_name} |
|
|
128 | payload["reporter"] = {'id': self.user_name} | |
|
121 | 129 | if field['required'] and field['id'] not in payload: |
|
122 | 130 | if field['type'] == 'array': |
|
123 | 131 | payload[field['id']] = [field['values'][0], ] |
|
124 | 132 | elif field['type'] == 'string': |
|
125 | 133 | payload[field['id']] = '' |
|
126 | 134 | new_issue = self.client.create_issue(fields=payload) |
|
127 | 135 | web_url = self.host_name + '/browse/' + new_issue.key |
|
128 | 136 | to_return = { |
|
129 | 137 | 'id': new_issue.id, |
|
130 | 138 | 'resource_url': new_issue.self, |
|
131 | 139 | 'web_url': web_url |
|
132 | 140 | } |
|
133 | 141 | return to_return |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
General Comments 0
You need to be logged in to leave comments.
Login now