Skip to content

Commit c2b4fed

Browse files
cli: Support --field and --field-json for bugzilla attach (#206)
Enables option passthrough for adding attachments too Resolves: #169 --------- Signed-off-by: Cole Robinson <crobinso@redhat.com> Co-authored-by: Andreas Hasenkopf <andreas@hasenkopf.xyz>
1 parent 46f6cc3 commit c2b4fed

3 files changed

Lines changed: 39 additions & 11 deletions

File tree

bugzilla/_cli.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,19 @@ def _parser_add_output_options(p):
185185
"section 'Output options' for more details.")
186186

187187

188+
def _parser_add_field_passthrough_opts(p):
189+
p.add_argument('--field',
190+
metavar="FIELD=VALUE", action="append", dest="fields",
191+
help="Manually specify a bugzilla API field. FIELD is "
192+
"the raw name used by the bugzilla instance. For example, if your "
193+
"bugzilla instance has a custom field cf_my_field, do:\n"
194+
" --field cf_my_field=VALUE")
195+
p.add_argument('--field-json',
196+
metavar="JSONSTRING", action="append", dest="field_jsons",
197+
help="Specify --field data as a JSON string. Example: --field-json "
198+
'\'{"cf_my_field": "VALUE", "cf_array_field": [1, 2]}\'')
199+
200+
188201
def _parser_add_bz_fields(rootp, command):
189202
cmd_new = (command == "new")
190203
cmd_query = (command == "query")
@@ -256,17 +269,7 @@ def _parser_add_bz_fields(rootp, command):
256269
p.add_argument('-F', '--fixed_in',
257270
help="RHBZ 'Fixed in version' field")
258271

259-
# Put this at the end, so it sticks out more
260-
p.add_argument('--field',
261-
metavar="FIELD=VALUE", action="append", dest="fields",
262-
help="Manually specify a bugzilla API field. FIELD is "
263-
"the raw name used by the bugzilla instance. For example, if your "
264-
"bugzilla instance has a custom field cf_my_field, do:\n"
265-
" --field cf_my_field=VALUE")
266-
p.add_argument('--field-json',
267-
metavar="JSONSTRING", action="append", dest="field_jsons",
268-
help="Specify --field data as a JSON string. Example: --field-json "
269-
'\'{"cf_my_field": "VALUE", "cf_array_field": [1, 2]}\'')
272+
_parser_add_field_passthrough_opts(p)
270273

271274
if not cmd_modify:
272275
_parser_add_output_options(rootp)
@@ -405,6 +408,8 @@ def _setup_action_attach_parser(subparsers):
405408
p.add_argument('--private', action='store_true', default=False,
406409
help='Mark new comment as private')
407410

411+
_parser_add_field_passthrough_opts(p)
412+
408413

409414
def _setup_action_login_parser(subparsers):
410415
usage = 'bugzilla login [--api-key] [username [password]]'
@@ -1172,6 +1177,8 @@ def _do_set_attach(bz, opt, parser):
11721177
kwargs["is_private"] = True
11731178
desc = opt.desc or os.path.basename(fileobj.name)
11741179

1180+
_merge_field_opts(kwargs, opt.fields, opt.field_jsons, parser)
1181+
11751182
# Upload attachments
11761183
for bugid in opt.ids:
11771184
attid = bz.attachfile(bugid, fileobj, desc, **kwargs)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(['123456'],
2+
'STRIPPED-BY-TESTSUITE',
3+
{'content_type': 'text/plain',
4+
'file_name': 'bz-attach-get1.txt',
5+
'flags': [{'name': 'review',
6+
'requestee': 'crobinso@redhat.com',
7+
'status': '-'}],
8+
'is_obsolete': '1',
9+
'summary': 'bz-attach-get1.txt'})

tests/test_cli_attach.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ def test_attach(run_cli):
4949
out = run_cli(cmd, fakebz, stdin=attachcontent)
5050
assert "Created attachment 1557949 on bug 123456" in out
5151

52+
# Test --field passthrough
53+
cmd = "bugzilla attach 123456 --file=%s " % attachfile
54+
cmd += "--field=is_obsolete=1 "
55+
cmd += "--field-json "
56+
cmd += ('\'{"flags": [{"name": "review"'
57+
', "requestee": "crobinso@redhat.com", "status": "-"}]}\'')
58+
fakebz = tests.mockbackend.make_bz(
59+
bug_attachment_create_args="data/mockargs/test_attach3.txt",
60+
bug_attachment_create_return={'ids': [1557949]})
61+
out = run_cli(cmd, fakebz)
62+
assert "Created attachment 1557949 on bug 123456" in out
63+
5264

5365
def _test_attach_get(run_cli):
5466
# Hit error when using ids with --get*

0 commit comments

Comments
 (0)