Skip to content

Commit 9d577a7

Browse files
authored
Jira: Add scheme related methods and issue archive method and + 1 example (atlassian-api#589)
* Jira: Adjust example notifications * update README.md * Adjust gitignore * Jira: add more scheme related methods and project update related + example for generic archive procedure
1 parent 5b3088b commit 9d577a7

File tree

3 files changed

+206
-16
lines changed

3 files changed

+206
-16
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ venv/
9696
ENV/
9797
env.bak/
9898
venv.bak/
99+
venv_/
99100

100101
# Spyder project settings
101102
.spyderproject

atlassian/jira.py

+119-16
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,23 @@ def issue_get_worklog(self, issue_id_or_key):
636636
:return:
637637
"""
638638
url = "rest/api/2/issue/{issueIdOrKey}/worklog".format(issueIdOrKey=issue_id_or_key)
639+
640+
return self.get(url)
641+
642+
def issue_archive(self, issue_id_or_key, notify_users=False):
643+
"""
644+
Archives an issue.
645+
:param issue_id_or_key: Issue id or issue key
646+
:param notify_users: send the email with notification that the issue was updated to users that watch it.
647+
Admin or project admin permissions are required to disable the notification.
648+
:return:
649+
"""
650+
params = {}
651+
if notify_users:
652+
params['notifyUsers'] = "true"
653+
else:
654+
params['notifyUsers'] = "false"
655+
url = "rest/api/2/issue/{issueIdOrKey}/archive".format(issueIdOrKey=issue_id_or_key)
639656
return self.get(url)
640657

641658
def issue_field_value(self, key, field):
@@ -1431,33 +1448,57 @@ def add_project_actor_in_role(self, project_key, role_id, actor, actor_type):
14311448
def update_project(self, project_key, data, expand=None):
14321449
"""
14331450
Updates a project.
1451+
Only non null values sent in JSON will be updated in the project.
1452+
Values available for the assigneeType field are: "PROJECT_LEAD" and "UNASSIGNED".
14341453
Update project: /rest/api/2/project/{projectIdOrKey}
14351454
14361455
:param project_key: project key of project that needs to be updated
14371456
:param data: dictionary containing the data to be updated
14381457
:param expand: the parameters to expand
14391458
"""
1459+
url = 'rest/api/2/project/{projectIdOrKey}'.format(projectIdOrKey=project_key)
1460+
params = {}
14401461
if expand:
1441-
url = 'rest/api/2/project/{projectIdOrKey}?expand={expand}'.format(projectIdOrKey=project_key,
1442-
expand=expand)
1443-
else:
1444-
url = 'rest/api/2/project/{projectIdOrKey}'.format(projectIdOrKey=project_key)
1445-
return self.put(url, data)
1462+
params['expand'] = expand
1463+
return self.put(url, data, params=params)
14461464

1447-
def get_project_permission_scheme(self, project_id_or_key, expand=None):
1465+
def update_project_category_for_project(self, project_key, new_project_category_id, expand=None):
14481466
"""
1449-
Gets a permission scheme assigned with a project
1450-
Use 'expand' to get details
1467+
Updates a project.
1468+
Update project: /rest/api/2/project/{projectIdOrKey}
14511469
1452-
:param project_id_or_key: str
1453-
:param expand: str
1454-
:return: data of project permission scheme
1470+
:param project_key: project key of project that needs to be updated
1471+
:param new_project_category_id:
1472+
:param expand: the parameters to expand
14551473
"""
1456-
url = 'rest/api/2/project/{}/permissionscheme'.format(project_id_or_key)
1457-
params = {}
1458-
if expand:
1459-
params['expand'] = expand
1460-
return self.get(url, params=params)
1474+
data = {'categoryId': new_project_category_id}
1475+
return self.update_project(project_key, data, expand=expand)
1476+
1477+
#######################################################################################################
1478+
# Resource for associating notification schemes and projects
1479+
# Reference:
1480+
# https://docs.atlassian.com/software/jira/docs/api/REST/8.5.0/#api/2/project/{projectKeyOrId}/notificationscheme
1481+
#######################################################################################################
1482+
def get_notification_scheme_for_project(self, project_id_or_key):
1483+
"""
1484+
Gets a notification scheme associated with the project.
1485+
Follow the documentation of /notificationscheme/{id} resource for all details about returned value.
1486+
:param project_id_or_key:
1487+
:return:
1488+
"""
1489+
url = 'rest/api/2/project/{}/notificationscheme'.format(project_id_or_key)
1490+
return self.get(url)
1491+
1492+
def assign_project_notification_scheme(self, project_key, new_notification_scheme=""):
1493+
"""
1494+
Updates a project.
1495+
Update project: /rest/api/2/project/{projectIdOrKey}
1496+
1497+
:param project_key: project key of project that needs to be updated
1498+
:param new_notification_scheme:
1499+
"""
1500+
data = {'notificationScheme': new_notification_scheme}
1501+
return self.update_project(project_key, data)
14611502

14621503
def get_notification_schemes(self):
14631504
"""
@@ -1466,10 +1507,41 @@ def get_notification_schemes(self):
14661507
url = 'rest/api/2/notificationscheme'
14671508
return self.get(url)
14681509

1510+
def get_all_notification_schemes(self):
1511+
"""
1512+
Returns a paginated list of notification schemes
1513+
"""
1514+
return self.get_notification_schemes().get("values") or []
1515+
14691516
def get_notification_scheme(self, notification_scheme_id, expand=None):
14701517
"""
14711518
Returns a full representation of the notification scheme for the given id.
14721519
Use 'expand' to get details
1520+
Returns a full representation of the notification scheme for the given id. This resource will return a notification scheme containing a list of events and recipient configured to receive notifications for these events. Consumer should allow events without recipients to appear in response. User accessing the data is required to have permissions to administer at least one project associated with the requested notification scheme.
1521+
Notification recipients can be:
1522+
1523+
current assignee - the value of the notificationType is CurrentAssignee
1524+
issue reporter - the value of the notificationType is Reporter
1525+
current user - the value of the notificationType is CurrentUser
1526+
project lead - the value of the notificationType is ProjectLead
1527+
component lead - the value of the notificationType is ComponentLead
1528+
all watchers - the value of the notification type is AllWatchers
1529+
configured user - the value of the notification type is User. Parameter will contain key of the user.
1530+
Information about the user will be provided if user expand parameter is used.
1531+
configured group - the value of the notification type is Group. Parameter will contain name of the group.
1532+
Information about the group will be provided if group expand parameter is used.
1533+
configured email address - the value of the notification type is EmailAddress, additionally
1534+
information about the email will be provided.
1535+
users or users in groups in the configured custom fields - the value of the notification type
1536+
is UserCustomField or GroupCustomField. Parameter will contain id of the custom field.
1537+
Information about the field will be provided if field expand parameter is used.
1538+
configured project role - the value of the notification type is ProjectRole.
1539+
Parameter will contain project role id.
1540+
Information about the project role will be provided if projectRole expand parameter is used.
1541+
Please see the example for reference.
1542+
The events can be JIRA system events or events configured by administrator.
1543+
In case of the system events, data about theirs ids, names and descriptions is provided.
1544+
In case of custom events, the template event is included as well.
14731545
:param notification_scheme_id: Id of scheme u wanna work with
14741546
:param expand: str
14751547
:return: full representation of the notification scheme for the given id
@@ -1480,6 +1552,37 @@ def get_notification_scheme(self, notification_scheme_id, expand=None):
14801552
params['expand'] = expand
14811553
return self.get(url, params=params)
14821554

1555+
#######################################################################################################
1556+
# Resource for associating permission schemes and projects.
1557+
# Reference:
1558+
# https://docs.atlassian.com/software/jira/docs/api/REST/8.5.0/#api/2/project/{projectKeyOrId}/permissionscheme
1559+
#######################################################################################################
1560+
def assign_project_permission_scheme(self, project_id_or_key, permission_scheme_id):
1561+
"""
1562+
Assigns a permission scheme with a project.
1563+
:param project_id_or_key:
1564+
:param permission_scheme_id:
1565+
:return:
1566+
"""
1567+
url = 'rest/api/2/project/{}/permissionscheme'.format(project_id_or_key)
1568+
data = {"id": permission_scheme_id}
1569+
return self.put(url, data=data)
1570+
1571+
def get_project_permission_scheme(self, project_id_or_key, expand=None):
1572+
"""
1573+
Gets a permission scheme assigned with a project
1574+
Use 'expand' to get details
1575+
1576+
:param project_id_or_key: str
1577+
:param expand: str
1578+
:return: data of project permission scheme
1579+
"""
1580+
url = 'rest/api/2/project/{}/permissionscheme'.format(project_id_or_key)
1581+
params = {}
1582+
if expand:
1583+
params['expand'] = expand
1584+
return self.get(url, params=params)
1585+
14831586
def create_issue_type(self, name, description='', type='standard'):
14841587
"""
14851588
Create a new issue type
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# coding=utf-8
2+
import argparse
3+
from os import environ
4+
from atlassian import Jira
5+
import logging
6+
7+
8+
def get_project_category_object(project_category_name="DEPRECATED"):
9+
categories = jira.get_all_project_categories()
10+
for category in categories:
11+
if category.get("name") == project_category_name:
12+
return category
13+
return None
14+
15+
16+
def get_project_permission_scheme_object(new_scheme="w Archive Permission Scheme"):
17+
permission_schemes = jira.get_all_permissionschemes()
18+
for permission_scheme in permission_schemes:
19+
if permission_scheme.get("name") == new_scheme:
20+
return permission_scheme
21+
return None
22+
23+
24+
def get_project_notification_scheme_object(new_scheme="Archived Notification Scheme"):
25+
schemes = jira.get_all_notification_schemes()
26+
for scheme in schemes:
27+
if scheme.get("name") == new_scheme:
28+
return scheme
29+
return None
30+
31+
32+
if __name__ == '__main__':
33+
jira = Jira(
34+
url=environ.get("JIRA_URL"),
35+
username=environ.get("ATLASSIAN_USER"),
36+
password=environ.get("ATLASSIAN_PASSWORD")
37+
)
38+
# Setting the logging level. INFO|ERROR|DEBUG are the most common.
39+
logging.basicConfig(level=logging.INFO)
40+
# Initialize argparse module with some program name and additional information
41+
parser = argparse.ArgumentParser(
42+
prog="Jira Archive Projects",
43+
description="Simple execution of th project key"
44+
)
45+
parser.add_argument("--project", dest="project", default="TEST", help="Jira project key")
46+
parser.add_argument("--category", dest="category", default="DEPRECATED", help="Project category")
47+
parser.add_argument("--permission", dest="permission", default="w Archive Permission Scheme",
48+
help="Permission scheme")
49+
parser.add_argument("--notification", dest="notification", default="Archived Notification Scheme",
50+
help="Notification scheme")
51+
52+
# Getting arguments
53+
args = parser.parse_args()
54+
55+
# dynamic variables
56+
archive_category_name = args.category
57+
archive_project_permission_scheme = args.permission
58+
archive_notification_scheme = args.notification
59+
60+
archive_project_key = args.project
61+
62+
new_project_category = get_project_category_object(archive_category_name)
63+
new_permission_scheme = get_project_permission_scheme_object(archive_project_permission_scheme)
64+
new_notification_scheme = get_project_notification_scheme_object(archive_notification_scheme)
65+
projects = jira.get_all_projects()
66+
new_project = None
67+
for project in projects:
68+
if archive_project_key == project.get("key"):
69+
new_project = project
70+
if new_project_category is None:
71+
print("Did not find the new project category")
72+
else:
73+
jira.update_project_category_for_project(archive_project_key, new_project_category.get("id"))
74+
75+
if new_permission_scheme:
76+
jira.assign_project_permission_scheme(archive_project_key, new_permission_scheme.get("id"))
77+
else:
78+
print("Did not find a permission scheme")
79+
80+
if new_notification_scheme:
81+
jira.assign_project_notification_scheme(archive_project_key, new_notification_scheme.get("id"))
82+
else:
83+
print("Did not find a notification scheme")
84+
break
85+
print("Everything is done")
86+
print("Thanks for the usage that script")

0 commit comments

Comments
 (0)