closedbugs.py 5.06 KB
Newer Older
Jonny Lamb's avatar
Jonny Lamb committed
1
2
3
4
5
6
# -*- coding: utf-8 -*-
#
#   closedbugs.py — closedbugs plugin
#
#   This file is part of debexpo - http://debexpo.workaround.org
#
Jonny Lamb's avatar
Jonny Lamb committed
7
#   Copyright © 2008 Jonny Lamb <jonny@debian.org>
8
#               2011 Arno Töll <debian@toell.net>
Jonny Lamb's avatar
Jonny Lamb committed
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
#
#   Permission is hereby granted, free of charge, to any person
#   obtaining a copy of this software and associated documentation
#   files (the "Software"), to deal in the Software without
#   restriction, including without limitation the rights to use,
#   copy, modify, merge, publish, distribute, sublicense, and/or sell
#   copies of the Software, and to permit persons to whom the
#   Software is furnished to do so, subject to the following
#   conditions:
#
#   The above copyright notice and this permission notice shall be
#   included in all copies or substantial portions of the Software.
#
#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
#   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
#   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
#   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
#   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
#   OTHER DEALINGS IN THE SOFTWARE.

"""
Holds the closedbugs plugin.
"""

35
36
__author__ = 'Arno Töll'
__copyright__ = 'Copyright © 2011 Arno Töll'
Jonny Lamb's avatar
Jonny Lamb committed
37
38
__license__ = 'MIT'

39
from collections import defaultdict
Jonny Lamb's avatar
Jonny Lamb committed
40
41
42
43
import logging

from debexpo.lib import constants
from debexpo.plugins import BasePlugin
44
import SOAPpy
Jonny Lamb's avatar
Jonny Lamb committed
45
46
47
48

log = logging.getLogger(__name__)

class ClosedBugsPlugin(BasePlugin):
49
50
    URL = "http://bugs.debian.org/cgi-bin/soap.cgi"
    NS = "Debbugs/SOAP"
Jonny Lamb's avatar
Jonny Lamb committed
51
52
53
54
55
56

    def test_closed_bugs(self):
        """
        Check to make sure the bugs closed belong to the package.
        """

57
        if 'Closes' not in self.changes:
58
59
60
            log.debug('Package does not close any bugs')
            return

61
        log.debug('Checking whether the bugs closed in the package belong to the package')
Jonny Lamb's avatar
Jonny Lamb committed
62

63
        bugs = [int(x) for x in self.changes['Closes'].split()]
Jonny Lamb's avatar
Jonny Lamb committed
64

65
        if bugs:
66
67
            log.debug('Creating SOAP proxy to bugs.debian.org')
            try:
68
                server = SOAPpy.SOAPProxy(self.URL, self.NS, simplify_objects = 1)
69
                bugs_retrieved = server.get_status( *bugs )
70
71
72
73
                if 'item' in bugs_retrieved:
                    bugs_retrieved = bugs_retrieved['item']
                else:
                    bugs_retrieved = []
74
75
                # Force argument to be a list, SOAPpy returns a dictionary instead of a dictionary list
                # if only one bug was found
76
77
                if not isinstance(bugs_retrieved, list):
                    bugs_retrieved = [bugs_retrieved]
78
            except Exception as e:
79
                log.critical('An error occurred when creating the SOAP proxy at "%s" (ns: "%s"): %s'
80
                    % (self.URL, self.NS, e))
81
                return
82
83
84
85
86
87
88
            
            data = {
                'buglist': bugs,
                'raw': {},
                'errors': [],
                'bugs': defaultdict(list),
                }
89
90
91

            # Index bugs retrieved
            for bug in bugs_retrieved:
92
93
                if 'key' in bug and 'value' in bug:
                    data["raw"][int(bug['key'])] = bug['value']
94
                else:
Jonny Lamb's avatar
Jonny Lamb committed
95
96
                    continue

97
98
            severity = constants.PLUGIN_SEVERITY_INFO
                
99
            for bug in bugs:
100
101
102
                if not bug in data['raw']:
                    data["errors"].append('Bug #%s does not exist' % bug)
                    severity = max(severity, constants.PLUGIN_SEVERITY_ERROR)
Jonny Lamb's avatar
Jonny Lamb committed
103

104
                name = data["raw"][bug]['package']
105
                data["bugs"][name].append((bug, data["raw"][bug]["subject"], data["raw"][bug]["severity"]))
106

107
                if not (data["raw"][bug]['source'] == self.changes["Source"] or name == "wnpp"):
108
109
110
111
112
113
114
115
116
117
118
119
                    data["errors"].append('Bug #%s does not belong to this package' % bug)
                    severity = max(severity, constants.PLUGIN_SEVERITY_ERROR)

            if severity != constants.PLUGIN_SEVERITY_INFO:
                outcome = "Package closes bugs in a wrong way"
            elif "wnpp" in data["bugs"] and len(data["bugs"]) == 1:
                outcome = "Package closes a WNPP bug"
            else:
                outcome = "Package closes bug%s" % ("s" if len(bugs) > 1 else "")

            self.failed(outcome, data, severity)
                    
120
        else:
Jonny Lamb's avatar
Jonny Lamb committed
121
122
            log.debug('Package does not close any bugs')

123

Jonny Lamb's avatar
Jonny Lamb committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
    def _package_in_descriptions(self, name, list):
        """
        Finds out whether a binary package is in a source package by looking at the Description
        field of the changes file for the binary package name.

        ``name``
            Name of the binary package.

        ``list``
            List of Description fields split by '\n'.
        """
        for item in list:
            if item.startswith(name + ' '):
                return True

        return False

plugin = ClosedBugsPlugin