File size: 3,120 Bytes
56d74b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# coding=utf-8
from atlassian import Jira

jira = Jira(url="http://localhost:8080", username="admin", password="admin")


def get_all_users(group, include_inactive=True):
    """
    Get all users for group. If there more, than 50 users in group:
    go through the pages and append other users to the list
    :param group:
    :param include_inactive:
    :return:
    """
    start = 0
    users = jira.get_all_users_from_group(group, include_inactive_users=include_inactive, start=start)
    processed_data = {
        "group_name": group,
        "total": users["total"],
        "users": [{"name": user["name"], "active": user["active"]} for user in users["values"]],
    }
    while "nextPage" in users:
        start += 50
        users = jira.get_all_users_from_group(group, include_inactive_users=include_inactive, start=start)
        user_list = [{"name": user["name"], "active": user["active"]} for user in users["values"]]
        processed_data["users"] = processed_data["users"] + user_list

    return processed_data


def sort_users_in_group(group):
    """
    Take group, sort users by the name and return group with sorted users
    """
    group["users"] = [sorted_group for sorted_group in sorted(group["users"], key=lambda k: k["name"])]
    return group


def get_groups_data():
    """
    Get all groups, get all users for each group and sort groups by users
    :return:
    """
    groups = [group["name"] for group in jira.get_groups(limit=200)["groups"]]
    groups_and_users = [get_all_users(group) for group in groups]
    groups_and_users = [sort_users_in_group(group) for group in groups_and_users]
    return groups_and_users


def get_inactive_users(groups):
    """
    Take group list and return groups only with inactive users
    :param groups:
    :return:
    """
    inactive_users_list = []
    for group in groups:
        inactive_users = {
            "group_name": group["group_name"],
            "users": [
                {"name": user["name"], "active": user["active"]} for user in group["users"] if not user["active"]
            ],
        }
        inactive_users_list.append(inactive_users)

    return inactive_users_list


def exclude_inactive_users(groups):
    """
    Excluding inactive users from groups.
    :param groups:
    :return:
    """
    for group in groups:
        for user in group["users"]:
            print("Trying to delete {} from group {}".format(user["name"], group["group_name"]))
            jira.remove_user_from_group(user["name"], group["group_name"])
    return True


def filter_groups_by_members(groups, quantity=1):
    """
    Take groups list and return empty groups
    :param groups:
    :param quantity:
    :return:
    """
    return [x for x in groups if int(x["total"]) < quantity]


def find_group(groups, group_name):
    """
    Take groups list and find group by the group name
    :param groups:
    :param group_name:
    :return:
    """
    for group in groups:

        if group["group_name"] == group_name:

            return group
        else:
            return "Group {} not in list".format(group_name)