Debian Codegraph @

ansible_collections/community/general/plugins/modules/taiga_issue.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2015, Alejandro Guirao <[email protected]>
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 module: taiga_issue
11 short_description: Creates/deletes an issue in a Taiga Project Management Platform
12 description:
13 - Creates/deletes an issue in a Taiga Project Management Platform (U(https://taiga.io)).
14 - An issue is identified by the combination of project, issue subject and issue type.
15 - This module implements the creation or deletion of issues (not the update).
16 extends_documentation_fragment:
17 - community.general._attributes
18 attributes:
19 check_mode:
20 support: full
21 diff_mode:
22 support: none
23 options:
24 taiga_host:
25 type: str
26 description:
27 - The hostname of the Taiga instance.
28 default: https://api.taiga.io
29 project:
30 type: str
31 description:
32 - Name of the project containing the issue. Must exist previously.
33 required: true
34 subject:
35 type: str
36 description:
37 - The issue subject.
38 required: true
39 issue_type:
40 type: str
41 description:
42 - The issue type. Must exist previously.
43 required: true
44 priority:
45 type: str
46 description:
47 - The issue priority. Must exist previously.
48 default: Normal
49 status:
50 type: str
51 description:
52 - The issue status. Must exist previously.
53 default: New
54 severity:
55 type: str
56 description:
57 - The issue severity. Must exist previously.
58 default: Normal
59 description:
60 type: str
61 description:
62 - The issue description.
63 default: ""
64 attachment:
65 type: path
66 description:
67 - Path to a file to be attached to the issue.
68 attachment_description:
69 type: str
70 description:
71 - A string describing the file to be attached to the issue.
72 default: ""
73 tags:
74 type: list
75 elements: str
76 description:
77 - A lists of tags to be assigned to the issue.
78 default: []
79 state:
80 type: str
81 description:
82 - Whether the issue should be present or not.
83 choices: ["present", "absent"]
84 default: present
85 author: Alejandro Guirao (@lekum)
86 requirements: [python-taiga]
87 notes:
88 - The authentication is achieved either by the environment variable E(TAIGA_TOKEN) or by the pair of environment variables
89 E(TAIGA_USERNAME) and E(TAIGA_PASSWORD).
90 """
91
92 EXAMPLES = r"""
93 - name: Create an issue in the my hosted Taiga environment and attach an error log
94 community.general.taiga_issue:
95 taiga_host: https://mytaigahost.example.com
96 project: myproject
97 subject: An error has been found
98 issue_type: Bug
99 priority: High
100 status: New
101 severity: Important
102 description: An error has been found. Please check the attached error log for details.
103 attachment: /path/to/error.log
104 attachment_description: Error log file
105 tags:
106 - Error
107 - Needs manual check
108 state: present
109
110 - name: Deletes the previously created issue
111 community.general.taiga_issue:
112 taiga_host: https://mytaigahost.example.com
113 project: myproject
114 subject: An error has been found
115 issue_type: Bug
116 state: absent
117 """
118
119 RETURN = """#"""
120 import traceback
121 from os import getenv
122 from os.path import isfile
123
124 from ansible.module_utils.basic import AnsibleModule, missing_required_lib
125
126 TAIGA_IMP_ERR = None
127 try:
128 from taiga import TaigaAPI
129 from taiga.exceptions import TaigaException
130
131 TAIGA_MODULE_IMPORTED = True
132 except ImportError:
133 TAIGA_IMP_ERR = traceback.format_exc()
134 TAIGA_MODULE_IMPORTED = False
135
136
137 def manage_issue(
138 taiga_host,
139 project_name,
140 issue_subject,
141 issue_priority,
142 issue_status,
143 issue_type,
144 issue_severity,
145 issue_description,
146 issue_attachment,
147 issue_attachment_description,
148 issue_tags,
149 state,
150 check_mode=False,
151 ):
152 """
153 Method that creates/deletes issues depending whether they exist and the state desired
154
155 The credentials should be passed via environment variables:
156 - TAIGA_TOKEN
157 - TAIGA_USERNAME and TAIGA_PASSWORD
158
159 Returns a tuple with these elements:
160 - A boolean representing the success of the operation
161 - A descriptive message
162 - A dict with the issue attributes, in case of issue creation, otherwise empty dict
163 """
164
165 changed = False
166
167 try:
168 token = getenv("TAIGA_TOKEN")
169 if token:
170 api = TaigaAPI(host=taiga_host, token=token)
171 else:
172 api = TaigaAPI(host=taiga_host)
173 username = getenv("TAIGA_USERNAME")
174 password = getenv("TAIGA_PASSWORD")
175 if not any([username, password]):
176 return False, changed, "Missing credentials", {}
177 api.auth(username=username, password=password)
178
179 user_id = api.me().id
180 project_list = [x for x in api.projects.list(member=user_id) if x.name == project_name]
181 if len(project_list) != 1:
182 return False, changed, f"Unable to find project {project_name}", {}
183 project = project_list[0]
184 project_id = project.id
185
186 priority_list = [x for x in api.priorities.list(project=project_id) if x.name == issue_priority]
187 if len(priority_list) != 1:
188 return False, changed, f"Unable to find issue priority {issue_priority} for project {project_name}", {}
189 priority_id = priority_list[0].id
190
191 status_list = [x for x in api.issue_statuses.list(project=project_id) if x.name == issue_status]
192 if len(status_list) != 1:
193 return False, changed, f"Unable to find issue status {issue_status} for project {project_name}", {}
194 status_id = status_list[0].id
195
196 type_list = [x for x in project.list_issue_types() if x.name == issue_type]
197 if len(type_list) != 1:
198 return False, changed, f"Unable to find issue type {issue_type} for project {project_name}", {}
199 type_id = type_list[0].id
200
201 severity_list = [x for x in project.list_severities() if x.name == issue_severity]
202 if len(severity_list) != 1:
203 return False, changed, f"Unable to find severity {issue_severity} for project {project_name}", {}
204 severity_id = severity_list[0].id
205
206 issue = {
207 "project": project_name,
208 "subject": issue_subject,
209 "priority": issue_priority,
210 "status": issue_status,
211 "type": issue_type,
212 "severity": issue_severity,
213 "description": issue_description,
214 "tags": issue_tags,
215 }
216
217 # An issue is identified by the project_name, the issue_subject and the issue_type
218 matching_issue_list = [x for x in project.list_issues() if x.subject == issue_subject and x.type == type_id]
219 matching_issue_list_len = len(matching_issue_list)
220
221 if matching_issue_list_len == 0:
222 # The issue does not exist in the project
223 if state == "present":
224 # This implies a change
225 changed = True
226 if not check_mode:
227 # Create the issue
228 new_issue = project.add_issue(
229 issue_subject,
230 priority_id,
231 status_id,
232 type_id,
233 severity_id,
234 tags=issue_tags,
235 description=issue_description,
236 )
237 if issue_attachment:
238 new_issue.attach(issue_attachment, description=issue_attachment_description)
239 issue["attachment"] = issue_attachment
240 issue["attachment_description"] = issue_attachment_description
241 return True, changed, "Issue created", issue
242
243 else:
244 # If does not exist, do nothing
245 return True, changed, "Issue does not exist", {}
246
247 elif matching_issue_list_len == 1:
248 # The issue exists in the project
249 if state == "absent":
250 # This implies a change
251 changed = True
252 if not check_mode:
253 # Delete the issue
254 matching_issue_list[0].delete()
255 return True, changed, "Issue deleted", {}
256
257 else:
258 # Do nothing
259 return True, changed, "Issue already exists", {}
260
261 else:
262 # More than 1 matching issue
263 return False, changed, f"More than one issue with subject {issue_subject} in project {project_name}", {}
264
265 except TaigaException as exc:
266 msg = f"An exception happened: {exc}"
267 return False, changed, msg, {}
268
269
270 def main():
271 module = AnsibleModule(
272 argument_spec=dict(
273 taiga_host=dict(type="str", default="https://api.taiga.io"),
274 project=dict(type="str", required=True),
275 subject=dict(type="str", required=True),
276 issue_type=dict(type="str", required=True),
277 priority=dict(type="str", default="Normal"),
278 status=dict(type="str", default="New"),
279 severity=dict(type="str", default="Normal"),
280 description=dict(type="str", default=""),
281 attachment=dict(type="path"),
282 attachment_description=dict(type="str", default=""),
283 tags=dict(default=[], type="list", elements="str"),
284 state=dict(type="str", choices=["present", "absent"], default="present"),
285 ),
286 supports_check_mode=True,
287 )
288
289 if not TAIGA_MODULE_IMPORTED:
290 module.fail_json(msg=missing_required_lib("python-taiga"), exception=TAIGA_IMP_ERR)
291
292 taiga_host = module.params["taiga_host"]
293 project_name = module.params["project"]
294 issue_subject = module.params["subject"]
295 issue_priority = module.params["priority"]
296 issue_status = module.params["status"]
297 issue_type = module.params["issue_type"]
298 issue_severity = module.params["severity"]
299 issue_description = module.params["description"]
300 issue_attachment = module.params["attachment"]
301 issue_attachment_description = module.params["attachment_description"]
302 if issue_attachment:
303 if not isfile(issue_attachment):
304 msg = f"{issue_attachment} is not a file"
305 module.fail_json(msg=msg)
306 issue_tags = module.params["tags"]
307 state = module.params["state"]
308
309 return_status, changed, msg, issue_attr_dict = manage_issue(
310 taiga_host,
311 project_name,
312 issue_subject,
313 issue_priority,
314 issue_status,
315 issue_type,
316 issue_severity,
317 issue_description,
318 issue_attachment,
319 issue_attachment_description,
320 issue_tags,
321 state,
322 check_mode=module.check_mode,
323 )
324 if return_status:
325 if issue_attr_dict:
326 module.exit_json(changed=changed, msg=msg, issue=issue_attr_dict)
327 else:
328 module.exit_json(changed=changed, msg=msg)
329 else:
330 module.fail_json(msg=msg)
331
332
333 if __name__ == "__main__":
334 main()