1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

taiga_issue - bugfix + pythonification (#3067)

* taiga_issue - bugfix + pythonification

* added changelog fragment
This commit is contained in:
Alexei Znamensky 2021-07-26 08:03:45 +12:00 committed by GitHub
parent 20db4fc560
commit 95ceb53676
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 26 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- taiga - some constructs in the module fixed to work also in Python 3 (https://github.com/ansible-collections/community.general/pull/3067).

View file

@ -129,7 +129,7 @@ except ImportError:
TAIGA_MODULE_IMPORTED = False TAIGA_MODULE_IMPORTED = False
def manage_issue(module, taiga_host, project_name, issue_subject, issue_priority, def manage_issue(taiga_host, project_name, issue_subject, issue_priority,
issue_status, issue_type, issue_severity, issue_description, issue_status, issue_type, issue_severity, issue_description,
issue_attachment, issue_attachment_description, issue_attachment, issue_attachment_description,
issue_tags, state, check_mode=False): issue_tags, state, check_mode=False):
@ -157,34 +157,34 @@ def manage_issue(module, taiga_host, project_name, issue_subject, issue_priority
username = getenv('TAIGA_USERNAME') username = getenv('TAIGA_USERNAME')
password = getenv('TAIGA_PASSWORD') password = getenv('TAIGA_PASSWORD')
if not any([username, password]): if not any([username, password]):
return (False, changed, "Missing credentials", {}) return False, changed, "Missing credentials", {}
api.auth(username=username, password=password) api.auth(username=username, password=password)
user_id = api.me().id user_id = api.me().id
project_list = filter(lambda x: x.name == project_name, api.projects.list(member=user_id)) project_list = list(filter(lambda x: x.name == project_name, api.projects.list(member=user_id)))
if len(project_list) != 1: if len(project_list) != 1:
return (False, changed, "Unable to find project %s" % project_name, {}) return False, changed, "Unable to find project %s" % project_name, {}
project = project_list[0] project = project_list[0]
project_id = project.id project_id = project.id
priority_list = filter(lambda x: x.name == issue_priority, api.priorities.list(project=project_id)) priority_list = list(filter(lambda x: x.name == issue_priority, api.priorities.list(project=project_id)))
if len(priority_list) != 1: if len(priority_list) != 1:
return (False, changed, "Unable to find issue priority %s for project %s" % (issue_priority, project_name), {}) return False, changed, "Unable to find issue priority %s for project %s" % (issue_priority, project_name), {}
priority_id = priority_list[0].id priority_id = priority_list[0].id
status_list = filter(lambda x: x.name == issue_status, api.issue_statuses.list(project=project_id)) status_list = list(filter(lambda x: x.name == issue_status, api.issue_statuses.list(project=project_id)))
if len(status_list) != 1: if len(status_list) != 1:
return (False, changed, "Unable to find issue status %s for project %s" % (issue_status, project_name), {}) return False, changed, "Unable to find issue status %s for project %s" % (issue_status, project_name), {}
status_id = status_list[0].id status_id = status_list[0].id
type_list = filter(lambda x: x.name == issue_type, project.list_issue_types()) type_list = list(filter(lambda x: x.name == issue_type, project.list_issue_types()))
if len(type_list) != 1: if len(type_list) != 1:
return (False, changed, "Unable to find issue type %s for project %s" % (issue_type, project_name), {}) return False, changed, "Unable to find issue type %s for project %s" % (issue_type, project_name), {}
type_id = type_list[0].id type_id = type_list[0].id
severity_list = filter(lambda x: x.name == issue_severity, project.list_severities()) severity_list = list(filter(lambda x: x.name == issue_severity, project.list_severities()))
if len(severity_list) != 1: if len(severity_list) != 1:
return (False, changed, "Unable to find severity %s for project %s" % (issue_severity, project_name), {}) return False, changed, "Unable to find severity %s for project %s" % (issue_severity, project_name), {}
severity_id = severity_list[0].id severity_id = severity_list[0].id
issue = { issue = {
@ -199,7 +199,7 @@ def manage_issue(module, taiga_host, project_name, issue_subject, issue_priority
} }
# An issue is identified by the project_name, the issue_subject and the issue_type # An issue is identified by the project_name, the issue_subject and the issue_type
matching_issue_list = filter(lambda x: x.subject == issue_subject and x.type == type_id, project.list_issues()) matching_issue_list = list(filter(lambda x: x.subject == issue_subject and x.type == type_id, project.list_issues()))
matching_issue_list_len = len(matching_issue_list) matching_issue_list_len = len(matching_issue_list)
if matching_issue_list_len == 0: if matching_issue_list_len == 0:
@ -209,16 +209,17 @@ def manage_issue(module, taiga_host, project_name, issue_subject, issue_priority
changed = True changed = True
if not check_mode: if not check_mode:
# Create the issue # Create the issue
new_issue = project.add_issue(issue_subject, priority_id, status_id, type_id, severity_id, tags=issue_tags, description=issue_description) new_issue = project.add_issue(issue_subject, priority_id, status_id, type_id, severity_id, tags=issue_tags,
description=issue_description)
if issue_attachment: if issue_attachment:
new_issue.attach(issue_attachment, description=issue_attachment_description) new_issue.attach(issue_attachment, description=issue_attachment_description)
issue["attachment"] = issue_attachment issue["attachment"] = issue_attachment
issue["attachment_description"] = issue_attachment_description issue["attachment_description"] = issue_attachment_description
return (True, changed, "Issue created", issue) return True, changed, "Issue created", issue
else: else:
# If does not exist, do nothing # If does not exist, do nothing
return (True, changed, "Issue does not exist", {}) return True, changed, "Issue does not exist", {}
elif matching_issue_list_len == 1: elif matching_issue_list_len == 1:
# The issue exists in the project # The issue exists in the project
@ -228,19 +229,19 @@ def manage_issue(module, taiga_host, project_name, issue_subject, issue_priority
if not check_mode: if not check_mode:
# Delete the issue # Delete the issue
matching_issue_list[0].delete() matching_issue_list[0].delete()
return (True, changed, "Issue deleted", {}) return True, changed, "Issue deleted", {}
else: else:
# Do nothing # Do nothing
return (True, changed, "Issue already exists", {}) return True, changed, "Issue already exists", {}
else: else:
# More than 1 matching issue # More than 1 matching issue
return (False, changed, "More than one issue with subject %s in project %s" % (issue_subject, project_name), {}) return False, changed, "More than one issue with subject %s in project %s" % (issue_subject, project_name), {}
except TaigaException as exc: except TaigaException as exc:
msg = "An exception happened: %s" % to_native(exc) msg = "An exception happened: %s" % to_native(exc)
return (False, changed, msg, {}) return False, changed, msg, {}
def main(): def main():
@ -257,15 +258,13 @@ def main():
attachment=dict(type='path', required=False, default=None), attachment=dict(type='path', required=False, default=None),
attachment_description=dict(type='str', required=False, default=""), attachment_description=dict(type='str', required=False, default=""),
tags=dict(required=False, default=[], type='list', elements='str'), tags=dict(required=False, default=[], type='list', elements='str'),
state=dict(type='str', required=False, choices=['present', 'absent'], state=dict(type='str', required=False, choices=['present', 'absent'], default='present'),
default='present'),
), ),
supports_check_mode=True supports_check_mode=True
) )
if not TAIGA_MODULE_IMPORTED: if not TAIGA_MODULE_IMPORTED:
module.fail_json(msg=missing_required_lib("python-taiga"), module.fail_json(msg=missing_required_lib("python-taiga"), exception=TAIGA_IMP_ERR)
exception=TAIGA_IMP_ERR)
taiga_host = module.params['taiga_host'] taiga_host = module.params['taiga_host']
project_name = module.params['project'] project_name = module.params['project']
@ -285,7 +284,6 @@ def main():
state = module.params['state'] state = module.params['state']
return_status, changed, msg, issue_attr_dict = manage_issue( return_status, changed, msg, issue_attr_dict = manage_issue(
module,
taiga_host, taiga_host,
project_name, project_name,
issue_subject, issue_subject,
@ -301,7 +299,7 @@ def main():
check_mode=module.check_mode check_mode=module.check_mode
) )
if return_status: if return_status:
if len(issue_attr_dict) > 0: if issue_attr_dict:
module.exit_json(changed=changed, msg=msg, issue=issue_attr_dict) module.exit_json(changed=changed, msg=msg, issue=issue_attr_dict)
else: else:
module.exit_json(changed=changed, msg=msg) module.exit_json(changed=changed, msg=msg)