-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove_label_from_spaces_on_confluence-cloud.py
executable file
·88 lines (65 loc) · 2.34 KB
/
remove_label_from_spaces_on_confluence-cloud.py
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
#!/usr/bin/env python
import sys
import requests
from requests.auth import HTTPBasicAuth
import json
import yaml
import pdb
from pprint import pprint
import Confluence_apis
import time
import logging
# configure logging
logging.basicConfig(
level=logging.DEBUG,
format="[%(asctime)s] %(levelname)s\t[%(name)s.%(funcName)s:%(lineno)d] %(message)s",
datefmt="%d/%b/%Y %H:%M:%S", stream=sys.stdout)
# user help message
usage = f"Usage: python3 {sys.argv[0]} <atlassian config yaml file> <label to add> [<space name filter (regex)>]"
# get the arguments
try:
atlassian_config_filename = sys.argv[1]
except IndexError:
print(f"{usage}\n\nERROR: Atlassian config file argument missing.")
sys.exit()
try:
label = sys.argv[2]
except IndexError:
print(f"{usage}\n\nERROR: Label missing.")
try:
name_filter = sys.argv[3]
except IndexError:
print(f"WARNING: Space name filter missing, removing label from all spaces in Confluence instance.")
name_filter = None
#time.sleep(3)
# read the atlassian config file
with open(atlassian_config_filename, 'r') as file:
try:
config = yaml.safe_load(file)
except yaml.YAMLError as exc:
print(exc)
# create confluence api instance
confluence = Confluence_apis.Confluence_cloud_api(config)
# request a list of all spaces
spaces = confluence.get_spaces()
# while testing, only rename the test space
#spaces = [ space for space in spaces if space['key'] == 'DAH' ]
# rename all spaces in list
c = 0
for i,space in enumerate(spaces):
# if a filter is specified
if name_filter:
import re
# check if filter does not match
if not re.search(name_filter, space['name']):
# skip if no match
logging.debug(f"Skipping space {space['name']}, not matching name filter '{name_filter}'")
continue
logging.info(f"Removing label '{label}' from space '{space['name']}'")
response = confluence.remove_label_from_space(space['key'], label)
#pdb.set_trace()
# warn if something is wrong
if not response.ok:
logging.error(f"ERROR: Response code {response.status_code}, {response.text}")
c += 1
logging.info(f"Done, removed label {label} from {c} spaces.")