~canonical-platform-qa/lrt/lrt-to-dep8

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
#!/usr/bin/env python2.7
import argparse
import os
import sys

from httplib2 import ServerNotFoundError

from launchpadlib.launchpad import Launchpad


class MonkeyRunnerError(Exception):
    pass


class BugAssistant(object):
    """Base bug assistant """

    def _no_credential():
        raise MonkeyRunnerError(
            "Can not proceed without Launchpad credential.")

    def launchpad_login(self):
        """Log in launchpad and create credentials file if needed"""
        launchpad = Launchpad.login_with(
            'monkey_runner_bug_reporter',
            'production',
            credential_save_failed=self._no_credential
        )
        # and verify authentication
        launchpad.me

        return launchpad


def parse_args():
    parser = argparse.ArgumentParser(
        description='Automatically make a launchpad bug based'
        ' on a crash file')

    parser.add_argument(
        '--title',
        dest='title',
        help='Bug title'
    )

    parser.add_argument(
        '--project',
        dest='project',
        default='lrt-crashes',
        help='Bug project'
    )

    parser.add_argument(
        '--comment',
        default='crash file',
        dest='comment',
        help='Bug comment for attachments'
    )

    parser.add_argument(
        '--series',
        default='utopic',
        dest='series',
        help='Bug series'
    )

    parser.add_argument(
        '--tag',
        default='qasoak',
        dest='tag',
        help='Bug tag'
    )

    parser.add_argument(
        '--description',
        default='a crash was detected',
        dest='description',
        help='Bug description',
    )

    parser.add_argument(
        '--importance',
        default='Critical',
        dest='importance',
        help='Bug importance'
    )

    parser.add_argument(
        '--attachment',
        dest='attachment',
        help='Path to crash file to attach to bug'
    )

    parser.add_argument(
        '--status',
        default='New',
        dest='status',
        help='Bug status'
    )

    parser.add_argument(
        '--login-only',
        dest='login_only',
        help='only log in to launchpad, do not create a bug',
        action='store_true'
    )

    return parser.parse_args()


def login():

    try:
        bug_assistant = BugAssistant()
        launchpad = bug_assistant.launchpad_login()
    except ServerNotFoundError:
        raise MonkeyRunnerError('Error Launchpad server not found')
    except Exception:
        raise MonkeyRunnerError('Error Launchpad authentication failed')
    return launchpad


def make_bug(
        title,
        description,
        attachment=None,
        series='utopic',
        project_name='lrt-crashes',
        tag='qasoak',
        status='New',
        importance='Critical',
        comment='Crash file',
):

    launchpad = login()

    try:
        print('Checking project name...')
        project = launchpad.projects[project_name]
    except ServerNotFoundError:
        raise MonkeyRunnerError('Error Launchpad server not found')
    except Exception:
        error_message = (
            '{0!r} launchpad project not found'.format(project_name)
        )
        raise MonkeyRunnerError(error_message)

    bug = launchpad.bugs.createBug(
        title=title,
        description=description,
        tags=tag,
        target=project,
    )

    if attachment:
        if not os.path.isfile(attachment):
            raise MonkeyRunnerError(
                'Error File {0!r} not found'.format(attachment)
            )

        try:
            print('Uploading crash report as attachment...')
            bug.addAttachment(
                comment=comment,
                filename=os.path.basename(attachment),
                data=open(attachment).read()
            )
        except Exception:
            raise MonkeyRunnerError(
                'Error unable upload crash file to bug'
            )

        print('Crash report successfully added')

    #add series
    #nomination = bug.addNomination(target=series)
    #nomination.approve()

    task = bug.bug_tasks[0]
    task.status = status
    task.importance = importance
    task.lp_save()

if __name__ == '__main__':
    args = parse_args()

    if args.login_only:
        login()
        sys.exit()

    make_bug(
        args.title,
        args.description,
        attachment=args.attachment,
        series=args.series,
        project_name=args.project,
        tag=args.tag,
        status=args.status,
        importance=args.importance,
        comment=args.comment
    )