File size: 2,853 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
# coding=utf-8
from atlassian import Confluence

CONFLUENCE_URL = "confluence.example.com"
CONFLUENCE_LOGIN = "gonchik.tsymzhitov"
CONFLUENCE_PASSWORD = "passwordpassword"
REMAINED_PAGE_HISTORY_COUNT = 1


def page_version_remover(content_id, remained_page_numbers):
    response = confluence.get_content_history(content_id)
    if not response or not response.get("latest"):
        return
    latest_version_count = int(response.get("lastUpdated").get("number"))
    if len(response) > 0 and latest_version_count > remained_page_numbers:
        print(
            "Number of {} latest version {}".format(
                confluence.url_joiner(confluence.url, "/pages/viewpage.action?pageId=" + content_id),
                latest_version_count,
            )
        )
        for version_page_counter in range(1, (latest_version_count - remained_page_numbers + 1), 1):
            confluence.remove_content_history(content_id, 1)
    else:
        print("Number of page history smaller than remained")


def get_all_page_ids_from_space(space):
    """
    :param space:
    :return:
    """
    limit = 500
    flag = True
    step = 0
    content_ids = []

    while flag:
        values = confluence.get_all_pages_from_space(space=space, start=limit * step, limit=limit)
        step += 1
        if len(values) == 0:
            flag = False
            print("Did not find any pages, please, check permissions")
        else:
            for value in values:
                print("Retrieve page with title: " + value["title"])
                content_ids.append((value["id"]))
    print("Found in space {} pages {}".format(space, len(content_ids)))
    return content_ids


def get_all_spaces():
    limit = 50
    flag = True
    i = 0
    space_key_list = []
    while flag:
        space_lists = confluence.get_all_spaces(start=i * limit, limit=limit)
        if space_lists and len(space_lists) != 0:
            i += 1
            for space_list in space_lists:
                print("Start review the space with key = " + space_list["key"])
                space_key_list.append(space_list["key"])
        else:
            flag = False

    return space_key_list


def reduce_page_numbers(page_id, remained_page_history_count):
    page_version_remover(page_id, remained_page_history_count)
    return


if __name__ == "__main__":
    confluence = Confluence(
        url=CONFLUENCE_URL,
        username=CONFLUENCE_LOGIN,
        password=CONFLUENCE_PASSWORD,
        timeout=190,
    )
    space_keys = get_all_spaces()
    counter = 0
    for space_key in space_keys:
        print("Starting review space with key {}".format(space_key))
        page_ids = get_all_page_ids_from_space(space_key)
        for page_id in page_ids:
            reduce_page_numbers(page_id=page_id, remained_page_history_count=REMAINED_PAGE_HISTORY_COUNT)