summaryrefslogtreecommitdiffstats
path: root/lib/cnucnu/bugzilla_reporter.py
blob: 1c336d924e71e81e00c1646147b84a256721d5d7 (plain)
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
#!/usr/bin/python
# vim: fileencoding=utf8 foldmethod=marker
#{{{ License header: GPLv2+
#    This file is part of cnucnu.
#
#    Cnucnu is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 2 of the License, or
#    (at your option) any later version.
#
#    Cnucnu is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with cnucnu.  If not, see <http://www.gnu.org/licenses/>.
#}}}

from bugzilla import Bugzilla
from config import Config
import pprint as pprint_module
pp = pprint_module.PrettyPrinter(indent=4)
pprint = pp.pprint

class BugzillaReporter(object):
    base_query = {'query_format': ['advanced'], 'emailreporter1': ['1'], 'emailtype1': ['exact']}

    bug_status_open = ['NEW', 'ASSIGNED', 'MODIFIED', 'ON_DEV', 'ON_QA', 'VERIFIED', 'FAILS_QA', 'RELEASE_PENDING', 'POST']
    bug_status_closed = ['CLOSED']

    new_bug = {'version': 'rawhide',
# Using assigned returns an exception:
# <Fault 32000: 'You are not allowed to file new bugs with the\n      ASSIGNED status.'>
# if the account is in editbugs, fedora_bugs, fedora_contrib, setpriority
# if not, then it is silently ignored
#               'status': 'ASSIGNED',
               'status': 'NEW',
               'keywords': 'FutureFeature'
            }
    summary_template = "%(name)s-%(latest_upstream)s is available"
    description_template = \
"""Latest upstream release: %(latest_upstream)s
Current version in %(repo_name)s: %(repo_version)s
URL: %(url)s

More information about the service that created this bug can be found at:
https://fedoraproject.org/wiki/Using_FEver_to_track_upstream_changes"""
            
    def __init__(self, bugzilla_url, bugzilla_username, bugzilla_password, bugzilla_product):
        bz = Bugzilla(url=bugzilla_url, user=bugzilla_username, password=bugzilla_password)
        self.bz = bz
        self.bz.login()
        self.product = bugzilla_product
        self.base_query['product'] = [bugzilla_product]
        self.base_query['email1'] = [bugzilla_username]
        self.new_bug['product'] = bugzilla_product
        self.bugzilla_username = bugzilla_username


    def report_outdated(self, package, dry_run=True):
        if package.upstream_newer:
            matching_bugs = self.get_bug(package)
            # TODO: warning in case of more than one matching bug, then something is wrong
            if not matching_bugs:
                open = self.get_open(package)
                if not open:
                    bug = {'component': package.name,
                           'summary': self.summary_template % package,
                           'description': self.description_template % package
                            }
                    bug.update(self.new_bug)

                    if not dry_run:
                        new_bug = self.bz.createbug(**bug)
                        print self.bz._proxy.bugzilla.changeStatus(new_bug.bug_id, "ASSIGNED", self.bugzilla_username, "", "", False, False, 1)
                        print "https://bugzilla.redhat.com/show_bug.cgi?id=%s" % new_bug.bug_id
                    else:
                        pprint(bug)
                else:
                    open_bug = open[0]
                    summary = open_bug.summary

                    # summary should be '<name>-<version> <some text>'
                    # To extract the version get everything before the first space
                    # with split and then remove the name and '-' via slicing
                    bug_version = summary.split(" ")[0][len(package.name)+1:]

                    if bug_version != package.latest_upstream:
                        # :TODO: comment creation untested
                        update = {'short_desc': self.summary_template % package,
                                  'comment': self.description_template % package
                                 }
                        res = self.bz._update_bugs(open_bug.bug_id, update)
                        print res
                        return res
            else:
                for bug in matching_bugs:
                    print "bug already filed: https://bugzilla.redhat.com/show_bug.cgi?id=%s %s" % (bug.bug_id, bug.bug_status)

    def get_bug(self, package):
        q = {'component': [package.name],
             'bug_status': self.bug_status_open + self.bug_status_closed,
             'short_desc': ['%(name)s-%(latest_upstream)s' % package],
             'short_desc_type': ['substring']
            }
       
        q.update(self.base_query)
        bugs = self.bz.query(q)
        return bugs

    def get_open(self, package):
        q = {'component': [package.name],
             'bug_status': self.bug_status_open
            }

        q.update(self.base_query)
        return self.bz.query(q)