aboutsummaryrefslogtreecommitdiff
path: root/overlays/bugwarrior/mantisbt.patch
blob: 85e5af1eb115f58953a3c57099f2183fd707bed5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
diff --git a/bugwarrior/services/mantisbt.py b/bugwarrior/services/mantisbt.py
new file mode 100644
index 0000000..e54af0d
--- /dev/null
+++ b/bugwarrior/services/mantisbt.py
@@ -0,0 +1,361 @@
+from builtins import filter
+import re
+import six
+
+import requests
+from jinja2 import Template
+
+from bugwarrior.config import asbool, aslist, die
+from bugwarrior.services import IssueService, Issue, ServiceClient
+
+import logging
+log = logging.getLogger(__name__)
+
+
+class MantisbtClient(ServiceClient):
+    def __init__(self, host, token):
+        self.host = host
+        self.session = requests.Session()
+        self.session.headers['Authorization'] = token
+
+    def _api_url(self, path, **context):
+        """ Build the full url to the API endpoint """
+        baseurl = "https://{}/api/rest".format(self.host)
+        return baseurl + path.format(**context)
+
+    def get_user(self):
+        return self.json_response(self.session.get(self._api_url("/users/me")))
+
+    def get_projects(self):
+        return self._getter(self._api_url("/projects"), subkey="projects")
+
+    def get_issues(self):
+        url = self._api_url("/issues?page_size=50")
+        return self._getter(url, page_size=50, subkey="issues")
+
+    def get_assigned_issues(self):
+        """ Returns all issues assigned to authenticated user.
+        """
+        url = self._api_url("/issues?page_size=50&filter_id=assigned")
+        return self._getter(url, page_size=50, subkey="issues")
+
+    def get_monitored_issues(self):
+        """ Returns all issues monitored by authenticated user.
+        """
+        url = self._api_url("/issues?page_size=50&filter_id=monitored")
+        return self._getter(url, page_size=50, subkey="issues")
+
+    def get_reported_issues(self):
+        """ Returns all issues reported by authenticated user.
+        """
+        url = self._api_url("/issues?page_size=50&filter_id=reported")
+        return self._getter(url, page_size=50, subkey="issues")
+
+    def _getter(self, url, page_size=None, subkey=None):
+        """ Pagination utility.  Obnoxious. """
+
+        results = []
+        link = dict(next=url)
+        page_number = 1
+
+        while 'next' in link:
+            if page_size is not None:
+                response = self.session.get(link['next'] + "&page=" + str(page_number))
+            else:
+                response = self.session.get(link['next'])
+
+            json_res = self.json_response(response)
+
+            if subkey is not None:
+                json_res = json_res[subkey]
+
+            results += json_res
+
+            if page_size is not None and len(json_res) == page_size:
+                page_number += 1
+            else:
+                break
+
+        return results
+
+class MantisbtIssue(Issue):
+    TITLE = 'mantisbttitle'
+    BODY = 'mantisbtbody'
+    CREATED_AT = 'mantisbtcreatedon'
+    UPDATED_AT = 'mantisbtupdatedat'
+    CLOSED_AT = 'mantisbtclosedon'
+    URL = 'mantisbturl'
+    PROJECT = 'mantisbtproject'
+    NUMBER = 'mantisbtnumber'
+    USER = 'mantisbtuser'
+    CATEGORY = 'mantisbtcategory'
+    STATE = 'mantisbtstate'
+
+    UDAS = {
+        TITLE: {
+            'type': 'string',
+            'label': 'Mantisbt Title',
+        },
+        BODY: {
+            'type': 'string',
+            'label': 'Mantisbt Body',
+        },
+        CREATED_AT: {
+            'type': 'date',
+            'label': 'Mantisbt Created',
+        },
+        UPDATED_AT: {
+            'type': 'date',
+            'label': 'Mantisbt Updated',
+        },
+        CLOSED_AT: {
+            'type': 'date',
+            'label': 'Mantisbt Closed',
+        },
+        PROJECT: {
+            'type': 'string',
+            'label': 'Mantisbt Project',
+        },
+        URL: {
+            'type': 'string',
+            'label': 'Mantisbt URL',
+        },
+        NUMBER: {
+            'type': 'numeric',
+            'label': 'Mantisbt Issue #',
+        },
+        USER: {
+            'type': 'string',
+            'label': 'Mantisbt User',
+        },
+        CATEGORY: {
+            'type': 'string',
+            'label': 'Mantisbt Category',
+        },
+        STATE: {
+            'type': 'string',
+            'label': 'Mantisbt State',
+        }
+    }
+    UNIQUE_KEY = (URL, NUMBER, )
+
+    def _normalize_tag(self, label):
+        return re.sub(r'[^a-zA-Z0-9]', '_', label)
+
+    def to_taskwarrior(self):
+        body = self.record.get('description')
+        if body:
+            body = body.replace('\r\n', '\n')
+
+        created = self.parse_date(self.record.get('created_at'))
+        updated = self.parse_date(self.record.get('updated_at'))
+        closed_date = None
+        if self.record["status"]["name"] in ["closed", "resolved"]:
+            for history in self.record.get("history", []):
+                if history.get("field", {}).get("name", "") == "status"\
+                        and history.get("new_value", {}).get("name", "") in ["closed", "resolved"]:
+                    closed_date = history["created_at"]
+        closed = self.parse_date(closed_date)
+
+        return {
+            'project': self.record['project']['name'],
+            'priority': self.origin['default_priority'],
+            'annotations': self.get_annotations(),
+            'tags': self.get_tags(),
+            'entry': created,
+            'end': closed,
+
+            self.TITLE: self.record.get('summary'),
+            self.BODY: body,
+            self.CREATED_AT: created,
+            self.UPDATED_AT: updated,
+            self.CLOSED_AT: closed,
+            self.URL: self.get_url(),
+            self.PROJECT: self.record['project'].get('name'),
+            self.NUMBER: self.record['id'],
+            self.USER: self.record['reporter'].get('name'),
+            self.CATEGORY: self.record['category'].get('name'),
+            self.STATE: self.record['status'].get('label'),
+        }
+
+    def get_url(self):
+        return "https://{}view.php?id={}".format(self.extra['host'], self.record["id"])
+
+    def get_annotations(self):
+        annotations = []
+
+        context = self.record.copy()
+        annotation_template = Template(self.origin['annotation_template'])
+
+        for annotation_dict in self.record.get('notes', []):
+            context.update({
+                'text': annotation_dict['text'],
+                'date': annotation_dict['created_at'],
+                'author': annotation_dict['reporter'].get('name', 'unknown'),
+                'view': annotation_dict['view_state']['label'],
+                })
+            annotations.append(
+                    annotation_template.render(context)
+                    )
+        return annotations
+
+    def get_tags(self):
+        tags = []
+
+        context = self.record.copy()
+        tag_template = Template(self.origin['tag_template'])
+
+        for tag_dict in self.record.get('tags', []):
+            context.update({
+                'tag': self._normalize_tag(tag_dict['name'])
+            })
+            tags.append(
+                tag_template.render(context)
+            )
+
+        return tags
+
+    def get_default_description(self):
+        return self.build_default_description(
+            title=self.record['summary'],
+            url=self.get_processed_url(self.get_url()),
+            number=self.record['id'],
+        )
+
+
+class MantisbtService(IssueService):
+    ISSUE_CLASS = MantisbtIssue
+    CONFIG_PREFIX = 'mantisbt'
+
+    def __init__(self, *args, **kw):
+        super(MantisbtService, self).__init__(*args, **kw)
+
+        self.host = self.config.get('host', 'www.mantisbt.org/bugs/')
+
+        token = self.get_password('token')
+
+        self.client = MantisbtClient(self.host, token)
+        self.user = None
+
+        self.exclude_projects = self.config.get('exclude_projects', [], aslist)
+        self.include_projects = self.config.get('include_projects', [], aslist)
+
+        self.involved_issues = self.config.get(
+            'involved_issues', default=True, to_type=asbool
+        )
+        self.assigned_issues = self.config.get(
+            'assigned_issues', default=False, to_type=asbool
+        )
+        self.monitored_issues = self.config.get(
+            'monitored_issues', default=False, to_type=asbool
+        )
+        self.reported_issues = self.config.get(
+            'reported_issues', default=False, to_type=asbool
+        )
+        self.tag_template = self.config.get(
+            'tag_template', default='{{tag}}', to_type=six.text_type
+        )
+        self.annotation_template = self.config.get(
+            'annotation_template', default='{{date}} {{author}} ({{view}}): {{text}}', to_type=six.text_type
+        )
+
+    def get_service_metadata(self):
+        return {
+            'tag_template': self.tag_template,
+            'annotation_template': self.annotation_template,
+        }
+
+    def filter_involved_issues(self, issue):
+        _, issue = issue
+        user = self.client.get_user()
+        uid = user["id"]
+        if issue["reporter"]["id"] != uid and \
+                issue.get("handler", {}).get("id") != uid and \
+                all([ x.get("user", {}).get("id") != uid for x in issue.get("history", [])]) and \
+                all([ x.get("user", {}).get("id") != uid for x in issue.get("monitors", [])]):
+            return False
+        return self.filter_project_name(issue["project"]["name"])
+
+    def filter_issues(self, issue):
+        _, issue = issue
+        return self.filter_project_name(issue["project"]["name"])
+
+    def filter_project_name(self, name):
+        if self.exclude_projects:
+            if name in self.exclude_projects:
+                return False
+
+        if self.include_projects:
+            if name in self.include_projects:
+                return True
+            else:
+                return False
+
+        return True
+
+    @staticmethod
+    def get_keyring_service(service_config):
+        host = service_config.get('host', 'www.mantisbt.org/bugs/')
+        username = service_config.get('username', default='nousername')
+        return "mantisbt://{username}@{host}".format(username=username,
+                host=host)
+
+    @staticmethod
+    def to_issue_dict(issues):
+        return { i['id']: i for i in issues }
+
+    def get_owner(self, issue):
+        return issue.get("handler", {}).get("name")
+
+    def get_author(self, issue):
+        return issue.get("reporter", {}).get("name")
+
+    def issues(self):
+        issues = {}
+        is_limited = self.assigned_issues or self.monitored_issues or self.reported_issues
+
+        if self.assigned_issues:
+            issues.update(
+                filter(self.filter_issues, self.to_issue_dict(self.client.get_assigned_issues()).items())
+            )
+        if self.monitored_issues:
+            issues.update(
+                filter(self.filter_issues, self.to_issue_dict(self.client.get_monitored_issues()).items())
+            )
+        if self.reported_issues:
+            issues.update(
+                filter(self.filter_issues, self.to_issue_dict(self.client.get_reported_issues()).items())
+            )
+
+        if not is_limited:
+            all_issues = self.to_issue_dict(self.client.get_issues())
+            if self.involved_issues:
+                issues.update(
+                    filter(self.filter_involved_issues, all_issues.items())
+                )
+            else:
+                issues.update(
+                    filter(self.filter_issues, all_issues.items())
+                )
+
+        log.debug(" Found %i issues.", len(issues))
+        if not is_limited:
+            issues = list(filter(self.include, issues.values()))
+        else:
+            issues = list(issues.values())
+        log.debug(" Pruned down to %i issues.", len(issues))
+
+        for issue in issues:
+            issue_obj = self.get_issue_for_record(issue)
+            extra = {
+                'host': self.host
+            }
+            issue_obj.update_extra(extra)
+            yield issue_obj
+
+    @classmethod
+    def validate_config(cls, service_config, target):
+        if 'token' not in service_config:
+            die("[%s] has no 'mantisbt.token'" % target)
+
+        super(MantisbtService, cls).validate_config(service_config, target)
diff --git a/setup.py b/setup.py
index d6d957a..665e36e 100644
--- a/setup.py
+++ b/setup.py
@@ -80,6 +80,7 @@ setup(name='bugwarrior',
       activecollab2=bugwarrior.services.activecollab2:ActiveCollab2Service
       activecollab=bugwarrior.services.activecollab:ActiveCollabService
       jira=bugwarrior.services.jira:JiraService
+      mantisbt=bugwarrior.services.mantisbt:MantisbtService
       megaplan=bugwarrior.services.megaplan:MegaplanService
       phabricator=bugwarrior.services.phab:PhabricatorService
       versionone=bugwarrior.services.versionone:VersionOneService