summaryrefslogtreecommitdiffstats
path: root/bugzilla
blob: 18ee41957619ca14247967b37b6f397444700c1e (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
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
#!/usr/bin/python
# bugzilla - a commandline frontend for the python bugzilla module
#
# Copyright (C) 2007 Red Hat Inc.
# Author: Will Woods <wwoods@redhat.com>
# 
# This program 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.  See http://www.gnu.org/copyleft/gpl.html for
# the full text of the license.

import bugzilla, optparse
import os, sys, glob, re
import logging

version = '0.1'
default_bz = 'https://bugzilla.redhat.com/xmlrpc.cgi'

# Initial simple logging stuff
logging.basicConfig()
log = logging.getLogger("bugzilla")
if '--debug' in sys.argv:
    log.setLevel(logging.DEBUG)
elif '--verbose' in sys.argv:
    log.setLevel(logging.INFO)
        
def findcookie():
    globs = ['~/.mozilla/firefox/*default*/cookies.txt']
    for g in globs:
        log.debug("Looking for cookies.txt in %s", g)
        cookiefiles = glob.glob(os.path.expanduser(g))
        if cookiefiles:
            # return the first one we find.
            # TODO: find all cookiefiles, sort by age, use newest
            return cookiefiles[0]
cookiefile = None

def setup_parser():
    u =   "usage: %prog info|query|new|modify [options]"
    p = optparse.OptionParser(usage=u)
    # General bugzilla connection options
    # TODO: OptionGroup
    p.add_option('--bugzilla',default=default_bz,
            help="bugzilla XMLRPC URI. default: %s" % default_bz)
    p.add_option('--user',
            help="username. Will attempt to use browser cookie if not specified.")
    p.add_option('--password',
            help="password. Will attempt to use browser cookie if not specified.")
    p.add_option('--cookiefile',
            help="cookie file to use for bugzilla authentication")
    p.add_option('--verbose',action='store_true',
            help="give more info about what's going on")
    p.add_option('--debug',action='store_true',
            help="output bunches of debugging info")
    return p

def modify_parser(parser,action):
    p = parser
        # TODO: product and version could default to current system
        # info (read from /etc/redhat-release?)
    if action == 'new':
        p.add_option('-p','--product',
                help="REQUIRED: product name (list with 'bugzilla info -p')")
        p.add_option('-v','--version',
                help="REQUIRED: product version")
        p.add_option('-c','--component',
                help="REQUIRED: component name (list with 'bugzilla info -c PRODUCT')")
        p.add_option('-l','--comment',
                help="REQUIRED: initial bug comment")
        p.add_option('-s','--summary',dest='short_desc',
                help="REQUIRED: bug summary")
        p.add_option('-o','--os',default='Linux',dest='op_sys',
                help="OPTIONAL: operating system (default: Linux)")
        p.add_option('-a','--arch',default='All',dest='rep_platform',
                help="OPTIONAL: arch this bug occurs on (default: All)")
        p.add_option('--severity',default='medium',dest='bug_severity',
                help="OPTIONAL: bug severity (default: medium)")
        p.add_option('--priority',default='medium',dest='priority',
                help="OPTIONAL: bug priority (default: medium)")
        p.add_option('-u','--url',dest='bug_file_loc',default='http://',
                help="OPTIONAL: URL for further bug info")
        p.add_option('--cc',
                help="OPTIONAL: add emails to initial CC list")
        # TODO: alias, assigned_to, reporter, qa_contact, dependson, blocked
    elif action == 'query':
        p.add_option('-p','--product',
                help="product name (list with 'bugzilla info -p')")
        p.add_option('-v','--version',
                help="product version")
        p.add_option('-c','--component',
                help="component name (list with 'bugzilla info -c PRODUCT')")
        p.add_option('-l','--long_desc',
                help="search inside bug comments")
        p.add_option('-s','--short_desc',
                help="search bug summaries")
        p.add_option('-o','--cc',
                help="search cc lists for given address")
        p.add_option('-a','--assigned_to',
                help="search for bugs assigned to this address")
        p.add_option('--blocked',
                help="search for bugs that block this bug ID")
        p.add_option('--dependson',
                help="search for bugs that depend on this bug ID")
        p.add_option('-b','--bug_id',
                help="specify individual bugs by IDs, separated with commas")
        p.add_option('-t','--bug_status',default="NEW,VERIFIED,ASSIGNED,NEEDINFO,ON_DEV,FAILS_QA,REOPENED",
                help="comma-separated list of bug statuses to accept")
    elif action == 'info':
        p.add_option('-p','--products',action='store_true',
                help='Get a list of products')
        p.add_option('-c','--components',metavar="PRODUCT",
                help='List the components in the given product')
        p.add_option('-o','--component_owners',metavar="PRODUCT",
                help='List components (and their owners)')
        p.add_option('-v','--versions',metavar="PRODUCT",
                help='List the versions for the given product')

    if action in ('new','query'):
        # output modifiers
        p.add_option('-f','--full',action='store_const',dest='output',
                const='full',default='normal',help="output detailed bug info")
        p.add_option('-i','--ids',action='store_const',dest='output',
                const='ids',help="output only bug IDs")
        p.add_option('--outputformat',
                help="Print output in the form given. You can use RPM-style "+
                "tags that match bug fields, e.g.: '%{bug_id}: %{short_desc}'")
    return p

if __name__ == '__main__':

    # Set up parser
    parser = setup_parser()
    # Get our action
    if len(sys.argv) > 1 and sys.argv[1] in ('info','query','new','modify'):
        action = sys.argv[1]
    else:
        parser.error("you must specify an action")
    # Add the action-specific args and such
    parser = modify_parser(parser,action)
    # Parse the commandline, woo
    (opt,args) = parser.parse_args()
    
    # Connect to bugzilla
    log.info('Connecting to %s',opt.bugzilla)
    bz=bugzilla.Bugzilla(url=opt.bugzilla)
    if opt.user and opt.password:
        log.info('Using username/password for authentication')
        bz.login(opt.user,opt.password)
    elif opt.cookiefile:
        log.info('Using cookies in %s for authentication', opt.cookiefile)
        bz.readcookiefile(opt.cookiefile)
    else:
        cookiefile = findcookie()
        if cookiefile:
            log.info('Using cookies in %s for authentication', cookiefile)
            bz.readcookiefile(cookiefile)
        else:
            parser.error("Could not find a Firefox cookie file. Try --user/--password.")
    
    # And now we actually execute the given command
    buglist = list() # save the results of query/new/modify here
    if action == 'info':
        if opt.products:
            for k in sorted(bz.products):
                print k

        if opt.components:
            for c in sorted(bz.getcomponents(opt.components)):
                print c

        if opt.component_owners:
            component_details = bz.getcomponentsdetails(opt.component_owners)
            for c in sorted(component_details):
                print "%s: %s" % (c, component_details[c]['initialowner'])

        if opt.versions:
            for p in bz.querydata['product']:
                if p['name'] == opt.versions:
                    for v in p['versions']:
                        print v

    elif action == 'query':
        # shortcut for specific bug_ids
        if opt.bug_id:
            log.debug("bz.getbugs(%s)", opt.bug_id.split(','))
            buglist=bz.getbugs(opt.bug_id.split(','))
        else:
            # Construct the query from the list of queryable options
            q = dict()
            email_count = 1
            chart_id = 0
            for a in ('product','component','version','long_desc','bug_id',
                      'short_desc','cc','assigned_to','bug_status',
                      'blocked','dependson'):
                if hasattr(opt,a):
                    i = getattr(opt,a)
                    if i:
                        if a in ('bug_status'): # list args
                            q[a] = i.split(',')
                        elif a in ('cc','assigned_to'):
                            # the email query fields are kind of weird - thanks
                            # to Florian La Roche for figuring this bit out.
                            # ex.: {'email1':'foo@bar.com','emailcc1':True}
                            q['email%i' % email_count] = i
                            q['email%s%i' % (a,email_count)] = True
                            email_count += 1
                        elif a in ('blocked','dependson'): 
                            # Chart args are weird.
                            q['field%i-0-0' % chart_id] = a
                            q['type%i-0-0' % chart_id]  = 'equals'
                            q['value%i-0-0' % chart_id] = i
                            chart_id += 1
                        else:
                            q[a] = i
            log.debug("bz.query: %s", q)
            buglist = bz.query(q)

    elif action == 'new':
        data = dict()
        required=['product','component','version','short_desc','comment',
             'rep_platform','bug_severity','op_sys','bug_file_loc','priority']
        optional=['cc']
        for a in required + optional:
            i = getattr(opt,a)
            if i:
                data[a] = i
        for k in required:
            if k not in data:
                parser.error('Missing required argument: %s' % k)
        log.debug("bz.createbug(%s)", data)
        b = bz.createbug(**data)
        buglist = [b]
        
    else:
        print "Sorry - '%s' not implemented yet." % action

    # If we're doing new/query/modify, output our results
    if action in ('new','query'):
        if opt.outputformat:
            format_field_re = re.compile("%{[a-z0-9_]+}")
            def bug_field(matchobj):
                fieldname = matchobj.group()[2:-1]
                return str(getattr(b,fieldname))
            for b in buglist:
                print format_field_re.sub(bug_field,opt.outputformat)
        elif opt.output == 'ids':
            for b in buglist:
                print b.bug_id
        elif opt.output == 'full':
            fullbuglist = bz.getbugs([b.bug_id for b in buglist])
            for b in fullbuglist:
                print b
                if b.cc: print "CC: %s" % " ".join(b.cc)
                if b.blocked: print "Blocked: %s" % " ".join([str(i) for i in b.blocked])
                if b.dependson: print "Depends: %s" % " ".join([str(i) for i in b.dependson])
                for c in b.longdescs:
                    print "* %s - %s (%s):\n%s\n" % (c['time'],c['name'],c['email'] or c['safe_email'],c['body'])
        elif opt.output == 'normal':
            for b in buglist:
                print b
        else:
            parser.error("opt.output was set to something weird.")