Last active
April 25, 2019 18:51
-
-
Save dennis1987/f4e602ff5180c3a44690044606e13ce1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
""" | |
Install: Execute 'pip install requests' | |
Run: 'python ticket_creation.py <YOUR_CONFIGURATION_FILE_HERE>' | |
Sample configuration file: | |
[server] | |
jira = https://jira.atlassian.net | |
confluence = https://confluence.atlassian.net | |
login = username | |
[users] | |
group = Some group | |
[project] | |
key = KEY | |
issue_type = Task | |
[issue] | |
summary = Some summary here | |
description : Values can also span multiple lines, | |
as long as they are indented depper than the first line of the value. | |
security_level = Some security level here | |
""" | |
import requests | |
import sys | |
import getpass | |
import base64 | |
import ConfigParser | |
class Page(object): | |
def __init__(self, title, version, link): | |
self.title = title | |
self.version = version | |
self.link = link | |
def __str__(self): | |
return self.title + " v" + str(self.version) + " @ " + self.link | |
class Repository(object): | |
def __init__(self, host, authorization): | |
self.host = host | |
self.headers = headers = { | |
'content-type': 'application/json', | |
'Authorization': authorization | |
} | |
self.authorization = authorization | |
pass | |
class JiraRepository(Repository): | |
@staticmethod | |
def __escape_reserved_chars(string): | |
reserved_chars = \ | |
['+', '-', '.', ',', ';', '?', '|', '*', '/', '%', '^', '$', | |
'#', '@', '[', ']'] | |
for reserved_char in reserved_chars: | |
string = string.replace(reserved_char, '\\\\' + reserved_char) | |
return string | |
def find_all_users_by_username(self, username): | |
response = requests.get( | |
url=self.host + '/rest/api/2/user/search?username=' + username, | |
headers=self.headers | |
).json() | |
users = list() | |
for user in response: | |
users.append(user['name']) | |
return users | |
def find_all_users_by_group_name(self, group_name, start_at=0, | |
users=list()): | |
response = requests.get( | |
url=self.host + '/rest/api/2/group/member?groupname=' + group_name, | |
headers=self.headers | |
).json() | |
for user in response['values']: | |
users.append(user['name']) | |
if not response['isLast']: | |
users += self.find_all_users_by_group_name(group_name, | |
start_at + response[ | |
'maxResults'], | |
users) | |
return users | |
def find_issues_by_summary(self, summary, assignee): | |
response = requests.get( | |
url=self.host + \ | |
'/rest/api/2/search?jql=summary~"' + \ | |
self.__escape_reserved_chars( | |
summary) + '" AND assignee in (' + assignee + ')', | |
headers=self.headers | |
).json()['issues'] | |
return response | |
def create_issue(self, fields_dict): | |
issue = { | |
"fields": fields_dict | |
} | |
response = requests.post(url=self.host + '/rest/api/2/issue', | |
headers=self.headers, json=issue) | |
return response | |
def create_issues(self, issue_updates_list): | |
issues = { | |
"issueUpdates": issue_updates_list | |
} | |
response = requests.post(url=self.host + '/rest/api/2/issue/bulk', | |
headers=self.headers, json=issues) | |
return response | |
class ConfluenceRepository(Repository): | |
def find_all_pages_by_cql(self, page_cql, start=0, pages=list()): | |
response = requests.get( | |
url=self.host + '/rest/api/content/search?cql=' + page_cql + \ | |
'&expand=version&limit=100&start=' + str(start), | |
headers=self.headers | |
).json() | |
for page in response['results']: | |
pages.append(Page(page['title'], page['version']['number'], | |
self.host + page['_links']['webui'])) | |
if response['size'] == response['limit']: | |
pages += self.find_all_pages_by_cql(page_cql, | |
start + response['limit'], | |
pages) | |
return pages | |
if __name__ == '__main__': | |
config = ConfigParser.RawConfigParser(allow_no_value=True, ) | |
if len(sys.argv) != 2: | |
print 'Please select exact on configuration file' | |
print 'E.g. python ticket_creation.py configuration.conf' | |
sys.exit(255) | |
config.readfp(file(sys.argv[1])) | |
jira_host = config.get('server', 'jira') | |
confluence_host = config.get('server', 'confluence') | |
atlassian_user_name = config.get('server', 'login') | |
group_name = config.get('users', 'group') | |
project_key = config.get('project', 'key') | |
issue_type = config.get('project', 'issue_type') | |
issue_summary = config.get('issue', 'summary') | |
issue_description = config.get('issue', 'description') | |
issue_security_level = config.get('issue', 'security_level') | |
p = getpass.getpass(prompt='Password for ' + atlassian_user_name + ': ') | |
jr = JiraRepository( | |
jira_host, | |
'Basic ' + base64.b64encode(atlassian_user_name + ':' + p) | |
) | |
users = jr.find_all_users_by_group_name(group_name) | |
issue_updates_list = list() | |
for user in users: | |
issue_updates_list.append( | |
{ | |
"update": {}, | |
"fields": { | |
"project": | |
{ | |
"key": project_key | |
}, | |
"assignee": { | |
"name": user | |
}, | |
"summary": issue_summary + ' - ' + user, | |
"description": issue_description, | |
"issuetype": { | |
"name": issue_type | |
}, | |
"security": { | |
"name": issue_security_level | |
}, | |
} | |
} | |
) | |
jr.create_issues(issue_updates_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment