Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added search function to jira module and bugfixes #22

Merged
merged 32 commits into from
Apr 9, 2020
Merged
Changes from 11 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
6340ed1
Added search function to jira module
Mar 19, 2020
ef11786
Added jira Operations in update function
Mar 20, 2020
8cc078c
Fixed Whitepsaces
Mar 20, 2020
bbe4241
Fixed Author header
Mar 20, 2020
08ca995
Update plugins/modules/web_infrastructure/jira.py
pertoft Mar 23, 2020
364d1d0
Update plugins/modules/web_infrastructure/jira.py
pertoft Mar 23, 2020
75646fb
Update plugins/modules/web_infrastructure/jira.py
pertoft Mar 23, 2020
707148c
Update plugins/modules/web_infrastructure/jira.py
pertoft Mar 23, 2020
6b2fe0c
Update plugins/modules/web_infrastructure/jira.py
pertoft Mar 23, 2020
dd844a1
Update plugins/modules/web_infrastructure/jira.py
pertoft Mar 23, 2020
fdac5a2
Merge branch 'master' of github.com:pertoft/community.general
Mar 30, 2020
762c247
Update plugins/modules/web_infrastructure/jira.py
pertoft Apr 3, 2020
619a158
Update plugins/modules/web_infrastructure/jira.py
pertoft Apr 3, 2020
6b1ad3a
Update plugins/modules/web_infrastructure/jira.py
pertoft Apr 3, 2020
7a5cf8f
Added changelog framgents and update with review comments
Apr 3, 2020
94c235d
Merge branch 'master' of github.com:pertoft/community.general
Apr 3, 2020
71cd947
Resolved https://github.com/ansible-collections/community.general/iss…
Apr 3, 2020
dcbf8c5
Added example for Transsition with comments
Apr 3, 2020
862eb7c
Fixed Whitespaces
Apr 3, 2020
830ad5f
Fixed whitepsaces again
Apr 3, 2020
2e58d30
Update changelogs/fragments/22-jira.yaml
pertoft Apr 3, 2020
e4ae7f0
Update plugins/modules/web_infrastructure/jira.py
pertoft Apr 3, 2020
bf14907
Added changes from Andersson007
Apr 3, 2020
f8c6f11
Merge branch 'master' of github.com:pertoft/community.general
Apr 3, 2020
5932f23
Update changelogs/fragments/22-jira.yaml
pertoft Apr 8, 2020
e0140a3
Updated Changelog framgnets as per reviews
Apr 8, 2020
0908826
Update changelogs/fragments/22-jira.yaml
pertoft Apr 8, 2020
64183f3
Added max results per review request, which supports limiting number …
Apr 8, 2020
3828f14
Merge branch 'master' of github.com:pertoft/community.general
Apr 8, 2020
b35c070
Removed whitespace
Apr 8, 2020
c3cab58
Removed version_added per request
pertoft Apr 8, 2020
e6d9e7d
Update plugins/modules/web_infrastructure/jira.py
pertoft Apr 8, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 83 additions & 8 deletions plugins/modules/web_infrastructure/jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# (c) 2014, Steve Smith <ssmith@atlassian.com>
# Atlassian open-source approval reference OSR-76.
#
# (c) 2020, Per Abildgaard Toft <per@minfejl.dk> Search and update function
#
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
Expand All @@ -15,7 +17,7 @@
'supported_by': 'community'}


DOCUMENTATION = '''
DOCUMENTATION = """
module: jira
short_description: create and modify issues in a JIRA instance
description:
Expand All @@ -30,7 +32,7 @@
operation:
required: true
aliases: [ command ]
choices: [ create, comment, edit, fetch, transition , link ]
choices: [ create, comment, edit, update, fetch, transition , link, search ]
pertoft marked this conversation as resolved.
Show resolved Hide resolved
description:
- The operation to perform.

Expand Down Expand Up @@ -106,6 +108,12 @@
(possibly after merging with other required data, as when passed to create). See examples for more information,
and the JIRA REST API for the structure required for various fields.

jql:
required: false
version_added: '2.10'
description:
- Query JIRA in JQL Syntax E.g. 'CMDB Hostname'='test.example.com'
pertoft marked this conversation as resolved.
Show resolved Hide resolved

timeout:
required: false
description:
Expand All @@ -122,8 +130,10 @@
notes:
- "Currently this only works with basic-auth."

author: "Steve Smith (@tarka)"
'''
author:
- "Steve Smith (@tarka)"
- "Per Abildgaard Toft (@pertoft)"
"""

EXAMPLES = """
# Create a new issue and add a comment to it:
Expand All @@ -137,6 +147,10 @@
summary: Example Issue
description: Created using Ansible
issuetype: Task
args:
fields:
customfield_13225: "test"
customfield_12931: '{"value": "Test"}'
register: issue

