From be7f11bf39183ae5d7ec103480b0a3ef760c2699 Mon Sep 17 00:00:00 2001 From: Hugo Prudente Date: Sun, 26 Mar 2023 08:31:06 +0100 Subject: [PATCH] Add worklog functionality to jira module (#6210) community.general#6209 Add worklog functionality to jira --- ...6210-add-worklog-functionality-to-jira.yml | 2 + plugins/modules/jira.py | 65 ++++++++++++++++++- tests/integration/targets/jira/tasks/main.yml | 18 +++++ 3 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/6210-add-worklog-functionality-to-jira.yml diff --git a/changelogs/fragments/6210-add-worklog-functionality-to-jira.yml b/changelogs/fragments/6210-add-worklog-functionality-to-jira.yml new file mode 100644 index 0000000000..baceedb249 --- /dev/null +++ b/changelogs/fragments/6210-add-worklog-functionality-to-jira.yml @@ -0,0 +1,2 @@ +minor_changes: + - jira - add worklog functionality (https://github.com/ansible-collections/community.general/issues/6209, https://github.com/ansible-collections/community.general/pull/6210). diff --git a/plugins/modules/jira.py b/plugins/modules/jira.py index 91aeb67037..85097c4b76 100644 --- a/plugins/modules/jira.py +++ b/plugins/modules/jira.py @@ -6,6 +6,7 @@ # # Copyright (c) 2020, Per Abildgaard Toft Search and update function # Copyright (c) 2021, Brandon McNama Issue attachment functionality +# Copyright (c) 2022, Hugo Prudente Worklog functionality # # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) # SPDX-License-Identifier: GPL-3.0-or-later @@ -40,9 +41,10 @@ options: type: str required: true aliases: [ command ] - choices: [ attach, comment, create, edit, fetch, link, search, transition, update ] + choices: [ attach, comment, create, edit, fetch, link, search, transition, update, worklog ] description: - The operation to perform. + - C(worklog) was added in community.genereal 6.5.0. username: type: str @@ -171,7 +173,6 @@ options: - When passed to comment, the data structure is merged at the first level since community.general 4.6.0. Useful to add JIRA properties for example. - Note that JIRA may not allow changing field values on specific transitions or states. default: {} - jql: required: false description: @@ -287,6 +288,47 @@ EXAMPLES = r""" value: internal: true +# Add an workog to an existing issue +- name: Worklog on issue + community.general.jira: + uri: '{{ server }}' + username: '{{ user }}' + password: '{{ pass }}' + issue: '{{ issue.meta.key }}' + operation: worklog + comment: A worklog added by Ansible + fields: + timeSpentSeconds: 12000 + +- name: Workflow on issue with comment restricted visibility + community.general.jira: + uri: '{{ server }}' + username: '{{ user }}' + password: '{{ pass }}' + issue: '{{ issue.meta.key }}' + operation: worklog + comment: A worklog added by Ansible + comment_visibility: + type: role + value: Developers + fields: + timeSpentSeconds: 12000 + +- name: Workflow on issue with comment property to mark it internal + community.general.jira: + uri: '{{ server }}' + username: '{{ user }}' + password: '{{ pass }}' + issue: '{{ issue.meta.key }}' + operation: worklog + comment: A worklog added by Ansible + fields: + properties: + - key: 'sd.public.comment' + value: + internal: true + timeSpentSeconds: 12000 + # Assign an existing issue using edit - name: Assign an issue using free-form fields community.general.jira: @@ -438,7 +480,7 @@ class JIRA(StateModuleHelper): uri=dict(type='str', required=True), operation=dict( type='str', - choices=['attach', 'create', 'comment', 'edit', 'update', 'fetch', 'transition', 'link', 'search'], + choices=['attach', 'create', 'comment', 'edit', 'update', 'fetch', 'transition', 'link', 'search', 'worklog'], aliases=['command'], required=True ), username=dict(type='str'), @@ -481,6 +523,7 @@ class JIRA(StateModuleHelper): ('operation', 'attach', ['issue', 'attachment']), ('operation', 'create', ['project', 'issuetype', 'summary']), ('operation', 'comment', ['issue', 'comment']), + ('operation', 'workflow', ['issue', 'comment']), ('operation', 'fetch', ['issue']), ('operation', 'transition', ['issue', 'status']), ('operation', 'link', ['linktype', 'inwardissue', 'outwardissue']), @@ -535,6 +578,22 @@ class JIRA(StateModuleHelper): url = self.vars.restbase + '/issue/' + self.vars.issue + '/comment' self.vars.meta = self.post(url, data) + @cause_changes(on_success=True) + def operation_worklog(self): + data = { + 'comment': self.vars.comment + } + # if comment_visibility is specified restrict visibility + if self.vars.comment_visibility is not None: + data['visibility'] = self.vars.comment_visibility + + # Use 'fields' to merge in any additional data + if self.vars.fields: + data.update(self.vars.fields) + + url = self.vars.restbase + '/issue/' + self.vars.issue + '/worklog' + self.vars.meta = self.post(url, data) + @cause_changes(on_success=True) def operation_edit(self): data = { diff --git a/tests/integration/targets/jira/tasks/main.yml b/tests/integration/targets/jira/tasks/main.yml index ffc03530dc..1290708714 100644 --- a/tests/integration/targets/jira/tasks/main.yml +++ b/tests/integration/targets/jira/tasks/main.yml @@ -67,6 +67,24 @@ that: - assign is changed +- name: Worklog on issue + community.general.jira: + uri: '{{ server }}' + username: '{{ user }}' + password: '{{ pass }}' + issue: '{{ issue.meta.key }}' + operation: worklog + comment: Worklog + fields: + timeSpentSeconds: 1200 + register: worklog +- name: assert worklog -> with comment + assert: + that: + - worklog is changed + - worklog.meta.comment == 'Worklog' + - worklog.meta.timeSpentSeconds == 1200 + - name: transition -> Resolved with comment community.general.jira: uri: "{{ uri }}"