Skip to content

Commit 1705946

Browse files
committed
Merge branch 'fix-jira-remove_user_from_group' of https://github.com/YugoHino/atlassian-python-api into fix-jira-remove_user_from_group
2 parents c316767 + 64ffc8f commit 1705946

File tree

6 files changed

+122
-1
lines changed

6 files changed

+122
-1
lines changed

atlassian/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.25.0
1+
3.26.0

atlassian/bitbucket/__init__.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,41 @@ def project_groups_with_administrator_permissions(self, key):
995995
"""
996996
return [group["group"]["name"] for group in self.project_groups(key) if group["permission"] == "PROJECT_ADMIN"]
997997

998+
def repo_users_with_administrator_permissions(self, project_key, repo_key):
999+
"""
1000+
Get repository administrators for repository
1001+
:param project_key: The project key
1002+
:param repo_key: The repository key
1003+
:return: List of repo administrators
1004+
"""
1005+
repo_administrators = []
1006+
for user in self.repo_users(project_key, repo_key):
1007+
if user["permission"] == "REPO_ADMIN":
1008+
repo_administrators.append(user)
1009+
for group in self.repo_groups_with_administrator_permissions(project_key, repo_key):
1010+
for user in self.group_members(group):
1011+
repo_administrators.append(user)
1012+
for user in self.project_users_with_administrator_permissions(project_key):
1013+
repo_administrators.append(user)
1014+
# We convert to a set to ensure uniqueness then back to a list for later useability
1015+
return list({user["id"]: user for user in repo_administrators}.values())
1016+
1017+
def repo_groups_with_administrator_permissions(self, project_key, repo_key):
1018+
"""
1019+
Get groups with admin permissions
1020+
:param project_key:
1021+
:param repo_key:
1022+
:return:
1023+
"""
1024+
repo_group_administrators = []
1025+
for group in self.repo_groups(project_key, repo_key):
1026+
if group["permission"] == "REPO_ADMIN":
1027+
repo_group_administrators.append(group["group"]["name"])
1028+
for group in self.project_groups_with_administrator_permissions(project_key):
1029+
repo_group_administrators.append(group)
1030+
# We convert to a set to ensure uniqueness, then back to a list for later useability
1031+
return list(set(repo_group_administrators))
1032+
9981033
def repo_grant_group_permissions(self, project_key, repo_key, groupname, permission):
9991034
"""
10001035
Grant the specified repository permission to an specific group

atlassian/jira.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,36 @@ def remove_user_from_group(self, username=None, group_name=None, account_id=None
795795
params = {"groupname": group_name, "username": username}
796796
return self.delete(url, params=params)
797797

798+
def get_users_with_browse_permission_to_a_project(
799+
self, username, issue_key=None, project_key=None, start=0, limit=100
800+
):
801+
"""
802+
Returns a list of active users that match the search string. This resource cannot be accessed anonymously
803+
and requires the Browse Users global permission. Given an issue key this resource will provide a list of users
804+
that match the search string and have the browse issue permission for the issue provided.
805+
806+
:param: username:
807+
:param: issueKey:
808+
:param: projectKey:
809+
:param: startAt: OPTIONAL
810+
:param: maxResults: OPTIONAL
811+
:return: List of active users who has browser permission for the given project_key or issue_key
812+
"""
813+
url = self.resource_url("user/viewissue/search")
814+
params = {}
815+
if username:
816+
params["username"] = username
817+
if issue_key:
818+
params["issueKey"] = issue_key
819+
if project_key:
820+
params["projectKey"] = project_key
821+
if start:
822+
params["startAt"] = start
823+
if limit:
824+
params["maxResults"] = limit
825+
826+
return self.get(url, params=params)
827+
798828
"""
799829
Issue
800830
Reference: https://docs.atlassian.com/software/jira/docs/api/REST/8.5.0/#api/2/issue

docs/jira.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ Manage projects
147147
# Use 'expand' to get details (default is None) possible values are notificationSchemeEvents,user,group,projectRole,field,all
148148
jira.get_priority_scheme_of_project(project_key_or_id, expand=None)
149149
150+
# Returns a list of active users who have browse permission for a project that matches the search string for username.
151+
# Using " " string (space) for username gives All the active users who have browse permission for a project
152+
jira.get_users_with_browse_permission_to_a_project(self, username, issue_key=None, project_key=None, start=0, limit=100)
153+
150154
Manage issues
151155
-------------
152156

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# coding=utf-8
2+
from atlassian import Jira
3+
4+
"""Remove stale nodes from cluster before version 8.10
5+
See https://confluence.atlassian.com/jirakb/remove-abandoned-or-offline-nodes-in-jira-data-center-946616137.html
6+
"""
7+
jira = Jira(url="http://localhost:8080", username="admin", password="admin")
8+
stale_node_ids = [_["nodeId"] for _ in jira.get_cluster_all_nodes() if not _["alive"] and _["state"] == "OFFLINE"]
9+
for _ in stale_node_ids:
10+
jira.delete_cluster_node(_)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from atlassian import Jira
2+
import os
3+
4+
""" Download the attachments from tickets """
5+
6+
JIRA_URL = "localhost:8080"
7+
JIRA_LOGIN = "admin"
8+
JIRA_TOKEN = "dsadd2c3s"
9+
10+
11+
def get_tickets(jql):
12+
pass
13+
14+
15+
if __name__ == "__main__":
16+
jira = Jira(url=JIRA_URL, username=JIRA_LOGIN, token=JIRA_TOKEN)
17+
jql = "project = DOC"
18+
tickets = jira.jql(jql=jql, fields=["key,attachment"], limit=1000).get("issues")
19+
20+
for ticket in tickets:
21+
mail_folder = "tickets"
22+
if not os.path.exists(mail_folder):
23+
os.makedirs(mail_folder)
24+
key = ticket.get("key")
25+
attachments = ticket.get("fields").get("attachment")
26+
27+
for attachment in attachments:
28+
dest_folder = mail_folder + "/" + key
29+
if not os.path.exists(dest_folder):
30+
os.makedirs(dest_folder)
31+
filename = attachment.get("filename")
32+
author = attachment.get("author").get("emailAddress")
33+
attachment_id = attachment.get("id")
34+
content_url = attachment.get("content")
35+
session = jira._session
36+
response = session.get(url=content_url)
37+
38+
if response.status_code != 200:
39+
continue
40+
with open(dest_folder + "/" + filename, "wb") as f:
41+
print(f"Saving for {key} the file {filename}")
42+
f.write(response.content)

0 commit comments

Comments
 (0)