6e94e79b5ca24f1dd7c5e2eeab216ee1f3bcdca2
[be.git] / libbe / command / new.py
1 # Copyright (C) 2005-2012 Aaron Bentley <abentley@panoramicfeedback.com>
2 #                         Andrew Cooper <andrew.cooper@hkcreations.org>
3 #                         Chris Ball <cjb@laptop.org>
4 #                         Gianluca Montecchi <gian@grys.it>
5 #                         Niall Douglas (s_sourceforge@nedprod.com) <spam@spamtrap.com>
6 #                         W. Trevor King <wking@tremily.us>
7 #
8 # This file is part of Bugs Everywhere.
9 #
10 # Bugs Everywhere is free software: you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by the Free
12 # Software Foundation, either version 2 of the License, or (at your option) any
13 # later version.
14 #
15 # Bugs Everywhere is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
18 # more details.
19 #
20 # You should have received a copy of the GNU General Public License along with
21 # Bugs Everywhere.  If not, see <http://www.gnu.org/licenses/>.
22
23 import libbe
24 import libbe.command
25 import libbe.command.util
26
27 from .assign import parse_assigned as _parse_assigned
28
29
30 class New (libbe.command.Command):
31     """Create a new bug
32
33     >>> import os
34     >>> import sys
35     >>> import time
36     >>> import libbe.bugdir
37     >>> import libbe.util.id
38     >>> bd = libbe.bugdir.SimpleBugDir(memory=False)
39     >>> io = libbe.command.StringInputOutput()
40     >>> io.stdout = sys.stdout
41     >>> ui = libbe.command.UserInterface(io=io)
42     >>> ui.storage_callbacks.set_storage(bd.storage)
43     >>> cmd = New()
44
45     >>> uuid_gen = libbe.util.id.uuid_gen
46     >>> libbe.util.id.uuid_gen = lambda: 'X'
47     >>> ui._user_id = u'Fran\\xe7ois'
48     >>> options = {'assigned': 'none'}
49     >>> ret = ui.run(cmd, options=options, args=['this is a test',])
50     Created bug with ID abc/X
51     >>> libbe.util.id.uuid_gen = uuid_gen
52     >>> bd.flush_reload()
53     >>> bug = bd.bug_from_uuid('X')
54     >>> print bug.summary
55     this is a test
56     >>> bug.creator
57     u'Fran\\xe7ois'
58     >>> bug.reporter
59     u'Fran\\xe7ois'
60     >>> bug.time <= int(time.time())
61     True
62     >>> print bug.severity
63     minor
64     >>> print bug.status
65     open
66     >>> print bug.assigned
67     None
68     >>> ui.cleanup()
69     >>> bd.cleanup()
70     """
71     name = 'new'
72
73     def __init__(self, *args, **kwargs):
74         libbe.command.Command.__init__(self, *args, **kwargs)
75         self.options.extend([
76                 libbe.command.Option(name='reporter', short_name='r',
77                     help='The user who reported the bug',
78                     arg=libbe.command.Argument(
79                         name='reporter', metavar='NAME')),
80                 libbe.command.Option(name='creator', short_name='c',
81                     help='The user who created the bug',
82                     arg=libbe.command.Argument(
83                         name='creator', metavar='NAME')),
84                 libbe.command.Option(name='assigned', short_name='a',
85                     help='The developer in charge of the bug',
86                     arg=libbe.command.Argument(
87                         name='assigned', metavar='NAME',
88                         completion_callback=libbe.command.util.complete_assigned)),
89                 libbe.command.Option(name='status', short_name='t',
90                     help='The bug\'s status level',
91                     arg=libbe.command.Argument(
92                         name='status', metavar='STATUS',
93                         completion_callback=libbe.command.util.complete_status)),
94                 libbe.command.Option(name='severity', short_name='s',
95                     help='The bug\'s severity',
96                     arg=libbe.command.Argument(
97                         name='severity', metavar='SEVERITY',
98                         completion_callback=libbe.command.util.complete_severity)),
99                 libbe.command.Option(name='bugdir', short_name='b',
100                     help='Short bugdir UUID for the new bug.  You '
101                     'only need to set this if you have multiple bugdirs in '
102                     'your repository.',
103                     arg=libbe.command.Argument(
104                         name='bugdir', metavar='ID', default=None,
105                         completion_callback=libbe.command.util.complete_bugdir_id)),
106                 libbe.command.Option(name='full-uuid', short_name='f',
107                     help='Print the full UUID for the new bug')
108                 ])
109         self.args.extend([
110                 libbe.command.Argument(name='summary', metavar='SUMMARY')
111                 ])
112
113     def _run(self, **params):
114         if params['summary'] == '-': # read summary from stdin
115             summary = self.stdin.readline()
116         else:
117             summary = params['summary']
118         storage = self._get_storage()
119         bugdirs = self._get_bugdirs()
120         if params['bugdir']:
121             bugdir = bugdirs[bugdir]
122         elif len(bugdirs) == 1:
123             bugdir = bugdirs.values()[0]
124         else:
125             raise libbe.command.UserError(
126                 'Ambiguous bugdir {}'.format(sorted(bugdirs.values())))
127         storage.writeable = False
128         bug = bugdir.new_bug(summary=summary.strip())
129         if params['creator'] != None:
130             bug.creator = params['creator']
131         else:
132             bug.creator = self._get_user_id()
133         if params['reporter'] != None:
134             bug.reporter = params['reporter']
135         else:
136             bug.reporter = bug.creator
137         if params['assigned'] != None:
138             bug.assigned = _parse_assigned(self, params['assigned'])
139         if params['status'] != None:
140             bug.status = params['status']
141         if params['severity'] != None:
142             bug.severity = params['severity']
143         storage.writeable = True
144         bug.save()
145         if params['full-uuid']:
146             bug_id = bug.id.long_user()
147         else:
148             bug_id = bug.id.user()
149         self.stdout.write('Created bug with ID %s\n' % (bug_id))
150         return 0
151
152     def _long_help(self):
153         return """
154 Create a new bug, with a new ID.  The summary specified on the
155 commandline is a string (only one line) that describes the bug briefly
156 or "-", in which case the string will be read from stdin.
157 """