- name: Comment on issue
Expand Down Expand Up @@ -185,6 +199,22 @@
- autocreated
- ansible

# Updating a field using operations: add, set & remove
- name: Change the value of a Select dropdown
jira:
uri: '{{ server }}'
username: '{{ user }}'
password: '{{ pass }}'
issue: '{{ issue.meta.key }}'
operation: update
args:
fields:
customfield_12931: [ {'set': {'value': 'Virtual'}} ]
customfield_13820: [ {'set': {'value':'Manually'}} ]
register: cmdb_issue
delegate_to: localhost


# Retrieve metadata for an issue and use it to create an account
- name: Get an issue
jira:
Expand All @@ -196,6 +226,21 @@
issue: ANS-63
register: issue

# Search for an issue
# You can limit the search for specific fields by adding optional args. Note! It must be a dict, hence, lastViewed: null
- name: Search for an issue
jira:
uri: '{{ server }}'
username: '{{ user }}'
password: '{{ pass }}'
project: ANS
operation: search
jql: project=cmdb AND cf[13225]="test"
args:
fields:
lastViewed: null
register: issue

- name: Create a unix account for the reporter
become: true
user:
Expand Down Expand Up @@ -228,6 +273,7 @@
import base64
import json
import sys
import urllib
from ansible.module_utils._text import to_text, to_bytes

from ansible.module_utils.basic import AnsibleModule
Expand All @@ -246,13 +292,18 @@ def request(url, user, passwd, timeout, data=None, method=None):
# inject the basic-auth header up-front to ensure that JIRA treats
# the requests as authorized for this user.
auth = to_text(base64.b64encode(to_bytes('{0}:{1}'.format(user, passwd), errors='surrogate_or_strict')))

response, info = fetch_url(module, url, data=data, method=method, timeout=timeout,
headers={'Content-Type': 'application/json',
'Authorization': "Basic %s" % auth})

if info['status'] not in (200, 201, 204):
module.fail_json(msg=info['msg'])
module.fail_json(msg=info)
error = json.loads(info['body'])
Andersson007 marked this conversation as resolved.
Show resolved Hide resolved
if error:
module.fail_json(msg=error['errorMessages'])
else:
# Fallback print body, if it cant be decoded
module.fail_json(msg=info['body'])

body = response.read()

Expand Down Expand Up @@ -320,12 +371,33 @@ def edit(restbase, user, passwd, params):
return ret


def update(restbase, user, passwd, params):
data = {
"update": params['fields']
pertoft marked this conversation as resolved.
Show resolved Hide resolved
}
url = restbase + '/issue/' + params['issue']

ret = put(url, user, passwd, params['timeout'], data)

return ret


def fetch(restbase, user, passwd, params):
url = restbase + '/issue/' + params['issue']
ret = get(url, user, passwd, params['timeout'])
return ret


def search(restbase, user, passwd, params):
url = restbase + '/search?jql=' + urllib.request.pathname2url(params['jql']) + '&maxResults=10'
Andersson007 marked this conversation as resolved.
Show resolved Hide resolved
if params['fields']:
fields = params['fields'].keys()
for f in fields:
url = url + '&fields=' + urllib.request.pathname2url(f)
pertoft marked this conversation as resolved.
Show resolved Hide resolved
ret = get(url, user, passwd, params['timeout'])
return ret


def transition(restbase, user, passwd, params):
# Find the transition id
turl = restbase + '/issue/' + params['issue'] + "/transitions"
Expand Down Expand Up @@ -369,9 +441,11 @@ def link(restbase, user, passwd, params):
OP_REQUIRED = dict(create=['project', 'issuetype', 'summary'],
comment=['issue', 'comment'],
edit=[],
update=[],
fetch=['issue'],
transition=['status'],
link=['linktype', 'inwardissue', 'outwardissue'])
link=['linktype', 'inwardissue', 'outwardissue'],
search=['jql'])


def main():
Expand All @@ -380,7 +454,7 @@ def main():
module = AnsibleModule(
argument_spec=dict(
uri=dict(required=True),
operation=dict(choices=['create', 'comment', 'edit', 'fetch', 'transition', 'link'],
operation=dict(choices=['create', 'comment', 'edit', 'update', 'fetch', 'transition', 'link', 'search'],
aliases=['command'], required=True),
username=dict(required=True),
password=dict(required=True, no_log=True),
Expand All @@ -396,6 +470,7 @@ def main():
linktype=dict(),
inwardissue=dict(),
outwardissue=dict(),
jql=dict(),
timeout=dict(type='float', default=10),
validate_certs=dict(default=True, type='bool'),
),
Expand Down