2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2014, Steve Smith <ssmith@atlassian.com>
|
|
|
|
# Atlassian open-source approval reference OSR-76.
|
|
|
|
#
|
2020-04-09 09:29:26 +02:00
|
|
|
# (c) 2020, Per Abildgaard Toft <per@minfejl.dk> Search and update function
|
2021-04-13 08:06:52 +02:00
|
|
|
# (c) 2021, Brandon McNama <brandonmcnama@outlook.com> Issue attachment functionality
|
2020-04-09 09:29:26 +02:00
|
|
|
#
|
2020-03-09 10:11:07 +01:00
|
|
|
# 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
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
2020-12-19 17:43:41 +01:00
|
|
|
DOCUMENTATION = r"""
|
2020-03-09 10:11:07 +01:00
|
|
|
module: jira
|
|
|
|
short_description: create and modify issues in a JIRA instance
|
|
|
|
description:
|
|
|
|
- Create and modify issues in a JIRA instance.
|
|
|
|
|
|
|
|
options:
|
|
|
|
uri:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
required: true
|
|
|
|
description:
|
|
|
|
- Base URI for the JIRA instance.
|
|
|
|
|
|
|
|
operation:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
required: true
|
|
|
|
aliases: [ command ]
|
2021-04-13 08:06:52 +02:00
|
|
|
choices: [ attach, comment, create, edit, fetch, link, search, transition, update ]
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- The operation to perform.
|
|
|
|
|
|
|
|
username:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
required: true
|
|
|
|
description:
|
|
|
|
- The username to log-in with.
|
|
|
|
|
|
|
|
password:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
required: true
|
|
|
|
description:
|
|
|
|
- The password to log-in with.
|
|
|
|
|
|
|
|
project:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
description:
|
|
|
|
- The project for this operation. Required for issue creation.
|
|
|
|
|
|
|
|
summary:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
description:
|
|
|
|
- The issue summary, where appropriate.
|
2021-04-07 08:39:06 +02:00
|
|
|
- Note that JIRA may not allow changing field values on specific transitions or states.
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
description:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
description:
|
|
|
|
- The issue description, where appropriate.
|
2021-04-07 08:39:06 +02:00
|
|
|
- Note that JIRA may not allow changing field values on specific transitions or states.
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
issuetype:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
description:
|
|
|
|
- The issue type, for issue creation.
|
|
|
|
|
|
|
|
issue:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
description:
|
|
|
|
- An existing issue key to operate on.
|
2020-10-31 13:53:57 +01:00
|
|
|
aliases: ['ticket']
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
comment:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
description:
|
|
|
|
- The comment text to add.
|
2021-04-07 08:39:06 +02:00
|
|
|
- Note that JIRA may not allow changing field values on specific transitions or states.
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
status:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
description:
|
2021-04-07 08:39:06 +02:00
|
|
|
- Only used when I(operation) is C(transition), and a bit of a misnomer, it actually refers to the transition name.
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
assignee:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
description:
|
2021-04-07 08:39:06 +02:00
|
|
|
- Sets the the assignee when I(operation) is C(create), C(transition) or C(edit).
|
|
|
|
- Recent versions of JIRA no longer accept a user name as a user identifier. In that case, use I(account_id) instead.
|
|
|
|
- Note that JIRA may not allow changing field values on specific transitions or states.
|
|
|
|
|
|
|
|
account_id:
|
|
|
|
type: str
|
|
|
|
description:
|
|
|
|
- Sets the account identifier for the assignee when I(operation) is C(create), C(transition) or C(edit).
|
|
|
|
- Note that JIRA may not allow changing field values on specific transitions or states.
|
|
|
|
version_added: 2.5.0
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
linktype:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
description:
|
|
|
|
- Set type of link, when action 'link' selected.
|
|
|
|
|
|
|
|
inwardissue:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
description:
|
|
|
|
- Set issue from which link will be created.
|
|
|
|
|
|
|
|
outwardissue:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
description:
|
|
|
|
- Set issue to which link will be created.
|
|
|
|
|
|
|
|
fields:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: dict
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
description:
|
|
|
|
- This is a free-form data structure that can contain arbitrary data. This is passed directly to the JIRA REST API
|
|
|
|
(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.
|
2021-04-07 08:39:06 +02:00
|
|
|
- Note that JIRA may not allow changing field values on specific transitions or states.
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2020-04-09 09:29:26 +02:00
|
|
|
jql:
|
|
|
|
required: false
|
|
|
|
description:
|
|
|
|
- Query JIRA in JQL Syntax, e.g. 'CMDB Hostname'='test.example.com'.
|
|
|
|
type: str
|
2020-06-13 15:01:19 +02:00
|
|
|
version_added: '0.2.0'
|
2020-04-09 09:29:26 +02:00
|
|
|
|
|
|
|
maxresults:
|
|
|
|
required: false
|
|
|
|
description:
|
|
|
|
- Limit the result of I(operation=search). If no value is specified, the default jira limit will be used.
|
|
|
|
- Used when I(operation=search) only, ignored otherwise.
|
|
|
|
type: int
|
2020-06-13 15:01:19 +02:00
|
|
|
version_added: '0.2.0'
|
2020-04-09 09:29:26 +02:00
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
timeout:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: float
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
description:
|
|
|
|
- Set timeout, in seconds, on requests to JIRA API.
|
|
|
|
default: 10
|
|
|
|
|
|
|
|
validate_certs:
|
|
|
|
required: false
|
|
|
|
description:
|
|
|
|
- Require valid SSL certificates (set to `false` if you'd like to use self-signed certificates)
|
|
|
|
default: true
|
|
|
|
type: bool
|
|
|
|
|
2021-04-13 08:06:52 +02:00
|
|
|
attachment:
|
|
|
|
type: dict
|
|
|
|
version_added: 2.5.0
|
|
|
|
description:
|
|
|
|
- Information about the attachment being uploaded.
|
|
|
|
suboptions:
|
|
|
|
filename:
|
|
|
|
required: true
|
|
|
|
type: path
|
|
|
|
description:
|
|
|
|
- The path to the file to upload (from the remote node) or, if I(content) is specified,
|
|
|
|
the filename to use for the attachment.
|
|
|
|
content:
|
|
|
|
type: str
|
|
|
|
description:
|
|
|
|
- The Base64 encoded contents of the file to attach. If not specified, the contents of I(filename) will be
|
|
|
|
used instead.
|
|
|
|
mimetype:
|
|
|
|
type: str
|
|
|
|
description:
|
|
|
|
- The MIME type to supply for the upload. If not specified, best-effort detection will be
|
|
|
|
done.
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
notes:
|
|
|
|
- "Currently this only works with basic-auth."
|
2021-04-07 08:39:06 +02:00
|
|
|
- "To use with JIRA Cloud, pass the login e-mail as the I(username) and the API token as I(password)."
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2020-04-09 09:29:26 +02:00
|
|
|
author:
|
|
|
|
- "Steve Smith (@tarka)"
|
|
|
|
- "Per Abildgaard Toft (@pertoft)"
|
2021-04-13 08:06:52 +02:00
|
|
|
- "Brandon McNama (@DWSR)"
|
2020-04-09 09:29:26 +02:00
|
|
|
"""
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2020-12-19 17:43:41 +01:00
|
|
|
EXAMPLES = r"""
|
2020-03-09 10:11:07 +01:00
|
|
|
# Create a new issue and add a comment to it:
|
|
|
|
- name: Create an issue
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.jira:
|
2020-03-09 10:11:07 +01:00
|
|
|
uri: '{{ server }}'
|
|
|
|
username: '{{ user }}'
|
|
|
|
password: '{{ pass }}'
|
|
|
|
project: ANS
|
|
|
|
operation: create
|
|
|
|
summary: Example Issue
|
|
|
|
description: Created using Ansible
|
|
|
|
issuetype: Task
|
2020-04-09 09:29:26 +02:00
|
|
|
args:
|
|
|
|
fields:
|
|
|
|
customfield_13225: "test"
|
2021-04-07 08:39:06 +02:00
|
|
|
customfield_12931: {"value": "Test"}
|
2020-03-09 10:11:07 +01:00
|
|
|
register: issue
|
|
|
|
|
|
|
|
- name: Comment on issue
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.jira:
|
2020-03-09 10:11:07 +01:00
|
|
|
uri: '{{ server }}'
|
|
|
|
username: '{{ user }}'
|
|
|
|
password: '{{ pass }}'
|
|
|
|
issue: '{{ issue.meta.key }}'
|
|
|
|
operation: comment
|
|
|
|
comment: A comment added by Ansible
|
|
|
|
|
|
|
|
# Assign an existing issue using edit
|
|
|
|
- name: Assign an issue using free-form fields
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.jira:
|
2020-03-09 10:11:07 +01:00
|
|
|
uri: '{{ server }}'
|
|
|
|
username: '{{ user }}'
|
|
|
|
password: '{{ pass }}'
|
|
|
|
issue: '{{ issue.meta.key}}'
|
|
|
|
operation: edit
|
|
|
|
assignee: ssmith
|
|
|
|
|
|
|
|
# Create an issue with an existing assignee
|
|
|
|
- name: Create an assigned issue
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.jira:
|
2020-03-09 10:11:07 +01:00
|
|
|
uri: '{{ server }}'
|
|
|
|
username: '{{ user }}'
|
|
|
|
password: '{{ pass }}'
|
|
|
|
project: ANS
|
|
|
|
operation: create
|
|
|
|
summary: Assigned issue
|
|
|
|
description: Created and assigned using Ansible
|
|
|
|
issuetype: Task
|
|
|
|
assignee: ssmith
|
|
|
|
|
|
|
|
# Edit an issue
|
|
|
|
- name: Set the labels on an issue using free-form fields
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.jira:
|
2020-03-09 10:11:07 +01:00
|
|
|
uri: '{{ server }}'
|
|
|
|
username: '{{ user }}'
|
|
|
|
password: '{{ pass }}'
|
|
|
|
issue: '{{ issue.meta.key }}'
|
|
|
|
operation: edit
|
|
|
|
args:
|
|
|
|
fields:
|
|
|
|
labels:
|
|
|
|
- autocreated
|
|
|
|
- ansible
|
|
|
|
|
2020-04-09 09:29:26 +02:00
|
|
|
# Updating a field using operations: add, set & remove
|
|
|
|
- name: Change the value of a Select dropdown
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.jira:
|
2020-04-09 09:29:26 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
# Retrieve metadata for an issue and use it to create an account
|
|
|
|
- name: Get an issue
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.jira:
|
2020-03-09 10:11:07 +01:00
|
|
|
uri: '{{ server }}'
|
|
|
|
username: '{{ user }}'
|
|
|
|
password: '{{ pass }}'
|
|
|
|
project: ANS
|
|
|
|
operation: fetch
|
|
|
|
issue: ANS-63
|
|
|
|
register: issue
|
|
|
|
|
2020-04-09 09:29:26 +02:00
|
|
|
# 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
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.jira:
|
2020-04-09 09:29:26 +02:00
|
|
|
uri: '{{ server }}'
|
|
|
|
username: '{{ user }}'
|
|
|
|
password: '{{ pass }}'
|
|
|
|
project: ANS
|
|
|
|
operation: search
|
|
|
|
maxresults: 10
|
|
|
|
jql: project=cmdb AND cf[13225]="test"
|
|
|
|
args:
|
|
|
|
fields:
|
|
|
|
lastViewed: null
|
|
|
|
register: issue
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
- name: Create a unix account for the reporter
|
|
|
|
become: true
|
|
|
|
user:
|
|
|
|
name: '{{ issue.meta.fields.creator.name }}'
|
|
|
|
comment: '{{ issue.meta.fields.creator.displayName }}'
|
|
|
|
|
|
|
|
# You can get list of valid linktypes at /rest/api/2/issueLinkType
|
|
|
|
# url of your jira installation.
|
|
|
|
- name: Create link from HSP-1 to MKY-1
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.jira:
|
2020-03-09 10:11:07 +01:00
|
|
|
uri: '{{ server }}'
|
|
|
|
username: '{{ user }}'
|
|
|
|
password: '{{ pass }}'
|
|
|
|
operation: link
|
|
|
|
linktype: Relates
|
|
|
|
inwardissue: HSP-1
|
|
|
|
outwardissue: MKY-1
|
|
|
|
|
2021-04-07 08:39:06 +02:00
|
|
|
# Transition an issue
|
|
|
|
- name: Resolve the issue
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.jira:
|
2020-03-09 10:11:07 +01:00
|
|
|
uri: '{{ server }}'
|
|
|
|
username: '{{ user }}'
|
|
|
|
password: '{{ pass }}'
|
|
|
|
issue: '{{ issue.meta.key }}'
|
|
|
|
operation: transition
|
2021-04-07 08:39:06 +02:00
|
|
|
status: Resolve Issue
|
|
|
|
account_id: 112233445566778899aabbcc
|
2020-12-27 13:56:48 +01:00
|
|
|
fields:
|
2021-04-07 08:39:06 +02:00
|
|
|
resolution:
|
|
|
|
name: Done
|
|
|
|
description: I am done! This is the last description I will ever give you.
|
2021-04-13 08:06:52 +02:00
|
|
|
|
|
|
|
# Attach a file to an issue
|
|
|
|
- name: Attach a file
|
|
|
|
community.general.jira:
|
|
|
|
uri: '{{ server }}'
|
|
|
|
username: '{{ user }}'
|
|
|
|
password: '{{ pass }}'
|
|
|
|
issue: HSP-1
|
|
|
|
operation: attach
|
|
|
|
attachment:
|
|
|
|
filename: topsecretreport.xlsx
|
2020-03-09 10:11:07 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
import base64
|
2021-04-13 08:06:52 +02:00
|
|
|
import binascii
|
2020-03-09 10:11:07 +01:00
|
|
|
import json
|
2021-04-13 08:06:52 +02:00
|
|
|
import mimetypes
|
|
|
|
import os
|
|
|
|
import random
|
|
|
|
import string
|
2020-03-09 10:11:07 +01:00
|
|
|
import sys
|
2020-12-27 13:56:48 +01:00
|
|
|
import traceback
|
2020-04-20 13:05:57 +02:00
|
|
|
|
|
|
|
from ansible.module_utils.six.moves.urllib.request import pathname2url
|
|
|
|
|
2020-07-28 07:59:33 +02:00
|
|
|
from ansible.module_utils._text import to_text, to_bytes, to_native
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils.urls import fetch_url
|
|
|
|
|
|
|
|
|
2021-04-13 08:06:52 +02:00
|
|
|
def request(
|
|
|
|
url,
|
|
|
|
user,
|
|
|
|
passwd,
|
|
|
|
timeout,
|
|
|
|
data=None,
|
|
|
|
method=None,
|
|
|
|
content_type='application/json',
|
|
|
|
additional_headers=None
|
|
|
|
):
|
|
|
|
if data and content_type == 'application/json':
|
2020-03-09 10:11:07 +01:00
|
|
|
data = json.dumps(data)
|
|
|
|
|
|
|
|
# NOTE: fetch_url uses a password manager, which follows the
|
|
|
|
# standard request-then-challenge basic-auth semantics. However as
|
|
|
|
# JIRA allows some unauthorised operations it doesn't necessarily
|
|
|
|
# send the challenge, so the request occurs as the anonymous user,
|
|
|
|
# resulting in unexpected results. To work around this we manually
|
|
|
|
# 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')))
|
2021-04-13 08:06:52 +02:00
|
|
|
|
|
|
|
headers = {}
|
2021-04-14 10:00:18 +02:00
|
|
|
if isinstance(additional_headers, dict):
|
2021-04-13 08:06:52 +02:00
|
|
|
headers = additional_headers.copy()
|
|
|
|
headers.update({
|
|
|
|
"Content-Type": content_type,
|
|
|
|
"Authorization": "Basic %s" % auth,
|
|
|
|
})
|
|
|
|
|
|
|
|
response, info = fetch_url(
|
|
|
|
module, url, data=data, method=method, timeout=timeout, headers=headers
|
|
|
|
)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if info['status'] not in (200, 201, 204):
|
2020-12-19 17:43:41 +01:00
|
|
|
error = None
|
|
|
|
try:
|
|
|
|
error = json.loads(info['body'])
|
|
|
|
except Exception:
|
2020-12-27 13:56:48 +01:00
|
|
|
module.fail_json(msg=to_native(info['body']), exception=traceback.format_exc())
|
2020-04-09 09:29:26 +02:00
|
|
|
if error:
|
2020-07-28 07:59:33 +02:00
|
|
|
msg = []
|
|
|
|
for key in ('errorMessages', 'errors'):
|
|
|
|
if error.get(key):
|
2020-07-29 23:38:51 +02:00
|
|
|
msg.append(to_native(error[key]))
|
2020-07-28 07:59:33 +02:00
|
|
|
if msg:
|
2020-07-29 23:38:51 +02:00
|
|
|
module.fail_json(msg=', '.join(msg))
|
2020-12-19 17:43:41 +01:00
|
|
|
module.fail_json(msg=to_native(error))
|
|
|
|
# Fallback print body, if it cant be decoded
|
|
|
|
module.fail_json(msg=to_native(info['body']))
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
body = response.read()
|
|
|
|
|
|
|
|
if body:
|
|
|
|
return json.loads(to_text(body, errors='surrogate_or_strict'))
|
2020-12-19 17:43:41 +01:00
|
|
|
return {}
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
2021-04-13 08:06:52 +02:00
|
|
|
def post(url, user, passwd, timeout, data, content_type='application/json', additional_headers=None):
|
|
|
|
return request(url, user, passwd, timeout, data=data, method='POST', content_type=content_type, additional_headers=additional_headers)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def put(url, user, passwd, timeout, data):
|
|
|
|
return request(url, user, passwd, timeout, data=data, method='PUT')
|
|
|
|
|
|
|
|
|
|
|
|
def get(url, user, passwd, timeout):
|
|
|
|
return request(url, user, passwd, timeout)
|
|
|
|
|
|
|
|
|
|
|
|
def create(restbase, user, passwd, params):
|
|
|
|
createfields = {
|
|
|
|
'project': {'key': params['project']},
|
|
|
|
'summary': params['summary'],
|
|
|
|
'issuetype': {'name': params['issuetype']}}
|
|
|
|
|
|
|
|
if params['description']:
|
|
|
|
createfields['description'] = params['description']
|
|
|
|
|
|
|
|
# Merge in any additional or overridden fields
|
|
|
|
if params['fields']:
|
|
|
|
createfields.update(params['fields'])
|
|
|
|
|
|
|
|
data = {'fields': createfields}
|
|
|
|
|
|
|
|
url = restbase + '/issue/'
|
|
|
|
|
2020-12-27 13:56:48 +01:00
|
|
|
return True, post(url, user, passwd, params['timeout'], data)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def comment(restbase, user, passwd, params):
|
|
|
|
data = {
|
|
|
|
'body': params['comment']
|
|
|
|
}
|
|
|
|
url = restbase + '/issue/' + params['issue'] + '/comment'
|
|
|
|
|
2020-12-27 13:56:48 +01:00
|
|
|
return True, post(url, user, passwd, params['timeout'], data)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def edit(restbase, user, passwd, params):
|
|
|
|
data = {
|
|
|
|
'fields': params['fields']
|
|
|
|
}
|
|
|
|
url = restbase + '/issue/' + params['issue']
|
|
|
|
|
2020-12-27 13:56:48 +01:00
|
|
|
return True, put(url, user, passwd, params['timeout'], data)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
2020-04-09 09:29:26 +02:00
|
|
|
def update(restbase, user, passwd, params):
|
|
|
|
data = {
|
|
|
|
"update": params['fields'],
|
|
|
|
}
|
|
|
|
url = restbase + '/issue/' + params['issue']
|
|
|
|
|
2020-12-27 13:56:48 +01:00
|
|
|
return True, put(url, user, passwd, params['timeout'], data)
|
2020-04-09 09:29:26 +02:00
|
|
|
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
def fetch(restbase, user, passwd, params):
|
|
|
|
url = restbase + '/issue/' + params['issue']
|
2020-12-27 13:56:48 +01:00
|
|
|
return False, get(url, user, passwd, params['timeout'])
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
2020-04-09 09:29:26 +02:00
|
|
|
def search(restbase, user, passwd, params):
|
2020-04-20 13:05:57 +02:00
|
|
|
url = restbase + '/search?jql=' + pathname2url(params['jql'])
|
2020-04-09 09:29:26 +02:00
|
|
|
if params['fields']:
|
|
|
|
fields = params['fields'].keys()
|
2020-04-20 13:05:57 +02:00
|
|
|
url = url + '&fields=' + '&fields='.join([pathname2url(f) for f in fields])
|
2020-04-09 09:29:26 +02:00
|
|
|
if params['maxresults']:
|
|
|
|
url = url + '&maxResults=' + str(params['maxresults'])
|
2020-12-27 13:56:48 +01:00
|
|
|
return False, get(url, user, passwd, params['timeout'])
|
2020-04-09 09:29:26 +02:00
|
|
|
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
def transition(restbase, user, passwd, params):
|
|
|
|
# Find the transition id
|
|
|
|
turl = restbase + '/issue/' + params['issue'] + "/transitions"
|
|
|
|
tmeta = get(turl, user, passwd, params['timeout'])
|
|
|
|
|
|
|
|
target = params['status']
|
|
|
|
tid = None
|
|
|
|
for t in tmeta['transitions']:
|
|
|
|
if t['name'] == target:
|
|
|
|
tid = t['id']
|
|
|
|
break
|
|
|
|
|
|
|
|
if not tid:
|
|
|
|
raise ValueError("Failed find valid transition for '%s'" % target)
|
|
|
|
|
2021-04-07 08:39:06 +02:00
|
|
|
fields = dict(params['fields'])
|
|
|
|
if params['summary'] is not None:
|
|
|
|
fields.update({'summary': params['summary']})
|
|
|
|
if params['description'] is not None:
|
|
|
|
fields.update({'description': params['description']})
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
# Perform it
|
|
|
|
url = restbase + '/issue/' + params['issue'] + "/transitions"
|
|
|
|
data = {'transition': {"id": tid},
|
2021-04-07 08:39:06 +02:00
|
|
|
'fields': fields}
|
|
|
|
if params['comment'] is not None:
|
|
|
|
data.update({"update": {
|
|
|
|
"comment": [{
|
|
|
|
"add": {"body": params['comment']}
|
|
|
|
}],
|
|
|
|
}})
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2020-12-27 13:56:48 +01:00
|
|
|
return True, post(url, user, passwd, params['timeout'], data)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def link(restbase, user, passwd, params):
|
|
|
|
data = {
|
|
|
|
'type': {'name': params['linktype']},
|
|
|
|
'inwardIssue': {'key': params['inwardissue']},
|
|
|
|
'outwardIssue': {'key': params['outwardissue']},
|
|
|
|
}
|
|
|
|
|
|
|
|
url = restbase + '/issueLink/'
|
|
|
|
|
2020-12-27 13:56:48 +01:00
|
|
|
return True, post(url, user, passwd, params['timeout'], data)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
2021-04-13 08:06:52 +02:00
|
|
|
def attach(restbase, user, passwd, params):
|
|
|
|
filename = params['attachment'].get('filename')
|
|
|
|
content = params['attachment'].get('content')
|
|
|
|
|
|
|
|
if not any((filename, content)):
|
|
|
|
raise ValueError('at least one of filename or content must be provided')
|
|
|
|
mime = params['attachment'].get('mimetype')
|
|
|
|
|
|
|
|
if not os.path.isfile(filename):
|
|
|
|
raise ValueError('The provided filename does not exist: %s' % filename)
|
|
|
|
|
|
|
|
content_type, data = _prepare_attachment(filename, content, mime)
|
|
|
|
|
|
|
|
url = restbase + '/issue/' + params['issue'] + '/attachments'
|
|
|
|
return True, post(
|
|
|
|
url, user, passwd, params['timeout'], data, content_type=content_type,
|
|
|
|
additional_headers={"X-Atlassian-Token": "no-check"}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# Ideally we'd just use prepare_multipart from ansible.module_utils.urls, but
|
|
|
|
# unfortunately it does not support specifying the encoding and also defaults to
|
|
|
|
# base64. Jira doesn't support base64 encoded attachments (and is therefore not
|
|
|
|
# spec compliant. Go figure). I originally wrote this function as an almost
|
|
|
|
# exact copypasta of prepare_multipart, but ran into some encoding issues when
|
|
|
|
# using the noop encoder. Hand rolling the entire message body seemed to work
|
|
|
|
# out much better.
|
|
|
|
#
|
|
|
|
# https://community.atlassian.com/t5/Jira-questions/Jira-dosen-t-decode-base64-attachment-request-REST-API/qaq-p/916427
|
|
|
|
#
|
|
|
|
# content is expected to be a base64 encoded string since Ansible doesn't
|
|
|
|
# support passing raw bytes objects.
|
|
|
|
def _prepare_attachment(filename, content=None, mime_type=None):
|
|
|
|
def escape_quotes(s):
|
|
|
|
return s.replace('"', '\\"')
|
|
|
|
|
|
|
|
boundary = "".join(random.choice(string.digits + string.ascii_letters) for i in range(30))
|
|
|
|
name = to_native(os.path.basename(filename))
|
|
|
|
|
|
|
|
if not mime_type:
|
|
|
|
try:
|
|
|
|
mime_type = mimetypes.guess_type(filename or '', strict=False)[0] or 'application/octet-stream'
|
|
|
|
except Exception:
|
|
|
|
mime_type = 'application/octet-stream'
|
|
|
|
main_type, sep, sub_type = mime_type.partition('/')
|
|
|
|
|
|
|
|
if not content and filename:
|
|
|
|
with open(to_bytes(filename, errors='surrogate_or_strict'), 'rb') as f:
|
|
|
|
content = f.read()
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
content = base64.decode(content)
|
|
|
|
except binascii.Error as e:
|
|
|
|
raise Exception("Unable to base64 decode file content: %s" % e)
|
|
|
|
|
|
|
|
lines = [
|
|
|
|
"--{0}".format(boundary),
|
|
|
|
'Content-Disposition: form-data; name="file"; filename={0}'.format(escape_quotes(name)),
|
|
|
|
"Content-Type: {0}".format("{0}/{1}".format(main_type, sub_type)),
|
|
|
|
'',
|
|
|
|
to_text(content),
|
|
|
|
"--{0}--".format(boundary),
|
|
|
|
""
|
|
|
|
]
|
|
|
|
|
|
|
|
return (
|
|
|
|
"multipart/form-data; boundary={0}".format(boundary),
|
|
|
|
"\r\n".join(lines)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
def main():
|
|
|
|
|
|
|
|
global module
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
2021-04-13 08:06:52 +02:00
|
|
|
attachment=dict(type='dict', options=dict(
|
|
|
|
content=dict(type='str'),
|
|
|
|
filename=dict(type='path', required=True),
|
|
|
|
mimetype=dict(type='str')
|
|
|
|
)),
|
2020-10-31 13:53:57 +01:00
|
|
|
uri=dict(type='str', required=True),
|
2021-04-13 08:06:52 +02:00
|
|
|
operation=dict(type='str', choices=['attach', 'create', 'comment', 'edit', 'update', 'fetch', 'transition', 'link', 'search'],
|
2020-03-09 10:11:07 +01:00
|
|
|
aliases=['command'], required=True),
|
2020-10-31 13:53:57 +01:00
|
|
|
username=dict(type='str', required=True),
|
|
|
|
password=dict(type='str', required=True, no_log=True),
|
|
|
|
project=dict(type='str', ),
|
|
|
|
summary=dict(type='str', ),
|
|
|
|
description=dict(type='str', ),
|
|
|
|
issuetype=dict(type='str', ),
|
|
|
|
issue=dict(type='str', aliases=['ticket']),
|
|
|
|
comment=dict(type='str', ),
|
|
|
|
status=dict(type='str', ),
|
|
|
|
assignee=dict(type='str', ),
|
2020-03-09 10:11:07 +01:00
|
|
|
fields=dict(default={}, type='dict'),
|
2020-10-31 13:53:57 +01:00
|
|
|
linktype=dict(type='str', ),
|
|
|
|
inwardissue=dict(type='str', ),
|
|
|
|
outwardissue=dict(type='str', ),
|
|
|
|
jql=dict(type='str', ),
|
2020-04-09 09:29:26 +02:00
|
|
|
maxresults=dict(type='int'),
|
2020-03-09 10:11:07 +01:00
|
|
|
timeout=dict(type='float', default=10),
|
|
|
|
validate_certs=dict(default=True, type='bool'),
|
2021-04-07 08:39:06 +02:00
|
|
|
account_id=dict(type='str'),
|
2020-03-09 10:11:07 +01:00
|
|
|
),
|
2020-12-27 13:56:48 +01:00
|
|
|
required_if=(
|
2021-04-13 08:06:52 +02:00
|
|
|
('operation', 'attach', ['issue', 'attachment']),
|
2020-12-27 13:56:48 +01:00
|
|
|
('operation', 'create', ['project', 'issuetype', 'summary']),
|
|
|
|
('operation', 'comment', ['issue', 'comment']),
|
|
|
|
('operation', 'fetch', ['issue']),
|
|
|
|
('operation', 'transition', ['issue', 'status']),
|
|
|
|
('operation', 'link', ['linktype', 'inwardissue', 'outwardissue']),
|
|
|
|
('operation', 'search', ['jql']),
|
|
|
|
),
|
2021-04-07 08:39:06 +02:00
|
|
|
mutually_exclusive=[('assignee', 'account_id')],
|
2020-03-09 10:11:07 +01:00
|
|
|
supports_check_mode=False
|
|
|
|
)
|
|
|
|
|
|
|
|
op = module.params['operation']
|
|
|
|
|
|
|
|
# Handle rest of parameters
|
|
|
|
uri = module.params['uri']
|
|
|
|
user = module.params['username']
|
|
|
|
passwd = module.params['password']
|
|
|
|
if module.params['assignee']:
|
|
|
|
module.params['fields']['assignee'] = {'name': module.params['assignee']}
|
2021-04-07 08:39:06 +02:00
|
|
|
if module.params['account_id']:
|
|
|
|
module.params['fields']['assignee'] = {'accountId': module.params['account_id']}
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if not uri.endswith('/'):
|
|
|
|
uri = uri + '/'
|
|
|
|
restbase = uri + 'rest/api/2'
|
|
|
|
|
|
|
|
# Dispatch
|
|
|
|
try:
|
|
|
|
|
|
|
|
# Lookup the corresponding method for this operation. This is
|
|
|
|
# safe as the AnsibleModule should remove any unknown operations.
|
|
|
|
thismod = sys.modules[__name__]
|
|
|
|
method = getattr(thismod, op)
|
|
|
|
|
2020-12-27 13:56:48 +01:00
|
|
|
changed, ret = method(restbase, user, passwd, module.params)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
except Exception as e:
|
2020-12-27 13:56:48 +01:00
|
|
|
return module.fail_json(msg=to_native(e), exception=traceback.format_exc())
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2020-12-27 13:56:48 +01:00
|
|
|
module.exit_json(changed=changed, meta=ret)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|