14
14
LOG = logging .getLogger (__name__ )
15
15
16
16
17
+ def _get_bundle (bundle_id ):
18
+ """Fetch bundle by ID or name.
19
+
20
+ Allow users to provide a string to search for bundles. This doesn't make
21
+ sense to expose via the API since there's no uniqueness constraint on
22
+ bundle names.
23
+ """
24
+ if bundle_id .isdigit ():
25
+ return api .detail ('bundles' , bundle_id )
26
+
27
+ bundles = api .index ('bundles' , [('q' , bundle_id )])
28
+ if len (bundles ) == 0 :
29
+ LOG .error ('No matching bundle found: %s' , bundle_id )
30
+ sys .exit (1 )
31
+ elif len (bundles ) > 1 :
32
+ LOG .error ('More than one bundle found: %s' , bundle_id )
33
+ sys .exit (1 )
34
+
35
+ return bundles [0 ]
36
+
37
+
17
38
@click .command (name = 'apply' )
18
- @click .argument ('bundle_id' , type = click . INT )
39
+ @click .argument ('bundle_id' )
19
40
@click .argument ('args' , nargs = - 1 )
20
41
def apply_cmd (bundle_id , args ):
21
42
"""Apply bundle.
22
43
23
44
Apply a bundle locally using the 'git-am' command.
24
45
"""
25
- LOG .debug ('Applying bundle: id=%d ' , bundle_id )
46
+ LOG .debug ('Applying bundle: id=%s ' , bundle_id )
26
47
27
- bundle = api . detail ( 'bundles' , bundle_id )
48
+ bundle = _get_bundle ( bundle_id )
28
49
mbox = api .download (bundle ['mbox' ]).text
29
50
30
51
utils .git_am (mbox , args )
31
52
32
53
33
54
@click .command (name = 'download' )
34
- @click .argument ('bundle_id' , type = click . INT )
55
+ @click .argument ('bundle_id' )
35
56
@click .argument ('output' , type = click .File ('wb' ), required = False )
36
57
def download_cmd (bundle_id , output ):
37
58
"""Download bundle in mbox format.
@@ -40,10 +61,10 @@ def download_cmd(bundle_id, output):
40
61
output path or ``-`` to output to ``stdout``. If ``OUTPUT`` is not
41
62
provided, the output path will be automatically chosen.
42
63
"""
43
- LOG .debug ('Downloading bundle: id=%d ' , bundle_id )
64
+ LOG .debug ('Downloading bundle: id=%s ' , bundle_id )
44
65
45
66
path = None
46
- bundle = api . detail ( 'bundles' , bundle_id )
67
+ bundle = _get_bundle ( bundle_id )
47
68
48
69
if output :
49
70
output .write (api .get (bundle ['mbox' ]).text )
@@ -58,15 +79,15 @@ def download_cmd(bundle_id, output):
58
79
59
80
60
81
@click .command (name = 'show' )
61
- @click .argument ('bundle_id' , type = click . INT )
82
+ @click .argument ('bundle_id' )
62
83
def show_cmd (bundle_id ):
63
84
"""Show information about bundle.
64
85
65
86
Retrieve Patchwork metadata for a bundle.
66
87
"""
67
- LOG .debug ('Showing bundle: id=%d ' , bundle_id )
88
+ LOG .debug ('Showing bundle: id=%s ' , bundle_id )
68
89
69
- bundle = api . detail ( 'bundles' , bundle_id )
90
+ bundle = _get_bundle ( bundle_id )
70
91
71
92
def _format_patch (patch ):
72
93
return '%-4d %s' % (patch .get ('id' ), patch .get ('name' ))
0 commit comments