~racb/apport/877852

320 by Brian Murray
add in bugpattern_written.py a bzr plugin for updating bugs for which a bugpattern was written
1
#!/usr/bin/python
2
# Author: Brian Murray <brian@canonical.com>
3
# Copyright (C) 2011 Canonical, Ltd.
4
# License: GPLv3
5
#
6
# this is a bzr plugin that belongs in ~/.bazaar/plugins and will
7
# modify the bug, modify tags and unsubsribe bugcontrol,
8
# for which a bugpattern was written
9
#
10
# it requires the package python-launchpadlib-toolkit be installed
11
12
import os
13
import re
14
15
from bzrlib import branch
16
from lpltk import LaunchpadService
17
from subprocess import Popen, PIPE
18
19
20
def post_push_hook(push_result):
21
    lp = LaunchpadService(config={})
22
    bugcontrol = lp.launchpad.people['ubuntu-bugcontrol']
23
    bug_numbers = []
24
    # assumes you are in the branch directory
25
    wb = branch.Branch.open(os.getcwd())
26
    if wb.get_parent() != 'bzr+ssh://bazaar.launchpad.net/~ubuntu-bugcontrol/apport/ubuntu-bugpatterns/':
27
        return
28
    delta = wb.get_revision_delta(wb.revno())
29
    if 'bugpatterns.xml' not in delta.get_changes_as_text():
30
        return
31
    # can't figure out how to get the contents of the diff
32
    diff = Popen(["bzr", "log", "-r-1", "-p"], stdout=PIPE).communicate()[0]
33
    for line in diff.split('\n'):
34
        if not line.startswith('+'):
35
            continue
36
        if 'pattern url' in line and 'launchpad.net/bugs' in line:
37
            bug_number = re.search('(\d+)', line).group(1)
38
            bug_numbers.append(bug_number)
39
    for bug_number in bug_numbers:
40
        bug = lp.get_bug(bug_number)
337 by Brian Murray
merge branch from RedSingularity adding a bug pattern for bug 844961
41
        if bug.private:
338 by Brian Murray
bugpattern_written.py: warn if the bug a pattern refers to is private
42
            print 'LP: #%s is private and should be made public' % bug_number
320 by Brian Murray
add in bugpattern_written.py a bzr plugin for updating bugs for which a bugpattern was written
43
        if not 'bugpattern-needed' in bug.tags:
325 by Brian Murray
bugpattern_written.py: clarify why a bug is not modified
44
            print 'Not modifying LP: #%s as it needs no pattern' % bug_number
320 by Brian Murray
add in bugpattern_written.py a bzr plugin for updating bugs for which a bugpattern was written
45
            continue
46
        bug.tags.remove('bugpattern-needed')
47
        bug.tags.append('bugpattern-written')
48
        bug.lpbug.unsubscribe(person=bugcontrol)
49
        print 'LP: #%s modified by bug pattern written bzr hook' % bug_number
50
51
branch.Branch.hooks.install_named_hook('post_push', post_push_hook,
52
    'Bug pattern written hook')