Passed
Push — master ( 62d7d9...8230b1 )
by Alexander
03:29
created

tcms.issuetracker.kiwitcms   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 49
dl 0
loc 106
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A KiwiTCMS.add_testexecution_to_issue() 0 23 4
A KiwiTCMS._rpc_connection() 0 2 1
A KiwiTCMS.is_adding_testcase_to_issue_disabled() 0 2 1
A KiwiTCMS.details() 0 14 1
A KiwiTCMS.report_issue_from_testexecution() 0 28 1
A KiwiTCMS.bug_id_from_url() 0 6 1
1
# Copyright (c) 2019 Alexander Todorov <[email protected]>
2
3
# Licensed under the GPL 2.0: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
4
5
"""
6
    This module implements integration with Kiwi TCMS own bug tracking system!
7
"""
8
9
from django.template.loader import render_to_string
10
11
from tcms.bugs.views import New
12
from tcms.bugs.models import Bug
13
from tcms.issuetracker.base import IssueTrackerType
14
from tcms.core.contrib.linkreference.models import LinkReference
15
16
17
class KiwiTCMS(IssueTrackerType):
18
    """
19
        Support for Kiwi TCMS. Required fields:
20
21
        :base_url: the FQDN of the current instance. Used to match against defect URLs.
22
23
        The rest of the fields are not used!
24
    """
25
26
    def _rpc_connection(self):
27
        return None
28
29
    def is_adding_testcase_to_issue_disabled(self):  # pylint: disable=invalid-name, no-self-use
30
        return False
31
32
    def details(self, url):
33
        """
34
            Provide more details from our own bug tracker!
35
        """
36
        bug_id = self.bug_id_from_url(url)
37
        bug = Bug.objects.get(pk=bug_id)
38
39
        result = {
40
            'title': bug.summary,
41
            'description': render_to_string('include/bug_details.html',
42
                                            {'object': bug})
43
        }
44
45
        return result
46
47
    def add_testexecution_to_issue(self, executions, issue_url):
48
        """
49
            Directly 'link' BUG and TE objects via their m2m
50
            relationship.
51
52
            .. note::
53
54
                This method takes extra steps to safeguard from
55
                bogus input b/c it is called unconditionally from
56
                API method ``TestCase.add_link()``!
57
        """
58
        try:
59
            bug_id = self.bug_id_from_url(issue_url)
60
        except AttributeError:
61
            return
62
63
        try:
64
            bug = Bug.objects.get(pk=bug_id)
65
        except Bug.DoesNotExist:
66
            return
67
68
        for execution in executions:
69
            bug.executions.add(execution)
70
71
    def report_issue_from_testexecution(self, execution, user):
72
        """
73
            Create the new bug using internal API instead of
74
            going through the RPC layer and return its URL
75
        """
76
        data = {
77
            'reporter': user,
78
            'summary': 'Test case failure: %s' % execution.case.summary,
79
            'product': execution.run.plan.product,
80
            'version': execution.run.product_version,
81
            'build': execution.build,
82
            'text': self._report_comment(execution),
83
            '_execution': execution,
84
        }
85
86
        bug = New.create_bug(data)
87
88
        # link Bug to TE via m2m
89
        bug.executions.add(execution)
90
91
        # and also add a link reference that will be shown in the UI
92
        LinkReference.objects.get_or_create(
93
            execution=execution,
94
            url=bug.get_full_url(),
95
            is_defect=True,
96
        )
97
98
        return bug.get_full_url()
99
100
    @classmethod
101
    def bug_id_from_url(cls, url):
102
        """
103
            Strips the last '/' and returns the PK
104
        """
105
        return super().bug_id_from_url(url.strip('/'))
106