Skip to content

Commit

Permalink
Add sub-command to get all fields
Browse files Browse the repository at this point in the history
Signed-off-by: Alexey Gladkov <[email protected]>
  • Loading branch information
legionus committed Oct 17, 2023
1 parent 309918e commit 6e258fd
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
22 changes: 22 additions & 0 deletions jiramail/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ def cmd_subs(cmdargs: argparse.Namespace) -> int:
return jiramail.subs.main(cmdargs)


def cmd_info(cmdargs: argparse.Namespace) -> int:
import jiramail.info
return jiramail.info.main(cmdargs)


def add_common_arguments(parser: argparse.ArgumentParser) -> None:
parser.add_argument("-v", "--verbose",
dest="verbose", action='count', default=0,
Expand Down Expand Up @@ -124,6 +129,7 @@ def setup_parser() -> argparse.ArgumentParser:
sp2_description = """\
receives updates based on saved queries. Saved queries aka substriptions are
read from the configuration file.
"""
sp2 = subparsers.add_parser("subs",
description=sp2_description,
Expand All @@ -133,6 +139,22 @@ def setup_parser() -> argparse.ArgumentParser:
sp2.set_defaults(func=cmd_subs)
add_common_arguments(sp2)

# jiramail info
sp3_description = """\
retrieves issue information from the jira API.
"""
sp3 = subparsers.add_parser("info",
description=sp3_description,
help=sp3_description,
epilog=epilog,
add_help=False)
sp3.set_defaults(func=cmd_info)

sp3.add_argument("--issue",
dest="issue", action="store", default=None, metavar="ISSUE-123",
help="specify the issue to export.")
add_common_arguments(sp3)

return parser


Expand Down
78 changes: 78 additions & 0 deletions jiramail/info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (C) 2023 Alexey Gladkov <[email protected]>

__author__ = 'Alexey Gladkov <[email protected]>'

import argparse
import re

from typing import Optional, Any

import jira
import jira.resources

import jiramail

logger = jiramail.logger


def get_issue_field(issue: jira.resources.Issue, name: str) -> Optional[Any]:
try:
return issue.get_field(field_name=name)
except AttributeError:
pass
field = jiramail.jserv.field_by_name(name.lower(), {})
if field:
try:
return issue.get_field(field["id"])
except AttributeError:
pass
return None


def main(cmdargs: argparse.Namespace) -> int:
config = jiramail.read_config()

if isinstance(config, jiramail.Error):
logger.critical("%s", config.message)
return jiramail.EX_FAILURE

try:
jiramail.jserv = jiramail.Connection(config.get("jira", {}))
except Exception as e:
logger.critical("unable to connect to jira: %s", e)
return jiramail.EX_FAILURE

if cmdargs.issue:
issue = jiramail.jserv.jira.issue(cmdargs.issue)

for field_id, field in jiramail.jserv.jira.editmeta(issue.key)["fields"].items():
match field_id:
case "description" | "comment":
continue

name = field.get("name", field_id)
value = get_issue_field(issue, field_id)
v_type = field.get("schema", {}).get("type", None)
i_type = field.get("schema", {}).get("items", None)

if i_type:
v_type += " / " + i_type

print("#", "=" * 79, sep="")
print("# Field :", str(name))
print("# Type :", str(v_type))
print("# ID :", str(field_id))
print("#", "-" * 79, sep="")
if isinstance(value, list):
for n, item in enumerate(value):
print(f":[{n}]")
print(re.sub(r'^(.*)$', r'| \1', f"{item}", flags=re.M))
elif value is None:
pass
else:
print(re.sub(r'^(.*)$', r'| \1', f"{value}", flags=re.M))
print("*")

return jiramail.EX_SUCCESS

0 comments on commit 6e258fd

Please sign in to comment.