summaryrefslogtreecommitdiffstats
path: root/update-blocker-wiki
blob: ce746370b333139782ed9c2989cdd7c8f9112e2b (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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/python

import os
import sys
import locale
import optparse
import getpass

try:
    import bugzilla
except ImportError:
    print "Unable to import bugzilla.  Is python-bugzilla installed?"
    sys.exit(1)

try:
    from simplemediawiki import MediaWiki
except ImportError:
    print "Unable to import simplemediawiki.  Is python-simpemediawiki installed?"
    sys.exit(1)

BUG_STATUS = ['NEW', 'ASSIGNED', 'ON_DEV', 'MODIFIED', 'POST', 'ON_QA', 'FAILS_QA',
              'PASSES_QA', 'REOPENED', 'VERIFIED', 'RELEASE_PENDING']

# Blocker query values
BLOCKER_ACCEPTED = {'status_whiteboard': 'AcceptedBlocker',
                    'status_whiteboard_type': 'anywords'}
BLOCKER_PROPOSED = {'status_whiteboard': 'AcceptedBlocker RejectedBlocker',
                    'status_whiteboard_type': 'nowords'}

# NTH query values
NTH_ACCEPTED = {'status_whiteboard': 'AcceptedNTH',
                    'status_whiteboard_type': 'anywords'}
NTH_PROPOSED = {'status_whiteboard': 'AcceptedNTH RejectedNTH',
                    'status_whiteboard_type': 'nowords'}

# UTF8 helper - "borrored" from python-bugzilla
def to_encoding(ustring):
    if isinstance(ustring, basestring):
        if isinstance(ustring, unicode):
            return ustring.encode(locale.getpreferredencoding(), 'replace')
        return ustring
    return u''

# Display list of bugs, organized by components
def display_bugs_by_component(bugs_by_component, bugs_by_id):
    buf = ''
    components = sorted(bugs_by_component.keys())
    for component in components:
        buf += '\n=== %s ===\n' % component
        # sorted list
        bugs = sorted(bugs_by_component.get(component,[]))
        for b in bugs:
            b = bugs_by_id[b]
            buf += '* [https://bugzilla.redhat.com/%s %s] (%s) - %s\n' % (b.bug_id, b.bug_id, b.bug_status, b.short_desc)
    return buf

def parse_args():
    '''Set up the option parser'''
    parser = optparse.OptionParser(usage="%prog [options]")
    parser.add_option('-v', '--verbose', action='store_true', default=False,
        help='Enable verbosity')
    parser.add_option('--hotdog', action='store_true', default=False,
        help='Enable hot dog (default: %default)')

    optgrp = optparse.OptionGroup(parser, "Required options")
    optgrp.add_option('-n', '--name', action='store', default=None,
        help='Wiki page name to save results')
    optgrp.add_option('--blocker', action='store', default=None,
        help='Blocker tracking bug number')
    optgrp.add_option('--nth', action='store', default=None,
        help='Nice-to-Have tracking bug number')
    optgrp.add_option('-u', '--user', action='store', default=None,
        help='Mediawiki username')
    optgrp.add_option('-p', '--passwd', action='store', default=None,
        help='Mediawiki password')
    parser.add_option_group(optgrp)

    (opts, args) = parser.parse_args()

    # sanitize helper
    def sanitize_input(parser, value, label, ispass=False):
        if value is None and sys.stdin.isatty():
            value = raw_input('Enter %s: ' % label)
        if value is None:
            parser.error('Must provide a valid %s' % label)
        return value

    opts.user = sanitize_input(parser, opts.user, "username")
    opts.passwd = sanitize_input(parser, opts.passwd, "password", ispass=True)
    opts.blocker = sanitize_input(parser, opts.blocker, "Blocker bug number")
    opts.nth = sanitize_input(parser, opts.nth, "Nice-to-have bug number")
    opts.name = sanitize_input(parser, opts.name, "Wiki page name")

    return opts

if __name__ == '__main__':

    opts = parse_args()

    # Connect to bugzilla
    bz = bugzilla.RHBugzilla3(url='https://bugzilla.redhat.com/xmlrpc.cgi')

    # Get a list of accepted blocker bugs
    if opts.verbose: print 'Querying accepted blocker bugs ...'
    q = {'bug_status': BUG_STATUS,
         'value0-0-0': opts.blocker,
         'type0-0-0': 'substring',
         'field0-0-0': 'blocked'}
    q.update(BLOCKER_ACCEPTED)
    accepted_blockers = bz.query(q)

    # Get a list of proposed blocker bugs
    if opts.verbose: print 'Querying proposed blocker bugs ...'
    q = {'bug_status': BUG_STATUS,
         'value0-0-0': opts.blocker,
         'type0-0-0': 'substring',
         'field0-0-0': 'blocked'}
    q.update(BLOCKER_PROPOSED)
    proposed_blockers = bz.query(q)

    # Get a list of accepted NTH bugs
    if opts.verbose: print 'Querying accepted nice-to-have bugs ...'
    q = {'bug_status': BUG_STATUS,
         'value0-0-0': opts.nth,
         'type0-0-0': 'substring',
         'field0-0-0': 'blocked'}
    q.update(NTH_ACCEPTED)
    accepted_nths = bz.query(q)

    # Get a list of proposed NTH bugs
    if opts.verbose: print 'Querying proposed nice-to-have bugs ...'
    q = {'bug_status': BUG_STATUS,
         'value0-0-0': opts.nth,
         'type0-0-0': 'substring',
         'field0-0-0': 'blocked'}
    q.update(NTH_PROPOSED)
    proposed_nths = bz.query(q)

    # Organize bugs for later reference
    if opts.verbose: print 'Organizing bugs ...'
    bugs_by_id = dict()

    # walk accepted blockers
    accepted_blocker_by_component = dict()
    for b in accepted_blockers:
        if not accepted_blocker_by_component.has_key(b.component):
            accepted_blocker_by_component[b.component] = list()
        accepted_blocker_by_component[b.component].append(b.bug_id)
        bugs_by_id[b.bug_id] = b

    # walk proposed blockers
    proposed_blocker_by_component = dict()
    for b in proposed_blockers:
        if not proposed_blocker_by_component.has_key(b.component):
            proposed_blocker_by_component[b.component] = list()
        proposed_blocker_by_component[b.component].append(b.bug_id)
        bugs_by_id[b.bug_id] = b

    # walk accepted nths
    accepted_nth_by_component = dict()
    for b in accepted_nths:
        if not accepted_nth_by_component.has_key(b.component):
            accepted_nth_by_component[b.component] = list()
        accepted_nth_by_component[b.component].append(b.bug_id)
        bugs_by_id[b.bug_id] = b

    # walk proposed nths
    proposed_nth_by_component = dict()
    for b in proposed_nths:
        if not proposed_nth_by_component.has_key(b.component):
            proposed_nth_by_component[b.component] = list()
        proposed_nth_by_component[b.component].append(b.bug_id)
        bugs_by_id[b.bug_id] = b

    # Generate page content
    page_content = '''

''This page generated automatically using [http://fedorapeople.org/gitweb?p=jlaska/public_git/scripts.git %s].''

''' % (os.path.basename(sys.argv[0]),)

    # Start with the dog
    if opts.hotdog:
        page_content += '[[File:Hotdog.gif|right]]'
    else:
        page_content += '[[File:F{{FedoraVersionNumber|next}}_anaconda_center.png|right]]'

    def join_lists(l):
        '''
        Takes a list of lists, and joins them into a single list. For example:
        [[1,2,3],[4],[5,6,7]] will become [1,2,3,4,5,6,7]
        '''
        return [item for sublist in l for item in sublist]

    # Display approved blockers
    page_content += '''
== Approved Blockers ==
The following list of bugs are approved blockers that must be resolved.  There
are %s bug(s) affecting %s component(s).

''' % (len(join_lists(accepted_blocker_by_component.values())), len(accepted_blocker_by_component))
    # Sorted list of approved bugs
    page_content += display_bugs_by_component(accepted_blocker_by_component, bugs_by_id)

    # Display proposed blockers
    page_content += '''
== Proposed Blockers ==
The following list of bugs are not yet approved to block the release.  There
are %s bug(s) affecting %s component(s).  For guidance on reviewing the following
bugs, refer to [[QA:SOP_blocker_bug_process]].

''' % (len(join_lists(proposed_blocker_by_component.values())), len(proposed_blocker_by_component))
    # Sorted list of proposed bugs
    page_content += display_bugs_by_component(proposed_blocker_by_component, bugs_by_id)

    # Display approved nths
    page_content += '''
== Approved NICE-TO-HAVE ==
The following list of bugs are approved nths that must be resolved.  There are
%s bug(s) affecting %s component(s).

''' % (len(join_lists(accepted_nth_by_component.values())), len(accepted_nth_by_component))

    # Sorted list of approved bugs
    page_content += display_bugs_by_component(accepted_nth_by_component, bugs_by_id)

    # Display proposed nths
    page_content += '''
== Proposed NICE-TO-HAVE ==
The following list of bugs are not yet approved to block the release.  There
are %s bug(s) affecting %s component(s).  For guidance on reviewing the following
bugs, refer to [[QA:SOP_nth_bug_process]].

''' % (len(join_lists(proposed_nth_by_component.values())), len(proposed_nth_by_component))

    # Sorted list of proposed bugs
    page_content += display_bugs_by_component(proposed_nth_by_component, bugs_by_id)

    # Create mediawiki handle
    if opts.verbose: print 'Connecting to mediawiki ...'
    wiki = MediaWiki('https://fedoraproject.org/w/api.php')

    # Login to the wiki
    if not wiki.login(opts.user, opts.passwd):
        print "Error: invalid mediawiki username or password"
        sys.exit(1)

    # Get an edit token
    q = dict(action='query',
             prop='info',
             intoken='edit',
             titles=opts.name)
    response = wiki.call(q)
    pages = [v for k,v in response.get('query', {}).get('pages',{}).items()]
    edit_token = list()
    if pages:
        edit_token = pages[0]

    # Make the wiki edit
    q = dict(action='edit',
             token=edit_token.get('edittoken',''),
             starttimestamp=edit_token.get('starttimestamp',''),
             summary='This scripted update brought to you by python-simplemediawiki and python-bugzilla.',
             text=to_encoding(u'%s' % page_content),
             title=opts.name)
    response = wiki.call(q)

    if response.get('edit',{}).get('result','').lower() == 'failure':
        # Is a captcha required?
        captcha = response.get('edit', {}).get('captcha', False)
        if captcha:
            q.update(dict(captchaid=captcha.get('id'),
                          captchaword=eval(captcha.get('question'))))
            response = wiki.call(q)
        if response.get('result','').lower() == 'failure':
            print "Failed to update '%s'\n%s" % (q.get('title'), response)
            sys.exit(1)
    print "Updated %s" % q.get('title')
    sys.exit(0)